summaryrefslogtreecommitdiff
path: root/client/lib/sampler.js
diff options
context:
space:
mode:
Diffstat (limited to 'client/lib/sampler.js')
-rw-r--r--client/lib/sampler.js50
1 files changed, 50 insertions, 0 deletions
diff --git a/client/lib/sampler.js b/client/lib/sampler.js
new file mode 100644
index 0000000..cbbf281
--- /dev/null
+++ b/client/lib/sampler.js
@@ -0,0 +1,50 @@
+import Tone from 'tone'
+import { choice } from './util'
+
+const player_count = 2
+
+const compressor = new Tone.Compressor(-30, 3).toMaster()
+
+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,
+ })
+ player.connect(compressor)
+ sample.players.push(player)
+ }
+ })
+
+ }
+ play(freq, time) {
+ 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
+ player.start(time)
+ }
+}