wait for audio ready before starting cart, add button to unsuspend audio

fixes missing sound when auto-starting cart in chrome
This commit is contained in:
2022-03-06 14:08:44 +01:00
parent 0d514c7dd3
commit 2033f9a172
5 changed files with 52 additions and 8 deletions

View File

@@ -104,9 +104,6 @@ export default function MicroW8(screen, config = {}) {
keepRunning = false;
}
await audioContext.audioWorklet.addModule(audioWorkletUrl);
let audioNode = new AudioNode(audioContext);
let cartridgeSize = data.byteLength;
config.setMessage(cartridgeSize);
@@ -114,6 +111,40 @@ export default function MicroW8(screen, config = {}) {
return;
}
await audioContext.audioWorklet.addModule(audioWorkletUrl);
let audioNode = new AudioNode(audioContext);
let audioReadyFlags = 0;
let audioReadyResolve;
let audioReadyPromise = new Promise(resolve => audioReadyResolve = resolve);
let updateAudioReady = (f) => {
audioReadyFlags |= f;
if(audioReadyFlags == 3 && audioReadyResolve) {
audioReadyResolve(true);
audioReadyResolve = null;
}
};
audioNode.port.onmessage = (e) => updateAudioReady(e.data);
let audioStateChange = () => {
if(audioContext.state == 'suspended') {
if(config.startButton) {
config.startButton.style = '';
screen.style = 'display:none';
}
(config.startButton || screen).onclick = () => {
audioContext.resume();
};
} else {
if(config.startButton) {
config.startButton.style = 'display:none';
screen.style = '';
}
updateAudioReady(1);
}
};
audioContext.onstatechange = audioStateChange;
audioStateChange();
currentData = data;
let newURL = window.location.pathname;
@@ -189,6 +220,8 @@ export default function MicroW8(screen, config = {}) {
let instance = await instantiate(data);
let buffer = U32(imageData.data.buffer);
await audioReadyPromise;
let startTime = Date.now();