@@ -247,8 +247,7 @@ const config = new Config(function (changed) {
247
247
if ( changed . microphoneChannel !== undefined ) {
248
248
const channel = changed . microphoneChannel ;
249
249
console . log ( `Moving microphone to channel ${ channel } ` ) ;
250
- processor . adconverter . setChannelSource ( channel , microphoneInput ) ;
251
- setupMicrophone ( ) . then ( ( ) => { } ) ;
250
+ setupMicrophone ( channel ) . then ( ( ) => { } ) ;
252
251
}
253
252
updateUrl ( ) ;
254
253
} ) ;
@@ -521,7 +520,20 @@ microphoneInput.setErrorCallback((message) => {
521
520
showError ( "accessing microphone" , message ) ;
522
521
} ) ;
523
522
524
- async function setupMicrophone ( ) {
523
+ async function ensureMicrophoneRunning ( ) {
524
+ if ( microphoneInput . audioContext && microphoneInput . audioContext . state !== "running" ) {
525
+ try {
526
+ await microphoneInput . audioContext . resume ( ) ;
527
+ console . log ( "Microphone: Audio context resumed, new state:" , microphoneInput . audioContext . state ) ;
528
+ } catch ( err ) {
529
+ console . error ( "Microphone: Error resuming audio context:" , err ) ;
530
+ return false ;
531
+ }
532
+ }
533
+ return true ;
534
+ }
535
+
536
+ async function setupMicrophone ( channel ) {
525
537
const $micPermissionStatus = $ ( "#micPermissionStatus" ) ;
526
538
$micPermissionStatus . text ( "Requesting microphone access..." ) ;
527
539
@@ -530,31 +542,15 @@ async function setupMicrophone() {
530
542
if ( success ) {
531
543
console . log ( "Microphone: Successfully initialised from URL parameters" ) ;
532
544
// Set microphone as source for its channel
533
- console . log ( `Setting microphone as source for channel ${ parsedQuery . microphoneChannel } ` ) ;
534
- processor . adconverter . setChannelSource ( parsedQuery . microphoneChannel , microphoneInput ) ;
545
+ console . log ( `Setting microphone as source for channel ${ channel } ` ) ;
546
+ processor . adconverter . setChannelSource ( channel , microphoneInput ) ;
535
547
$micPermissionStatus . text ( "Microphone connected successfully" ) ;
536
548
537
- // Ensure audio context is running
538
- if ( microphoneInput . audioContext ) {
539
- console . log (
540
- "Microphone: Ensuring audio context is running, current state:" ,
541
- microphoneInput . audioContext . state ,
542
- ) ;
543
- try {
544
- await microphoneInput . audioContext . resume ( ) ;
545
- console . log ( "Microphone: Audio context resumed, new state:" , microphoneInput . audioContext . state ) ;
546
- } catch ( err ) {
547
- console . error ( "Microphone: Error resuming audio context:" , err ) ;
548
- }
549
- }
549
+ await ensureMicrophoneRunning ( ) ;
550
550
551
551
// Try starting audio context from user gesture
552
- const tryAgain = ( ) => {
553
- if ( microphoneInput . audioContext && microphoneInput . audioContext . state !== "running" ) {
554
- console . log ( "Microphone: Auto-starting audio context from click" ) ;
555
- microphoneInput . audioContext . resume ( ) ;
556
- }
557
- document . removeEventListener ( "click" , tryAgain ) ;
552
+ const tryAgain = async ( ) => {
553
+ if ( await ensureMicrophoneRunning ( ) ) document . removeEventListener ( "click" , tryAgain ) ;
558
554
} ;
559
555
document . addEventListener ( "click" , tryAgain ) ;
560
556
} else {
@@ -574,7 +570,7 @@ if (parsedQuery.microphoneChannel !== undefined) {
574
570
// This is needed because some browsers require user interaction for audio context
575
571
setTimeout ( async ( ) => {
576
572
console . log ( "Microphone: Delayed initialisation starting" ) ;
577
- await setupMicrophone ( ) ;
573
+ await setupMicrophone ( parsedQuery . microphoneChannel ) ;
578
574
} , 1000 ) ;
579
575
}
580
576
@@ -659,14 +655,8 @@ keyboard.registerKeyHandler(
659
655
660
656
// Setup key handlers
661
657
document . onkeydown = ( evt ) => {
662
- audioHandler . tryResume ( ) ;
663
- // Also try to resume microphone audio context if needed
664
- if ( microphoneInput . audioContext && microphoneInput . audioContext . state !== "running" ) {
665
- console . log ( "Microphone: Auto-resuming audio context on keydown" ) ;
666
- microphoneInput . audioContext
667
- . resume ( )
668
- . catch ( ( err ) => console . error ( "Error resuming microphone audio context:" , err ) ) ;
669
- }
658
+ audioHandler . tryResume ( ) . then ( ( ) => { } ) ;
659
+ ensureMicrophoneRunning ( ) . then ( ( ) => { } ) ;
670
660
keyboard . keyDown ( evt ) ;
671
661
} ;
672
662
document . onkeypress = ( evt ) => keyboard . keyPress ( evt ) ;
0 commit comments