Skip to content

Commit 05c4752

Browse files
committed
Feedback
1 parent 243b522 commit 05c4752

File tree

1 file changed

+23
-33
lines changed

1 file changed

+23
-33
lines changed

src/main.js

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,7 @@ const config = new Config(function (changed) {
247247
if (changed.microphoneChannel !== undefined) {
248248
const channel = changed.microphoneChannel;
249249
console.log(`Moving microphone to channel ${channel}`);
250-
processor.adconverter.setChannelSource(channel, microphoneInput);
251-
setupMicrophone().then(() => {});
250+
setupMicrophone(channel).then(() => {});
252251
}
253252
updateUrl();
254253
});
@@ -521,7 +520,20 @@ microphoneInput.setErrorCallback((message) => {
521520
showError("accessing microphone", message);
522521
});
523522

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) {
525537
const $micPermissionStatus = $("#micPermissionStatus");
526538
$micPermissionStatus.text("Requesting microphone access...");
527539

@@ -530,31 +542,15 @@ async function setupMicrophone() {
530542
if (success) {
531543
console.log("Microphone: Successfully initialised from URL parameters");
532544
// 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);
535547
$micPermissionStatus.text("Microphone connected successfully");
536548

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();
550550

551551
// 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);
558554
};
559555
document.addEventListener("click", tryAgain);
560556
} else {
@@ -574,7 +570,7 @@ if (parsedQuery.microphoneChannel !== undefined) {
574570
// This is needed because some browsers require user interaction for audio context
575571
setTimeout(async () => {
576572
console.log("Microphone: Delayed initialisation starting");
577-
await setupMicrophone();
573+
await setupMicrophone(parsedQuery.microphoneChannel);
578574
}, 1000);
579575
}
580576

@@ -659,14 +655,8 @@ keyboard.registerKeyHandler(
659655

660656
// Setup key handlers
661657
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(() => {});
670660
keyboard.keyDown(evt);
671661
};
672662
document.onkeypress = (evt) => keyboard.keyPress(evt);

0 commit comments

Comments
 (0)