2017-10-30 18:25:58 +01:00
|
|
|
|
|
|
|
namespace pxsim.music {
|
|
|
|
export function fromWAV(buf: RefBuffer) {
|
|
|
|
return incr(buf)
|
|
|
|
}
|
2017-12-14 01:31:42 +01:00
|
|
|
|
2017-12-15 19:42:44 +01:00
|
|
|
export function stopAllSounds() {
|
2017-12-14 01:31:42 +01:00
|
|
|
SoundMethods.stop()
|
|
|
|
}
|
2017-10-30 18:25:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace pxsim.SoundMethods {
|
2017-12-14 01:31:42 +01:00
|
|
|
let audio: HTMLAudioElement;
|
2017-10-30 18:25:58 +01:00
|
|
|
|
|
|
|
export function buffer(buf: RefBuffer) {
|
|
|
|
return incr(buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function uint8ArrayToString(input: Uint8Array) {
|
|
|
|
let len = input.length;
|
|
|
|
let res = ""
|
|
|
|
for (let i = 0; i < len; ++i)
|
|
|
|
res += String.fromCharCode(input[i]);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function play(buf: RefBuffer, volume: number) {
|
2018-01-04 21:20:31 +01:00
|
|
|
if (!buf) {
|
2017-12-08 01:27:28 +01:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2017-10-30 18:25:58 +01:00
|
|
|
return new Promise<void>(resolve => {
|
|
|
|
let url = "data:audio/wav;base64," + btoa(uint8ArrayToString(buf.data))
|
2017-12-14 01:31:42 +01:00
|
|
|
audio = new Audio(url)
|
2018-01-04 21:20:31 +01:00
|
|
|
audio.onended = () => resolve();
|
|
|
|
audio.onpause = () => resolve();
|
|
|
|
audio.play();
|
2017-10-30 18:25:58 +01:00
|
|
|
})
|
|
|
|
}
|
2017-12-14 01:31:42 +01:00
|
|
|
|
|
|
|
export function stop() {
|
2017-12-14 18:41:01 +01:00
|
|
|
return new Promise<void>(resolve => {
|
|
|
|
if (audio) {
|
|
|
|
audio.pause();
|
|
|
|
}
|
2018-01-04 21:20:31 +01:00
|
|
|
resolve();
|
2017-12-14 18:41:01 +01:00
|
|
|
})
|
2017-12-14 01:31:42 +01:00
|
|
|
}
|
|
|
|
|
2017-10-30 18:25:58 +01:00
|
|
|
}
|
|
|
|
|