Initial commit
This commit is contained in:
commit
91a1e2e312
22 files changed
+885
No files matched your search
@@ -0,0 +1,23 @@
|
||||
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));
|
||||
Reference in new issue
Block a user