This repository has been archived by the owner on Nov 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmorse-pro-listener.js
398 lines (367 loc) · 18.2 KB
/
morse-pro-listener.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*!
This code is © Copyright Stephen C. Phillips, 2018.
Email: [email protected]
*/
/*
Licensed under the EUPL, Version 1.2 or – as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence");
You may not use this work except in compliance with the Licence.
You may obtain a copy of the Licence at: https://joinup.ec.europa.eu/community/eupl/
Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the Licence for the specific language governing permissions and limitations under the Licence.
*/
/**
* A class to 'listen' for Morse code from the microphone or an audio file, filter the sound and pass timings to a decoder to convert to text.
*/
export default class MorseListener {
/**
* @param {number} fftSize - Size of the discrete Fourier transform to use. Must be a power of 2 >= 256 (defaults to 256). A smaller fftSize gives better time resolution but worse frequency resolution.
* @param {number} [volumeFilterMin=-60] - Sound less than this volume (in dB) is ignored.
* @param {number} [volumeFilterMax=-30] - Sound greater than this volume (in dB) is ignored.
* @param {number} [frequencyFilterMin=550] - Sound less than this frequency (in Hz) is ignored.
* @param {number} [frequencyFilterMax=550] - Sound greater than this frequency (in Hz) is ignored.
* @param {number} [volumeThreshold=220] - If the volume is greater than this then the signal is taken as "on" (part of a dit or dah) (range 0-255).
* @param {Object} decoder - An instance of a configured decoder class.
* @param {function()} spectrogramCallback - Called every time fftSize samples are read.
Called with a dictionary parameter:
{
frequencyData: output of the DFT (the real values including DC component)
frequencyStep: frequency resolution in Hz
timeStep: time resolution in Hz
filterBinLow: index of the lowest frequency bin being analysed
filterBinHigh: index of the highest frequency bin being analysed
filterRegionVolume: volume in the analysed region
isOn: whether the analysis detected a signal or not
}
* @param {function()} frequencyFilterCallback - Called when the frequency filter parameters change.
Called with a dictionary parameter:
{
min: lowest frequency in Hz
max: highest frequency in Hz
}
The frequencies may well be different to that which is set as they are quantised.
* @param {function()} volumeFilterCallback - Called when the volume filter parameters change.
Called with a dictionary parameter:
{
min: low volume (in dB)
max: high volume (in dB)
}
If the set volumes are not numeric or out of range then the callback will return in range numbers.
* @param {function()} volumeThresholdCallback - Called with a single number as the argument when the volume filter threshold changes.
* @param {function()} micSuccessCallback - Called when the microphone has successfully been connected.
* @param {function()} micErrorCallback - Called with the error as the argument if there is an error connecting to the microphone.
* @param {function()} fileLoadCallback - Called with the AudioBuffer object as the argument when a file has successfully been loaded (and decoded).
* @param {function()} fileErrorCallback - Called with the error as the argument if there is an error in decoding a file.
* @param {function()} EOFCallback - Called when the playback of a file ends.
*/
constructor(
fftSize,
volumeFilterMin, volumeFilterMax,
frequencyFilterMin, frequencyFilterMax,
volumeThreshold,
decoder,
spectrogramCallback,
frequencyFilterCallback, volumeFilterCallback, volumeThresholdCallback,
micSuccessCallback, micErrorCallback,
fileLoadCallback, fileErrorCallback, EOFCallback
)
{
// audio input and output
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
this.audioContext = new window.AudioContext() || new window.webkitAudioContext();
if (spectrogramCallback !== undefined) this.spectrogramCallback = spectrogramCallback;
if (frequencyFilterCallback !== undefined) this.frequencyFilterCallback = frequencyFilterCallback;
if (volumeFilterCallback !== undefined) this.volumeFilterCallback = volumeFilterCallback;
if (volumeThresholdCallback !== undefined) this.volumeThresholdCallback = volumeThresholdCallback;
if (micSuccessCallback !== undefined) this.micSuccessCallback = micSuccessCallback;
if (micErrorCallback !== undefined) this.micErrorCallback = micErrorCallback;
if (fileLoadCallback !== undefined) this.fileLoadCallback = fileLoadCallback;
if (fileErrorCallback !== undefined) this.fileErrorCallback = fileErrorCallback;
if (EOFCallback !== undefined) this.EOFCallback = EOFCallback;
this.fftSize = fftSize;
// basic parameters from the sample rate
this.sampleRate = this.audioContext.sampleRate; // in Hz, 48000 on Chrome
this.maxFreq = this.sampleRate / 2; // in Hz; Nyquist theorem
this.freqBins = this.fftSize / 2;
this.timeStep = 1000 / (this.sampleRate / this.fftSize); // in ms
this.freqStep = this.maxFreq / this.freqBins;
this.initialiseAudioNodes();
this.defaults = {
fftSize: 256,
volumeFilterMin: -60,
volumeFilterMax: -30,
frequencyFilterMin: 550,
frequencyFilterMax: 550,
volumeThreshold: 200
};
this.volumeFilterMin = volumeFilterMin; // in dB
this.volumeFilterMax = volumeFilterMax;
this.frequencyFilterMin = frequencyFilterMin; // in Hz
this.frequencyFilterMax = frequencyFilterMax;
this.volumeThreshold = volumeThreshold; // in range [0-255]
this.decoder = decoder;
this.notStarted = true;
this.flushTime = 500; // how long to wait before pushing data to the decoder if e.g. you have a very long pause
this.input = undefined; // current state: undefined, "mic", "file"
}
/**
* Set the minimum threshold on the volume filter. i.e. the minimum volume considered to be a signal.
* Input validation is done to set a sensible default on non-numeric input and clamp the maximum to be zero.
* The volumFilterMax property is also set by this to be no less than the minimum.
* Calls the volumeFilterCallback with the new min and max.
* @param {number} v - the minimum volume in dB (-ve).
*/
set volumeFilterMin(v) {
if (isNaN(v)) v = this.defaults.volumeFilterMin;
// v is in dB and therefore -ve
v = Math.min(0, v);
this.analyserNode.minDecibels = v;
this.analyserNode.maxDecibels = Math.max(this.analyserNode.maxDecibels, v);
this.volumeFilterCallback({min: this.analyserNode.minDecibels, max: this.analyserNode.maxDecibels});
}
get volumeFilterMin() {
return this.analyserNode.minDecibels;
}
/**
* Set the maximum threshold on the volume filter. i.e. the maximum volume considered to be a signal.
* Input validation is done to set a sensible default on non-numeric input and clamp the maximum to be zero.
* The volumFilterMin property is also set by this to be no more than the maximum.
* Calls the volumeFilterCallback with the new min and max.
* @param {number} v - the maximum volume in dB (-ve).
*/
set volumeFilterMax(v) {
if (isNaN(v)) v = this.defaults.volumeFilterMax;
// v is in dB and therefore -ve
v = Math.min(0, v);
this.analyserNode.maxDecibels = v;
this.analyserNode.minDecibels = Math.min(this.analyserNode.minDecibels, v);
this.volumeFilterCallback({min: this.analyserNode.minDecibels, max: this.analyserNode.maxDecibels});
}
get volumeFilterMax() {
return this.analyserNode.maxDecibels;
}
/**
* Set the minimum threshold on the frequency filter. i.e. the minimum frequency to be considered.
* Input validation is done to set a sensible default on non-numeric input and the value is clamped to be between zero and the current maximum frequency.
* The actual minimum will be the minimum frequency of the frequency bin the chosen frequency lies in.
* Calls the frequencyFilterCallback with the new min and max.
* @param {number} v - the minimum frequency in Hz.
*/
set frequencyFilterMin(f) {
if (isNaN(f)) f = this.defaults.frequencyFilterMin;
f = Math.min(Math.max(f, 0), this.maxFreq);
this._filterBinLow = Math.min(Math.max(Math.round(f / this.freqStep), 1), this.freqBins); // at least 1 to avoid DC component
this._filterBinHigh = Math.max(this._filterBinLow, this._filterBinHigh); // high must be at least low
this.frequencyFilterCallback({min: this.frequencyFilterMin, max: this.frequencyFilterMax});
}
get frequencyFilterMin() {
return Math.max(this._filterBinLow * this.freqStep, 0);
}
/**
* Set the maximum threshold on the frequency filter. i.e. the maximum frequency to be considered.
* Input validation is done to set a sensible default on non-numeric input and the value is clamped to be between zero and the current maximum frequency.
* The actual minimum will be the maximum frequency of the frequency bin the chosen frequency lies in.
* Calls the frequencyFilterCallback with the new min and max.
* @param {number} v - the maximum frequency in Hz.
*/
set frequencyFilterMax(f) {
if (isNaN(f)) f = this.defaults.frequencyFilterMin;
f = Math.min(Math.max(f, 0), this.maxFreq);
this._filterBinHigh = Math.min(Math.max(Math.round(f / this.freqStep), 1), this.freqBins); // at least 1 to avoid DC component
this._filterBinLow = Math.min(this._filterBinHigh, this._filterBinLow); // low must be at most high
this.frequencyFilterCallback({min: this.frequencyFilterMin, max: this.frequencyFilterMax});
}
get frequencyFilterMax() {
return Math.min(this._filterBinHigh * this.freqStep, this.maxFreq);
}
/**
* Set the minimum and maximum frequency filter values to be closely surrounding a specific frequency.
* @param {number} f - the frequency to target.
*/
set frequencyFilter(f) {
this.frequencyFilterMin = f;
this.frequencyFilterMax = f;
}
/**
* Set the threshold used to determine if an anlaysed region has sufficient sound to be "on".
* Input validation is done to set a sensible default on non-numeric input and the value is clamped to be between zero and 255.
* @param {number} v - threshold volume [0, 255]
*/
set volumeThreshold(v) {
if (isNaN(v)) v = this.defaults.volumeThreshold;
this._volumeThreshold = Math.min(Math.max(Math.round(v), 0), 255);
this.volumeThresholdCallback(this._volumeThreshold);
}
get volumeThreshold() {
return this._volumeThreshold;
}
/**
* @access: private
*/
initialiseAudioNodes() {
// set up a javascript node (BUFFER_SIZE, NUM_INPUTS, NUM_OUTPUTS)
this.jsNode = this.audioContext.createScriptProcessor(this.fftSize, 1, 1); // buffer size can be 256, 512, 1024, 2048, 4096, 8192 or 16384
// set the event handler for when the buffer is full
this.jsNode.onaudioprocess = this.processSound.bind(this);
// set up an analyserNode
this.analyserNode = this.audioContext.createAnalyser();
this.analyserNode.smoothingTimeConstant = 0; // no mixing with the previous frame
this.analyserNode.fftSize = this.fftSize; // can be 32 to 2048 in webkit
this.frequencyData = new Uint8Array(this.freqBins); // create an arrray of the right size for the frequency data
}
/**
* Start the decoder listening to the microphone.
* Calls micSuccessCallback or micErrorCallback on success or error.
*/
startListening() {
this.stop();
// TODO: replace this with navigator.mediaDevices.getUserMedia() instead and shim for legacy browsers (https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia)
navigator.getUserMedia(
{
audio: true,
video: false
},
function(stream) {
// create an audio node from the stream
this.sourceNode = this.audioContext.createMediaStreamSource(stream);
this.input = "mic";
// connect nodes but don't connect mic to audio output to avoid feedback
this.sourceNode.connect(this.analyserNode);
this.jsNode.connect(this.audioContext.destination);
this.micSuccessCallback();
}.bind(this),
function(error) {
this.input = undefined;
this.micErrorCallback(error);
}.bind(this)
);
}
/**
* Load audio data from an ArrayBuffer. Use a FileReader to load from a file.
* Calls fileLoadCallback or fileErrorCallback on success or error.
* @param {ArrayBuffer} arrayBuffer
*/
loadArrayBuffer(arrayBuffer) {
// by separating loading (decoding) and playing, the playing can be done multiple times
this.audioContext.decodeAudioData(
arrayBuffer,
function(audioBuffer) {
this.fileAudioBuffer = audioBuffer;
this.fileLoadCallback(audioBuffer);
}.bind(this),
function(error) {
this.fileAudioBuffer = undefined;
this.fileErrorCallback(error);
}.bind(this)
);
}
/**
* Play a loaded audio file (through speakers) and decode it.
* Calls EOFCallback when buffer ends.
*/
playArrayBuffer() {
this.stop();
// make BufferSource node
this.sourceNode = this.audioContext.createBufferSource();
this.sourceNode.buffer = this.fileAudioBuffer;
this.sourceNode.onended = function() {
this.stop();
this.EOFCallback();
}.bind(this);
// connect nodes
this.jsNode.connect(this.audioContext.destination);
this.sourceNode.connect(this.analyserNode);
this.sourceNode.connect(this.audioContext.destination);
this.input = "file";
// play
this.sourceNode.start();
}
/**
* Stop listening.
*/
stop() {
if (this.input === undefined) {
return;
}
if (this.input === "file") {
this.sourceNode.stop();
this.sourceNode.disconnect(this.audioContext.destination);
}
this.sourceNode.disconnect(this.analyserNode);
this.jsNode.disconnect(this.audioContext.destination);
this.flush();
this.decoder.flush();
this.input = undefined;
}
/**
* This ScriptProcessorNode is called when it is full, we then actually look at the data in the analyserNode node to measure the volume in the frequency band of interest. We don't actually use the input or output of the ScriptProcesorNode.
* @access: private
*/
processSound() {
// get the data from the analyserNode node and put into frequencyData
this.analyserNode.getByteFrequencyData(this.frequencyData);
// find the average volume in the filter region
var filterRegionVolume = 0;
for (var i = this._filterBinLow; i <= this._filterBinHigh; i++) {
filterRegionVolume += this.frequencyData[i];
}
filterRegionVolume /= 1.0 * (this._filterBinHigh - this._filterBinLow + 1);
// record the data
var isOn = filterRegionVolume >= this._volumeThreshold;
this.recordOnOrOff(isOn);
this.spectrogramCallback({
frequencyData: this.frequencyData,
frequencyStep: this.freqStep,
timeStep: this.timeStep,
filterBinLow: this._filterBinLow,
filterBinHigh: this._filterBinHigh,
filterRegionVolume: filterRegionVolume,
isOn: isOn
});
}
/**
* Called each tick with whether the sound is judged to be on or off. If a change from on to off or off to on is detected then the number of ticks of the segment is passed to the decoder.
* @access: private
*/
recordOnOrOff(soundIsOn) {
if (this.notStarted) {
if (!soundIsOn) {
// wait until we hear something
return;
} else {
this.notStarted = false;
this.lastSoundWasOn = true;
this.ticks = 0;
}
}
if (this.lastSoundWasOn === soundIsOn) {
// accumulating an on or an off
this.ticks++;
if (this.ticks * this.timeStep > this.flushTime) {
// then it's e.g. a very long pause, so flush it through to decoder and keep counting
this.flush(soundIsOn);
this.ticks = 0;
}
} else {
// we've just changed from on to off or vice versa
this.flush(!soundIsOn); // flush the previous segment
this.lastSoundWasOn = soundIsOn;
this.ticks = 1; // at this moment we just saw the first tick of the new segment
}
}
/**
* Flush the current ticks to the decoder. Parameter is whether the ticks represent sound (on) or not.
*/
flush(on = this.lastSoundWasOn) {
this.decoder.addTiming((on ? 1 : -1) * this.ticks * this.timeStep);
}
// empty callbacks to avoid errors
spectrogramCallback(jsonData) { }
frequencyFilterCallback(jsonData) { }
volumeFilterCallback(jsonData) { }
volumeThresholdCallback(volume) { }
micSuccessCallback() { }
micErrorCallback(error) { }
fileLoadCallback(audioBuffer) { }
fileErrorCallback(error) { }
EOFCallback() { }
}