summaryrefslogtreecommitdiff
path: root/client/lib/kalimba.js
blob: e79e5f7f539c064861930911e680d3bf305a1aad (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
import Tone from "tone";
import { choice } from "./util";
import { getOutput } from "./output";

const player_count = 4;

const samples = [
  { root: 226, fn: "samples/380737__cabled-mess__sansula-01-a-raw.wav" },
  // { root: 267, fn: "samples/380736__cabled-mess__sansula-02-c-raw.wav" },
  // { root: 340, fn: "samples/380735__cabled-mess__sansula-03-e-raw.wav" },
  // { root: 452, fn: "samples/380733__cabled-mess__sansula-06-a-02-raw.wav" },
  // { root: 226, fn: "samples/380737__cabled-mess__sansula-01-a-raw.wav" },
  // { root: 267, fn: "samples/380736__cabled-mess__sansula-02-c-raw.wav" },
  // { root: 340, fn: "samples/380735__cabled-mess__sansula-03-e-raw.wav" },
  // { root: 452, fn: "samples/380733__cabled-mess__sansula-06-a-02-raw.wav" },
  //  { root: 507, fn: 'samples/380734__cabled-mess__sansula-07-b-h-raw.wav', },
  //  { root: 535, fn: 'samples/380731__cabled-mess__sansula-08-c-raw.wav', },
  //  { root: 671, fn: 'samples/380732__cabled-mess__sansula-09-e-raw.wav', },
];

function load() {
  const output = getOutput();
  samples.forEach((sample) => {
    sample.players = [];
    sample.index = -1;
    for (let i = 0; i < player_count; i++) {
      let fn = sample.fn;
      if (window.location.href.match(/asdf.us/)) {
        fn = "//asdf.us/kalimba/" + fn.replace("wav", "mp3");
      }
      let player = new Tone.Player({
        url: fn,
        retrigger: true,
        playbackRate: 1,
      });
      player.connect(output);
      sample.players.push(player);
    }
  });
}

function play(freq) {
  const best = choice(samples);
  best.index = (best.index + 1) % player_count;

  const player = best.players[best.index];
  player.playbackRate = freq / best.root;

  player.start();
}
function pause() {
  // no-op
}

export default { load, play, pause };