24 lines
694 B
JavaScript
24 lines
694 B
JavaScript
const { instance } = await WebAssembly.instantiateStreaming(fetch("beep.wasm"));
|
|
|
|
let ctx = undefined;
|
|
|
|
const count = 44100;
|
|
const ptr = instance.exports.get_samples();
|
|
|
|
async function playTone(frequency = 440) {
|
|
ctx ??= new AudioContext();
|
|
await ctx.resume();
|
|
|
|
instance.exports.beep(ctx.sampleRate, frequency);
|
|
const audioBuffer = ctx.createBuffer(1, count, ctx.sampleRate);
|
|
const audioChannel = audioBuffer.getChannelData(0);
|
|
audioChannel.set(new Float32Array(instance.exports.memory.buffer, ptr, count));
|
|
|
|
const src = ctx.createBufferSource();
|
|
src.buffer = audioBuffer;
|
|
src.connect(ctx.destination);
|
|
src.start();
|
|
}
|
|
|
|
window.addEventListener("click", () => playTone(440));
|