diff options
Diffstat (limited to 'client/index.js')
| -rw-r--r-- | client/index.js | 40 |
1 files changed, 25 insertions, 15 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); |
