summaryrefslogtreecommitdiff
path: root/client/lib/sine.js
blob: b414b27c8c349b207e3d6a26f7c81e855ecfb44d (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
 * Sine wave organ
 * @module lib/sine.js;
 */

import Tone from "tone";
import { roundInterval } from "./util";

let root = 440;

let oscillators = {};
let output;
let lastPlayed;

function load(out) {
  output = out;
}
function isPlaying(interval) {
  const rounded = roundInterval(interval);
  const osc = oscillators[rounded];
  return osc && osc.playing;
}
function play(interval) {
  if (!output) {
    return;
  }
  const rounded = roundInterval(interval);
  const osc = (oscillators[rounded] = oscillators[rounded] || {});
  if (!osc.el) {
    osc.interval = interval;
    osc.el = new Tone.Oscillator(interval * root, "sine");
    osc.gain = new Tone.Gain(0.025);
    osc.el.connect(osc.gain);
    osc.gain.connect(output);
  }
  osc.el.start();
  osc.playing = true;
  lastPlayed = osc;
  return osc;
}

function pause(interval) {
  const rounded = roundInterval(interval);
  if (!oscillators[rounded]) return;
  const osc = (oscillators[rounded] = oscillators[rounded] || {});
  if (osc.el) {
    osc.el.stop();
  }
  osc.playing = false;
  return osc;
}
function toggle(interval) {
  const rounded = roundInterval(interval);
  const osc = (oscillators[rounded] = oscillators[rounded] || {});
  if (osc && osc.playing) {
    pause(interval);
  } else {
    play(interval);
  }
}

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 = {};
}
function getPlaying() {
  return Object.values(oscillators)
    .filter((osc) => osc.playing)
    .map((osc) => osc.interval);
}

export default {
  load,
  isPlaying,
  getPlaying,
  play,
  pause,
  toggle,
  stop,
  setRoot,
};