diff options
| author | Martin Hafskjold Thoresen <martin@vind.ai> | 2025-01-10 18:40:59 +0100 |
|---|---|---|
| committer | Martin Hafskjold Thoresen <martin@vind.ai> | 2025-01-10 18:40:59 +0100 |
| commit | 4b090965a2c76af9c997853c8cc16c643befdceb (patch) | |
| tree | 795faa984632eaabc4c240cf08d0434f11021a61 /static/main.js | |
| parent | ed791666b2575fd1e60934f0a178441e063bdfca (diff) | |
| download | musicgame-4b090965a2c76af9c997853c8cc16c643befdceb.tar.gz musicgame-4b090965a2c76af9c997853c8cc16c643befdceb.zip | |
Randomly choose sound effect
Also some JS cleanup
Diffstat (limited to 'static/main.js')
| -rw-r--r-- | static/main.js | 22 |
1 files changed, 22 insertions, 0 deletions
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); +} |
