speaker-calibration 2.2.88 → 2.2.89

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