Skip to content

Commit

Permalink
Try to prime the audio system so that sounds starting after a timer w…
Browse files Browse the repository at this point in the history
…ill work
  • Loading branch information
curiousdannii committed Dec 18, 2024
1 parent 012f916 commit 051f550
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/glkote/web/schannels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number, SoundChannel> {
private context: AudioContext
private glkote: WebGlkOte
Expand All @@ -23,6 +26,9 @@ export class SoundChannelManager extends Map<number, SoundChannel> {
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[]) {
Expand Down Expand Up @@ -50,6 +56,21 @@ export class SoundChannelManager extends Map<number, SoundChannel> {
}
}
}

private async prime() {
const context = this.context

const source = this.context.createBufferSource()
const data = await parse_base64(priming_mp3)
source.buffer = await context.decodeAudioData(data.buffer)

source.connect(context.destination)
source.start()
setTimeout(() => {
source.disconnect()
source.stop()
}, 10)
}
}

export class SoundChannel {
Expand Down

0 comments on commit 051f550

Please sign in to comment.