summaryrefslogtreecommitdiff
path: root/client/lib/sampler.js
blob: fdca29ad82dee7129613c1ae4792bcddeb5ac178 (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
import Tone from 'tone'
import { choice, clamp } from './util'
import spectrum from './spectrum'

const player_count = 1

export default class Sampler {
  constructor(path, count){
    this.samples = (() => {
      let s = '', a = []
      for (let i = 1; i < count; i++) {
        s = i < 10 ? '0' + i : i;
        a.push({ root: 100, fn: path.replace(/{}/, s) })
      }
      return a
    })()
    this.length = this.samples.length

    this.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/glass/' + fn.replace('wav','mp3')
        }
        let player = new Tone.Player({
          url: fn,
          retrigger: true,
          playbackRate: 1,
        })
        sample.players.push(player)
      }
    })
  }
  choice(){
    return choice(this.samples)
  }
  play(freq, time, output) {
    const { player, best } = this.getPlayer()

    freq = freq || best.root
    player.playbackRate = freq / best.root

    time = time || Tone.now()

    if (player.loaded) {
      player.stop()
      player.disconnect()
      player.connect(output)
      player.start(time)
    } else {
      // console.log('loading')
    }

    return player
  }
  getRandomPlayer(){
    const best = this.choice()
    best.index = (best.index + 1) % player_count

    const player = best.players[ best.index ]
    return { player, best }
  }
  getPlayer(n){
    n = n || 0
    if (n < 1) n *= this.samples.length
    const best = this.samples[clamp(n|0, 0, this.samples.length-1)]
    best.index = (best.index + 1) % player_count

    const player = best.players[ best.index ]
    return { player, best }
  }
  getWaveAndSpectrum(n){
    const { player } = this.getPlayer(n)
    const buf = player._buffer.get()
    if (! buf) return { pcm: null, spec: null }
    const pcm = buf.getChannelData(0)
    if (! player._spectrum) {
      const sr = buf.sampleRate
      player._spectrum = spectrum.toSpectrum(pcm, sr)
    }
    return {
      pcm: pcm,
      spec: player._spectrum,
    }
  }
}