-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
129 lines (105 loc) · 3.39 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import * as GuitarTrainerUI from './ui/guitarTrainer.ts';
import * as GuitarTrainerApp from './apps/guitarTrainer.ts';
import { Accidental, Note } from './apps/notes.ts';
import { Sound } from './sound/sound.ts';
import { newSound } from './sound/cmd.ts';
// Resources
export const noteSoundMap: Record<Note, Sound> = {
A: newSound('alphabet/A.mp3'),
B: newSound('alphabet/B.mp3'),
C: newSound('alphabet/C.mp3'),
D: newSound('alphabet/D.mp3'),
E: newSound('alphabet/E.mp3'),
F: newSound('alphabet/F.mp3'),
G: newSound('alphabet/G.mp3'),
};
export const accidentalSoundMap: Record<Accidental, Sound> = {
'#': newSound('sharp.mp3'),
b: newSound('flat.mp3'),
};
const tic: Sound = newSound('tic.wav');
const toc: Sound = newSound('toc.wav');
const yes: Sound = newSound('yes.wav');
const no: Sound = newSound('no.wav');
// Logic
const ui = GuitarTrainerUI.make();
const app = GuitarTrainerApp.make();
const updateMetronomeUi = (currentBeat: number) => {
ui.metronome.flickerBeatBackground();
ui.metronome.currentBeat.value = currentBeat.toString();
};
const updateNeckTrainingUi = (note: Note, accidental?: Accidental) => {
ui.neckTraining.noteSignal.value = `${note}${accidental ?? ''}`;
};
const handleMetronomeBeatTic = (event: CustomEvent<{ newBeat: number }>) => {
tic.play();
updateMetronomeUi(event.detail.newBeat);
};
const handleMetronomeBeatToc = (event: CustomEvent<{ newBeat: number }>) => {
toc.play();
updateMetronomeUi(event.detail.newBeat);
};
const handleNeckTrainingNoteChange = (
event: CustomEvent<{ note: Note; accidental?: Accidental }>,
) => {
const { note, accidental } = event.detail;
updateNeckTrainingUi(note, accidental);
noteSoundMap[note].play();
if (accidental) {
setTimeout(() => {
accidentalSoundMap[accidental].play();
}, 300);
}
};
const handleUiMetronomeBpmIncrease = () => {
app.state.metronome.state.setBpm(app.state.metronome.state.bpm + 1);
ui.metronome.bpm.value = app.state.metronome.state.bpm.toString();
};
const handleUiMetronomeBpmDecrease = () => {
app.state.metronome.state.setBpm(app.state.metronome.state.bpm - 1);
ui.metronome.bpm.value = app.state.metronome.state.bpm.toString();
};
const handlePitchChange = (
event: CustomEvent<{ note: Note; octave: number }>,
) => {
const { note } = event.detail;
const { neckTraining } = app.state;
if (note === neckTraining.state.currentNote) {
yes.play();
} else {
no.play();
}
};
const init = () => {
const { metronome: { state: { bpm, beats, quarters } } } = app.state;
ui.metronome.bpm.value = bpm.toString();
ui.metronome.timeSignature.value = `${beats}/${quarters}`;
addEventListener(
'state:metronome:beat:tic',
handleMetronomeBeatTic as EventListenerOrEventListenerObject,
);
addEventListener(
'state:metronome:beat:toc',
handleMetronomeBeatToc as EventListenerOrEventListenerObject,
);
addEventListener(
'state:neckTraining:note:change',
handleNeckTrainingNoteChange as EventListenerOrEventListenerObject,
);
addEventListener(
'ui:metronome:bpm:increase',
handleUiMetronomeBpmIncrease as EventListenerOrEventListenerObject,
);
addEventListener(
'ui:metronome:bpm:decrease',
handleUiMetronomeBpmDecrease as EventListenerOrEventListenerObject,
);
addEventListener(
'state:pitch:change',
handlePitchChange as EventListenerOrEventListenerObject,
);
};
// Main
init();
app.start();
ui.run();