speaker-calibration 2.2.65 → 2.2.67

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/.eslintignore +71 -71
  2. package/.eslintrc.json +40 -40
  3. package/.gitignore +81 -0
  4. package/.prettierignore +69 -69
  5. package/.prettierrc +14 -14
  6. package/LICENSE +20 -20
  7. package/README.md +133 -133
  8. package/__mocks__/fileMock.js +1 -1
  9. package/__mocks__/styleMock.js +1 -1
  10. package/babel.config.js +3 -3
  11. package/coverage/clover.xml +71 -71
  12. package/coverage/coverage-final.json +224 -224
  13. package/coverage/lcov-report/PythonServerInterface.js.html +265 -265
  14. package/coverage/lcov-report/base.css +354 -354
  15. package/coverage/lcov-report/block-navigation.js +82 -82
  16. package/coverage/lcov-report/index.html +123 -123
  17. package/coverage/lcov-report/prettify.css +101 -101
  18. package/coverage/lcov-report/prettify.js +937 -937
  19. package/coverage/lcov-report/sorter.js +189 -189
  20. package/coverage/lcov-report/src/index.html +121 -121
  21. package/coverage/lcov-report/src/server/PythonServerInterface.js.html +268 -268
  22. package/coverage/lcov-report/src/server/index.html +123 -123
  23. package/coverage/lcov-report/src/tasks/audioCalibrator.js.html +499 -499
  24. package/coverage/lcov-report/src/tasks/audioRecorder.js.html +412 -412
  25. package/coverage/lcov-report/src/tasks/index.html +143 -143
  26. package/coverage/lcov-report/src/tasks/volume/index.html +123 -123
  27. package/coverage/lcov-report/src/tasks/volume/volume.js.html +409 -409
  28. package/coverage/lcov-report/src/utils.js.html +172 -172
  29. package/coverage/lcov.info +91 -91
  30. package/dist/example/fetch-languages-sheets.js +77 -77
  31. package/dist/example/i18n.js +35846 -35846
  32. package/dist/example/index.html +47 -47
  33. package/dist/example/listener.html +62 -62
  34. package/dist/example/listener.js +282 -282
  35. package/dist/example/server.js +51 -51
  36. package/dist/example/speaker.html +145 -145
  37. package/dist/example/speakerUI.js +272 -272
  38. package/dist/example/styles.css +92 -92
  39. package/dist/main.js +17 -17
  40. package/dist/mlsGen.js +6814 -6814
  41. package/dist/mlsGen.wasm +0 -0
  42. package/dist/package-lock.json +1018 -1018
  43. package/dist/package.json +18 -18
  44. package/doc/AudioCalibrator.html +417 -417
  45. package/doc/AudioPeer.html +251 -251
  46. package/doc/AudioRecorder.html +195 -195
  47. package/doc/ImpulseResponse.html +215 -215
  48. package/doc/Listener.html +308 -308
  49. package/doc/MlsGenInterface.html +226 -226
  50. package/doc/MyEventEmitter.html +274 -274
  51. package/doc/PythonServerAPI.html +109 -109
  52. package/doc/Speaker.html +276 -276
  53. package/doc/Takes%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +128 -128
  54. package/doc/Takes%20the%20url%20of%20the%20current%20site%0Aand%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +138 -138
  55. package/doc/Takes%20the%20url%20of%20the%20current%20site%20and%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +137 -137
  56. package/doc/Volume.html +88 -88
  57. package/doc/audioCalibrator.js.html +179 -179
  58. package/doc/audioPeer.js.html +175 -175
  59. package/doc/audioRecorder.js.html +163 -163
  60. package/doc/creates%20a%20new%20AudioRecorder%20instance.%20%0ASets%20up%20the%20audio%20context%20and%20file%20reader..html +114 -114
  61. package/doc/fonts/OpenSans-Bold-webfont.svg +1829 -1829
  62. package/doc/fonts/OpenSans-BoldItalic-webfont.svg +1829 -1829
  63. package/doc/fonts/OpenSans-Italic-webfont.svg +1829 -1829
  64. package/doc/fonts/OpenSans-Light-webfont.svg +1830 -1830
  65. package/doc/fonts/OpenSans-LightItalic-webfont.svg +1834 -1834
  66. package/doc/fonts/OpenSans-Regular-webfont.svg +1830 -1830
  67. package/doc/global.html +308 -308
  68. package/doc/index.html +58 -58
  69. package/doc/listener.js.html +170 -170
  70. package/doc/mlsGen_mlsGenInterface.js.html +117 -117
  71. package/doc/myEventEmitter.js.html +124 -124
  72. package/doc/peer-connection_audioPeer.js.html +188 -188
  73. package/doc/peer-connection_listener.js.html +311 -311
  74. package/doc/peer-connection_speaker.js.html +381 -381
  75. package/doc/scripts/linenumber.js +25 -25
  76. package/doc/scripts/prettify/Apache-License-2.0.txt +202 -202
  77. package/doc/scripts/prettify/lang-css.js +24 -24
  78. package/doc/scripts/prettify/prettify.js +640 -640
  79. package/doc/server_PythonServerAPI.js.html +160 -160
  80. package/doc/speaker.js.html +248 -248
  81. package/doc/styles/jsdoc-default.css +371 -371
  82. package/doc/styles/prettify-jsdoc.css +111 -111
  83. package/doc/styles/prettify-tomorrow.css +163 -163
  84. package/doc/tasks_audioCalibrator.js.html +207 -207
  85. package/doc/tasks_audioRecorder.js.html +190 -190
  86. package/doc/tasks_impulse-response_impulseResponse.js.html +442 -442
  87. package/doc/tasks_impulse-response_mlsGen_mlsGenInterface.js.html +175 -175
  88. package/doc/tasks_volume_volume.js.html +185 -185
  89. package/doc/utils.js.html +105 -105
  90. package/jest.config.js +173 -173
  91. package/netlify.toml +26 -26
  92. package/package.json +69 -69
  93. package/src/config/firebase.js +25 -25
  94. package/src/index.html +21 -21
  95. package/src/main.js +23 -23
  96. package/src/myEventEmitter.js +83 -83
  97. package/src/peer-connection/audioPeer.js +151 -151
  98. package/src/peer-connection/listener.js +306 -306
  99. package/src/peer-connection/peerErrors.js +25 -25
  100. package/src/peer-connection/speaker.js +471 -469
  101. package/src/server/PythonServerAPI.js +587 -587
  102. package/src/tasks/audioCalibrator.js +308 -308
  103. package/src/tasks/audioRecorder.js +281 -281
  104. package/src/tasks/combination/combination.js +2092 -2034
  105. package/src/tasks/combination/mlsGen/mlsGen.cpp +98 -98
  106. package/src/tasks/combination/mlsGen/mlsGen.hpp +303 -303
  107. package/src/tasks/combination/mlsGen/mlsGenInterface.js +131 -131
  108. package/src/tasks/combination/mlsGen/mlsGenTest.cpp +180 -180
  109. package/src/tasks/impulse-response/impulseResponse.js +610 -610
  110. package/src/tasks/impulse-response/mlsGen/mlsGen.cpp +98 -98
  111. package/src/tasks/impulse-response/mlsGen/mlsGen.hpp +303 -303
  112. package/src/tasks/impulse-response/mlsGen/mlsGenInterface.js +131 -131
  113. package/src/tasks/impulse-response/mlsGen/mlsGenTest.cpp +180 -180
  114. package/src/tasks/volume/volume.cpp +2 -2
  115. package/src/tasks/volume/volume.hpp +22 -22
  116. package/src/tasks/volume/volume.js +279 -279
  117. package/src/utils.js +88 -88
  118. package/webpack.config.js +37 -37
  119. package/makefile +0 -74
