From 636b62aa6d0d77e19a4b7bb3c039924eeb217a71 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Sat, 3 Mar 2018 05:38:35 +0100 Subject: hall demo --- client/lib/sampler.js | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 client/lib/sampler.js (limited to 'client/lib/sampler.js') diff --git a/client/lib/sampler.js b/client/lib/sampler.js new file mode 100644 index 0000000..675178b --- /dev/null +++ b/client/lib/sampler.js @@ -0,0 +1,58 @@ +import Tone from 'tone' +import { lerp, choice } from './util' + +const player_count = 2 +const filter_count = 3 + +const crossfaders = [] + +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; + a.push({ root: 100, fn: path.replace(/{}/, s) }) + } + return a + })() + + 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 = 'http://asdf.us/glass/' + fn.replace('wav','mp3') + } + let player = new Tone.Player({ + url: fn, + retrigger: true, + playbackRate: 1, + }) + sample.players.push(player) + } + }) + } + play(freq, time, output) { + const best = { sample: choice(this.samples) } + best.sample.index = (best.sample.index + 1) % player_count + + const player = best.sample.players[ best.sample.index ] + + freq = freq || best.sample.root + time = time || Tone.now() + + player.playbackRate = freq / best.sample.root + if (player.loaded) { + player.stop() + player.disconnect() + player.connect(output) + player.start(time) + } else { + console.log('loading') + } + + return player + } +} -- cgit v1.2.3-70-g09d2