Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented "midimessage" event dispatching on noteOn/noteOff #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 35 additions & 16 deletions js/synth.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,49 @@ var revNode = null;
var revGain = null;
var revBypassGain = null;

function createMidiMessageEvent(note, velocity) {

var evt = document.createEvent( "Event" );
evt.initEvent( "midimessage", false, false );
evt.data = new Uint8Array(3);
evt.data[0] = 144; // channel 1
evt.data[1] = note;
evt.data[2] = velocity || 0;

if (window.onmidimessage && typeof window.onmidimessage == 'function') {
window.onmidimessage( evt );
}
}

function frequencyFromNoteNumber( note ) {
return 440 * Math.pow(2,(note-69)/12);
return 440 * Math.pow(2,(note-69)/12);
}

function noteOn( note, velocity ) {
if (voices[note] == null) {
// Create a new synth node
voices[note] = new Voice(note, velocity);
var e = document.getElementById( "k" + note );
if (e)
e.classList.add("pressed");
}

createMidiMessageEvent(note, velocity);

if (voices[note] == null) {
// Create a new synth node
voices[note] = new Voice(note, velocity);
var e = document.getElementById( "k" + note );
if (e)
e.classList.add("pressed");
}
}

function noteOff( note ) {
if (voices[note] != null) {
// Shut off the note playing and clear it
voices[note].noteOff();
voices[note] = null;
var e = document.getElementById( "k" + note );
if (e)
e.classList.remove("pressed");
}

createMidiMessageEvent(note);

if (voices[note] != null) {
// Shut off the note playing and clear it
voices[note].noteOff();
voices[note] = null;
var e = document.getElementById( "k" + note );
if (e)
e.classList.remove("pressed");
}
}

function $(id) {
Expand Down