diff options
Diffstat (limited to 'client/lib/sampler.js')
| -rw-r--r-- | client/lib/sampler.js | 51 |
1 files changed, 39 insertions, 12 deletions
diff --git a/client/lib/sampler.js b/client/lib/sampler.js index 481a940..9a1b1fc 100644 --- a/client/lib/sampler.js +++ b/client/lib/sampler.js @@ -1,21 +1,20 @@ import Tone from 'tone' -import { lerp, choice } from './util' +import { choice, clamp } from './util' +import spectrum from './spectrum' -const player_count = 2 -const filter_count = 3 - -const crossfaders = [] +const player_count = 1 export default class Sampler { constructor(path, count){ this.samples = (() => { let s = '', a = [] for (let i = 1; i < count; i++) { - const s = i < 10 ? '0' + i : 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 = [] @@ -38,24 +37,52 @@ export default class Sampler { return choice(this.samples) } play(freq, time, output) { - const best = this.choice() - best.index = (best.index + 1) % player_count - - const player = best.players[ best.index ] + const { player, best } = this.getPlayer() freq = freq || best.root + player.playbackRate = freq / best.root + time = time || Tone.now() - player.playbackRate = freq / best.root if (player.loaded) { player.stop() player.disconnect() player.connect(output) player.start(time) } else { - console.log('loading') + // 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, + } + } } |
