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); } }