diff options
Diffstat (limited to 'client/index.js')
| -rw-r--r-- | client/index.js | 63 |
1 files changed, 30 insertions, 33 deletions
diff --git a/client/index.js b/client/index.js index 5de7b91..f3cdd4c 100644 --- a/client/index.js +++ b/client/index.js @@ -24,8 +24,8 @@ let dragging = false; let erasing = false; let lastFreq = 0; let notes = []; -let base_x = 0; -let base_y = 0; +let base_x = 128; +let base_y = 128; let is_split = false; requestAudioContext(() => { @@ -53,40 +53,35 @@ function rebuild() { build(); } function play(freq) { - if (instrument === organ && freq.playing) return; - freq.playing = true; - let frequency = freq.frequency; - - // while (frequency < root) { - // frequency *= 2; - // } - // while (frequency > root) { - // frequency /= 2; - // } - - instrument.play(frequency); - if (instrument === organ || hash) { - // || life.isRunning()) { + if (!freq.playing) { + freq.playing = true; + let frequency = freq.frequency; + // while (frequency < root) { + // frequency *= 2; + // } + // while (frequency > root) { + // frequency /= 2; + // } + organ.play(frequency); freq.div.classList.add("playing"); } - // life.assign_item(freq, true); } -function pause(freq) { - if (!freq.playing) return; - freq.playing = false; - instrument.pause(freq.frequency); - freq.div.classList.remove("playing"); - // life.assign_item(freq, false); +function trigger(freq) { + kalimba.play(freq.frequency); } -function assign(freq, state) { - if (state) { - play(freq); - } else { - pause(freq); +function pause(freq) { + if (freq.playing) { + freq.playing = false; + organ.pause(freq.frequency); + freq.div.classList.remove("playing"); } } function toggle(freq) { - assign(freq, !freq.playing); + if (freq.playing) { + pause(freq); + } else { + play(freq); + } } function add(i, j) { @@ -159,6 +154,7 @@ function add(i, j) { event.preventDefault(); // notes.forEach((row) => row.forEach((note) => note.recolor(a, b))); is_split = [a, b]; + toggle(freq); return; } div.style.backgroundColor = color(frac, add + add_on, mul_on); @@ -167,7 +163,7 @@ function add(i, j) { toggle(freq); erasing = !freq.playing; } else { - play(freq); + trigger(freq); } }); div.addEventListener("mouseenter", function () { @@ -180,7 +176,7 @@ function add(i, j) { toggle(freq); } } else { - play(freq); + trigger(freq); } } }); @@ -247,7 +243,6 @@ function swap_instrument() { instrument = instrument === kalimba ? organ : kalimba; } -window.addEventListener("keydown", keydown, true); function keydown(e) { // console.log(e.keyCode) if (e.altKey || e.ctrlKey || e.metaKey) return; @@ -270,6 +265,8 @@ function keydown(e) { break; } } +window.addEventListener("keydown", keydown, true); + keys.listen(function (index) { index += 7; const x = index % 7; @@ -278,7 +275,7 @@ keys.listen(function (index) { const b = y + 1; const freq = notes[a][b]; console.log(a, b, freq.frequency); - play(freq); + trigger(freq); // const freq = scales.current().index(index) // document.body.style.backgroundColor = color( index / scales.current().scale.length ) // instrument.toggle(freq) |
