djipco / webmidi

Tame the Web MIDI API. Send and receive MIDI messages with ease. Control instruments with user-friendly functions (playNote, sendPitchBend, etc.). React to MIDI input with simple event listeners (noteon, pitchbend, controlchange, etc.).
Apache License 2.0
1.53k stars 115 forks source link

Easiest way to forward all messages from an input to an output #153

Closed d-kersten closed 3 years ago

d-kersten commented 3 years ago

Hey there, thanks for making this!

I would like to implement routing capabilities and forward all messages from an input device to another output device.

What would be the easiest way to do so? I thought I could use the midimessage event on the input (https://webmidijs.org/docs/v2.5.3/classes/Input.html#event_midimessage) and the send method (https://webmidijs.org/docs/v2.5.3/classes/Output.html#method_send) on the output.

But the send method requires a status parameter which is not available from the midimessage input event.

I'm currently trying to capture all possible event types individually and map them to the appropriate send methods but I face some discrepancies and fear I'll miss something – so I thought there might be an easier 'catch all' way to do it.

Thanks!

djipco commented 3 years ago

The ability to route messages has been requested quite a few times and will be integrated into v3. Meanwhile, you can do the following:

WebMidi.inputs[0].addListener("noteon", "all", function (e) {
  console.log(e);
  WebMidi.outputs[0].send(e.data[0], [e.data[1], e.data[2]]);
})

The first element of the e.data array is the so-called status byte. It is followed by 0 or more data bytes. The fact that the property is called data is a bit confusing. I will try to address that in v3.

d-kersten commented 3 years ago

Thank you, that makes sense and will make things easier and more error proof I guess.