/** * Sine wave organ * @module lib/organ.js; */ import Tone from "tone"; import { roundFreq } from "./util"; const oscillators = {}; let output; let lastPlayed; function load(out) { output = out; } function isPlaying(freq) { const rounded = roundFreq(freq); const osc = oscillators[rounded]; return osc && osc.playing; } function play(freq) { if (!output) { return; } const rounded = roundFreq(freq); const osc = (oscillators[rounded] = oscillators[rounded] || {}); if (!osc.el) { osc.el = new Tone.Oscillator(freq, "sine"); osc.el.connect(output); } osc.el.start(); osc.playing = true; lastPlayed = osc; return osc; } function pause(freq) { const rounded = roundFreq(freq); if (!oscillators[rounded]) return; const osc = (oscillators[rounded] = oscillators[rounded] || {}); if (osc.el) osc.el.stop(); osc.playing = false; return osc; } export default { load, isPlaying, play, pause, oscillators };