diff --git a/gswaMIDIControllersManager/gswaMIDIControllerInput.js b/gswaMIDIControllersManager/gswaMIDIControllerInput.js index 52bbc4f..6360d70 100644 --- a/gswaMIDIControllersManager/gswaMIDIControllerInput.js +++ b/gswaMIDIControllersManager/gswaMIDIControllerInput.js @@ -3,37 +3,38 @@ class gswaMIDIControllerInput { constructor( portID, name, input, sysexEnabled ) { this.portID = portID; - this.name = name; - this.input = input; + this.name = name; + this.input = input; this.sysexEnabled = sysexEnabled; this.input.onmidimessage = this.#midiEvents.bind( this ); this.listeners = { onNoteOn: [], - onNoteOff: [] + onNoteOff: [], }; } + $getInput() { return this.input; } - $onNoteOnAdd( callback ) { - this.listeners.onNoteOn.push( callback ); + $onNoteOnAdd( cb ) { + this.listeners.onNoteOn.push( cb ); } - $onNoteOffAdd( callback ) { - this.listeners.onNoteOff.push( callback ); + $onNoteOffAdd( cb ) { + this.listeners.onNoteOff.push( cb ); } - #midiEvents( event ) { - if ( !this.sysexEnabled && event.data.length !== 3 ) { - console.log( 'GSLog : Invalid midi event'); - } else if ( this.#isNoteOn( event.data[0], event.data[2] )) { - this.listeners.onNoteOn.forEach( callback => callback( event.data )); - } else if ( this.#isNoteOff( event.data[0], event.data[2] )) { - this.listeners.onNoteOff.forEach( callback => callback( event.data )); + #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 ) ); } } #isNoteOn( channel, velocity ) { return ( channel === MIDI_CHANNEL_NOTEON && velocity !== 0 ); } #isNoteOff( channel, velocity ) { - return ( channel === MIDI_CHANNEL_NOTEOFF || velocity === 0); + return ( channel === MIDI_CHANNEL_NOTEOFF || velocity === 0 ); } } diff --git a/gswaMIDIControllersManager/gswaMIDIControllerOutput.js b/gswaMIDIControllersManager/gswaMIDIControllerOutput.js index 2d9090d..b80ac5a 100644 --- a/gswaMIDIControllersManager/gswaMIDIControllerOutput.js +++ b/gswaMIDIControllersManager/gswaMIDIControllerOutput.js @@ -3,10 +3,11 @@ class gswaMIDIControllerOutput { constructor( portID, name, output, sysexEnabled ) { this.portID = portID; - this.name = name; + this.name = name; this.output = output; this.sysexEnabled = sysexEnabled; } + $getOutput() { return this.output; } diff --git a/gswaMIDIControllersManager/gswaMIDIControllersManager.js b/gswaMIDIControllersManager/gswaMIDIControllersManager.js index 645a045..104626e 100644 --- a/gswaMIDIControllersManager/gswaMIDIControllersManager.js +++ b/gswaMIDIControllersManager/gswaMIDIControllersManager.js @@ -5,18 +5,27 @@ const MIDI_CHANNEL_NOTEOFF = 0x80; class gswaMIDIControllersManager { #uiKeys = null; - #MIDIControllersInput = new Map(); - #MIDIControllersOutput = new Map(); + #midiCtrlInputs = new Map(); + #midiCtrlOutputs = new Map(); $initMidiAccess( midiAccess ) { this.sysexEnabled = midiAccess.sysexEnabled; midiAccess.onstatechange = this.#onControllerStateChange.bind( this ); - this.#openAlreadyConnectedControllers( midiAccess ); } $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 ) ); + } + + // ......................................................................... #openAlreadyConnectedControllers( midiAccess ) { for ( const entry of midiAccess.inputs ) { entry[ 1 ].open(); @@ -25,63 +34,42 @@ class gswaMIDIControllersManager { entry[ 1 ].open(); } } - #onControllerStateChange( event ) { - if ( event.port.state === "connected" && event.port.connection === "open" ) { - this.#addController( event.port, event.currentTarget.sysexEnabled ) - } else if ( event.port.state === "disconnected" ) { - this.#removeDevice( event.port ); + #onControllerStateChange( e ) { + if ( e.port.state === "connected" && e.port.connection === "open" ) { + this.#addController( e.port, e.currentTarget.sysexEnabled ) + } else if ( e.port.state === "disconnected" ) { + this.#removeDevice( e.port ); } } #addController( port, sysexEnabled ) { - if ( port.type === "input" && !this.#MIDIControllersInput.has( port.id ) ) { + if ( port.type === "input" && !this.#midiCtrlInputs.has( port.id ) ) { const ctrler = new gswaMIDIControllerInput( port.id, port.name, port, sysexEnabled ); - this.#MIDIControllersInput.set( port.id, ctrler ); + this.#midiCtrlInputs.set( port.id, ctrler ); this.$linkToPianoroll( ctrler ); - } else if ( port.type === "output" && !this.#MIDIControllersOutput.has( port.id ) ) { + } else if ( port.type === "output" && !this.#midiCtrlOutputs.has( port.id ) ) { const ctrler = new gswaMIDIControllerOutput( port.id, port.name, port, sysexEnabled ); - this.#MIDIControllersOutput.set( port.id, ctrler ); + this.#midiCtrlOutputs.set( port.id, ctrler ); } } #removeDevice( port ) { - const MIDIControllers = port.type === "input" - ? this.#MIDIControllersInput - : this.#MIDIControllersOutput; - - MIDIControllers.delete( port.id ); + ( port.type === "input" + ? this.#midiCtrlInputs + : this.#midiCtrlOutputs ).delete( port.id ); } - - // ----------------- // Linkable functions - // These functions are used to associate controllers buttons, keys, etc to actions or instruments - $linkToPianoroll( MIDIController ) { - MIDIController.$onNoteOnAdd( this.#pianorollLiveKeyPressed.bind( this ) ); - MIDIController.$onNoteOffAdd( this.#pianoRollLiveKeyReleased.bind( this ) ); - } - $unlinkFromPianoroll( MIDIController ) { - MIDIController.$onNoteOnRemove( this.#pianorollLiveKeyPressed.bind( this ) ); - MIDIController.$onNoteOffRemove( this.#pianoRollLiveKeyReleased.bind( this ) ); + #pianoRollLiveKeyReleased( midiCtrlData ) { + this.#uiKeys?.$midiKeyUp( midiCtrlData[ 1 ] ); } - - // ---------------- // Action functions - #pianoRollLiveKeyReleased( MIDIControllerData ) { - this.#uiKeys?.$midiKeyUp( MIDIControllerData[ 1 ] ); + #pianorollLiveKeyPressed( midiCtrlData ) { + this.#uiKeys?.$midiKeyDown( midiCtrlData[ 1 ] ); } - #pianorollLiveKeyPressed( MIDIControllerData ) { - this.#uiKeys?.$midiKeyDown( MIDIControllerData[ 1 ] ); - } - - // ---------------- // print #printControllers() { - console.log( "Inputs" ); - for ( const [ ctrlerID, ctrler ] of this.#MIDIControllersInput ) { - console.log( "ID : ", ctrlerID ); - console.log( "Name : ", ctrler.name ); + for ( const [ id, ctrl ] of this.#midiCtrlInputs ) { + console.log( `input: id[${ id }] name[${ ctrl.name }]` ); } - console.log( "Outputs" ); - for ( const [ ctrlerID, ctrler ] of this.#MIDIControllersOutput ) { - console.log( "ID : ", ctrlerID ); - console.log( "Name : ", ctrler.name ); + for ( const [ id, ctrl ] of this.#midiCtrlOutputs ) { + console.log( `output: id[${ id }] name[${ ctrl.name }]` ); } } }