summaryrefslogtreecommitdiffstats
path: root/main.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'main.mjs')
-rw-r--r--main.mjs70
1 files changed, 38 insertions, 32 deletions
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);