@@ -1,469 +1,471 @@
1
- import QRCode from 'qrcode';
2
- import AudioPeer from './audioPeer';
3
- import {sleep} from '../utils';
4
- import {
5
- UnsupportedDeviceError,
6
- MissingSpeakerIdError,
7
- CalibrationTimedOutError,
8
- } from './peerErrors';
9
-
10
- import database from '../config/firebase';
11
- import {ref, set, get, child} from 'firebase/database';
12
- import {phrases} from '../../dist/example/i18n';
13
-
14
- /**
15
- * @class Handles the speaker's side of the connection. Responsible for initiating the connection,
16
- * rendering the QRCode, and answering the call.
17
- * @augments AudioPeer
18
- */
19
- class Speaker extends AudioPeer {
20
- /**
21
- * Takes the url of the current site and a target element where html elements will be appended.
22
- *
23
- * @param params - See type definition for initParameters.
24
- * @param Calibrator - An instance of the AudioCalibrator class, should not use AudioCalibrator directly, instead use an extended class available in /tasks/.
25
- * @param CalibratorInstance
26
- * @example
27
- */
28
- constructor(params, CalibratorInstance) {
29
- super(params);
30
- this.siteUrl += '/listener?';
31
- this.ac = CalibratorInstance;
32
- this.result = null;
33
- this.debug = params?.debug ?? false;
34
- this.isSmartPhone = params?.isSmartPhone ?? false;
35
- this.calibrateSoundHz = params?.calibrateSoundHz ?? 48000;
36
- this.calibrateSoundSamplingDesiredBits = params?.calibrateSoundSamplingDesiredBits ?? 24;
37
- this.instructionDisplayId = params?.instructionDisplayId ?? '';
38
- this.timeToCalibrateDisplay = params?.timeToCalibrateId ?? '';
39
- this.soundMessageId = params?.soundMessageId ?? '';
40
- this.titleDisplayId = params?.titleDisplayId ?? '';
41
- this.timeToCalibrate = params?.timeToCalibrate ?? 10;
42
-
43
- /* Set up callbacks that handle any events related to our peer object. */
44
- this.peer.on('open', this.#onPeerOpen);
45
- this.peer.on('connection', this.#onPeerConnection);
46
- this.peer.on('close', this.#onPeerClose);
47
- this.peer.on('disconnected', this.#onPeerDisconnected);
48
- this.peer.on('error', this.#onPeerError);
49
- }
50
-
51
- static getMicrophoneNamesFromDatabase = async () => {
52
- const dbRef = ref(database);
53
- const snapshot = await get(child(dbRef, 'Microphone'));
54
- if (snapshot.exists()) {
55
- return Object.keys(snapshot.val());
56
- }
57
- return null;
58
- };
59
-
60
- static doesMicrophoneExist = async (speakerID, OEM) => {
61
- const dbRef = ref(database);
62
- const snapshot = await get(child(dbRef, `Microphone2/${OEM}/${speakerID}`));
63
- if (snapshot.exists()) {
64
- return true;
65
- }
66
- return false;
67
- };
68
-
69
- /**
70
- * Async factory method that creates the Speaker object, and returns a promise that resolves to the result of the calibration.
71
- *
72
- * @param params - The parameters to be passed to the peer object.
73
- * @param Calibrator - The class that defines the calibration process.
74
- * @param CalibratorInstance
75
- * @param timeOut - The amount of time to wait before timing out the connection (in milliseconds).
76
- * @public
77
- * @example
78
- */
79
- static startCalibration = async (params, CalibratorInstance, timeOut = 180000) => {
80
- window.speaker = new Speaker(params, CalibratorInstance);
81
- const {speaker} = window;
82
-
83
- // wrap the calibration process in a promise so we can await it
84
- return new Promise((resolve, reject) => {
85
- // when a call is received
86
- speaker.peer.on('call', async call => {
87
- // Answer the call (one way)
88
-
89
- call.answer();
90
- speaker.#removeUIElems();
91
- speaker.#showSpinner();
92
- speaker.ac.createLocalAudio(document.getElementById(speaker.targetElement));
93
- // when we start receiving audio
94
- call.on('stream', async stream => {
95
- window.localStream = stream;
96
- window.localAudio.srcObject = stream;
97
- window.localAudio.autoplay = false;
98
-
99
- // if the sinkSamplingRate is not set sleep
100
- while (!speaker.ac.sampleRatesSet()) {
101
- console.log('SinkSamplingRate is undefined, sleeping');
102
- await sleep(1);
103
- }
104
- // resolve when we have a result
105
- speaker.result = await speaker.ac.startCalibration(
106
- stream,
107
- params.gainValues,
108
- params.ICalib,
109
- params.knownIR,
110
- params.microphoneName,
111
- params.calibrateSoundCheck,
112
- params.isSmartPhone,
113
- params.calibrateSoundBurstDb,
114
- params.calibrateSoundBurstRepeats,
115
- params.calibrateSoundBurstSec,
116
- params.calibrateSoundBurstsWarmup,
117
- params.calibrateSoundHz,
118
- params.calibrateSoundIIRSec,
119
- params.calibrateSound1000HzPreSec,
120
- params.calibrateSound1000HzSec,
121
- params.calibrateSound1000HzPostSec,
122
- params.calibrateSoundBackgroundSecs,
123
- params.micManufacturer,
124
- params.micSerialNumber,
125
- params.micModelNumber,
126
- params.micModelName
127
- );
128
- speaker.#removeUIElems();
129
- resolve(speaker.result);
130
- });
131
- // if we do not receive a result within the timeout, reject
132
- setTimeout(() => {
133
- reject(
134
- new CalibrationTimedOutError(
135
- `Calibration failed to produce a result after ${
136
- timeOut / 1000
137
- } seconds. Please try again.`
138
- )
139
- );
140
- }, timeOut);
141
- });
142
- });
143
- };
144
-
145
- static testIIR = async (params, CalibratorInstance, IIR, timeOut = 180000) => {
146
- window.speaker = new Speaker(params, CalibratorInstance);
147
- const {speaker} = window;
148
-
149
- // wrap the calibration process in a promise so we can await it
150
- return new Promise((resolve, reject) => {
151
- // when a call is received
152
- speaker.peer.on('call', async call => {
153
- // Answer the call (one way)
154
- call.answer();
155
- speaker.#removeUIElems();
156
- speaker.#showSpinner();
157
- speaker.ac.createLocalAudio(document.getElementById(speaker.targetElement));
158
- // when we start receiving audio
159
- call.on('stream', async stream => {
160
- window.localStream = stream;
161
- window.localAudio.srcObject = stream;
162
- window.localAudio.autoplay = false;
163
-
164
- // if the sinkSamplingRate is not set sleep
165
- while (!speaker.ac.sampleRatesSet()) {
166
- console.log('SinkSamplingRate is undefined, sleeping');
167
- await sleep(1);
168
- }
169
- // resolve when we have a result
170
- speaker.result = await speaker.ac.playMLSwithIIR(stream, IIR);
171
- speaker.#removeUIElems();
172
- resolve(speaker.result);
173
- });
174
- // if we do not receive a result within the timeout, reject
175
- setTimeout(() => {
176
- reject(
177
- new CalibrationTimedOutError(
178
- `Calibration failed to produce a result after ${
179
- timeOut / 1000
180
- } seconds. Please try again.`
181
- )
182
- );
183
- }, timeOut);
184
- });
185
- });
186
- };
187
-
188
- /**
189
- * Called after the peer conncection has been opened.
190
- * Generates a QR code for the connection and displays it.
191
- *
192
- * @private
193
- * @example
194
- */
195
- #showQRCode = () => {
196
- // Get query string, the URL parameters to specify a Listener
197
- const queryStringParameters = {
198
- speakerPeerId: this.peer.id,
199
- isSmartPhone: this.isSmartPhone,
200
- calibrateSoundHz: this.calibrateSoundHz,
201
- calibrateSoundSamplingDesiredBits: this.calibrateSoundSamplingDesiredBits,
202
- };
203
- const queryString = this.queryStringFromObject(queryStringParameters);
204
- const uri = this.siteUrl + queryString;
205
- if (this.isSmartPhone) {
206
- // Display QR code for the participant to scan
207
- const qrCanvas = document.createElement('canvas');
208
- qrCanvas.setAttribute('id', 'qrCanvas');
209
- console.log(uri);
210
- QRCode.toCanvas(qrCanvas, uri, error => {
211
- if (error) console.error(error);
212
- });
213
- document.getElementById(this.targetElement).appendChild(qrCanvas);
214
- } else {
215
- // show the link to the user
216
- // If specified HTML Id is available, show QR code there
217
- if (document.getElementById(this.targetElement)) {
218
- // const linkTag = document.createElement('a');
219
- // linkTag.setAttribute('href', uri);
220
- // linkTag.innerHTML = 'Click here to start the calibration';
221
- // linkTag.target = '_blank';
222
- // document.getElementById(this.targetElement).appendChild(linkTag);
223
- // document.getElementById(this.targetElement).appendChild(qrCanvas);
224
-
225
- const proceedButton = document.createElement('button');
226
- proceedButton.setAttribute('id', 'calibrationProceedButton');
227
- proceedButton.setAttribute('class', 'btn btn-success');
228
- proceedButton.innerHTML = 'Proceed';
229
- proceedButton.onclick = () => {
230
- // open the link in a new tab
231
- window.open(uri, '_blank');
232
- // remove the button
233
- document.getElementById('calibrationProceedButton').remove();
234
- };
235
- document.getElementById(this.targetElement).appendChild(proceedButton);
236
- }
237
- }
238
- // or just print it to console
239
- console.log('TEST: Peer reachable at: ', uri);
240
- };
241
-
242
- #showSpinner = () => {
243
- const spinner = document.createElement('div');
244
- spinner.className = 'spinner-border ml-auto';
245
- spinner.role = 'status';
246
- spinner.ariaHidden = 'true';
247
- document.getElementById(this.targetElement).appendChild(spinner);
248
-
249
- // clear instructionDisplay
250
- const soundMessage = document.getElementById(this.soundMessageId);
251
- soundMessage.innerHTML = '';
252
- soundMessage.style.display = 'none';
253
- const instructionDisplay = document.getElementById(this.instructionDisplayId);
254
- const background = document.getElementById('background'); // todo: get background id from params
255
- if (instructionDisplay) {
256
- instructionDisplay.innerHTML = '';
257
- instructionDisplay.style.whiteSpace = 'nowrap';
258
- instructionDisplay.style.fontWeight = 'bold';
259
- instructionDisplay.style.width = 'fit-content';
260
- instructionDisplay.innerHTML = phrases.RC_soundRecording['en-US'];
261
- let fontSize = 100;
262
- instructionDisplay.style.fontSize = fontSize + 'px';
263
- while (instructionDisplay.scrollWidth > background.scrollWidth * 0.9 && fontSize > 10) {
264
- fontSize--;
265
- instructionDisplay.style.fontSize = fontSize + 'px';
266
- }
267
- // const p = document.createElement('p');
268
- // // font size
269
- // p.style.fontSize = '1.1rem';
270
- // p.style.fontWeight = 'normal';
271
- // p.style.paddingTop = '20px';
272
- // const timeToCalibrateText = phrases.RC_howLongToCalibrate['en-US'];
273
- // p.innerHTML = timeToCalibrateText.replace('111', this.timeToCalibrate);
274
- // instructionDisplay.appendChild(p);
275
- }
276
-
277
- const timeToCalibrateDisplay = document.getElementById(this.timeToCalibrateDisplay);
278
- if (timeToCalibrateDisplay) {
279
- const timeToCalibrateText = phrases.RC_howLongToCalibrate['en-US'];
280
- timeToCalibrateDisplay.innerHTML = timeToCalibrateText.replace('111', this.timeToCalibrate);
281
- timeToCalibrateDisplay.style.fontWeight = 'normal';
282
- timeToCalibrateDisplay.style.fontSize = '1rem';
283
- // timeToCalibrateDisplay.style.paddingTop = '20px';
284
- }
285
-
286
- // Update title - titleDisplayId
287
- const titleDisplay = document.getElementById(this.titleDisplayId);
288
- if (titleDisplay) {
289
- // replace 5 with 6
290
- titleDisplay.innerHTML = titleDisplay.innerHTML.replace('5', '6');
291
- }
292
- };
293
-
294
- #removeUIElems = () => {
295
- const parent = document.getElementById(this.targetElement);
296
- while (parent.firstChild) {
297
- parent.firstChild.remove();
298
- }
299
- };
300
-
301
- /**
302
- * Called when the peer connection is opened.
303
- * Saves the peer id and calls the QR code generator.
304
- *
305
- * @param peerId - The peer id of the peer connection.
306
- * @param id
307
- * @private
308
- * @example
309
- */
310
- #onPeerOpen = id => {
311
- // Workaround for peer.reconnect deleting previous id
312
- if (id === null) {
313
- console.error('Received null id from peer open');
314
- this.peer.id = this.lastPeerId;
315
- } else {
316
- this.lastPeerId = this.peer.id;
317
- }
318
-
319
- if (id !== this.peer.id) {
320
- console.warn('DEBUG Check you assumption that id === this.peer.id');
321
- }
322
-
323
- this.#showQRCode();
324
- };
325
-
326
- /**
327
- * Called when the peer connection is established.
328
- * Enforces a single connection.
329
- *
330
- * @param connection - The connection object.
331
- * @private
332
- * @example
333
- */
334
- #onPeerConnection = connection => {
335
- // Allow only a single connection
336
- if (this.conn && this.conn.open) {
337
- connection.on('open', () => {
338
- connection.send('Already connected to another client');
339
- setTimeout(() => {
340
- connection.close();
341
- }, 500);
342
- });
343
- return;
344
- }
345
-
346
- this.conn = connection;
347
- console.log('Connected to: ', this.conn.peer);
348
- this.#ready();
349
- };
350
-
351
- /**
352
- * Called when the peer connection is closed.
353
- *
354
- * @private
355
- * @example
356
- */
357
- #onPeerClose = () => {
358
- this.conn = null;
359
- console.log('Connection destroyed');
360
- };
361
-
362
- /**
363
- * Called when the peer connection is disconnected.
364
- * Attempts to reconnect.
365
- *
366
- * @private
367
- * @example
368
- */
369
- #onPeerDisconnected = () => {
370
- console.log('Connection lost. Please reconnect');
371
-
372
- // Workaround for peer.reconnect deleting previous id
373
- this.peer.id = this.lastPeerId;
374
- // eslint-disable-next-line no-underscore-dangle
375
- this.peer._lastServerId = this.lastPeerId;
376
- this.peer.reconnect();
377
- };
378
-
379
- /**
380
- * Called when the peer connection encounters an error.
381
- *
382
- * @param error
383
- * @private
384
- * @example
385
- */
386
- #onPeerError = error => {
387
- // TODO: check if this function is needed or not
388
- console.error(error);
389
- };
390
-
391
- /**
392
- * Called when data is received from the peer connection.
393
- *
394
- * @param data
395
- * @private
396
- * @example
397
- */
398
- #onIncomingData = data => {
399
- // enforce object type
400
- if (
401
- !Object.prototype.hasOwnProperty.call(data, 'name') ||
402
- !Object.prototype.hasOwnProperty.call(data, 'payload')
403
- ) {
404
- console.error('Received malformed data: ', data);
405
- return;
406
- }
407
-
408
- switch (data.name) {
409
- case 'samplingRate':
410
- this.ac.setSamplingRates(data.payload);
411
- break;
412
- case 'sampleSize':
413
- this.ac.setSampleSize(data.payload);
414
- break;
415
- case 'deviceType':
416
- this.ac.setDeviceType(data.payload);
417
- break;
418
- case 'deviceName':
419
- this.ac.setDeviceName(data.payload);
420
- break;
421
- case 'deviceInfo':
422
- this.ac.setDeviceInfo(data.payload);
423
- console.log('Received device info from listener: ', data.payload);
424
- break;
425
- case UnsupportedDeviceError.name:
426
- case MissingSpeakerIdError.name:
427
- throw data.payload;
428
- break;
429
- default:
430
- break;
431
- }
432
- };
433
-
434
- /**
435
- * Called when the peer connection is #ready.
436
- *
437
- * @private
438
- * @example
439
- */
440
- #ready = () => {
441
- // Perform callback with data
442
- this.conn.on('data', this.#onIncomingData);
443
- this.conn.on('close', () => {
444
- console.log('Connection reset<br>Awaiting connection...');
445
- this.conn = null;
446
- });
447
- };
448
-
449
- /** .
450
- * .
451
- * .
452
- * Debug method for downloading the recorded audio
453
- *
454
- * @public
455
- * @example
456
- */
457
- downloadData = () => {
458
- this.ac.downloadData();
459
- };
460
- }
461
-
462
- /*
463
- Referenced links:
464
- https://stackoverflow.com/questions/28016664/when-you-pass-this-as-an-argument/28016676#28016676
465
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
466
- https://stackoverflow.com/questions/879152/how-do-i-make-javascript-beep [3]
467
- */
468
-
469
- export default Speaker;
1
+ import QRCode from 'qrcode';
2
+ import AudioPeer from './audioPeer';
3
+ import {sleep} from '../utils';
4
+ import {
5
+ UnsupportedDeviceError,
6
+ MissingSpeakerIdError,
7
+ CalibrationTimedOutError,
8
+ } from './peerErrors';
9
+
10
+ import database from '../config/firebase';
11
+ import {ref, set, get, child} from 'firebase/database';
12
+ import {phrases} from '../../dist/example/i18n';
13
+
14
+ /**
15
+ * @class Handles the speaker's side of the connection. Responsible for initiating the connection,
16
+ * rendering the QRCode, and answering the call.
17
+ * @augments AudioPeer
18
+ */
19
+ class Speaker extends AudioPeer {
20
+ /**
21
+ * Takes the url of the current site and a target element where html elements will be appended.
22
+ *
23
+ * @param params - See type definition for initParameters.
24
+ * @param Calibrator - An instance of the AudioCalibrator class, should not use AudioCalibrator directly, instead use an extended class available in /tasks/.
25
+ * @param CalibratorInstance
26
+ * @example
27
+ */
28
+ constructor(params, CalibratorInstance) {
29
+ super(params);
30
+ this.siteUrl += '/listener?';
31
+ this.ac = CalibratorInstance;
32
+ this.result = null;
33
+ this.debug = params?.debug ?? false;
34
+ this.isSmartPhone = params?.isSmartPhone ?? false;
35
+ this.calibrateSoundHz = params?.calibrateSoundHz ?? 48000;
36
+ this.calibrateSoundSamplingDesiredBits = params?.calibrateSoundSamplingDesiredBits ?? 24;
37
+ this.instructionDisplayId = params?.instructionDisplayId ?? '';
38
+ this.timeToCalibrateDisplay = params?.timeToCalibrateId ?? '';
39
+ this.soundMessageId = params?.soundMessageId ?? '';
40
+ this.titleDisplayId = params?.titleDisplayId ?? '';
41
+ this.timeToCalibrate = params?.timeToCalibrate ?? 10;
42
+
43
+ /* Set up callbacks that handle any events related to our peer object. */
44
+ this.peer.on('open', this.#onPeerOpen);
45
+ this.peer.on('connection', this.#onPeerConnection);
46
+ this.peer.on('close', this.#onPeerClose);
47
+ this.peer.on('disconnected', this.#onPeerDisconnected);
48
+ this.peer.on('error', this.#onPeerError);
49
+ }
50
+
51
+ static getMicrophoneNamesFromDatabase = async () => {
52
+ const dbRef = ref(database);
53
+ const snapshot = await get(child(dbRef, 'Microphone'));
54
+ if (snapshot.exists()) {
55
+ return Object.keys(snapshot.val());
56
+ }
57
+ return null;
58
+ };
59
+
60
+ static doesMicrophoneExist = async (speakerID, OEM) => {
61
+ const dbRef = ref(database);
62
+ const snapshot = await get(child(dbRef, `Microphone2/${OEM}/${speakerID}`));
63
+ if (snapshot.exists()) {
64
+ return true;
65
+ }
66
+ return false;
67
+ };
68
+
69
+ /**
70
+ * Async factory method that creates the Speaker object, and returns a promise that resolves to the result of the calibration.
71
+ *
72
+ * @param params - The parameters to be passed to the peer object.
73
+ * @param Calibrator - The class that defines the calibration process.
74
+ * @param CalibratorInstance
75
+ * @param timeOut - The amount of time to wait before timing out the connection (in milliseconds).
76
+ * @public
77
+ * @example
78
+ */
79
+ static startCalibration = async (params, CalibratorInstance, timeOut = 180000) => {
80
+ window.speaker = new Speaker(params, CalibratorInstance);
81
+ const {speaker} = window;
82
+
83
+ // wrap the calibration process in a promise so we can await it
84
+ return new Promise((resolve, reject) => {
85
+ // when a call is received
86
+ speaker.peer.on('call', async call => {
87
+ // Answer the call (one way)
88
+
89
+ call.answer();
90
+ speaker.#removeUIElems();
91
+ speaker.#showSpinner();
92
+ speaker.ac.createLocalAudio(document.getElementById(speaker.targetElement));
93
+ // when we start receiving audio
94
+ call.on('stream', async stream => {
95
+ window.localStream = stream;
96
+ window.localAudio.srcObject = stream;
97
+ window.localAudio.autoplay = false;
98
+
99
+ // if the sinkSamplingRate is not set sleep
100
+ while (!speaker.ac.sampleRatesSet()) {
101
+ console.log('SinkSamplingRate is undefined, sleeping');
102
+ await sleep(1);
103
+ }
104
+ // resolve when we have a result
105
+ speaker.result = await speaker.ac.startCalibration(
106
+ stream,
107
+ params.gainValues,
108
+ params.ICalib,
109
+ params.knownIR,
110
+ params.microphoneName,
111
+ params.calibrateSoundCheck,
112
+ params.isSmartPhone,
113
+ params.calibrateSoundBurstDb,
114
+ params.calibrateSoundBurstRepeats,
115
+ params.calibrateSoundBurstSec,
116
+ params.calibrateSoundBurstsWarmup,
117
+ params.calibrateSoundHz,
118
+ params.calibrateSoundIIRSec,
119
+ params.calibrateSound1000HzPreSec,
120
+ params.calibrateSound1000HzSec,
121
+ params.calibrateSound1000HzPostSec,
122
+ params.calibrateSoundBackgroundSecs,
123
+ params.micManufacturer,
124
+ params.micSerialNumber,
125
+ params.micModelNumber,
126
+ params.micModelName
127
+ );
128
+ speaker.#removeUIElems();
129
+ resolve(speaker.result);
130
+ });
131
+ // if we do not receive a result within the timeout, reject
132
+ setTimeout(() => {
133
+ reject(
134
+ new CalibrationTimedOutError(
135
+ `Calibration failed to produce a result after ${
136
+ timeOut / 1000
137
+ } seconds. Please try again.`
138
+ )
139
+ );
140
+ }, timeOut);
141
+ });
142
+ });
143
+ };
144
+
145
+ static testIIR = async (params, CalibratorInstance, IIR, timeOut = 180000) => {
146
+ window.speaker = new Speaker(params, CalibratorInstance);
147
+ const {speaker} = window;
148
+
149
+ // wrap the calibration process in a promise so we can await it
150
+ return new Promise((resolve, reject) => {
151
+ // when a call is received
152
+ speaker.peer.on('call', async call => {
153
+ // Answer the call (one way)
154
+ call.answer();
155
+ speaker.#removeUIElems();
156
+ speaker.#showSpinner();
157
+ speaker.ac.createLocalAudio(document.getElementById(speaker.targetElement));
158
+ // when we start receiving audio
159
+ call.on('stream', async stream => {
160
+ window.localStream = stream;
161
+ window.localAudio.srcObject = stream;
162
+ window.localAudio.autoplay = false;
163
+
164
+ // if the sinkSamplingRate is not set sleep
165
+ while (!speaker.ac.sampleRatesSet()) {
166
+ console.log('SinkSamplingRate is undefined, sleeping');
167
+ await sleep(1);
168
+ }
169
+ // resolve when we have a result
170
+ speaker.result = await speaker.ac.playMLSwithIIR(stream, IIR);
171
+ speaker.#removeUIElems();
172
+ resolve(speaker.result);
173
+ });
174
+ // if we do not receive a result within the timeout, reject
175
+ setTimeout(() => {
176
+ reject(
177
+ new CalibrationTimedOutError(
178
+ `Calibration failed to produce a result after ${
179
+ timeOut / 1000
180
+ } seconds. Please try again.`
181
+ )
182
+ );
183
+ }, timeOut);
184
+ });
185
+ });
186
+ };
187
+
188
+ /**
189
+ * Called after the peer conncection has been opened.
190
+ * Generates a QR code for the connection and displays it.
191
+ *
192
+ * @private
193
+ * @example
194
+ */
195
+ #showQRCode = () => {
196
+ // Get query string, the URL parameters to specify a Listener
197
+ const queryStringParameters = {
198
+ speakerPeerId: this.peer.id,
199
+ isSmartPhone: this.isSmartPhone,
200
+ calibrateSoundHz: this.calibrateSoundHz,
201
+ calibrateSoundSamplingDesiredBits: this.calibrateSoundSamplingDesiredBits,
202
+ };
203
+ const queryString = this.queryStringFromObject(queryStringParameters);
204
+ const uri = this.siteUrl + queryString;
205
+ if (this.isSmartPhone) {
206
+ // Display QR code for the participant to scan
207
+ const qrCanvas = document.createElement('canvas');
208
+ qrCanvas.setAttribute('id', 'qrCanvas');
209
+ console.log(uri);
210
+ QRCode.toCanvas(qrCanvas, uri, error => {
211
+ if (error) console.error(error);
212
+ });
213
+ document.getElementById(this.targetElement).appendChild(qrCanvas);
214
+ } else {
215
+ // show the link to the user
216
+ // If specified HTML Id is available, show QR code there
217
+ if (document.getElementById(this.targetElement)) {
218
+ // const linkTag = document.createElement('a');
219
+ // linkTag.setAttribute('href', uri);
220
+ // linkTag.innerHTML = 'Click here to start the calibration';
221
+ // linkTag.target = '_blank';
222
+ // document.getElementById(this.targetElement).appendChild(linkTag);
223
+ // document.getElementById(this.targetElement).appendChild(qrCanvas);
224
+
225
+ const proceedButton = document.createElement('button');
226
+ proceedButton.setAttribute('id', 'calibrationProceedButton');
227
+ proceedButton.setAttribute('class', 'btn btn-success');
228
+ proceedButton.innerHTML = 'Proceed';
229
+ proceedButton.onclick = () => {
230
+ // open the link in a new tab
231
+ window.open(uri, '_blank');
232
+ // remove the button
233
+ document.getElementById('calibrationProceedButton').remove();
234
+ };
235
+ document.getElementById(this.targetElement).appendChild(proceedButton);
236
+ }
237
+ }
238
+ // or just print it to console
239
+ console.log('TEST: Peer reachable at: ', uri);
240
+ };
241
+
242
+ #showSpinner = () => {
243
+ const spinner = document.createElement('div');
244
+ spinner.className = 'spinner-border ml-auto';
245
+ spinner.role = 'status';
246
+ spinner.ariaHidden = 'true';
247
+ document.getElementById(this.targetElement).appendChild(spinner);
248
+
249
+ // clear instructionDisplay
250
+ const soundMessage = document.getElementById(this.soundMessageId);
251
+ soundMessage.innerHTML = '';
252
+ soundMessage.style.display = 'none';
253
+ const instructionDisplay = document.getElementById(this.instructionDisplayId);
254
+ const background = document.getElementById('background'); // todo: get background id from params
255
+ if (instructionDisplay) {
256
+ instructionDisplay.innerHTML = '';
257
+ instructionDisplay.style.whiteSpace = 'nowrap';
258
+ instructionDisplay.style.fontWeight = 'bold';
259
+ instructionDisplay.style.width = 'fit-content';
260
+ instructionDisplay.innerHTML = phrases.RC_soundRecording['en-US'];
261
+ let fontSize = 100;
262
+ instructionDisplay.style.fontSize = fontSize + 'px';
263
+ while (instructionDisplay.scrollWidth > background.scrollWidth * 0.9 && fontSize > 10) {
264
+ fontSize--;
265
+ instructionDisplay.style.fontSize = fontSize + 'px';
266
+ }
267
+ // const p = document.createElement('p');
268
+ // // font size
269
+ // p.style.fontSize = '1.1rem';
270
+ // p.style.fontWeight = 'normal';
271
+ // p.style.paddingTop = '20px';
272
+ // const timeToCalibrateText = phrases.RC_howLongToCalibrate['en-US'];
273
+ // p.innerHTML = timeToCalibrateText.replace('111', this.timeToCalibrate);
274
+ // instructionDisplay.appendChild(p);
275
+ }
276
+
277
+ const timeToCalibrateDisplay = document.getElementById(this.timeToCalibrateDisplay);
278
+ if (timeToCalibrateDisplay) {
279
+ const timeToCalibrateText = phrases.RC_howLongToCalibrate['en-US'];
280
+ timeToCalibrateDisplay.innerHTML = timeToCalibrateText.replace('111', this.timeToCalibrate);
281
+ timeToCalibrateDisplay.style.fontWeight = 'normal';
282
+ timeToCalibrateDisplay.style.fontSize = '1rem';
283
+ // timeToCalibrateDisplay.style.paddingTop = '20px';
284
+ }
285
+
286
+ // Update title - titleDisplayId
287
+ const titleDisplay = document.getElementById(this.titleDisplayId);
288
+ if (titleDisplay) {
289
+ // replace 5 with 6
290
+ titleDisplay.innerHTML = this.isSmartPhone
291
+ ? titleDisplay.innerHTML.replace('2', '3')
292
+ : titleDisplay.innerHTML.replace('5', '6');
293
+ }
294
+ };
295
+
296
+ #removeUIElems = () => {
297
+ const parent = document.getElementById(this.targetElement);
298
+ while (parent.firstChild) {
299
+ parent.firstChild.remove();
300
+ }
301
+ };
302
+
303
+ /**
304
+ * Called when the peer connection is opened.
305
+ * Saves the peer id and calls the QR code generator.
306
+ *
307
+ * @param peerId - The peer id of the peer connection.
308
+ * @param id
309
+ * @private
310
+ * @example
311
+ */
312
+ #onPeerOpen = id => {
313
+ // Workaround for peer.reconnect deleting previous id
314
+ if (id === null) {
315
+ console.error('Received null id from peer open');
316
+ this.peer.id = this.lastPeerId;
317
+ } else {
318
+ this.lastPeerId = this.peer.id;
319
+ }
320
+
321
+ if (id !== this.peer.id) {
322
+ console.warn('DEBUG Check you assumption that id === this.peer.id');
323
+ }
324
+
325
+ this.#showQRCode();
326
+ };
327
+
328
+ /**
329
+ * Called when the peer connection is established.
330
+ * Enforces a single connection.
331
+ *
332
+ * @param connection - The connection object.
333
+ * @private
334
+ * @example
335
+ */
336
+ #onPeerConnection = connection => {
337
+ // Allow only a single connection
338
+ if (this.conn && this.conn.open) {
339
+ connection.on('open', () => {
340
+ connection.send('Already connected to another client');
341
+ setTimeout(() => {
342
+ connection.close();
343
+ }, 500);
344
+ });
345
+ return;
346
+ }
347
+
348
+ this.conn = connection;
349
+ console.log('Connected to: ', this.conn.peer);
350
+ this.#ready();
351
+ };
352
+
353
+ /**
354
+ * Called when the peer connection is closed.
355
+ *
356
+ * @private
357
+ * @example
358
+ */
359
+ #onPeerClose = () => {
360
+ this.conn = null;
361
+ console.log('Connection destroyed');
362
+ };
363
+
364
+ /**
365
+ * Called when the peer connection is disconnected.
366
+ * Attempts to reconnect.
367
+ *
368
+ * @private
369
+ * @example
370
+ */
371
+ #onPeerDisconnected = () => {
372
+ console.log('Connection lost. Please reconnect');
373
+
374
+ // Workaround for peer.reconnect deleting previous id
375
+ this.peer.id = this.lastPeerId;
376
+ // eslint-disable-next-line no-underscore-dangle
377
+ this.peer._lastServerId = this.lastPeerId;
378
+ this.peer.reconnect();
379
+ };
380
+
381
+ /**
382
+ * Called when the peer connection encounters an error.
383
+ *
384
+ * @param error
385
+ * @private
386
+ * @example
387
+ */
388
+ #onPeerError = error => {
389
+ // TODO: check if this function is needed or not
390
+ console.error(error);
391
+ };
392
+
393
+ /**
394
+ * Called when data is received from the peer connection.
395
+ *
396
+ * @param data
397
+ * @private
398
+ * @example
399
+ */
400
+ #onIncomingData = data => {
401
+ // enforce object type
402
+ if (
403
+ !Object.prototype.hasOwnProperty.call(data, 'name') ||
404
+ !Object.prototype.hasOwnProperty.call(data, 'payload')
405
+ ) {
406
+ console.error('Received malformed data: ', data);
407
+ return;
408
+ }
409
+
410
+ switch (data.name) {
411
+ case 'samplingRate':
412
+ this.ac.setSamplingRates(data.payload);
413
+ break;
414
+ case 'sampleSize':
415
+ this.ac.setSampleSize(data.payload);
416
+ break;
417
+ case 'deviceType':
418
+ this.ac.setDeviceType(data.payload);
419
+ break;
420
+ case 'deviceName':
421
+ this.ac.setDeviceName(data.payload);
422
+ break;
423
+ case 'deviceInfo':
424
+ this.ac.setDeviceInfo(data.payload);
425
+ console.log('Received device info from listener: ', data.payload);
426
+ break;
427
+ case UnsupportedDeviceError.name:
428
+ case MissingSpeakerIdError.name:
429
+ throw data.payload;
430
+ break;
431
+ default:
432
+ break;
433
+ }
434
+ };
435
+
436
+ /**
437
+ * Called when the peer connection is #ready.
438
+ *
439
+ * @private
440
+ * @example
441
+ */
442
+ #ready = () => {
443
+ // Perform callback with data
444
+ this.conn.on('data', this.#onIncomingData);
445
+ this.conn.on('close', () => {
446
+ console.log('Connection reset<br>Awaiting connection...');
447
+ this.conn = null;
448
+ });
449
+ };
450
+
451
+ /** .
452
+ * .
453
+ * .
454
+ * Debug method for downloading the recorded audio
455
+ *
456
+ * @public
457
+ * @example
458
+ */
459
+ downloadData = () => {
460
+ this.ac.downloadData();
461
+ };
462
+ }
463
+
464
+ /*
465
+ Referenced links:
466
+ https://stackoverflow.com/questions/28016664/when-you-pass-this-as-an-argument/28016676#28016676
467
+ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
468
+ https://stackoverflow.com/questions/879152/how-do-i-make-javascript-beep [3]
469
+ */
470
+
471
+ export default Speaker;