Skip to content

Commit ebeba8a

Browse files
committed
changed note input to frequency or string format midi
1 parent 0f4887c commit ebeba8a

File tree

4 files changed

+69
-40
lines changed

4 files changed

+69
-40
lines changed

examples/polyphonicSynth-Keyboard/sketch.js

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ var colors = {};
66
var keys = {};
77
var notes = {};
88

9+
var compressor;
10+
911
var description;
1012
function setup() {
1113

@@ -17,15 +19,21 @@ function setup() {
1719
}
1820
}
1921

20-
keys = {'A':60, 'W':61, 'S':62, 'E':63, 'D':64, 'F':65, 'T':66,'G':67,
21-
'Y':68, 'H':69, 'U':70, 'J':71, 'K':72, 'O':73, 'L':74};
22-
notes = {60:0, 62:1 , 64:2 , 65:3 , 67:4 , 69:5 , 71:6 , 72:7 , 74:8,
23-
61: 9, 63:10, 66:12, 68:13, 70:14, 73:16};
24-
octave = 0;
22+
keys = {'A':'C0', 'W':'C#0', 'S':'D0', 'E':'Eb0', 'D':'E0', 'F':'F0', 'T':'F#0','G':'G0',
23+
'Y':'G#0', 'H':'A1', 'U':'Bb1', 'J':'B1', 'K':'C1', 'O':'C#1', 'L':'D1'};
24+
25+
notes = {'C0':0, 'D0':1 , 'E0':2 , 'F0':3, 'G0':4 , 'A1':5 , 'B1':6 , 'C1':7 , 'D1':8,
26+
'C#0': 9, 'Eb0':10, 'F#0':12, 'G#0':13, 'Bb1':14, 'C#1':16};
27+
octave = 3;
28+
2529

2630
description = createP('p5.PolySynth is a handler class for monophonic extensions '+
2731
'of the p5.AudioVoice class. Use the computer keyboard to play notes on '+
2832
'the piano roll. Use UP_ARROW and DOWN_ARROW to change octave');
33+
polySynth.disconnect();
34+
compressor = new p5.Compressor();
35+
polySynth.connect(compressor);
36+
2937

3038
}
3139

