summaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
authorjulian laplace <julescarbon@gmail.com>2025-07-05 18:57:07 +0200
committerjulian laplace <julescarbon@gmail.com>2025-07-05 18:57:07 +0200
commit0e0c63f658fe5e0d0249bcbfba87fdb5e0484208 (patch)
tree1684dae526382ca4fcea18a2cd2929f184bde925 /client
parentc0f04ce84a66955f6363c5a7fe565359079c6457 (diff)
local proxy, json
Diffstat (limited to 'client')
-rw-r--r--client/index.js40
-rw-r--r--client/lib/keys.js1
-rw-r--r--client/lib/midi.js33
3 files changed, 58 insertions, 16 deletions
diff --git a/client/index.js b/client/index.js
index 7d1bd55..38d9b78 100644
--- a/client/index.js
+++ b/client/index.js
@@ -10,10 +10,10 @@ import color from "./lib/color";
import kalimba from "./lib/kalimba";
import sampler from "./lib/sampler";
import organ from "./lib/organ";
+import midi from "./lib/midi";
import { getOutput } from "./lib/output";
import { browser, requestAudioContext, choice, roundFreq } from "./lib/util";
import { PRIMES } from "./lib/primes";
-// import life from "./lib/life";
let instrument = kalimba;
@@ -33,6 +33,7 @@ let notes = [];
let base_x = 0;
let base_y = 0;
let is_split = false;
+let frequencies;
requestAudioContext(() => {
const output = getOutput();
@@ -56,11 +57,26 @@ function build() {
notes[i][j] = add(i, j);
}
}
+ log();
}
function rebuild() {
notes.forEach((row) => row.forEach((note) => note.destroy()));
build();
}
+function log() {
+ const seen = {};
+ for (let i = 0; i < 8; i++) {
+ for (let j = 0; j < 8; j++) {
+ const rounded = roundFreq(notes[i][j].frequency);
+ if (!seen[rounded]) {
+ seen[rounded] = notes[i][j].frequency;
+ }
+ }
+ }
+ frequencies = Object.values(seen).sort((a, b) => a - b);
+ console.log(frequencies);
+ console.log(frequencies.length, "unique frequencies in 8x8");
+}
function play(freq) {
if (!organ.isPlaying(freq.frequency)) {
let frequency = freq.frequency;
@@ -82,6 +98,10 @@ function play(freq) {
function trigger(freq) {
instrument.play(freq.frequency);
}
+function trigger_index(index) {
+ const frequency = frequencies[index];
+ instrument.play(frequency);
+}
function pause(freq) {
organ.pause(freq.frequency);
const rounded = roundFreq(freq.frequency);
@@ -250,7 +270,6 @@ function bind() {
}
function keydown(e) {
- // console.log(e.keyCode)
if (e.altKey || e.ctrlKey || e.metaKey) return;
let step = 1;
if (e.shiftKey) {
@@ -273,20 +292,11 @@ function keydown(e) {
base_y += step;
rebuild();
break;
+ case 220: // \
+ midi.enable(trigger_index);
+ break;
}
}
window.addEventListener("keydown", keydown, true);
-keys.listen(function (index) {
- index += 7;
- const x = index % 7;
- const y = Math.floor(index / 7);
- const a = x;
- const b = y + 1;
- const freq = notes[a][b];
- console.log(a / b, freq.frequency);
- trigger(freq);
- // const freq = scales.current().index(index)
- // document.body.style.backgroundColor = color( index / scales.current().scale.length )
- // instrument.toggle(freq)
-});
+keys.listen(trigger_index);
diff --git a/client/lib/keys.js b/client/lib/keys.js
index 84f8103..687517f 100644
--- a/client/lib/keys.js
+++ b/client/lib/keys.js
@@ -38,7 +38,6 @@ function keydown(e) {
if (!(e.keyCode in keys)) return;
var index = keys[e.keyCode];
if (e.shiftKey) index += letters.length;
- index -= 7;
callback(index);
}
diff --git a/client/lib/midi.js b/client/lib/midi.js
new file mode 100644
index 0000000..06cb266
--- /dev/null
+++ b/client/lib/midi.js
@@ -0,0 +1,33 @@
+/**
+ * MIDI
+ * @module midi.js;
+ */
+
+import { WebMidi } from "webmidi";
+
+function enable(play) {
+ WebMidi.enable()
+ .then(onEnabled)
+ .catch((error) => console.error(error));
+
+ // Function triggered when WEBMIDI.js is ready
+ function onEnabled() {
+ // Display available MIDI input devices
+ if (WebMidi.inputs.length < 1) {
+ console.log("No device detected.");
+ return;
+ } else {
+ WebMidi.inputs.forEach((device, index) => {
+ document.body.innerHTML += `${index}: ${device.name} <br>`;
+ });
+ const mySynth = WebMidi.inputs[0];
+ // const mySynth = WebMidi.getInputByName("TYPE NAME HERE!")
+
+ mySynth.channels[1].addListener("noteon", (event) => {
+ console.log(event.note);
+ });
+ }
+ }
+}
+
+export default { enable };