-
Notifications
You must be signed in to change notification settings - Fork 0
/
miditeach-1.0.js
280 lines (256 loc) · 7.5 KB
/
miditeach-1.0.js
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
class Miditeach {
constructor() {
this.playedNotes = new Array(12).fill(0);
this.totalChords = 0;
this.totalCorrect = 0;
this.totalIncorrect = 0;
this.times = [];
this.start = Date.now();
this.notes = [
"C",
"C#\nDb",
"D",
"D#\nEb",
"E",
"F",
"F#\nGb",
"G",
"G#\nAb",
"A",
"A#\nBb",
"B",
];
this.intervals = [
"1",
"2m",
"2",
"3m",
"3",
"4",
"T",
"5",
"6m",
"6",
"7m",
"7",
];
this.formulas = {
min: ["1", "3m", "5"],
maj: ["1", "3", "5"],
aug: ["1", "3", "T"],
dim: ["1", "3", "4"],
maj7: ["1", "3", "5", "7"],
min7: ["1", "3m", "5", "7m"],
dom: ["1", "3", "5", "7m"],
minmaj7: ["1", "3m", "5", "7"],
};
this.formulaTexts = {
min: "min",
maj: "maj",
aug: "aug",
dim: "dim",
maj7: "maj7",
min7: "min7",
dom: "7",
minmaj7: "mM7",
};
this.formulaSelected = {
min: false,
maj: false,
aug: false,
dim: false,
maj7: false,
min7: false,
dom: false,
minmaj7: false,
};
this.selectChord("min");
this.selectChord("maj");
this.sampleNextChord();
}
randChoice(array) {
// Returns an array element randomly
return array[Math.floor(Math.random() * array.length)];
}
sampleNextChord() {
// Samples a new chord from availables ones
this.playedNotes = new Array(12).fill(0);
this.formulaName = this.randChoice(
Object.entries(this.formulaSelected).filter(([k, v]) => v == true)
)[0];
this.formulaText = this.formulaTexts[this.formulaName];
this.formula = this.formulas[this.formulaName];
this.root = this.randChoice(this.notes);
this.rootSelect = this.randChoice(this.root.split("\n"));
this.rootText = this.rootSelect + this.formulaText;
this.expectedNotes = new Array(12).fill(0);
this.formula.forEach(
(element) =>
(this.expectedNotes[
(this.notes.indexOf(this.root) + this.intervals.indexOf(element)) % 12
] = 1)
);
document.querySelector("#chord").innerText = this.rootText;
document.querySelector("#formula").innerText = this.formula.join(" ");
}
isIncorrect() {
// Checks if the given midi inputs are incorrect for active chord
var correctNotes = 0;
var incorrectNotes = 0;
for (let i = 0; i < 12; i++) {
correctNotes += this.expectedNotes[i] && this.playedNotes[i];
}
for (let i = 0; i < 12; i++) {
incorrectNotes += this.playedNotes[i] > 0;
}
incorrectNotes -= correctNotes;
var res = incorrectNotes > 0;
return res;
}
isCorrect() {
// Checks if the given midi inputs are correct for active chord
var correctNotes = 0;
for (let i = 0; i < 12; i++) {
correctNotes += this.expectedNotes[i] && this.playedNotes[i];
}
var res = correctNotes == this.formula.length;
return res;
}
checkNext() {
// Checks for success or error
return this.isIncorrect() || this.isCorrect();
}
playedNotesStr() {
// Format the notes for display
var str = [];
this.playedNotes.forEach((element, index) => {
if (element > 0) {
str.push(this.notes[index].replace("\n", "/"));
}
});
return str.join(" ");
}
updatePiano() {
// Update piano display with pressed notes
for (let index = 1; index <= 12; index++) {
if (this.playedNotes[index - 1]) {
document
.getElementById("note" + index)
.setAttribute(
"style",
"fill:var(--primary-color);stroke:var(--background-color)"
);
} else {
document
.getElementById("note" + index)
.setAttribute(
"style",
"fill:var(--gray-color);stroke:var(--background-color)"
);
}
}
}
updatePlayedNotes(midiData) {
// Update game logic from midi inputs
if (!this.paused) {
var msgType = midiData[0];
var msgNote = midiData[1];
if (msgType == 144) {
this.playedNotes[msgNote % 12] = 1;
} else if (msgType == 128) {
this.playedNotes[msgNote % 12] = 0;
}
this.updatePiano()
}
}
selectChord(e) {
// Chord selection callback
this.formulaSelected[e] = !this.formulaSelected[e];
console.log(20 + this.formulaSelected[e] * 80);
document.querySelector("#select_" + e).style.opacity =
0.2 + this.formulaSelected[e] * 0.8;
}
reset() {
// Reset statistics
this.totalChords = 0;
this.totalCorrect = 0;
this.totalIncorrect = 0;
this.times = [];
this.start = Date.now()
this.sampleNextChord()
document.querySelector("#lastTime").innerText = 0;
document.querySelector("#meanTime").innerText = 0;
document.querySelector("#correct").innerText = miditeach.totalCorrect;
document.querySelector("#wrong").innerText = miditeach.totalIncorrect;
}
}
var miditeach = new Miditeach();
var paused = false;
var devices = [];
function loop() {
// Main game loop
var next = miditeach.checkNext() && !paused;
document.querySelector("#notes").innerText = miditeach.playedNotesStr(
miditeach.playedNotes
);
// On success or error
if (next) {
// Update time stats
var delta = Date.now() - miditeach.start;
miditeach.times.push(delta);
var mean = (miditeach.times.reduce((a, b) => a + b, 0) / miditeach.times.length || 0) / 1000;
document.querySelector("#lastTime").innerText = (delta / 1000).toFixed(2);
document.querySelector("#meanTime").innerText = mean.toFixed(2);
// Update colors
if (miditeach.isCorrect()) {
document.querySelector("#chord").style.color = getComputedStyle(
document.documentElement
).getPropertyValue("--correct-color");
document.querySelector("#footer").style.background = getComputedStyle(
document.documentElement
).getPropertyValue("--correct-color");
miditeach.totalCorrect += 1;
document.querySelector("#correct").innerText = miditeach.totalCorrect;
} else {
document.querySelector("#chord").style.color = getComputedStyle(
document.documentElement
).getPropertyValue("--wrong-color");
document.querySelector("#footer").style.background = getComputedStyle(
document.documentElement
).getPropertyValue("--wrong-color");
miditeach.totalIncorrect += 1;
document.querySelector("#wrong").innerText = miditeach.totalIncorrect;
}
// Add delay of 1 sec before next chord
paused = true;
setTimeout(function () {
paused = false;
miditeach.sampleNextChord();
miditeach.start = Date.now();
document.querySelector("#chord").style.color = document
.querySelector("#footer")
.style.getPropertyValue("--primary-color");
document.querySelector("#footer").style.background = document
.querySelector("#footer")
.style.getPropertyValue("--secondary-color");
}, 1000);
}
window.requestAnimationFrame(loop);
}
// MIDI inputs
navigator.requestMIDIAccess().then((access) => {
const inputs = access.inputs;
// Showing devices
access.inputs.forEach(function (input) {
devices.push(input.name);
});
document.querySelector("#devices").innerText =
"Devices detected : " + devices.join(", ");
// Callback on played notes
inputs.forEach((midiInput) => {
midiInput.onmidimessage = function (message) {
miditeach.updatePlayedNotes(message.data);
};
});
});
window.requestAnimationFrame(loop);