From 4b090965a2c76af9c997853c8cc16c643befdceb Mon Sep 17 00:00:00 2001 From: Martin Hafskjold Thoresen Date: Fri, 10 Jan 2025 18:40:59 +0100 Subject: Randomly choose sound effect Also some JS cleanup --- static/main.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 static/main.js (limited to 'static') diff --git a/static/main.js b/static/main.js new file mode 100644 index 0000000..144254f --- /dev/null +++ b/static/main.js @@ -0,0 +1,22 @@ +let _context; +function getContext() { + if (!_context) { + _context = new AudioContext(); + } + return _context; +} + +function loadSample(url) { + return fetch(url) + .then((response) => response.arrayBuffer()) + .then((buffer) => getContext().decodeAudioData(buffer)); +} + +function playSoundSample(sample, sampleNote, noteToPlay) { + const ctx = getContext(); + const source = ctx.createBufferSource(); + source.buffer = sample; + source.playbackRate.value = 2 ** ((noteToPlay - sampleNote) / 12); + source.connect(ctx.destination); + source.start(0); +} -- cgit v1.2.3