-
Notifications
You must be signed in to change notification settings - Fork 0
/
webaudio.js
44 lines (36 loc) · 1010 Bytes
/
webaudio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import decode from "../decode.js";
async function load() {
console.time("load");
const buffer = await (
await fetch("fixtures/sting_banjo_humorous_02.qoa")
).arrayBuffer();
console.timeEnd("load");
console.time("decode");
const out = decode(new Uint8Array(buffer));
console.timeEnd("decode");
return out;
}
async function play(audio) {
const el = document.querySelector("#play");
el.disabled = true;
const context = new AudioContext();
const audioBuffer = context.createBuffer(
audio.channels,
audio.samples,
audio.sampleRate
);
for (let i = 0; i < audio.channels; i++) {
audioBuffer.copyToChannel(audio.channelData[i], i);
}
const source = context.createBufferSource();
source.buffer = audioBuffer;
source.connect(context.destination);
source.onended = () => {
el.disabled = false;
};
source.start(0);
}
(async () => {
const audio = await load();
document.querySelector("#play").addEventListener("click", () => play(audio));
})();