-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathsynth.js
145 lines (128 loc) · 4.03 KB
/
synth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
var PER_VOICE_LEVEL = 0.125 / 6; // nominal per-voice level borrowed from Hexter
var PITCH_BEND_RANGE = 2; // semitones (in each direction)
var MIDI_CC_MODULATION = 1,
MIDI_CC_SUSTAIN_PEDAL = 64;
// TODO: Probably reduce responsibility to voice management; rename VoiceManager, MIDIChannel, etc.
function Synth(voiceClass, polyphony) {
this.voices = [];
this.voiceClass = voiceClass;
this.polyphony = polyphony || 12;
this.sustainPedalDown = false;
this.eventQueue = [];
}
Synth.prototype.queueMidiEvent = function(ev) {
this.eventQueue.push(ev);
};
Synth.prototype.processQueuedEventsUpToSampleTime = function(sampleTime) {
if (this.eventQueue.length && this.eventQueue[0].timeStamp < sampleTime) {
this.processMidiEvent(this.eventQueue.shift());
}
}
Synth.prototype.processMidiEvent = function(ev) {
var cmd = ev.data[0] >> 4;
var channel = ev.data[0] & 0xf;
var noteNumber = ev.data[1];
var velocity = ev.data[2];
// console.log( "" + ev.data[0] + " " + ev.data[1] + " " + ev.data[2])
// console.log("midi: ch %d, cmd %d, note %d, vel %d", channel, cmd, noteNumber, velocity);
if (channel === 9) // Ignore drum channel
return;
if (cmd === 8 || (cmd === 9 && velocity === 0)) { // with MIDI, note on with velocity zero is the same as note off
this.noteOff(noteNumber);
} else if (cmd === 9) {
this.noteOn(noteNumber, velocity/99.0); // changed 127 to 99 to incorporate "overdrive"
} else if (cmd === 10) {
//this.polyphonicAftertouch(noteNumber, velocity/127);
} else if (cmd === 11) {
this.controller(noteNumber, velocity/127);
} else if (cmd === 12) {
//this.programChange(noteNumber);
} else if (cmd === 13) {
this.channelAftertouch(noteNumber/127);
} else if (cmd === 14) {
this.pitchBend(((velocity * 128.0 + noteNumber) - 8192)/8192.0);
}
};
Synth.prototype.getLatestNoteDown = function() {
var voice = this.voices[this.voices.length - 1] || { note: 64 };
return voice.note;
};
Synth.prototype.controller = function(controlNumber, value) {
// see http://www.midi.org/techspecs/midimessages.php#3
switch (controlNumber) {
case MIDI_CC_MODULATION:
this.voiceClass.modulationWheel(value);
break;
case MIDI_CC_SUSTAIN_PEDAL:
this.sustainPedal(value > 0.5);
break;
}
};
Synth.prototype.channelAftertouch = function(value) {
this.voiceClass.channelAftertouch(value);
};
Synth.prototype.sustainPedal = function(down) {
if (down) {
this.sustainPedalDown = true;
} else {
this.sustainPedalDown = false;
for (var i = 0, l = this.voices.length; i < l; i++) {
if (this.voices[i] && this.voices[i].down === false)
this.voices[i].noteOff();
}
}
};
Synth.prototype.pitchBend = function(value) {
this.voiceClass.pitchBend(value * PITCH_BEND_RANGE);
for (var i = 0, l = this.voices.length; i < l; i++) {
if (this.voices[i])
this.voices[i].updatePitchBend();
}
};
Synth.prototype.noteOn = function(note, velocity) {
var voice = new this.voiceClass(note, velocity);
if (this.voices.length >= this.polyphony) {
// TODO: fade out removed voices
this.voices.shift(); // remove first
}
this.voices.push(voice);
};
Synth.prototype.noteOff = function(note) {
for (var i = 0, voice; i < this.voices.length, voice = this.voices[i]; i++) {
if (voice && voice.note === note && voice.down === true) {
voice.down = false;
if (this.sustainPedalDown === false)
voice.noteOff();
break;
}
}
};
Synth.prototype.panic = function() {
this.sustainPedalDown = false;
for (var i = 0, l = this.voices.length; i < l; i++) {
if (this.voices[i])
this.voices[i].noteOff();
}
this.voices = [];
};
Synth.prototype.render = function() {
var output;
var outputL = 0;
var outputR = 0;
for (var i = 0, length = this.voices.length; i < length; i++) {
var voice = this.voices[i];
if (voice) {
if (voice.isFinished()) {
// Clear the note after release
this.voices.splice(i, 1);
i--; // undo increment
} else {
output = voice.render();
outputL += output[0];
outputR += output[1];
}
}
}
return [outputL * PER_VOICE_LEVEL, outputR * PER_VOICE_LEVEL];
};
module.exports = Synth;