summaryrefslogtreecommitdiff
path: root/client/lib/organ.js
blob: 9d0ac907262034775c71502b7cfd399db1eab75f (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
37
38
39
40
41
42
43
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 };