summaryrefslogtreecommitdiff
path: root/client/lib/sampler.js
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2018-03-06 13:28:12 +0100
committerJules Laplace <julescarbon@gmail.com>2018-03-06 13:28:12 +0100
commit25fec83f6f7db468721a38adb2c84a0c0a2121f1 (patch)
tree5ea8b40951e98639b88b0378618a9ca2f835de7a /client/lib/sampler.js
displaying waveform and spectrogram
Diffstat (limited to 'client/lib/sampler.js')
-rw-r--r--client/lib/sampler.js61
1 files changed, 61 insertions, 0 deletions
diff --git a/client/lib/sampler.js b/client/lib/sampler.js
new file mode 100644
index 0000000..481a940
--- /dev/null
+++ b/client/lib/sampler.js
@@ -0,0 +1,61 @@
+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)
+ }
+ })
+ }
+ choice(){
+ 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 ]
+
+ freq = 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')
+ }
+
+ return player
+ }
+}