Skip to content

Commit

Permalink
gswaMIDIControllersManager: renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
mr21 committed Jun 8, 2024
1 parent 164f3a8 commit 06a4f6c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 60 deletions.
31 changes: 16 additions & 15 deletions gswaMIDIControllersManager/gswaMIDIControllerInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
3 changes: 2 additions & 1 deletion gswaMIDIControllersManager/gswaMIDIControllerOutput.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
76 changes: 32 additions & 44 deletions gswaMIDIControllersManager/gswaMIDIControllersManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 }]` );
}
}
}

0 comments on commit 06a4f6c

Please sign in to comment.