summaryrefslogtreecommitdiff
path: root/static
diff options
context:
space:
mode:
authorMartin Hafskjold Thoresen <martin@vind.ai>2025-01-10 18:40:59 +0100
committerMartin Hafskjold Thoresen <martin@vind.ai>2025-01-10 18:40:59 +0100
commit4b090965a2c76af9c997853c8cc16c643befdceb (patch)
tree795faa984632eaabc4c240cf08d0434f11021a61 /static
parented791666b2575fd1e60934f0a178441e063bdfca (diff)
downloadmusicgame-4b090965a2c76af9c997853c8cc16c643befdceb.tar.gz
musicgame-4b090965a2c76af9c997853c8cc16c643befdceb.zip
Randomly choose sound effect
Also some JS cleanup
Diffstat (limited to 'static')
-rw-r--r--static/main.js22
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);
+}