summaryrefslogtreecommitdiff
path: root/client/lib
diff options
context:
space:
mode:
authorjulian laplace <julescarbon@gmail.com>2025-07-05 17:06:59 +0200
committerjulian laplace <julescarbon@gmail.com>2025-07-05 17:06:59 +0200
commit200c1a4f0ebd3188faebaea8e7278fc5105227cf (patch)
tree12f61dcd756ba57e0f718b87e4eb0e9c30d90f4d /client/lib
parentd82f99fd89e23fdb62598b616e39537360a10f74 (diff)
laod sample
Diffstat (limited to 'client/lib')
-rw-r--r--client/lib/color.js5
-rw-r--r--client/lib/kalimba.js15
-rw-r--r--client/lib/keys.js66
-rw-r--r--client/lib/life.js5
-rw-r--r--client/lib/organ.js5
-rw-r--r--client/lib/output.js5
-rw-r--r--client/lib/primes.js5
-rw-r--r--client/lib/sampler.js112
-rw-r--r--client/lib/util.js5
9 files changed, 189 insertions, 34 deletions
diff --git a/client/lib/color.js b/client/lib/color.js
index d0fdc24..bea0330 100644
--- a/client/lib/color.js
+++ b/client/lib/color.js
@@ -1,3 +1,8 @@
+/**
+ * Iquizeles color generator
+ * @module lib/color.js;
+ */
+
const palettes = [
[
[0.5, 0.5, 0.5],
diff --git a/client/lib/kalimba.js b/client/lib/kalimba.js
index 3d239a5..64ace00 100644
--- a/client/lib/kalimba.js
+++ b/client/lib/kalimba.js
@@ -1,3 +1,8 @@
+/**
+ * Kalimba
+ * @module lib/kalimba.js;
+ */
+
import Tone from "tone";
import { choice } from "./util";
@@ -44,13 +49,11 @@ let last = 440;
function play(freq) {
last = freq;
- const best = choice(samples);
- best.index = (best.index + 1) % player_count;
-
- const player = best.players[best.index];
- player.playbackRate = freq / best.root;
- // console.log(player.name);
+ const sample = choice(samples);
+ sample.index = (sample.index + 1) % sample.players.length;
+ const player = sample.players[sample.index];
+ player.playbackRate = freq / sample.root;
player.start();
}
diff --git a/client/lib/keys.js b/client/lib/keys.js
index c9e51ac..84f8103 100644
--- a/client/lib/keys.js
+++ b/client/lib/keys.js
@@ -1,39 +1,49 @@
-const keys = {}
-const key_numbers = {}
-const letters = "zxcvbnmasdfghjklqwertyuiop"
-const numbers = "1234567890"
+/**
+ * Keyboard helper
+ * @module lib/keys.js;
+ */
-let callback = function(){}
+const keys = {};
+const key_numbers = {};
+const letters = "zxcvbnmasdfghjklqwertyuiop";
+const numbers = "1234567890";
-letters.toUpperCase().split("").map(function(k,i){
- keys[k.charCodeAt(0)] = i
-})
+let callback = function () {};
-numbers.split("").map(function(k,i){
- keys[k.charCodeAt(0)] = i+letters.length
- key_numbers[k.charCodeAt(0)] = true
-})
+letters
+ .toUpperCase()
+ .split("")
+ .map(function (k, i) {
+ keys[k.charCodeAt(0)] = i;
+ });
-window.addEventListener("keydown", keydown, true)
-function keydown (e) {
+numbers.split("").map(function (k, i) {
+ keys[k.charCodeAt(0)] = i + letters.length;
+ key_numbers[k.charCodeAt(0)] = true;
+});
+
+window.addEventListener("keydown", keydown, true);
+function keydown(e) {
if (e.altKey || e.ctrlKey || e.metaKey) {
- e.stopPropagation()
- return
+ e.stopPropagation();
+ return;
}
- if (document.activeElement instanceof HTMLInputElement &&
- (e.keyCode in key_numbers)) {
- e.stopPropagation()
- return
+ if (
+ document.activeElement instanceof HTMLInputElement &&
+ e.keyCode in key_numbers
+ ) {
+ e.stopPropagation();
+ return;
}
- if (! (e.keyCode in keys)) return
- var index = keys[e.keyCode]
- if (e.shiftKey) index += letters.length
- index -= 7
- callback(index)
+ if (!(e.keyCode in keys)) return;
+ var index = keys[e.keyCode];
+ if (e.shiftKey) index += letters.length;
+ index -= 7;
+ callback(index);
}
-function listen (fn) {
- callback = fn
+function listen(fn) {
+ callback = fn;
}
-export default { listen } \ No newline at end of file
+export default { listen };
diff --git a/client/lib/life.js b/client/lib/life.js
index 301535d..43187ca 100644
--- a/client/lib/life.js
+++ b/client/lib/life.js
@@ -1,3 +1,8 @@
+/**
+ * Game of Life
+ * @module lib/life.js;
+ */
+
let w, h, a, b, notes, assign;
function init(z, fn) {
// really bad
diff --git a/client/lib/organ.js b/client/lib/organ.js
index 9d0ac90..652351e 100644
--- a/client/lib/organ.js
+++ b/client/lib/organ.js
@@ -1,3 +1,8 @@
+/**
+ * Sine wave organ
+ * @module lib/organ.js;
+ */
+
import Tone from "tone";
import { roundFreq } from "./util";
diff --git a/client/lib/output.js b/client/lib/output.js
index e67b4d4..cc8d841 100644
--- a/client/lib/output.js
+++ b/client/lib/output.js
@@ -1,3 +1,8 @@
+/**
+ * Common output stage
+ * @module lib/output.js;
+ */
+
import Tone from "tone";
let output = null;
diff --git a/client/lib/primes.js b/client/lib/primes.js
index c14868b..06fbec1 100644
--- a/client/lib/primes.js
+++ b/client/lib/primes.js
@@ -1,3 +1,8 @@
+/**
+ * List of prime numbers
+ * @module lib/primes.js;
+ */
+
export const PRIMES = [
1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151,
diff --git a/client/lib/sampler.js b/client/lib/sampler.js
new file mode 100644
index 0000000..08f253d
--- /dev/null
+++ b/client/lib/sampler.js
@@ -0,0 +1,112 @@
+/**
+ * Sampler
+ * @module lib/sampler.js;
+ */
+
+import Tone from "tone";
+
+let output;
+let ready;
+let current = "";
+let samples = {};
+
+const player_count = 12;
+
+export function load(out, readyCallback) {
+ output = out;
+ ready = readyCallback;
+ document.body.addEventListener("dragover", dragOver);
+ document.body.addEventListener("drop", drop);
+}
+
+/**
+ * Drag and drop
+ */
+export function dragOver(event) {
+ event.preventDefault();
+}
+export function drop(event) {
+ event.preventDefault();
+ const files = event.dataTransfer.items
+ ? [...event.dataTransfer.items]
+ .filter((item) => item.kind === "file")
+ .map((item) => item.getAsFile())
+ : [...event.dataTransfer.files];
+
+ const file = files[0];
+ const reader = new FileReader();
+
+ reader.addEventListener(
+ "load",
+ () => loadSampleFromFile(file, reader.result),
+ false,
+ );
+
+ if (file) {
+ reader.readAsDataURL(file);
+ }
+}
+
+export function loadSampleFromFile(file, url) {
+ const { name } = file;
+ current = name;
+
+ const sample = (samples[name] = samples[name] || {});
+ sample.root = 440;
+ sample.players = [];
+ sample.index = -1;
+ for (let i = 0; i < player_count; i++) {
+ let player = new Tone.Player({
+ url,
+ retrigger: true,
+ playbackRate: 1,
+ });
+ player.name = name;
+ player.connect(output);
+ sample.players.push(player);
+ }
+ console.log("+ Sampler:", name, `(${sample.players.length} voices)`);
+ ready();
+}
+
+/**
+ * Player
+ */
+let last = 440;
+
+function play(freq) {
+ last = freq;
+ const sample = samples[current];
+ sample.index = (sample.index + 1) % sample.players.length;
+ const player = sample.players[sample.index];
+ player.playbackRate = freq / sample.root;
+ player.start();
+}
+
+function pause() {
+ // no-op
+}
+
+export default { load, play, pause };
+
+// for help tuning
+function keydown(e) {
+ // console.log(e.keyCode)
+ if (e.metaKey && last && current) {
+ const sample = samples[current];
+ const step = e.shiftKey ? (e.ctrlKey ? 0.1 : 1) : 10;
+ switch (e.keyCode) {
+ case 38: // up
+ e.preventDefault();
+ sample.root -= step;
+ play(last);
+ break;
+ case 40: // down
+ e.preventDefault();
+ sample.root += step;
+ play(last);
+ break;
+ }
+ }
+}
+window.addEventListener("keydown", keydown, true);
diff --git a/client/lib/util.js b/client/lib/util.js
index 7fad313..8696a50 100644
--- a/client/lib/util.js
+++ b/client/lib/util.js
@@ -1,3 +1,8 @@
+/**
+ * Utilities
+ * @module lib/util.js;
+ */
+
import Tone from "tone";
import StartAudioContext from "./startAudioContext";