From 77f5e81d0a1adc497040eaa89095635f3de3924e Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Mon, 28 Jul 2025 08:52:51 -0400 Subject: attach octave values to frets sometimes just do the simple thing --- main.mjs | 70 +++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 38 insertions(+), 32 deletions(-) (limited to 'main.mjs') diff --git a/main.mjs b/main.mjs index 931543a..6c71e9c 100644 --- a/main.mjs +++ b/main.mjs @@ -5,6 +5,41 @@ import String from './string.mjs'; import Player from './player.mjs'; import { Note, toCents } from './scale.mjs'; +let player = undefined; + +function save(e) { + document.querySelectorAll(History.name).forEach(h => { + console.debug('h is', h, 'e is', e.detail); + h.add(e.detail); + }); +} + +function play(e) { + console.debug('got playEvent', e, e.detail.notes); + if (player) { + player.stop(); + } + + const played = e.detail.notes.map((n, i) => { + if (n !== 'x') { + return [Note.fromString(n), e.detail.octaves[i]]; + } + }).filter(n => n); + + const a = Note.fromString('A'); + player = new Player(played.map(([n, o]) => { + return toCents([a, 4], [n, o]); + })); + player.start(); +} + +function stop(e) { + console.debug('got stopEvent', e); + if (player) { + player.stop(); + } +} + function init() { console.debug('init()', this); @@ -13,40 +48,11 @@ function init() { KeyPicker.register(); History.register(); - let player = undefined; - // todo: maybe just attach the listener to document? document.querySelectorAll(Fretboard.name).forEach(f => { - f.addEventListener(f.saveEvent, e => { - document.querySelectorAll(History.name).forEach(h => { - console.debug('h is', h, 'e is', e.detail); - h.add(e.detail); - }); - }); - - f.addEventListener(f.playEvent, e => { - console.debug('got playEvent', e, e.detail.notes); - const a = Note.fromString('A'); - if (player) { - player.stop(); - } - const played = e.detail.notes.map((n, i) => { - if (n !== 'x') { - return [Note.fromString(n), e.detail.octaves[i]] - } - }).filter(n => n); - player = new Player(played.map(([n, o]) => { - return toCents([a, 4], [n, o]); - })); - player.start(); - }); - - f.addEventListener(f.stopEvent, e => { - console.debug('got stopEvent', e); - if (player) { - player.stop(); - } - }); + f.addEventListener(f.saveEvent, save); + f.addEventListener(f.playEvent, play); + f.addEventListener(f.stopEvent, stop); }); } document.addEventListener('DOMContentLoaded', init); -- cgit v1.3