summaryrefslogtreecommitdiff
path: root/client/lib/organ.js
blob: f67a3e875ef20226c306cc5b1dfa5c20915d6587 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import Tone from "tone";

const oscillators = {};

let output;
let lastPlayed;

function load(out) {
  output = out;
}
function play(freq) {
  if (!output) {
    return;
  }
  const rounded = Math.floor(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 = Math.floor(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, play, pause, oscillators };