summaryrefslogtreecommitdiff
path: root/static/main.js
blob: 144254f6380eb2ac2bd8b197d32f0c6d32601183 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
let _context;
function getContext() {
  if (!_context) {
    _context = new AudioContext();
  }
  return _context;
}

function loadSample(url) {
  return fetch(url)
    .then((response) => response.arrayBuffer())
    .then((buffer) => getContext().decodeAudioData(buffer));
}

function playSoundSample(sample, sampleNote, noteToPlay) {
  const ctx = getContext();
  const source = ctx.createBufferSource();
  source.buffer = sample;
  source.playbackRate.value = 2 ** ((noteToPlay - sampleNote) / 12);
  source.connect(ctx.destination);
  source.start(0);
}