Merge pull request #399 from tidalcycles/awaitable-audio-init

can now await initAudio + initAudioOnFirstClick
This commit is contained in:
Felix Roos 2023-02-01 22:45:16 +01:00 committed by GitHub
commit c6491a3ccd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -126,21 +126,24 @@ function getWorklet(ac, processor, params) {
} }
// this function should be called on first user interaction (to avoid console warning) // this function should be called on first user interaction (to avoid console warning)
export function initAudio() { export async function initAudio() {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
try { try {
getAudioContext().resume(); await getAudioContext().resume();
loadWorklets(); await loadWorklets();
} catch (err) { } catch (err) {
console.warn('could not load AudioWorklet effects coarse, crush and shape', err); console.warn('could not load AudioWorklet effects coarse, crush and shape', err);
} }
} }
} }
export function initAudioOnFirstClick() { export async function initAudioOnFirstClick() {
document.addEventListener('click', function listener() { return new Promise((resolve) => {
initAudio(); document.addEventListener('click', async function listener() {
document.removeEventListener('click', listener); await initAudio();
resolve();
document.removeEventListener('click', listener);
});
}); });
} }