summaryrefslogtreecommitdiff
path: root/src/lib/sampler.js
diff options
context:
space:
mode:
authorjulian laplace <julescarbon@gmail.com>2023-05-09 01:37:02 +0200
committerjulian laplace <julescarbon@gmail.com>2023-05-09 01:37:02 +0200
commit86a1fbe06907ed79d7890376bb14993c1b94473e (patch)
treedf48297fb83dab7438226ed1017f5764818e57ce /src/lib/sampler.js
parentb9dc2f677e7c3021aeeea6b1ab609a9b40806b48 (diff)
refactor boundary conditions
Diffstat (limited to 'src/lib/sampler.js')
-rw-r--r--src/lib/sampler.js46
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);
+ }
+}