Skip to content

Commit

Permalink
Move MIDI event queue processing into synth.js
Browse files Browse the repository at this point in the history
  • Loading branch information
mmontag committed Jan 22, 2022
1 parent 6bbbf4e commit 8694dcb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
5 changes: 1 addition & 4 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ function setupAudioGraph() {

for (var i = 0, length = buffer.length; i < length; i++) {
sampleTime += msPerSample;
if (synth.eventQueue.length && synth.eventQueue[0].timeStamp < sampleTime) {
synth.processMidiEvent(synth.eventQueue.shift());
}

synth.processQueuedEventsUpToSampleTime(sampleTime);
var output = synth.render();
outputL[i] = output[0];
outputR[i] = output[1];
Expand Down
26 changes: 16 additions & 10 deletions src/synth.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,35 @@ 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
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
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) {
} else if (cmd === 9) {
this.noteOn(noteNumber, velocity/99.0); // changed 127 to 99 to incorporate "overdrive"
} else if (cmd == 10) {
} else if (cmd === 10) {
//this.polyphonicAftertouch(noteNumber, velocity/127);
} else if (cmd == 11) {
} else if (cmd === 11) {
this.controller(noteNumber, velocity/127);
} else if (cmd == 12) {
} else if (cmd === 12) {
//this.programChange(noteNumber);
} else if (cmd == 13) {
} else if (cmd === 13) {
this.channelAftertouch(noteNumber/127);
} else if (cmd == 14) {
this.pitchBend( ((velocity * 128.0 + noteNumber)-8192)/8192.0 );
} else if (cmd === 14) {
this.pitchBend(((velocity * 128.0 + noteNumber) - 8192)/8192.0);
}

};
Expand Down Expand Up @@ -136,4 +142,4 @@ Synth.prototype.render = function() {
return [outputL * PER_VOICE_LEVEL, outputR * PER_VOICE_LEVEL];
};

module.exports = Synth;
module.exports = Synth;

0 comments on commit 8694dcb

Please sign in to comment.