-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-template.html
74 lines (56 loc) · 1.62 KB
/
test-template.html
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<html>
<head>
<title>@MODULE_NAME@ test page</title>
</head>
<body>
<button onclick="play()">Play</button>
</body>
<script>
const audioCtx = new (window.AudioContext || window.webkitAudioContext)({
sampleRate: 48000,
});
let osc;
const _osc_white = () => {
let retval = Math.fround(Math.random() * 2 - 1);
return retval;
};
const loadOsc = async() => {
return await WebAssembly.instantiateStreaming(fetch("@[email protected]"), {
env: { _osc_white },
});
};
const oscPromise = loadOsc();
const init = async () => {
osc = await oscPromise;
};
init();
const play = async () => {
osc = await oscPromise;
const { init, cycle, allocate_sample_buffer } = osc.instance.exports;
const frames = audioCtx.sampleRate * 3;
const buf = allocate_sample_buffer(frames);
init(0, 0);
cycle(buf, frames)
// Create an empty three-second stereo buffer at the sample rate of the AudioContext
const audioBuffer = audioCtx.createBuffer(
1,
frames,
audioCtx.sampleRate
);
for (let channel = 0; channel < audioBuffer.numberOfChannels; channel++) {
const source = new Float32Array(osc.instance.exports.memory.buffer, buf, frames);
audioBuffer.copyToChannel(source, channel);
}
// Get an AudioBufferSourceNode.
// This is the AudioNode to use when we want to play an AudioBuffer
const source = audioCtx.createBufferSource();
// set the buffer in the AudioBufferSourceNode
source.buffer = audioBuffer;
// connect the AudioBufferSourceNode to the
// destination so we can hear the sound
source.connect(audioCtx.destination);
// start the source playing
source.start();
}
</script>
</html>