-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtone.html
More file actions
279 lines (256 loc) · 9.34 KB
/
tone.html
File metadata and controls
279 lines (256 loc) · 9.34 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>tone</title>
</head>
<style>
body {
display: flex; flex-direction: row; justify-content: center; align-items: center;
width: 100%; min-height: 100vh; margin: 0;
}
#keypad button {
font-size: 28px;
padding: 10px 24px;
width: 100%;
}
#keypad button:disabled { pointer-events: none; }
.extra {
display: none;
}
#settings label, #settings select, #settings input {
display: inline-block;
vertical-align: middle;
margin: 3px 2px;
}
</style>
<script>
var tone = tone || {};
tone.dtmf = [1209, 1336, 1477, 1633, 697, 770, 852, 941];
tone.extras = [
[...tone.dtmf], // all dtmf tones combined
[480, 620], // busy tone
[350, 440], // dial tone
[440, 480], // ringing tone
[1400, 2060, 2450, 2600] // off hook tone
];
tone.buttons = ["1", "2", "3", "A", "4", "5", "6", "B", "7", "8", "9", "C", "*", "0", "#", "D"];
tone.tones = [];
tone.cur_time = null;
tone.cur_tone = null;
tone.cur_string = null;
tone.cur_mode = null;
tone.cur_challenge = null;
tone.cur_beaten = false;
tone.cur_guessed = false;
tone.score_wins = 0;
tone.score_total = 0;
tone.build_wave = function(tones, length) {
var i2 = n => [n & 0xff, n >> 8 & 0xff];
var i4 = n => [...i2(n), ...i2(n >> 16)];
var smp_rt = 8000, smp_bits = 16, signed = true;
var end_fade = 0.005 * smp_rt;
var bit_rt = (smp_rt * smp_bits + 7) >> 3;
var block = (smp_bits + 7) >> 3;
var smp_count = Math.ceil(length / 1000 * smp_rt);
var data_len = smp_count * block;
var riff_len = 12 + 16 + 8 + data_len;
var cycls = tones.map(x => smp_rt / x / 2 / Math.PI);
var ceil = smp_bits == 32 ? 0xffffffff : (1 << smp_bits) - 1;
var ampl = 1 / tones.length * (ceil >>> 1);
var ar = [], out = new Uint8Array(8 + riff_len);
ar.push(...[0x52, 0x49, 0x46, 0x46, ...i4(riff_len) ]);
ar.push(...[0x57, 0x41, 0x56, 0x45, 0x66, 0x6d, 0x74, 0x20]);
ar.push(...[0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00]);
ar.push(...[...i4(smp_rt), ...i4(bit_rt) ]);
ar.push(...[...i2(block), ...i2(smp_bits) ]);
ar.push(...[0x64, 0x61, 0x74, 0x61, ...i4(data_len) ]);
for (var i = 0; i < ar.length; i++) out[i] = ar[i];
var samp, pos = ar.length;
for (var i = 0; i < smp_count; i++) {
samp = 0;
for (var j = 0; j < cycls.length; j++) samp += Math.sin(i / cycls[j]);
if (i > smp_count - end_fade) samp *= (smp_count - i) / end_fade;
if (!signed) samp = samp + 1;
samp = Math.floor(samp * ampl);
if (samp < 0) samp = samp + ceil + 1;
for (var j = 0; j < block; j++) out[pos + j] = samp >>> (j << 3) & 0xff;
pos += block;
}
return new Blob([out], {type: 'audio/wav'});
};
tone.build_tones = function() {
var time = parseInt(document.querySelector('#o-time').value);
if (time == tone.cur_time) return;
tone.cur_time = time;
var dtmf = [...Array(16).keys()].map(x => [tone.dtmf[x & 3], tone.dtmf[(x >> 2) + 4]]);
var slots = [...dtmf, ...tone.extras];
for (var i = 0; i < slots.length; i++) {
var blob = tone.build_wave(slots[i], time);
var audio = new Audio(URL.createObjectURL(blob));
audio.load();
tone.tones[i] = audio;
}
tone.volume(parseInt(document.querySelector('#o-volume').value));
};
tone.volume = function(volume) {
document.querySelector('#o-volume').value = volume;
volume /= 100;
for (var item of tone.tones) item.volume = volume;
};
tone.play = function(id, pause) {
var buffer = null;
if (Array.isArray(id)) {
if (id.length == 0) return;
if (id.length != 1) buffer = id.slice(1);
id = id[0];
}
if (tone.cur_tone != null) tone.cur_tone.stop();
if (id == null) return;
var sound = tone.tones[id];
if (sound == null) return;
var active = tone.cur_tone = {
sound: sound.cloneNode(),
buffer: buffer,
pause: pause || null,
play: function() {
if (tone.cur_tone != this) return;
this.handle = setTimeout(active.stop.bind(active, [true]), tone.cur_time + (this.pause || 10));
this.sound.play();
},
stop: function(use_cb) {
if (this.handle != null) clearTimeout(this.handle);
if (tone.cur_tone != this) return;
this.sound.pause();
this.sound.currentTime = 0;
tone.cur_tone = null;
if (!use_cb || this.buffer == null || this.buffer.length == 0) return;
while (this.buffer.length > 0) {
if (typeof this.buffer[0] != "function") {
tone.play(this.buffer, this.pause);
break;
}
this.buffer.shift()();
continue;
}
},
};
active.play();
};
tone.mode = function(mode) {
if (tone.cur_mode == mode) return;
tone.cur_mode = mode;
tone.score_wins = tone.score_total = 0;
tone.play(null);
tone.next(true);
};
tone.next = function(force) {
if (!force && !tone.cur_beaten) return;
var disp = document.querySelector("#misc-disp");
var options = [];
for (var item of document.querySelectorAll("#keypad button")) {
item.style.cssText = "";
if (item.checkVisibility() && !item.disabled) options.push(parseInt(item.dataset.id));
}
tone.play(null);
tone.set_beat(false);
tone.cur_guessed = false;
if (tone.cur_mode == "single") {
tone.cur_challenge = options[Math.floor(Math.random() * options.length)];
disp.textContent = "Score: " + tone.score_wins + " / " + tone.score_total;
}
};
tone.challenge = function() {
if (tone.cur_mode == "single") {
tone.play(tone.cur_challenge);
}
};
tone.set_beat = function(beat) {
tone.cur_beaten = beat;
document.querySelector("#c-beat").disabled = !beat;
};
tone.keypad_press = function(key) {
if (tone.cur_mode == "lock" && key.tagName.toLowerCase() == "td") key = key.querySelector("button");
if (key == null || key.tagName.toLowerCase() != "button") return;
var key_id = parseInt(key.dataset.id);
if (tone.cur_mode == "single") {
tone.play(key_id);
var beat = tone.cur_challenge == key_id;
tone.score_total += tone.cur_guessed ? 0 : 1;
tone.score_wins += tone.cur_guessed || !beat ? 0 : 1;
tone.cur_guessed = true;
if (!tone.cur_beaten) key.style.color = beat ? "#00FF00" : "#FF0000";
if (beat) tone.set_beat(true);
} else if (tone.cur_mode == "free") {
tone.play(key_id);
} else if (tone.cur_mode == "lock") {
key.disabled = !key.disabled;
}
};
window.onload = function() {
var table = document.querySelector("#keypad");
for (var i = 0; i < 16; i += 4) {
var row = document.createElement("tr");
for (var j = 0; j < 4; j++) {
var id = i + j;
var cell = document.createElement("td");
var button = document.createElement("button");
button.textContent = tone.buttons[i + j];
button.dataset.id = id;
cell.appendChild(button);
row.appendChild(cell);
if (j == 3) cell.classList.add("extra");
}
table.appendChild(row);
}
tone.build_tones();
tone.set_beat(false);
tone.mode("single");
//const link = document.createElement('a');
//link.href = url;
//link.download = "test.wav";
//document.body.appendChild(link);
//link.click();
//document.body.removeChild(link);
};
</script>
<body>
<div style="display: flex; flex-direction: column; align-items: center;">
<h2>DTMF tone trainer</h2>
<table id="settings" style="margin-bottom: 15px;" cellspacing="0" cellpadding="1">
<tr>
<td><label for="mode">Mode</label></td>
<td><select name="mode" id="s-mode" onchange="tone.mode(this.value)">
<option value="single">Single</option>
<option value="string">String</option>
<option value="free">Free</option>
<option value="lock">Lock/Unlock</option>
</select></td>
</tr>
<tr>
<td><label for="volume">Volume:</label></td>
<td><input type="range" id="s-volume" name="volume" min="0" max="20" value="15" oninput="tone.volume(this.value * 5)"></td>
<td><output id="o-volume">75</output></td>
</tr>
<tr>
<td><label for="digits">Time:</label></td>
<td><input type="range" id="s-time" name="time" min="1" max="20" value="7" oninput="document.querySelector('#o-time').value = this.value * 50; tone.build_tones()" onmouseup=""></td>
<td style="min-width: 35px;"><output id="o-time">350</output></td>
</tr>
<tr>
<td><label for="digits">Digits:</label></td>
<td><input type="range" id="s-digits" name="digits" min="1" max="16" value="10" oninput="document.querySelector('#o-digits').value = this.value"></td>
<td><output id="o-digits">10</output></td>
</tr>
</table>
<h3 id="misc-disp" style="margin-bottom: 15px; margin-top: 0px;"></h3>
<div id="controls" style="margin-bottom: 15px;">
<button id="c-play" style="display: inline-block" onclick="tone.challenge()">Play Tone</button>
<button id="c-beat" style="display: inline-block" onclick="tone.next(false)">Next Question</button>
<button id="c-confuse" style="display: inline-block" onclick="tone.play(16)">Mem</button>
</div>
<table id="keypad" style="min-width: 250px;" onclick="tone.keypad_press(event.target)"></table>
</div>
</body>
</html>