diff options
Diffstat (limited to 'client/lib/organ.js')
| -rw-r--r-- | client/lib/organ.js | 43 |
1 files changed, 31 insertions, 12 deletions
diff --git a/client/lib/organ.js b/client/lib/organ.js index 652351e..e66f89d 100644 --- a/client/lib/organ.js +++ b/client/lib/organ.js @@ -4,30 +4,31 @@ */ import Tone from "tone"; -import { roundFreq } from "./util"; +import { roundInterval } from "./util"; -const oscillators = {}; +let root = 440; +let oscillators = {}; let output; let lastPlayed; function load(out) { output = out; } - -function isPlaying(freq) { - const rounded = roundFreq(freq); +function isPlaying(interval) { + const rounded = roundInterval(interval); const osc = oscillators[rounded]; return osc && osc.playing; } -function play(freq) { +function play(interval) { if (!output) { return; } - const rounded = roundFreq(freq); + const rounded = roundInterval(interval); const osc = (oscillators[rounded] = oscillators[rounded] || {}); if (!osc.el) { - osc.el = new Tone.Oscillator(freq, "sine"); + osc.interval = interval; + osc.el = new Tone.Oscillator(interval * root, "sine"); osc.el.connect(output); } osc.el.start(); @@ -36,13 +37,31 @@ function play(freq) { return osc; } -function pause(freq) { - const rounded = roundFreq(freq); +function pause(interval) { + const rounded = roundInterval(interval); if (!oscillators[rounded]) return; const osc = (oscillators[rounded] = oscillators[rounded] || {}); - if (osc.el) osc.el.stop(); + if (osc.el) { + osc.el.stop(); + } osc.playing = false; return osc; } -export default { load, isPlaying, play, pause, oscillators }; +function setRoot(newRoot) { + root = newRoot; + for (const osc of Object.values(oscillators)) { + osc.el.frequency.value = osc.interval * newRoot; + } +} +function stop() { + for (const osc of Object.values(oscillators)) { + osc.el.stop(); + osc.el.disconnect(); + osc.playing = false; + delete osc.el; + } + oscillators = {}; +} + +export default { load, isPlaying, play, pause, stop, setRoot }; |
