Here is the midicode i hacked together so far but probably i do unecessary things, and maybe create objects that i am not even aware about.
Basicly i read midi in and out ports into select list adn create options. And have field to set/write program for each midichannel in an optionlist.
Probably i would want an object "track" that hold both channel and program and that also take care of the midi in port messages my keyboard send and forward to the midi out module.
In a sequenser one probably also want the object to also have a timecode with each message.
But it gets confusing is the track the object holding messages in a object? "if there is such"
I can probably make a sequenser oldschool by starting a timer and set timecodes on the messages, and store them as strings in an array and it will work. But it will be a hack?
var context = new AudioContext(),
oscillators = {};
outportindex=10;
programC=16;
channelC=1;
NOTE_ON=0x90;
NOTE_OFF=0x80;
function midiInit(){
if (navigator.requestMIDIAccess) {
navigator.requestMIDIAccess()
.then(success, failure);
}
}
function failure () {
console.error('No access to your midi devices.'+ + msg )
}
function onMIDISuccess( midiAccess ) {
console.log( "MIDI ready!" );
var input = midiAccess.inputs.entries.next();
if (input)
input.onmidimessage = echoMIDIMessage;
output = midiAccess.outputs.values().next().value;
if (!input || !output)
console.log("Uh oh! Couldn't get i/o ports.");
}
function success (midi) {
mid=midi;
inputs = midi.inputs.values();
outputs = midi.outputs.values();
// inputs outputs are Iterators
for (output = outputs.next(); output && !output.done; output = outputs.next()) {
var deviceOut =
output.value.name;
console.log(deviceOut);
var optout = document.createElement("option");
optout.text = deviceOut;
document.getElementById("out_portsel").add(optout);
}
for (input = inputs.next(); input && !input.done; input = inputs.next()) {
var deviceIn =
input.value.name;
var optin = document.createElement("option");
optin.text = deviceIn;
document.getElementById("in_portsel").add(optin);
// each time there is a midi message call the onMIDIMessage function
input.value.onmidimessage = onMIDIMessage;
}
document.getElementById("out_portsel").selectedIndex=outportindex;
changeProgram();
}
function echoMIDIMessage( event ) {
if (output) {
output.send( event.data, event.timestamp );
}
}
function changeMidiChannel(){
console.log("Change MIDI channel");
channelC=document.getElementById("out_channel").selectedIndex+1;
console.log("channel ",channelC);
if (channelC==1) {NOTE_ON=0xC0}
else if (channelC==1) {NOTE_ON=0x90; NOTE_OFF=0x80;}
else if (channelC==2) {NOTE_ON=0x91; NOTE_OFF=0x81;}
else if (channelC==3) {NOTE_ON=0x92;NOTE_OFF=0x82;}
else if (channelC==4) {NOTE_ON=0x93;NOTE_OFF=0x83;}
else if (channelC==5) {NOTE_ON=0x94;NOTE_OFF=0x84;}
else if (channelC==6) {NOTE_ON=0x95;NOTE_OFF=0x85;}
else if (channelC==7) {NOTE_ON=0x96;NOTE_OFF=0x86;}
else if (channelC==8) {NOTE_ON=0x97;NOTE_OFF=0x87;}
else if (channelC==9) {NOTE_ON=0x98;NOTE_OFF=0x88;}
else if (channelC==10) {NOTE_ON=0x99;NOTE_OFF=0x89;}
else if (channelC==11) {NOTE_ON=0xA0;NOTE_OFF=0x8A;}
else if (channelC==12) {NOTE_ON=0xA1;NOTE_OFF=0x8B;}
else if (channelC==13) {NOTE_ON=0xA2;NOTE_OFF=0x8C;}
else if (channelC==14) {NOTE_ON=0xA3;NOTE_OFF=0x8D;}
else if (channelC==15) {NOTE_ON=0xA4;NOTE_OFF=0x8E;}
else if (channelC==16) {NOTE_ON=0xA5;NOTE_OFF=0x8F;}
output = mid.outputs.get(outportindex);
console.log("Channel ",NOTE_ON," program ",programC);
}
function midiToOutPort(message){
console.log(message.data[1]);
output = mid.outputs.get(outportindex);
console.log("Messag type",message.data[0]);
if(message.data[0]==144 && channelC!=1){message.data[0]=NOTE_ON;}
if(message.data[0]==128 && channelC!=1){message.data[0]=NOTE_OFF;}
console.log("Messag type",message.data[0]);
var noteOnMessage = [message.data[0], message.data[1], message.data[2]];
output.send( noteOnMessage );
}
function changeProgram(){
console.log("Change program");
sysexC=192+channelC-1;
programC=document.getElementById("prog_sel").value
output = mid.outputs.get(outportindex);
var programChange = [sysexC,programC];
output.send(programChange );
}
function changeOutPort(){
//outportindex = document.getElementById("out_portsel").value;
outportindex=document.getElementById("out_portsel").selectedIndex;
console.log("selected index "+outportindex);
}
function onMIDIMessage (message) {
midiToOutPort(message);
//synth(message);
}