-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
318 lines (278 loc) · 10 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
316
317
318
<!DOCTYPE html>
<html>
<head>
<title>Perfect Pitch Tester</title>
<meta charset="utf-8">
<script>
const ls = localStorage;
const tuning = {
12: {
name: "12-tone",
desc: "12-tone equal temperament",
notes: ["C", "C#/Db", "D", "D#/Eb", "E", "F", "F#/Gb", "G", "G#/Ab", "A", "A#/Bb", "B"],
pitch: (note, octave) => 261.6255653 * (2 ** (note/12 + octave - 4))
},
24: {
name: "24-tone",
desc: "24-tone equal temperament. `+` indicates half-sharp",
notes: ["C", "C+", "C#/Db", "C#+", "D", "D+", "D#/Eb", "D#+", "E", "E+", "F", "F+",
"F#/Gb", "F#+", "G", "G+", "G#/Ab", "G#+", "A", "A+", "A#/Bb", "A#+", "B", "B+"],
pitch: (note, octave) => 261.6255653 * (2 ** (note/24 + octave - 4))
}
};
const settings = {
range: [
// { id, name, min=0, max, value, step=1 }
{
id: "MinOctave",
name: "Minimum octave",
max: 8, value: 2
},
{
id: "MaxOctave",
name: "Maximum octave",
max: 8, value: 6
},
{
id: "Duration",
name: "Duration (s)",
max: 5, value: 1, step: 0.1
},
{
id: "FailsDisplayed",
name: "# most missed",
max: Math.max(...Object.keys(tuning)), value: 3
}
]
};
const setting = name => ls["setting" + name];
const initls = (name, val = 0) => ls[name] ?? (ls[name] = val);
for (tune of Object.keys(tuning)) {
initls("stat" + tune + "Right");
initls("stat" + tune + "Wrong");
initls("stat" + tune + "Notes", new Array(tune * 2).fill(0));
// stats are stored in an array, of two values per note, the former being false count and former being true count
}
initls("system", 12);
// Question answered?
let answered;
// Tuning system
let system;
// Index of correct answer button
let answer;
let octave;
let removalMode = false;
const allowed = new Set();
function toggleRemoval() {
if (removalMode) reset();
removalMode = !removalMode;
document.getElementById("removalMode").innerText = removalMode ? "DISABLE" : "ENABLE";
}
const getPitch = () => tuning[system].pitch(answer, octave);
const stat = type => "stat" + system + type;
// https://github.com/escottalexander/simpleTones.js
const context = new (window.AudioContext || window.webkitAudioContext)();
function hearTone() {
const oscillatorNode = context.createOscillator();
const gainNode = context.createGain();
oscillatorNode.type = "sine";
oscillatorNode.frequency.setValueAtTime(getPitch(), context.currentTime);
gainNode.gain.setValueAtTime(0.3, context.currentTime);
gainNode.gain.setValueCurveAtTime([1.0, 0.61, 0.37, 0.22, 0.14, 0.08, 0.05, 0.0], context.currentTime, 2.0);
oscillatorNode.connect(gainNode);
gainNode.connect(context.destination);
oscillatorNode.start();
oscillatorNode.stop(context.currentTime + parseFloat(setting("Duration")));
}
function renderStats() {
const correct = ~~ls[stat("Right")];
const total = ~~ls[stat("Wrong")] + correct;
document.getElementById("statSuccessDisplay").textContent = "Correct: " + correct;
document.getElementById("statFailureDisplay").textContent = "Total: " + total;
document.getElementById("statPercentDisplay").textContent = "Rate: " + ~~(100 * correct / total) + "%";
const amount = Math.min(system, ~~setting("FailsDisplayed"));
if (amount == 0) document.getElementById("statMissingDisplay").textContent = "";
else {
const perNote = ls[stat("Notes")].split(",").map(x => ~~x);
const perNoteMap = new Map(); // note : fail%
for (let i = 0; i < system; i++) {
const score = perNote[i * 2] / (perNote[i * 2] + perNote[i * 2 + 1]);
perNoteMap.set(tuning[system].notes[i], isNaN(score) ? 0 : score);
}
const sorted = [...new Map([...perNoteMap.entries()].sort((a, b) => b[1] - a[1]))];
let text = "Most missed:";
for (let i = 0; i < amount; i++) {
text += "\n" + sorted[i][0] + " (" + ~~(100 * sorted[i][1]) + "%)";
}
document.getElementById("statMissingDisplay").textContent = text;
}
}
function resetStats() {
if (confirm("Reset stats for this tuning system?")) {
ls[stat("Right")] = ls[stat("Wrong")] = 0;
ls[stat("Notes")] = new Array(system * 2).fill(0);
renderStats();
}
}
function reset() {
for (button of document.getElementById("test").childNodes) {
button.classList.remove("buttonWrong");
button.classList.remove("buttonRight");
}
document.getElementById("testAnswerDisplay").innerText = " ";
answered = false;
if (allowed.size == 0) {
alert("You must have at least one enabled!");
return;
}
answer = Array.from(allowed)[~~(Math.random() * allowed.size)];
const min = ~~setting("MinOctave");
octave = ~~(Math.random() * (~~setting("MaxOctave") - min + 1)) + min;
}
function select(num) {
if (removalMode) {
const ta = document.getElementById("test").childNodes;
if (allowed.has(num)) {
allowed.delete(num);
ta[num].classList.add("disabled");
} else {
allowed.add(num);
ta[num].classList.remove("disabled");
}
return;
}
if (answered || !allowed.has(num)) return;
answered = true;
ls[num == answer ? "stat" + system + "Right" : "stat" + system + "Wrong"]++;
document.getElementById("testAnswerDisplay").innerText =
"The answer was " + tuning[system].notes[answer] + octave + " (" + ~~(100 * getPitch())/100 + "Hz)";
const ta = document.getElementById("test").childNodes;
ta[num].classList.add("buttonWrong");
ta[answer].classList.add("buttonRight");
const perNote = ls[stat("Notes")].split(",").map(x => ~~x);
perNote[answer * 2 + (num == answer)]++;
ls[stat("Notes")] = perNote;
renderStats();
}
function changeTuningSystem(count) {
system = ~~count;
const test = document.getElementById("test");
// remove all buttons
test.innerHTML = "";
for (let i = 0; i < count; i++) {
const button = document.createElement("button");
button.setAttribute("onclick", "select(" + i + ")");
button.textContent = tuning[count].notes[i];
test.appendChild(button);
}
document.getElementById("description").innerText = tuning[system].desc;
allowed.clear();
[...Array(system).keys()].forEach(x => allowed.add(x));
renderStats();
}
window.onload = () => {
// System is special and treated differently from other settings
const systemsMenu = document.getElementById("system");
for (system of Object.keys(tuning)) {
const option = document.createElement("option");
option.setAttribute("value", system);
option.innerText = tuning[system].name;
systemsMenu.appendChild(option);
}
changeTuningSystem(systemsMenu.value = ls["system"]);
const setlist = document.getElementById("settings");
for (range of settings.range) {
const id = range.id;
const name = range.name; // needed for callback to work right
const display = document.createElement("p");
display.id = "settingRange" + id + "Display";
setlist.appendChild(display);
const slider = document.createElement("input");
slider.id = "settingRange" + id;
slider.setAttribute("type", "range");
slider.setAttribute("min", range.min ?? 0);
slider.setAttribute("max", range.max);
slider.setAttribute("value", range.value);
slider.setAttribute("step", range.step ?? 0);
setlist.appendChild(slider);
slider.value = ls["setting" + id] ?? slider.value
const callback = () => display.textContent = name + ": " + (ls["setting" + id] = slider.value);
callback();
// special case
slider.addEventListener("input", id == "FailsDisplayed" ? () => { callback(); renderStats(); } : callback);
}
document.getElementById("system").addEventListener("input", e => {
changeTuningSystem(ls["system"] = e.target.value);
reset();
});
reset();
renderStats();
}
</script>
<style>
/* Formatting */
body {
background-color: #111;
color: #FFF;
/* Children */
display: flex;
gap: 20px;
}
p, h1, h2, h3, h4, input, label, button { white-space: pre; }
button, select, option {
color: #FFF;
border-color: #666;
background-color: #222;
}
/* Special formatting */
.buttonWrong { background-color: #844; }
.buttonRight { background-color: #484; }
.disabled {
color: #666;
cursor: not-allowed;
}
/* Make answer buttons bigger */
#test > button {
width: 5em;
height: 2em;
cursor: pointer;
}
#center { flex-grow: 1; }
#footer {
position: fixed;
bottom: 0;
}
</style>
</head>
<body>
<div>
<h3>STATS:</h3>
<!-- I don't think it's worth it to generate this HTML because of how simple & how few stats -->
<p id="statSuccessDisplay"></p>
<p id="statFailureDisplay"></p>
<p id="statPercentDisplay"></p>
<p id="statMissingDisplay"></p>
<button onclick="resetStats()">RESET</button>
</div>
<div id="center">
<h2> Perfect Pitch Tester</h2>
<p>This is a test to allow you to train/practice perfect pitch for various tuning systems.</p>
<label for="system">Tuning system: </label>
<select name="system" id="system"></select>
<p id="description"></p>
<h3>TEST:</h3>
<button onclick="hearTone()">HEAR</button>
<button onclick="reset()">NEW TEST</button>
<br>
<p>To toggle removal mode, click this button. In removal mode, clicking on a button will<br>toggle it out of the possible notes. Naturally, you cannot test while in removal mode.</p>
<button id="removalMode" onclick="toggleRemoval()">ENABLE</button>
<p id="testAnswerDisplay"> </p>
<div id="test"></div>
<p id="footer">Website copyright Napkin Technologies. <a href="https://github.com/NapkinTech/perfect-pitch">Source code</a>. Tones generated with code from <a href="https://github.com/escottalexander/simpleTones.js">simpleTones library</a>.</p>
</div>
<div>
<h3>SETTINGS:</h3>
<div id="settings"></div>
</div>
</body>
</html>