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

Add MIDI compatibilty (usb) #2322

Merged
merged 3 commits into from
Jul 2, 2020
Merged
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
135 changes: 135 additions & 0 deletions js/widgets/musickeyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,15 @@ function MusicKeyboard() {
that._createAddRowPieSubmenu();
};

this.midiButton = widgetWindow.addButton(
null,
ICONSIZE,
_("MIDI")
)
this.midiButton.onclick = function () {
that.doMIDI();
};

// var cell = this._addButton(row1, 'table.svg', ICONSIZE, _('Table'));

//that._createKeyboard();
Expand Down Expand Up @@ -2628,4 +2637,130 @@ function MusicKeyboard() {
return cell;
};

this.doMIDI = () => {

var duration = 0;
var startTime = 0;

this.getElement = {};

for (let idx = 0; idx < this.layout.length; idx++) {
key = this.layout[idx];
this.getElement[key.noteName.toString()+ key.noteOctave.toString()] = key.objId;
this.getElement[FIXEDSOLFEGE1[key.noteName.toString()] + "" + key.noteOctave] = key.objId ; //convet solfege to alphabetic.
}

let __startNote = (event, element) => {
if (!element) return;
startTime = event.timeStamp ; // Milliseconds();
element.style.backgroundColor = platformColor.orange;
this._logo.synth.trigger(
0,
this.noteMapper[element.id],
1,
this.instrumentMapper[element.id],
null,
null
);
};

let __endNote = (event, element) => {
if (!element) return;

var id = element.id;
if (id.includes("blackRow")) {
element.style.backgroundColor = "black";
} else {
element.style.backgroundColor = "white";
}

var now = event.timeStamp;
duration = now - startTime;
duration /= 1000;
this._logo.synth.stopSound(
0,
this.instrumentMapper[element.id],
this.noteMapper[element.id]
);
if (beginnerMode === "true") {
duration = parseFloat(
(Math.round(duration * 8) / 8).toFixed(3)
);
} else {
duration = parseFloat(
(Math.round(duration * 16) / 16).toFixed(4)
);
}

if (duration === 0) {
duration = 0.125;
} else if (duration < 0) {
duration = -duration;
}

this._notesPlayed.push({
startTime: startTime,
noteOctave: this.noteMapper[element.id],
objId: element.id,
duration: duration,
voice: this.instrumentMapper[element.id],
blockNumber: this.blockNumberMapper[element.id]
});
this._createTable();
};

let numberToPitch = (num) => {
let offset = 4;
let octave = offset + Math.floor((num - 60) / 12);
let pitch1 = NOTESSHARP[num % 12];
let pitch2 = NOTESFLAT[num % 12];
return [pitch1, pitch2, octave];
}

//event attributes : timeStamp , data
//data : length -3 [0] : 144/128 : noteOn/NoteOff
// [1] : noteNumber : middle C always 60
// [2] : velocity ,(currently not used).

let onMIDIMessage = (event) => {
let pitchOctave = numberToPitch(event.data[1]);
let pitch1 = pitchOctave[0];
let pitch2 = pitchOctave[1];
let octave = pitchOctave[2];
let key = this.getElement[pitch1 +""+ octave] || this.getElement[pitch2 +"" + octave];
console.debug(pitch1, octave);
if (event.data[0] == 144 && event.data[2] != 0) {
__startNote(event, docById(key));

}
else {
__endNote(event, docById(key));
}
}

let onMIDISuccess = ( midiAccess) => {
// re-init widget
if (this.midiON){
this.midiButton.style.background = "#00FF00" ;
logo.textMsg("MIDI device present")
return ;
}
midiAccess.inputs.forEach((input) => { input.onmidimessage = onMIDIMessage; });
if (midiAccess.inputs.size) {
this.midiButton.style.background = "#00FF00" ;
logo.textMsg("MIDI device present")
this.midiON =true ;
}
else logo.textMsg("MIDI device not present");
}

let onMIDIFailure= () => {
logo.errorMsg( "Failed to get MIDI access in browser");
this.midiON =false ;
}

navigator.requestMIDIAccess()
.then(onMIDISuccess,onMIDIFailure);

}
}