From 0c12b6e1ed8a890ee057a09b35d92b812c8335bd Mon Sep 17 00:00:00 2001 From: Dannii Willis Date: Wed, 18 Dec 2024 12:25:26 +1000 Subject: [PATCH] Try to prime the audio system so that sounds starting after a timer will work --- src/glkote/web/schannels.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/glkote/web/schannels.ts b/src/glkote/web/schannels.ts index 95fb455..ce868c5 100644 --- a/src/glkote/web/schannels.ts +++ b/src/glkote/web/schannels.ts @@ -9,12 +9,15 @@ https://github.com/curiousdannii/asyncglk */ -import {fetch_resource} from '../../common/file/browser.js' +import {fetch_resource, parse_base64} from '../../common/file/browser.js' import * as protocol from '../../common/protocol.js' import WebGlkOte from './web.js' import GlkAudio_init, {decode as GlkAudio_decode, wasm as GlkAudio_is_ready} from 'glkaudio' +// From https://github.com/compulim/web-speech-cognitive-services/issues/34 +const priming_mp3 = 'SUQzBAAAAAAAI1RTU0UAAAAPAAADTGF2ZjU3LjU2LjEwMQAAAAAAAAAAAAAA//tAwAAAAAAAAAAAAAAAAAAAAAAASW5mbwAAAA8AAAACAAABhgC7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7//////////////////////////////////////////////////////////////////8AAAAATGF2YzU3LjY0AAAAAAAAAAAAAAAAJAUHAAAAAAAAAYYoRBqpAAAAAAD/+xDEAAPAAAGkAAAAIAAANIAAAARMQU1FMy45OS41VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVf/7EMQpg8AAAaQAAAAgAAA0gAAABFVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV' + export class SoundChannelManager extends Map { private context: AudioContext private glkote: WebGlkOte @@ -23,6 +26,9 @@ export class SoundChannelManager extends Map { super() this.glkote = glkote this.context = new AudioContext() + + // Try to prime the audio system so that sounds starting after a timer will work + this.prime() } async update(schannels: protocol.SoundChannelUpdate[]) { @@ -50,6 +56,23 @@ export class SoundChannelManager extends Map { } } } + + private async prime() { + const context = this.context + const gain = context.createGain() + + const source = this.context.createBufferSource() + const data = await parse_base64(priming_mp3) + source.buffer = await context.decodeAudioData(data.buffer) + source.connect(gain) + + gain.connect(context.destination) + source.start() + setTimeout(() => { + gain.disconnect() + source.stop() + }, 10) + } } export class SoundChannel {