-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
315 lines (311 loc) · 15.1 KB
/
index.html
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.keyboard .key:not(.acc) {
width: 23;
height: 120;
fill: white;
stroke: black;
}
.keyboard .key.acc {
width: 13;
height: 80;
fill: black;
stroke: black;
}
.keyboard .key.highlight {
fill: teal !important;
}
.keyboard text {
text-anchor: middle;
fill: white;
visibility: hidden;
}
.keyboard text.highlight {
visibility: visible;
}
.keyboard text.acc {
font-size: 8px;
font-weight: normal;
}
::-webkit-scrollbar {
-webkit-appearance: none;
width: 0.5rem;
height: 1rem;
-webkit-overflow-scrolling: auto;
}
::-webkit-scrollbar-thumb {
border-radius: 0.25rem;
background-color: rgba(0,0,0,.5);
-webkit-box-shadow: 0 0 1px rgba(255,255,255,.5);
}
</style>
</head>
<body style="text-align: center; font-family:'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;">
<div style="width: 800px; max-width: 100%; margin: 0 auto;">
<svg class="keyboard" xml:space="preserve" viewBox="-0.5 -0.5 323 121" style="margin: 0 auto; width: 100%">
<g octave="0">
<rect class="key" nval="c3" x="0" y="0" />
<rect class="key" nval="d3" x="23" y="0" />
<rect class="key" nval="e3" x="46" y="0" />
<rect class="key" nval="f3" x="69" y="0" />
<rect class="key" nval="g3" x="92" y="0" />
<rect class="key" nval="a4" x="115" y="0" />
<rect class="key" nval="b4" x="138" y="0" />
<rect class="key acc" nval="c-3" x="14.33333" y="0" />
<rect class="key acc" nval="d-3" x="41.66666" y="0" />
<rect class="key acc" nval="f-3" x="82.25" y="0" />
<rect class="key acc" nval="g-3" x="108.25" y="0" />
<rect class="key acc" nval="a-4" x="134.75" y="0" />
<text x="11" y="110" nval="c3">C</text>
<text x="34" y="110" nval="d3">D</text>
<text x="57" y="110" nval="e3">E</text>
<text x="80" y="110" nval="f3">F</text>
<text x="103" y="110" nval="g3">G</text>
<text x="126" y="110" nval="a4">A</text>
<text x="149" y="110" nval="b4">B</text>
<text class="acc sharp" x="20.8333" y="75" nval="c-3">C#</text>
<text class="acc sharp" x="47.5" y="75" nval="d-3">D#</text>
<text class="acc sharp" x="88.75" y="75" nval="f-3">F#</text>
<text class="acc sharp" x="114.75" y="75" nval="g-3">G#</text>
<text class="acc sharp" x="141.25" y="75" nval="a-4">A#</text>
<text class="acc flat" x="20.8333" y="75" nval="c-3">Db</text>
<text class="acc flat" x="47.5" y="75" nval="d-3">Eb</text>
<text class="acc flat" x="88.75" y="75" nval="f-3">Gb</text>
<text class="acc flat" x="114.75" y="75" nval="g-3">Ab</text>
<text class="acc flat" x="141.25" y="75" nval="a-4">Bb</text>
</g>
<g octave="1" transform="translate(161, 0)">
<rect class="key" nval="c4" x="0" y="0" />
<rect class="key" nval="d4" x="23" y="0" />
<rect class="key" nval="e4" x="46" y="0" />
<rect class="key" nval="f4" x="69" y="0" />
<rect class="key" nval="g4" x="92" y="0" />
<rect class="key" nval="a5" x="115" y="0" />
<rect class="key" nval="b5" x="138" y="0" />
<rect class="key acc" nval="c-4" x="14.33333" y="0" />
<rect class="key acc" nval="d-4" x="41.66666" y="0" />
<rect class="key acc" nval="f-4" x="82.25" y="0" />
<rect class="key acc" nval="g-4" x="108.25" y="0" />
<rect class="key acc" nval="a-5" x="134.75" y="0" />
<text x="11" y="110" nval="c4">C</text>
<text x="34" y="110" nval="d4">D</text>
<text x="57" y="110" nval="e4">E</text>
<text x="80" y="110" nval="f4">F</text>
<text x="103" y="110" nval="g4">G</text>
<text x="126" y="110" nval="a5">A</text>
<text x="149" y="110" nval="b5">B</text>
<text class="acc sharp" x="20.8333" y="75" nval="c-4">C#</text>
<text class="acc sharp" x="47.5" y="75" nval="d-4">D#</text>
<text class="acc sharp" x="88.75" y="75" nval="f-4">F#</text>
<text class="acc sharp" x="114.75" y="75" nval="g-4">G#</text>
<text class="acc sharp" x="141.25" y="75" nval="a-5">A#</text>
<text class="acc flat" x="20.8333" y="75" nval="c-4">Db</text>
<text class="acc flat" x="47.5" y="75" nval="d-4">Eb</text>
<text class="acc flat" x="88.75" y="75" nval="f-4">Gb</text>
<text class="acc flat" x="114.75" y="75" nval="g-4">Ab</text>
<text class="acc flat" x="141.25" y="75" nval="a-5">Bb</text>
</g>
</svg>
<div style="margin: 1rem 0 0.75rem; background-color: whitesmoke; width: auto;">
<div class="chord-name" style="font-weight: bold; font-size: 3rem; color: teal; padding: 1rem;">
--
</div>
<div class="countdown animate" style="width: 100%; height: 0.25rem; background-color: teal;"></div>
</div>
<div style="margin: 1rem 0 0.75rem; display: flex; gap: 1rem;">
<div style="display: flex; flex-direction: column; gap: 1rem; flex-grow: 1; flex-basis: 0; height: 10rem; overflow-y: scroll; padding-right: 1rem; box-sizing: border-box;">
<button style="border: none; background-color: whitesmoke; padding: 1rem; cursor: pointer; font-weight: bold;" onclick="loadChords(notes_in_c)">Notes in Key of C</button>
<button style="border: none; background-color: whitesmoke; padding: 1rem; cursor: pointer; font-weight: bold;" onclick="loadChords(triads_in_c)">Major & Minor in Key of C</button>
<button style="border: none; background-color: whitesmoke; padding: 1rem; cursor: pointer; font-weight: bold;" onclick="loadChords(triads_root_c)">Major & Minor w/ Root in C</button>
<button style="border: none; background-color: whitesmoke; padding: 1rem; cursor: pointer; font-weight: bold;" onclick="loadChords(triads)">All Major & Minor</button>
<button style="border: none; background-color: whitesmoke; padding: 1rem; cursor: pointer; font-weight: bold;" onclick="loadChords(sevenths_root_c)">7ths w/ Root in C</button>
<button style="border: none; background-color: whitesmoke; padding: 1rem; cursor: pointer; font-weight: bold;" onclick="loadChords(sevenths)">All 7ths</button>
<button style="border: none; background-color: whitesmoke; padding: 1rem; cursor: pointer; font-weight: bold;" onclick="loadChords(triads_and_sevenths)">Major, Minor & 7ths</button>
<button style="border: none; background-color: whitesmoke; padding: 1rem; cursor: pointer; font-weight: bold;" onclick="loadChords(suspensions)">Suspensions</button>
</div>
<div style="flex-grow: 1; flex-basis: 0; height: 10rem;">
<textarea autofocus class="chordset" style="width: 100%; height: 100%; border: none; background-color: whitesmoke; padding: 1rem; font-family: monospace; font-size: 14px;"
placeholder="Enter chords to practice here separated by commas, or click one of the preloaded chord sets on the left. Set the timing below if needed. Click 'Start' to begin."></textarea>
</div>
</div>
<div style="display: flex; text-transform: uppercase; font-size: 0.75rem; text-align: left; gap: 1rem; flex-wrap: wrap">
<label style="flex-grow: 1; flex-basis: 0;">
<div>
Time to Answer
</div>
<input class="time" type="text" value="5" style="margin-top: 0.25rem; border: none; padding: 1rem; background-color: whitesmoke; width: 100%">
</label>
<label style="flex-grow: 1; flex-basis: 0;">
<div>
Speedup / Cycle
</div>
<input class="speedup" type="text" value="0.01" style="margin-top: 0.25rem; border: none; padding: 1rem; background-color: whitesmoke; width: 100%">
</label>
<label style="flex-grow: 1; flex-basis: 0;">
<div>
Display Time
</div>
<input class="display-time" type="text" value="2.5" style="margin-top: 0.25rem; border: none; padding: 1rem; background-color: whitesmoke; width: 100%">
</label>
<button class="start" onclick="start()" style="padding: 1rem 2.5rem; font-size: 2rem; background-color: teal; color: white; border: none; font-weight: bold; cursor: pointer; flex-grow: 1; flex-basis: 0;">
START
</button>
</div>
</div>
<script>
var note_names = ["A", "A#", "Bb", "B", "C", "C#", "Db", "D", "D#", "Eb", "E", "F", "F#", "Gb", "G", "G#", "Ab"];
var triads = note_names.concat(note_names.map(note => note + "m"));
var sevenths = [].concat(note_names.map(note => note + "7"), note_names.map(note => note + "maj7"), note_names.map(note => note + "m7"));
var root_c_filter = chord_name => !chord_name.includes("#") && !chord_name.includes("b");
var notes_in_c = note_names.filter(note => note.length === 1).map(note => note + ".");
var triads_in_c = ["C", "Dm", "Em", "F", "G", "Am"];
var triads_root_c = triads.filter(root_c_filter);
var sevenths_root_c = sevenths.filter(root_c_filter);
var triads_and_sevenths = triads.concat(sevenths);
var suspensions = [].concat(note_names.map(note => note + "sus2"), note_names.map(note => note + "sus4"));
var keyboard_keys = note_names
.filter(note => !note.endsWith("b"))
.map(note => note.toLowerCase().replace("#", "-"));
let shifted_keys = keyboard_keys.splice(0, 3);
keyboard_keys = keyboard_keys.concat(shifted_keys);
keyboard_keys = keyboard_keys.map(
key => key + (key.startsWith("a") || key.startsWith("b") ? 4 : 3)).concat(keyboard_keys.map(
key => key + (key.startsWith("a") || key.startsWith("b") ? 5 : 4)));
let audio_files = keyboard_keys.map(key => new Audio(`audio/${key}.mp3`))
let extension_map = {
".": [0],
"": [0, 4, 7],
"m": [0, 3, 7],
"m7": [0, 3, 7, 10],
"7": [0, 4, 7, 10],
"maj7": [0, 4, 7, 11],
"sus2": [0, 2, 7],
"sus4": [0, 5, 7],
}
var note_values = {};
var [last_note, last_value] = [null, 0];
for (let note of note_names) {
var new_value = last_value
if (last_note != null && (note.length == 1 || last_note.length == 1)) {
new_value += 1;
}
note_values[note] = new_value
last_note = note;
last_value = new_value;
}
var splitChordName = (chord_name) => {
let root = note_names
.filter(note => chord_name.startsWith(note))
.reduce((a, b) => a.length > b.length ? a : b);
let extension = chord_name.substring(root.length);
return [root, extension];
}
var getChordKeys = (chord_name) => {
let [root, extension] = splitChordName(chord_name);
let root_value = note_values[root];
let extension_values = extension_map[extension];
let chord_vals = extension_values.map(delta => (root_value + delta) % 12);
let chord_w_octaves = [];
let [prev_adjusted_val, prev_octave] = [-1, 0]
for (let val of chord_vals) {
let adjusted_val = (val + 9) % 12;
let octave = adjusted_val > prev_adjusted_val ? prev_octave : prev_octave + 1;
chord_w_octaves.push(keyboard_keys[adjusted_val + 12 * octave])
prev_adjusted_val = adjusted_val;
prev_octave = octave;
}
return chord_w_octaves;
}
var clearHighlights = () => {
document.querySelectorAll(`.key.highlight`).forEach(
key => key.classList.remove("highlight")
);
document.querySelectorAll(`.keyboard text`).forEach(n => n.classList.remove("highlight"));
}
var highlightChord = (chord_name) => {
clearHighlights();
let chord_keys = getChordKeys(chord_name);
for (let key of chord_keys) {
document.querySelector(`.keyboard .key[nval="${key}"]`).classList.add("highlight");
let accidental = "";
if (key.includes("-")) {
let [root, extension] = splitChordName(chord_name);
console.log(root, " + ", extension)
if (root.includes("#")) {
accidental = ".sharp";
} else if (root.includes("b")) {
accidental = ".flat";
} else if (extension_map[extension].includes(3)) {
accidental = ".flat";
} else {
accidental = ".sharp";
}
}
document.querySelector(`.keyboard text${accidental}[nval="${key}"]`).classList.add("highlight");
}
}
var playChord = (chord_name) => {
let chord_keys = getChordKeys(chord_name);
for (let key of chord_keys) {
let audio = audio_files[keyboard_keys.indexOf(key)];
audio.play();
}
}
var loadChords = (chords) => {
document.querySelector(".chordset").value = chords.join(",");
}
var state = "STOPPED";
var chordset = [];
var timeout = null;
var start = () => {
if (state == "STOPPED") {
var time = parseFloat(document.querySelector(".time").value)
var speedup = parseFloat(document.querySelector(".speedup").value)
var display_time = parseFloat(document.querySelector(".display-time").value)
chordset = document.querySelector(".chordset").value.split(",").map(c => c.trim());
if (chordset.length === 1 && chordset[0] === "") {
alert("Enter chords.")
return;
}
startIteration(time, speedup, display_time);
document.querySelector(".start").innerHTML = "STOP";
state = "STARTED";
} else {
document.querySelector(".start").innerHTML = "START";
stopIteration();
state = "STOPPED";
}
}
var startIteration = (time, speedup, display_time, previous_chord) => {
clearHighlights();
var selected_chord_index = Math.floor(chordset.length * Math.random());
var selected_chord = chordset[selected_chord_index];
if (selected_chord === previous_chord) {
selected_chord = chordset[(selected_chord_index + chordset.length - 1) % chordset.length];
}
document.querySelector(".chord-name").innerHTML = selected_chord;
document.querySelector(".countdown").style.transition = `width ${time}s linear`;
document.querySelector(".countdown").style.visibility = `visible`;
document.querySelector(".countdown").style.width = "0%";
timeout = window.setTimeout(endInteration.bind(null, time, speedup, display_time, selected_chord), time * 1000);
}
var endInteration = (time, speedup, display_time, selected_chord) => {
document.querySelector(".countdown").style.transition = `none`;
document.querySelector(".countdown").style.width = "100%";
document.querySelector(".countdown").style.visibility = `hidden`;
highlightChord(selected_chord);
playChord(selected_chord);
time = Math.max(time - speedup, 1);
timeout = window.setTimeout(startIteration.bind(null, time, speedup, display_time, selected_chord), display_time * 1000);
}
var stopIteration = () => {
window.clearTimeout(timeout);
document.querySelector(".countdown").style.transition = `none`;
document.querySelector(".countdown").style.width = "100%";
document.querySelector(".countdown").style.visibility = `visible`;
}
</script>
</body>
</html>