@@ -51,13 +59,18 @@ function draw() {
5159
function keyPressed() {
5260
//OCTAVEf
5361
if (keyCode === UP_ARROW) {
54-
octave +=12;
62+
octave +=1;
5563
} else if (keyCode === DOWN_ARROW) {
56-
octave -=12;
64+
octave -=1;
5765
}
5866
else if (keyToMidi() && keysDown[key] !== true){
59-
polySynth.noteAttack(keyToMidi() + octave);
67+
// var keyToMidi() = keyToMidi();
68+
var currentOctave = Number(keyToMidi()[keyToMidi().length-1]) + octave;
69+
var currentKey= keyToMidi().substr(0,keyToMidi().length -1) + currentOctave;
70+
71+
polySynth.noteAttack(currentKey);
6072
var index = notes[keyToMidi()];
73+
6174
colors[index][1] = !colors[index][1];
6275
colors[index][1] ? colors[index][0] = color(random(255),random(255),random(255)) : colors[index][0] = color(255);
6376
if (keysDown[key] === undefined) {
@@ -68,9 +81,15 @@ function keyPressed() {
6881

6982
function keyReleased() {
7083
Object.keys(keysDown).forEach(function(key) {
71-
if(!keyIsDown(key)){
72-
polySynth.noteRelease(keyToMidi(key) + octave);
73-
var index = notes[keyToMidi(key)];
84+
85+
if(!keyIsDown(key.charCodeAt(0))){
86+
// var keyToMidi() = keyToMidi();
87+
88+
var currentOctave = Number(keyToMidi()[keyToMidi().length - 1]) + octave;
89+
currentKey = keyToMidi().substr(0,keyToMidi().length -1) + currentOctave;
90+
polySynth.noteRelease(currentKey);
91+
92+
var index = notes[keyToMidi(keyCodeToLetter(key))];
7493
colors[index][1] = !colors[index][1];
7594
colors[index][1] ? colors[index][0] = color(random(255),random(255),random(255)) : colors[index][0] = color(255);
7695
delete keysDown[key];
@@ -81,4 +100,8 @@ function keyReleased() {
81100
function keyToMidi(keyboard) {
82101
var thisKey = typeof keyboard ==='undefined' ? key : keyboard
83102
return keys[thisKey];
103+
}
104+
105+
function keyCodeToLetter(code) {
106+
84107
}

src/audioVoice.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ define(function() {
1414
};
1515

1616
p5.AudioVoice.prototype._setNote = function(note) {
17+
var wholeNotes = {A:21, B:23, C:24, D:26, E:28, F:29, G:31};
18+
19+
var value = wholeNotes[ note[0] ];
20+
var octave = typeof Number(note.slice(-1)) === 'number'? note.slice(-1) : 0;
21+
value += 12 * octave;
22+
value = note[1] === '#' ? value+1 : note[1] ==='b' ? value - 1 : value;
23+
24+
//return midi value converted to frequency
25+
return p5.prototype.midiToFreq(value);
1726
};
1827

1928
p5.AudioVoice.prototype.play = function (note, velocity, secondsFromNow, sustime) {

src/monosynth.js

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,31 +64,15 @@ define(function (require) {
6464

6565
p5.MonoSynth.prototype = Object.create(p5.AudioVoice.prototype);
6666

67-
/**
68-
* Used internally by play() and triggerAttack()
69-
* to set the note of this.oscillator to a MIDI value.
70-
*
71-
* Synth creators with more complex setups may have overridden
72-
* the oscillator chain with more than one oscillator,
73-
* and should override this method accordingly.
74-
*
75-
* @param {Number} note midi value of a note, where
76-
* middle c is 60
77-
* @param {Number} [secondsFromNow] (optional) a time (in seconds
78-
* from now) at which
79-
* to set the note
80-
* @private
81-
*/
82-
p5.MonoSynth.prototype._setNote = function(note, secondsFromNow) {
83-
var freqVal = p5.prototype.midiToFreq(note);
84-
this.oscillator.freq(freqVal, 0, secondsFromNow);
85-
};
86-
8767
/**
8868
* Play tells the MonoSynth to start playing a note
8969
*
9070
* @method play
91-
* @param {Number} [note] midi note to play (ranging from 0 to 127 - 60 being a middle C)
71+
* @param {String | Number} note the note you want to play, specified as a
72+
* frequency in Hertz (Number) or as a midi
73+
* value in Note/Octave format ("C4", "Eb3"...etc")
74+
* See <a href = "https://github.com/Tonejs/Tone.js/wiki/Instruments">
75+
* Tone</a>. Defaults to 440 hz.
9276
* @param {Number} [velocity] velocity of the note to play (ranging from 0 to 1)
9377
* @param {Number} [secondsFromNow] time from now (in seconds) at which to play
9478
* @param {Number} [sustainTime] time to sustain before releasing the envelope
@@ -99,7 +83,7 @@ define(function (require) {
9983
// set range of env (TO DO: allow this to be scheduled in advance)
10084
var susTime = susTime || this.sustain;
10185
this.susTime = susTime;
102-
this.triggerAttack(note, veocity, secondsFromNow);
86+
this.triggerAttack(note, velocity, secondsFromNow);
10387
this.triggerRelease(secondsFromNow + susTime);
10488
};
10589

@@ -108,14 +92,20 @@ define(function (require) {
10892
* Similar to holding down a key on a piano, but it will
10993
* hold the sustain level until you let go.
11094
*
95+
* @param {String | Number} note the note you want to play, specified as a
96+
* frequency in Hertz (Number) or as a midi
97+
* value in Note/Octave format ("C4", "Eb3"...etc")
98+
* See <a href = "https://github.com/Tonejs/Tone.js/wiki/Instruments">
99+
* Tone</a>. Defaults to 440 hz
111100
* @param {Number} secondsFromNow time from now (in seconds)
112101
* @param {Number} [velocity] velocity of the note to play (ranging from 0 to 1)
113102
* @param {Number} [secondsFromNow] time from now (in seconds) at which to play
114103
* @method triggerAttack
115104
*/
116105
p5.MonoSynth.prototype.triggerAttack = function (note, velocity, secondsFromNow) {
117106
var secondsFromNow = secondsFromNow || 0;
118-
var freq = p5.prototype.midiToFreq(note);
107+
var freq = typeof note === 'string' ? this._setNote(note)
108+
: typeof note === 'number' ? note : 440;
119109
var vel = velocity || 1;
120110

121111
this._isOn = true;
@@ -211,6 +201,7 @@ define(function (require) {
211201
},
212202
});
213203

204+
214205
/**
215206
* MonoSynth amp
216207
* @method amp

src/polysynth.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,10 @@ define(function (require) {
183183
//this value is used by this._voicesInUse
184184
var t = now + tFromNow;
185185

186-
var note = _note === undefined ? 60 : _note;
186+
// var note = _note === undefined ? 60 : _note;
187+
//
188+
var note = typeof _note === 'string' ? this.AudioVoice.prototype._setNote(_note)
189+
: typeof _note === 'number' ? _note : 440;
187190
var velocity = _velocity === undefined ? 1 : _velocity;
188191

189192
var currentVoice;
@@ -242,11 +245,14 @@ define(function (require) {
242245
*/
243246

244247
p5.PolySynth.prototype.noteRelease = function (_note,secondsFromNow) {
245-
var note = _note === undefined ?
246-
p5.prototype.freqToMidi(this.audiovoices[this._newest].oscillator.freq().value)
247-
: _note;
248+
// var note = _note === undefined ?
249+
// p5.prototype.freqToMidi(this.audiovoices[this._newest].oscillator.freq().value)
250+
// : _note;
251+
var note = typeof _note === 'string' ? this.AudioVoice.prototype._setNote(_note)
252+
: typeof _note === 'number' ? _note : this.audiovoices[this._newest].oscillator.freq().value;
248253

249-
if (this.notes[_note] === undefined) {
254+
255+
if (this.notes[note] === undefined) {
250256
console.warn('Cannot release a note that is not already playing');
251257
} else {
252258
var now = p5sound.audiocontext.currentTime;
@@ -259,7 +265,7 @@ define(function (require) {
259265
this._voicesInUse.setValueAtTime(previousVal - 1, t);
260266
this._updateAfter(t, -1);
261267

262-
268+
// console.log('RELEASE ' + note);
263269
this.audiovoices[ this.notes[note] ].triggerRelease(tFromNow);
264270
this.notes[note] = undefined;
265271

0 commit comments

Comments
 (0)