diff options
Diffstat (limited to 'client/index.js')
| -rw-r--r-- | client/index.js | 66 |
1 files changed, 52 insertions, 14 deletions
diff --git a/client/index.js b/client/index.js index a7bdd9e..7fdaa9b 100644 --- a/client/index.js +++ b/client/index.js @@ -9,7 +9,8 @@ import keys from "./lib/keys"; import color from "./lib/color"; import kalimba from "./lib/kalimba"; import sampler from "./lib/sampler"; -import organ from "./lib/organ"; +import sine from "./lib/sine"; +import bandpass from "./lib/bandpass"; import midi from "./lib/midi"; import oktransition from "./vendor/oktransition"; import { getOutput } from "./lib/output"; @@ -25,6 +26,7 @@ import { import { scales } from "./lib/scales"; let instrument = kalimba; +let organ = sine; let grid = document.createElement("grid"); let root = 440; @@ -47,18 +49,6 @@ let scaleMode = 0; let is_split = false; let intervals; -requestAudioContext(() => { - const output = getOutput(); - document.body.appendChild(grid); - kalimba.load(output); - organ.load(output); - sampler.load(output, function ready() { - instrument = sampler; - }); - build(); - bind(); -}); - function build() { w = window.innerWidth; h = window.innerHeight; @@ -115,6 +105,12 @@ function play(note) { } } function trigger(note) { + if (organ === bandpass) { + toggle(note); + if (instrument === kalimba) { + return; + } + } if (intervalInRange(note.interval, root)) { instrument.play(note.interval, root); } @@ -127,7 +123,6 @@ function trigger_index(index) { } function pause(note) { organ.pause(note.interval); - trigger(note); const rounded = roundInterval(note.interval); notes.forEach((row) => row.forEach( @@ -144,6 +139,29 @@ function toggle(note) { } } +const modes = { + sine, + bandpass, +}; + +let modus = null; +function toggleModus() { + let intervals; + if (modes[modus]) { + intervals = modes[modus].getPlaying(); + modes[modus].stop(); + document.querySelector(`#modus .${modus}`).classList.remove("visible"); + // rebuild(); + } + + modus = modus === "bandpass" ? "sine" : "bandpass"; + organ = modes[modus]; + document.querySelector(`#modus .${modus}`).classList.add("visible"); + if (intervals) { + intervals.forEach((interval) => modes[modus].play(interval)); + } +} + function add(i, j) { const ii = i + Math.round(base_x); const jj = j + Math.round(base_y); @@ -334,6 +352,9 @@ function bind() { .addEventListener("click", () => document.querySelector("#help").classList.toggle("visible"), ); + // toggleModus(); + toggleModus(); + document.querySelector("#modus").addEventListener("click", toggleModus); Array.from(document.querySelectorAll(".mode")).forEach((el) => { console.log(el.getAttribute("name")); el.addEventListener("click", (event) => { @@ -410,7 +431,9 @@ function keydown(e) { document.querySelector("#help").classList.toggle("visible"); break; case 189: // - + case 173: // - e.preventDefault(); + e.stopPropagation(); if (e.altKey || e.metaKey) { root = clamp(root - (e.shiftKey ? 10 : 1), 1, 200000); organ.setRoot(root); @@ -422,7 +445,9 @@ function keydown(e) { } break; case 187: // = + case 61: // = e.preventDefault(); + e.stopPropagation(); if (e.altKey || e.metaKey) { root = clamp(root + (e.shiftKey ? 10 : 1), 1, 200000); organ.setRoot(root); @@ -453,3 +478,16 @@ function showMessage(message) { easing: oktransition.easing.circ_out, }); } + +requestAudioContext(() => { + const output = getOutput(); + document.body.appendChild(grid); + kalimba.load(output); + sine.load(output); + bandpass.load(output); + sampler.load(output, function ready() { + instrument = sampler; + }); + build(); + bind(); +}); |
