summaryrefslogtreecommitdiff
path: root/static/main.js
diff options
context:
space:
mode:
Diffstat (limited to 'static/main.js')
-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);
+}