diff options
Diffstat (limited to 'src/lib/sampler.js')
| -rw-r--r-- | src/lib/sampler.js | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/lib/sampler.js b/src/lib/sampler.js new file mode 100644 index 0000000..0db53e1 --- /dev/null +++ b/src/lib/sampler.js @@ -0,0 +1,46 @@ +import * as Tone from "tone"; +import { choice, randrange } from "./util"; +import output from "./output"; + +const PLAYER_COUNT = 4; + +export default class Sampler { + constructor({ samples, tonal }) { + this.tonal = tonal; + this.samples = samples.map(({ ...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; + } + let player = new Tone.Player({ + url: fn, + retrigger: true, + playbackRate: 1, + }); + player.connect(output); + sample.players.push(player); + } + return sample; + }); + } + + play(time, options) { + const sound = options.index + ? this.samples[options.index] + : choice(this.samples); + + sound.index = (sound.index + 1) % PLAYER_COUNT; + + const player = sound.players[sound.index]; + + if (this.tonal) { + player.playbackRate = + (options.frequency * choice([0.5, 1]) + randrange(0, 10)) / sound.root; + } + + player.start(time || 0); + } +} |
