diff --git a/gswaMIDIControllersManager/gswaMIDIControllerInput.js b/gswaMIDIControllersManager/gswaMIDIControllerInput.js index 6360d70..fec6201 100644 --- a/gswaMIDIControllersManager/gswaMIDIControllerInput.js +++ b/gswaMIDIControllersManager/gswaMIDIControllerInput.js @@ -1,40 +1,27 @@ "use strict"; class gswaMIDIControllerInput { - constructor( portID, name, input, sysexEnabled ) { - this.portID = portID; - this.name = name; - this.input = input; - this.sysexEnabled = sysexEnabled; - this.input.onmidimessage = this.#midiEvents.bind( this ); - this.listeners = { - onNoteOn: [], - onNoteOff: [], - }; - } + #sysex = false; + #onNoteOn = null; + #onNoteOff = null; - $getInput() { - return this.input; - } - $onNoteOnAdd( cb ) { - this.listeners.onNoteOn.push( cb ); + constructor( input, sysex, on ) { + this.#sysex = sysex; + this.#onNoteOn = on.$onNoteOn; + this.#onNoteOff = on.$onNoteOff; + input.onmidimessage = this.#onmidimessage.bind( this ); } - $onNoteOffAdd( cb ) { - this.listeners.onNoteOff.push( cb ); - } - #midiEvents( e ) { - if ( !this.sysexEnabled && e.data.length !== 3 ) { - console.log( "GSLog : Invalid midi event" ); - } else if ( this.#isNoteOn( e.data[ 0 ], e.data[ 2 ] ) ) { - this.listeners.onNoteOn.forEach( cb => cb( e.data ) ); - } else if ( this.#isNoteOff( e.data[ 0 ], e.data[ 2 ] ) ) { - this.listeners.onNoteOff.forEach( cb => cb( e.data ) ); + + #onmidimessage( e ) { + if ( !this.#sysex && e.data.length !== 3 ) { + console.warn( "gswaMIDIInput: Unrecognized midi message", e ); + } else { + const [ msg, key, vel ] = e.data; + + switch ( msg ) { + case 0x90: this.#onNoteOn( key ); break; + case 0x80: this.#onNoteOff( key ); break; + } } } - #isNoteOn( channel, velocity ) { - return ( channel === MIDI_CHANNEL_NOTEON && velocity !== 0 ); - } - #isNoteOff( channel, velocity ) { - return ( channel === MIDI_CHANNEL_NOTEOFF || velocity === 0 ); - } } diff --git a/gswaMIDIControllersManager/gswaMIDIControllerOutput.js b/gswaMIDIControllersManager/gswaMIDIControllerOutput.js index b80ac5a..13575ec 100644 --- a/gswaMIDIControllersManager/gswaMIDIControllerOutput.js +++ b/gswaMIDIControllersManager/gswaMIDIControllerOutput.js @@ -1,18 +1,16 @@ "use strict"; class gswaMIDIControllerOutput { - constructor( portID, name, output, sysexEnabled ) { - this.portID = portID; - this.name = name; - this.output = output; - this.sysexEnabled = sysexEnabled; - } + #port = null; + #sysex = false; - $getOutput() { - return this.output; + constructor( output, sysex ) { + this.#port = output; + this.#sysex = sysex; } + #send( bytes ) { - this.output.send( bytes ); + this.#port.send( bytes ); } #sendToTarget( bytes, targetOutput ) { targetOutput.send( bytes ); diff --git a/gswaMIDIControllersManager/gswaMIDIControllersManager.js b/gswaMIDIControllersManager/gswaMIDIControllersManager.js index 0e48176..e555268 100644 --- a/gswaMIDIControllersManager/gswaMIDIControllersManager.js +++ b/gswaMIDIControllersManager/gswaMIDIControllersManager.js @@ -1,8 +1,5 @@ "use strict"; -const MIDI_CHANNEL_NOTEON = 0x90; -const MIDI_CHANNEL_NOTEOFF = 0x80; - class gswaMIDIControllersManager { #uiKeys = null; #midiAccess = null; @@ -17,19 +14,11 @@ class gswaMIDIControllersManager { $setPianorollKeys( uiKeys ) { this.#uiKeys = uiKeys; } - $linkToPianoroll( midiCtrl ) { - midiCtrl.$onNoteOnAdd( this.#pianorollLiveKeyPressed.bind( this ) ); - midiCtrl.$onNoteOffAdd( this.#pianoRollLiveKeyReleased.bind( this ) ); - } - $unlinkFromPianoroll( midiCtrl ) { - midiCtrl.$onNoteOnRemove( this.#pianorollLiveKeyPressed.bind( this ) ); - midiCtrl.$onNoteOffRemove( this.#pianoRollLiveKeyReleased.bind( this ) ); + #pianoRollLiveKeyReleased( key ) { + this.#uiKeys?.$midiKeyUp( key ); } - #pianoRollLiveKeyReleased( midiCtrlData ) { - this.#uiKeys?.$midiKeyUp( midiCtrlData[ 1 ] ); - } - #pianorollLiveKeyPressed( midiCtrlData ) { - this.#uiKeys?.$midiKeyDown( midiCtrlData[ 1 ] ); + #pianorollLiveKeyPressed( key ) { + this.#uiKeys?.$midiKeyDown( key ); } // ......................................................................... @@ -57,15 +46,15 @@ class gswaMIDIControllersManager { switch ( port.type ) { case "input": if ( !this.#midiCtrlInputs.has( port.id ) ) { - const ctrl = new gswaMIDIControllerInput( port.id, port.name, port, sysexEnabled ); - - this.#midiCtrlInputs.set( port.id, ctrl ); - this.$linkToPianoroll( ctrl ); + this.#midiCtrlInputs.set( port.id, new gswaMIDIControllerInput( port, sysexEnabled, { + $onNoteOn: this.#pianorollLiveKeyPressed.bind( this ), + $onNoteOff: this.#pianoRollLiveKeyReleased.bind( this ), + } ) ); } break; case "output": if ( !this.#midiCtrlOutputs.has( port.id ) ) { - this.#midiCtrlOutputs.set( port.id, new gswaMIDIControllerOutput( port.id, port.name, port, sysexEnabled ) ); + this.#midiCtrlOutputs.set( port.id, new gswaMIDIControllerOutput( port, sysexEnabled ) ); } break; }