-
Notifications
You must be signed in to change notification settings - Fork 1
/
morse.js
147 lines (126 loc) · 3.88 KB
/
morse.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
const DOT = 0.1
const DASH = 3 * DOT;
const SPACE_BETWEEN_PARTS = DOT;
const SPACE_BETWEEN_WORDS = 7 * DOT;
const BREAKDOWN = {
'e': [DOT],
't': [DASH],
'i': [DOT, DOT],
'a': [DOT, DASH],
'n': [DASH, DOT],
'm': [DASH, DASH],
's': [DOT, DOT, DOT],
'u': [DOT, DOT, DASH],
'r': [DOT, DASH, DOT],
'w': [DOT, DASH, DASH],
'd': [DASH, DOT, DOT],
'k': [DASH, DOT, DASH],
'g': [DASH, DASH, DOT],
'o': [DASH, DASH, DASH],
'h': [DOT, DOT, DOT, DOT],
'v': [DOT, DOT, DOT, DASH],
'f': [DOT, DOT, DASH, DOT],
'l': [DOT, DASH, DOT, DOT],
'p': [DOT, DASH, DASH, DOT],
'j': [DOT, DASH, DASH, DASH],
'b': [DASH, DOT, DOT, DOT],
'x': [DASH, DOT, DOT, DASH],
'c': [DASH, DOT, DASH, DOT],
'y': [DASH, DOT, DASH, DASH],
'z': [DASH, DASH, DOT, DOT],
'q': [DASH, DASH, DOT, DASH],
}
class Player {
constructor() {
// Initialize WebAudio elements.
this.context = new AudioContext();
this.key = new GainNode(this.context, { gain: 0, });
this.amplification = new GainNode(this.context, { gain: 1, });
this.oscillator = new OscillatorNode(this.context, {
frequency: 800,
type: "sine",
});
// After when, the player can play a new code.
this.availableAfter = this.context.currentTime;
// Make the connections.
this.oscillator.connect(this.key);
this.key.connect(this.amplification);
this.amplification.connect(this.context.destination);
// Start the oscillator.
this.oscillator.start();
}
// Play the audio of a morse code.
play(code) {
const t = this.context.currentTime;
if (t <= this.availableAfter) return;
// Cancel future values so that the only playing code is the current one.
this.key.gain.cancelScheduledValues(t);
// Create morse audio.
let k = t;
for (let i = 0, k = t; i < BREAKDOWN[code].length; i++) {
const duration = BREAKDOWN[code][i];
this.key.gain.setValueAtTime(1, k);
this.key.gain.setValueAtTime(0, k + duration);
k += SPACE_BETWEEN_PARTS + duration;
}
this.availableAfter = k + SPACE_BETWEEN_WORDS;
}
volume(value) {
this.amplification.gain.value = value;
}
}
class ToggleMap {
constructor() {
// The list of selected codes.
this.selected = [];
// The code toggle map.
this.map = {
'e': false, 't': false, 'i': false, 'a': false, 'n': false,
'm': false, 's': false, 'u': false, 'r': false, 'w': false,
'd': false, 'k': false, 'g': false, 'o': false, 'h': false,
'v': false, 'f': false, 'l': false, 'p': false, 'j': false,
'b': false, 'x': false, 'c': false, 'y': false, 'z': false,
'q': false,
}
}
// Select or unselect a morse code.
set(code, value) {
this.map[code] = value;
this.selected = Object.entries(this.map)
.filter(([_, selected]) => selected)
.map(([code, _]) => code);
}
}
class Trainer {
constructor() {
this.code = 'e';
this.correct = 0;
this.wrong = 0;
this.player = new Player();
this.map = new ToggleMap();
this.map.set('e', true);
}
play() {
this.player.play(this.code);
}
randomize() {
const index = Math.floor(Math.random() * this.map.selected.length);
this.code = this.map.selected[index];
}
feed(code) {
if (code == this.code) {
this.correct++;
this.randomize();
this.play();
} else {
this.wrong++;
}
}
stats() {
return {
correct: this.correct,
wrong: this.wrong,
accuracy: 100 * this.correct / (this.correct + this.wrong),
}
}
}