@@ -297,6 +297,8 @@ class SkMidi {
297297 private _noteonListeners : ( ( e : InputEventNoteon ) => void ) [ ] = [ ] ;
298298 private _noteoffListeners : ( ( e : InputEventNoteoff ) => void ) [ ] = [ ] ;
299299
300+ private static _initializedWithUserConfig = false ;
301+
300302 private midiAccess : WebMidi . MIDIAccess ;
301303
302304 private _state : 'nodevice' | 'notsupported' | 'ready' ;
@@ -312,7 +314,6 @@ class SkMidi {
312314 navigator . requestMIDIAccess ( ) . then ( ( access ) => {
313315 this . midiAccess = access ;
314316 this . midiAccess . onstatechange = ( e ) => {
315- // this.init().then(this._stateChangeListener);
316317 alertUser ( {
317318 text : `Midi device ${ e . port . name } is ${ e . port . state } ` ,
318319 status : e . port . state === 'connected' ? Status . ok : Status . error ,
@@ -324,6 +325,7 @@ class SkMidi {
324325 this . _state = 'notsupported' ;
325326 }
326327 }
328+
327329 try {
328330 this . webmidi . disable ( ) ;
329331 } catch ( e ) {
@@ -332,7 +334,7 @@ class SkMidi {
332334 }
333335
334336 return new Promise < boolean > ( ( resolve , reject ) => {
335- this . webmidi . enable ( ( err ) => {
337+ this . webmidi . enable ( async ( err ) => {
336338 if ( err ) {
337339 this . _state = 'notsupported' ;
338340 console . log ( `Webmidi not enabled: ${ err } ` ) ;
@@ -345,9 +347,16 @@ class SkMidi {
345347 console . log ( `Inputs: ${ JSON . stringify ( this . webmidi . inputs ) } ` ) ;
346348 console . log ( `Outputs: ${ JSON . stringify ( this . webmidi . outputs ) } ` ) ;
347349
350+ // set defaults first
348351 this . output = this . webmidi . outputs [ 0 ] ;
349352 this . input = this . webmidi . inputs [ 0 ] ;
350353
354+ // but try to load configurfed devices
355+ if ( ! SkMidi . _initializedWithUserConfig ) {
356+ await this . loadUserConfiguration ( ) ;
357+ SkMidi . _initializedWithUserConfig = true ;
358+ }
359+
351360 if ( this . input && this . output ) {
352361 console . log ( 'midi init state: ready' ) ;
353362 this . _state = 'ready' ;
@@ -363,6 +372,43 @@ class SkMidi {
363372 } ) ;
364373 } ) ;
365374 }
375+
376+ private async loadUserConfiguration ( ) : Promise < void > {
377+ try {
378+ // Import necessary modules
379+ const { getCurrentUser } = await import ( '@/stores/useAuthStore' ) ;
380+ const user = await getCurrentUser ( ) ;
381+
382+ if ( ! user ) {
383+ console . log ( 'No user found, using default MIDI configuration' ) ;
384+ return ;
385+ }
386+
387+ // Get all course settings that might have MIDI configurations
388+ const courses = await user . getActiveCourses ( ) ;
389+ for ( const course of courses ) {
390+ const settings = await user . getCourseSettings ( course . courseID ) ;
391+ if ( settings ?. midiinput || settings ?. midioutput ) {
392+ // We found MIDI settings, use the first valid ones
393+ if ( settings . midiinput && this . webmidi . getInputById ( settings . midiinput . toString ( ) ) ) {
394+ this . selectInput ( settings . midiinput . toString ( ) ) ;
395+ console . log ( `Loaded saved MIDI input: ${ settings . midiinput } ` ) ;
396+ }
397+
398+ if ( settings . midioutput && this . webmidi . getOutputById ( settings . midioutput . toString ( ) ) ) {
399+ this . selectOutput ( settings . midioutput . toString ( ) ) ;
400+ console . log ( `Loaded saved MIDI output: ${ settings . midioutput } ` ) ;
401+ }
402+
403+ // Once we've found and applied settings, we can stop searching
404+ break ;
405+ }
406+ }
407+ } catch ( error ) {
408+ console . error ( 'Failed to load user MIDI configuration:' , error ) ;
409+ }
410+ }
411+
366412 private _stateChangeListener : ( ) => void ;
367413
368414 public setStateChangeListener ( f : ( ) => void ) {
0 commit comments