Skip to content

Instantly share code, notes, and snippets.

@Yuffster
Created November 24, 2016 05:32
Show Gist options
  • Save Yuffster/d3fc3e72c268e103873a4c8350b42b40 to your computer and use it in GitHub Desktop.
Save Yuffster/d3fc3e72c268e103873a4c8350b42b40 to your computer and use it in GitHub Desktop.
class AlphabetGenerator {
constructor() {
const context_name = '_MORSE_AUDIO_CONTEXT'
var ac = window[context_name] || new window.AudioContext();
var an = ac.createAnalyser();
an.connect(ac.destination);
an.minDecibels = -140;
an.maxDecibels = 0;
this._audio_loaded = false;
this._queue = [];
this.context = ac;
this.analyser = an;
var my = this;
this.parseSubtitles();
this.loadAudio('nato.ogg', function() { my.audioLoaded(); });
}
loadAudio(url, fun=function(){}) {
var req = new XMLHttpRequest();
req.open("GET", url, true);
req.responseType = "arraybuffer";
var ac = this.context;
req.onreadystatechange = function(b) {
if (req.readyState != 4) return;
ac.decodeAudioData(req.response, function(buffer) {
if (!buffer.state) fun(buffer);
});
}
req.send();
}
audioLoaded() {
this._audio_loaded = true;
for (let m in this._queue) this.spellString(m);
}
parseSubtitles() {
var subs = NATO_SUBTITLES.split('\n');
this.times = {};
var i = 0;
var time = false;
for (let l of subs) { // Chunk into 4.
if (i == 1) time = l;
if (i == 2) this.times[l] = this.parseTime(time);
if (i == 3) i = 0;
else i++;
}
}
parseTime(time) {
console.log("parsing")
var times = [];
for (let t of time.split(' --> ')) {
let [h, m, s] = t.split(":");
times.push(h*60*60+m*60+parseFloat(s.replace(',', '.')));
}
console.log("done parsing")
return times;
}
spellString(str) {
for (let c in str) {
c = c.toUpperCase();
}
}
}
// Subtitles from Wikipedia.
const NATO_SUBTITLES = `1
00:00:00,700 --> 00:00:01,800
A
2
00:00:01,900 --> 00:00:03,000
B
3
00:00:03,100 --> 00:00:04,000
C
4
00:00:04,100 --> 00:00:05,000
D
5
00:00:05,200 --> 00:00:06,300
E
6
00:00:06,300 --> 00:00:07,500
F
7
00:00:07,900 --> 00:00:08,700
G
8
00:00:08,900 --> 00:00:10,000
H
9
00:00:10,100 --> 00:00:11,100
I
10
00:00:11,400 --> 00:00:12,400
J
11
00:00:12,500 --> 00:00:13,500
K
12
00:00:13,700 --> 00:00:14,700
L
13
00:00:15,000 --> 00:00:16,000
M
14
00:00:16,200 --> 00:00:17,300
N
15
00:00:17,500 --> 00:00:18,500
O
16
00:00:18,700 --> 00:00:19,500
P
17
00:00:19,900 --> 00:00:21,000
Q
18
00:00:21,100 --> 00:00:22,200
R
19
00:00:22,400 --> 00:00:23,300
S
20
00:00:23,700 --> 00:00:24,500
T
21
00:00:24,900 --> 00:00:26,000
U
22
00:00:26,200 --> 00:00:27,300
V
23
00:00:27,400 --> 00:00:28,500
W
24
00:00:28,700 --> 00:00:30,000
X
25
00:00:30,200 --> 00:00:31,200
Y
26
00:00:31,500 --> 00:00:32,500
Z
27
00:00:33,500 --> 00:00:34,500
0
28
00:00:34,700 --> 00:00:35,500
1
29
00:00:35,700 --> 00:00:36,900
2
30
00:00:37,000 --> 00:00:38,000
3
31
00:00:38,100 --> 00:00:39,000
4
32
00:00:39,300 --> 00:00:40,200
5
33
00:00:40,400 --> 00:00:41,500
6
34
00:00:41,600 --> 00:00:42,700
7
35
00:00:43,000 --> 00:00:44,000
8
36
00:00:44,100 --> 00:00:44,700
9`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment