Skip to content

Commit

Permalink
gswaMIDIControllersManager: events, simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
mr21 committed Jun 8, 2024
1 parent fcd03f9 commit 05f983b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 61 deletions.
51 changes: 19 additions & 32 deletions gswaMIDIControllersManager/gswaMIDIControllerInput.js
Original file line number Diff line number Diff line change
@@ -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 );
}
}
16 changes: 7 additions & 9 deletions gswaMIDIControllersManager/gswaMIDIControllerOutput.js
Original file line number Diff line number Diff line change
@@ -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 );
Expand Down
29 changes: 9 additions & 20 deletions gswaMIDIControllersManager/gswaMIDIControllersManager.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
"use strict";

const MIDI_CHANNEL_NOTEON = 0x90;
const MIDI_CHANNEL_NOTEOFF = 0x80;

class gswaMIDIControllersManager {
#uiKeys = null;
#midiAccess = null;
Expand All @@ -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 );
}

// .........................................................................
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 05f983b

Please sign in to comment.