speaker-calibration 2.2.270 → 2.2.272

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 (120) hide show
  1. package/.env.example +1 -1
  2. package/.eslintignore +71 -71
  3. package/.eslintrc.json +40 -40
  4. package/.gitignore +81 -81
  5. package/.prettierignore +69 -69
  6. package/.prettierrc +14 -14
  7. package/LICENSE +21 -21
  8. package/README.md +133 -133
  9. package/__mocks__/fileMock.js +1 -1
  10. package/__mocks__/styleMock.js +1 -1
  11. package/babel.config.js +3 -3
  12. package/coverage/clover.xml +71 -71
  13. package/coverage/coverage-final.json +224 -224
  14. package/coverage/lcov-report/PythonServerInterface.js.html +265 -265
  15. package/coverage/lcov-report/base.css +354 -354
  16. package/coverage/lcov-report/block-navigation.js +82 -82
  17. package/coverage/lcov-report/index.html +123 -123
  18. package/coverage/lcov-report/prettify.css +101 -101
  19. package/coverage/lcov-report/prettify.js +937 -937
  20. package/coverage/lcov-report/sorter.js +189 -189
  21. package/coverage/lcov-report/src/index.html +121 -121
  22. package/coverage/lcov-report/src/server/PythonServerInterface.js.html +268 -268
  23. package/coverage/lcov-report/src/server/index.html +123 -123
  24. package/coverage/lcov-report/src/tasks/audioCalibrator.js.html +499 -499
  25. package/coverage/lcov-report/src/tasks/audioRecorder.js.html +412 -412
  26. package/coverage/lcov-report/src/tasks/index.html +143 -143
  27. package/coverage/lcov-report/src/tasks/volume/index.html +123 -123
  28. package/coverage/lcov-report/src/tasks/volume/volume.js.html +409 -409
  29. package/coverage/lcov-report/src/utils.js.html +172 -172
  30. package/coverage/lcov.info +91 -91
  31. package/dist/example/NoSleep.min.js +1 -1
  32. package/dist/example/index.html +47 -47
  33. package/dist/example/server.js +51 -51
  34. package/dist/example/speaker.html +145 -145
  35. package/dist/example/speakerUI.js +273 -273
  36. package/dist/example/styles.css +152 -152
  37. package/dist/main.js +3208 -2
  38. package/dist/main.js.LICENSE.txt +118 -118
  39. package/dist/mlsGen.js +6814 -6814
  40. package/dist/mlsGen.wasm +0 -0
  41. package/dist/package-lock.json +1018 -1018
  42. package/dist/package.json +18 -18
  43. package/dist/phonePeer.js +1242 -1
  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 +78 -78
  93. package/src/config/firebase.js +27 -27
  94. package/src/index.html +21 -21
  95. package/src/listener-app/PhonePeer.js +504 -504
  96. package/src/main.js +22 -22
  97. package/src/myEventEmitter.js +83 -83
  98. package/src/peer-connection/audioPeer.js +100 -100
  99. package/src/peer-connection/listener.js +298 -298
  100. package/src/peer-connection/peerErrors.js +25 -25
  101. package/src/peer-connection/speaker.js +996 -996
  102. package/src/powerCheck.js +110 -110
  103. package/src/server/PythonServerAPI.js +1001 -1001
  104. package/src/tasks/audioCalibrator.js +451 -451
  105. package/src/tasks/audioRecorder.js +351 -351
  106. package/src/tasks/combination/combination.js +3988 -3988
  107. package/src/tasks/combination/mlsGen/mlsGen.cpp +98 -98
  108. package/src/tasks/combination/mlsGen/mlsGen.hpp +303 -303
  109. package/src/tasks/combination/mlsGen/mlsGenInterface.js +131 -131
  110. package/src/tasks/combination/mlsGen/mlsGenTest.cpp +180 -180
  111. package/src/tasks/impulse-response/impulseResponse.js +610 -610
  112. package/src/tasks/impulse-response/mlsGen/mlsGen.cpp +98 -98
  113. package/src/tasks/impulse-response/mlsGen/mlsGen.hpp +303 -303
  114. package/src/tasks/impulse-response/mlsGen/mlsGenInterface.js +131 -131
  115. package/src/tasks/impulse-response/mlsGen/mlsGenTest.cpp +180 -180
  116. package/src/tasks/volume/volume.cpp +2 -2
  117. package/src/tasks/volume/volume.hpp +22 -22
  118. package/src/tasks/volume/volume.js +279 -279
  119. package/src/utils.js +220 -220
  120. package/webpack.config.js +72 -72
@@ -1,451 +1,451 @@
1
- /* eslint-disable no-await-in-loop */
2
- import AudioRecorder from './audioRecorder';
3
- import PythonServerAPI from '../server/PythonServerAPI';
4
- import {sleep, saveToCSV} from '../utils';
5
-
6
- /**
7
- * .
8
- * .
9
- * .
10
- * Provides methods for calibrating the user's speakers
11
- *
12
- * @extends AudioRecorder
13
- */
14
- class AudioCalibrator extends AudioRecorder {
15
- /**
16
- *
17
- * @param numCaptures
18
- * @param numMLSPerCapture
19
- * @example
20
- */
21
- constructor(numCaptures = 1, numMLSPerCapture = 1) {
22
- super();
23
- this.numCaptures = numCaptures;
24
- this.numMLSPerCapture = numMLSPerCapture;
25
- this.pyServerAPI = new PythonServerAPI();
26
- this.currentTime = 0;
27
- }
28
-
29
- /** @private */
30
- isCalibrating = false;
31
-
32
- /** @private */
33
- sourceAudioContext;
34
-
35
- /** @private */
36
- sourceAudioContextConvolved;
37
-
38
- /** @protected */
39
- numCalibratingRounds = 1;
40
-
41
- /** @protected */
42
- numSuccessfulCaptured = 0;
43
-
44
- /** @private */
45
- sourceSamplingRate;
46
-
47
- /** @protected */
48
- calibrationNodes = [];
49
-
50
- /** @protected */
51
- calibrationNodesConvolved = [];
52
-
53
- /** @protected */
54
- localAudio;
55
-
56
- /** @private */
57
- startTime;
58
-
59
- numCalibratingRoundsCompleted = 0;
60
- /**
61
- * Called when a call is received.
62
- * Creates a local audio DOM element and attaches it to the page.
63
- *
64
- * @param targetElement
65
- * @example
66
- */
67
- createLocalAudio = targetElement => {
68
- this.localAudio = document.createElement('audio');
69
- this.localAudio.setAttribute('id', 'localAudio');
70
- targetElement.appendChild(this.localAudio);
71
- };
72
-
73
- addTimeStamp = taskName => {
74
- const currentTime = new Date().getTime(); // ms
75
- const elapsedTime = (currentTime - this.startTime) / 1000; // s
76
- const stepDuration = elapsedTime - this.currentTime;
77
- const startTimeSec = elapsedTime - stepDuration;
78
-
79
- this.currentTime = elapsedTime; // for the next call
80
-
81
- const startStr = startTimeSec.toFixed(1);
82
- const stepStr = stepDuration.toFixed(1);
83
-
84
- if (taskName === "Plot results") {
85
- // first push your normal Plot‐results line
86
- this.timeStamp.push(
87
- `${startStr} s. ∆ ${stepStr} s. ${taskName}`
88
- );
89
- // *then* push the final “Done” line using the final elapsedTime
90
- const endStr = elapsedTime.toFixed(1);
91
- this.timeStamp.push(
92
- `${endStr} s. Done`
93
- );
94
- }
95
- else {
96
- this.timeStamp.push(
97
- `${startStr} s. ∆ ${stepStr} s. ${taskName}`
98
- );
99
- }
100
- };
101
-
102
-
103
- recordBackground = async (
104
- stream,
105
- loopCondition = () => false,
106
- duringRecord = async () => {},
107
- afterRecord = async () => {},
108
- mode,
109
- checkRec
110
- ) => {
111
- console.warn('before recording background noise');
112
- // calibration loop
113
- while (loopCondition()) {
114
- // start recording
115
- console.warn('startRecording');
116
- await this.startRecording(stream);
117
-
118
- // do something during the recording such as sleep n amount of time
119
- console.warn('duringRecord');
120
- await duringRecord();
121
-
122
- // when done, stop recording
123
- console.warn('stopRecording');
124
- await this.stopRecording(mode, checkRec);
125
-
126
- // do something after recording such as start processing values
127
- console.warn('afterRecord');
128
- await afterRecord();
129
-
130
- // eslint-disable-next-line no-await-in-loop
131
- await sleep(1);
132
- }
133
- };
134
-
135
- /**
136
- *
137
- * @param {MediaStream} stream
138
- * @param {Function} playCalibrationAudio - (async) function that plays the calibration audio
139
- * @param {*} beforePlay - (async) function that is called before playing the audio
140
- * @param {*} beforeRecord - (async) function that is called before recording
141
- * @param {*} duringRecord - (async) function that is called while recording
142
- * @param {*} afterRecord - (async) function that is called after recording
143
- * @example
144
- */
145
- calibrationSteps = async (
146
- stream,
147
- playCalibrationAudio,
148
- beforePlay = async () => {},
149
- beforeRecord = async () => {},
150
- loopCondition = () => false,
151
- duringRecord = async () => {},
152
- afterRecord = async () => {},
153
- mode,
154
- checkRec
155
- ) => {
156
- // if it finished 2 attempts, it move to next iteration so reset numSuccessfulCaptured
157
- if (this.numSuccessfulCaptured >= 2) {
158
- this.numSuccessfulCaptured = 0;
159
- }
160
-
161
- // do something before playing such as using the MLS to fill the buffers
162
- console.warn('beforePlay');
163
- await beforePlay();
164
-
165
- // play calibration audio
166
- console.warn('playCalibrationAudio');
167
- playCalibrationAudio();
168
-
169
- // do something before recording such as awaiting a certain amount of time
170
- console.warn('beforeRecord');
171
- await beforeRecord();
172
- const totalSec =
173
- this._calibrateSoundBurstPreSec +
174
- (this.numMLSPerCapture - this.num_mls_to_skip) * this._calibrateSoundBurstSec +
175
- this._calibrateSoundBurstPostSec;
176
-
177
- // calibration loop
178
- while (loopCondition()) {
179
- if (this.isCalibrating) break;
180
- // start recording
181
- console.warn('startRecording');
182
- await this.startRecording(stream);
183
-
184
- if (this.isCalibrating) break;
185
- // do something during the recording such as sleep n amount of time
186
- console.warn('duringRecord');
187
- await duringRecord();
188
-
189
- if (this.isCalibrating) break;
190
- // when done, stop recording
191
- console.warn('stopRecording');
192
- await this.stopRecording(mode, checkRec);
193
-
194
- if (this.isCalibrating) break;
195
- // do something after recording such as start processing values
196
- console.warn('afterRecord');
197
- await afterRecord();
198
-
199
- // eslint-disable-next-line no-await-in-loop
200
- await sleep(1);
201
- }
202
- };
203
-
204
- /**
205
- *
206
- * @param {MediaStream} stream
207
- * @param {Function} playCalibrationAudio - (async) function that plays the calibration audio
208
- * @param {*} beforeRecord - (async) function that is called before recording
209
- * @param {*} afterRecord - (async) function that is called after recording
210
- * @param {Number} gainValue - the gain value to set the gain node to
211
- */
212
- volumeCalibrationSteps = async (
213
- stream,
214
- playCalibrationAudio,
215
- beforeRecord = () => {},
216
- afterRecord = () => {},
217
- gainValue,
218
- lCalib = 104.92978421490648,
219
- checkRec,
220
- checkSD,
221
- maxSD,
222
- maxRetry
223
- ) => {
224
- this.numCalibratingRoundsCompleted = 0;
225
- console.log('maxSD in VolumeCaibrationSteps: ', maxSD, '0' >= maxSD);
226
- // calibration loop
227
- while (!this.isCalibrating && this.numCalibratingRoundsCompleted < maxRetry) {
228
- if (this.isCalibrating) break;
229
- // before recording
230
- await beforeRecord(gainValue);
231
- if (this.isCalibrating) break;
232
- // start recording
233
- await this.startRecording(stream);
234
- if (this.isCalibrating) break;
235
- // play calibration audio
236
- console.log(`Calibration Round ${this.numCalibratingRoundsCompleted}`);
237
- await playCalibrationAudio();
238
- if (this.isCalibrating) break;
239
- // when done, stop recording
240
- console.log('Calibration Round Complete');
241
- await this.stopRecording('volume', checkRec);
242
- if (this.isCalibrating) break;
243
- // after recording
244
- await afterRecord(lCalib);
245
- const sd = await checkSD();
246
- let sdMessage;
247
- if (sd <= maxSD) {
248
- console.log(`SD =${sd}, less than calibrateSound1000HzMaxSD_dB=${maxSD}`);
249
- this.numCalibratingRoundsCompleted += maxRetry;
250
- sdMessage = `. SD = ${sd} dB`;
251
- } else {
252
- // if exist the maxSD do it one more time and only one more time
253
- console.log(`SD =${sd}, greater than calibrateSound1000HzMaxSD_dB=${maxSD}`);
254
- this.numCalibratingRoundsCompleted += 1;
255
- sdMessage = `. SD = ${sd} > ${this.calibrateSound1000HzMaxSD_dB} dB.`;
256
- }
257
- this.addTimeStamp(
258
- `${this.calibrateSound1000HzPreSec.toFixed(1)}` +
259
- `+${this.calibrateSound1000HzSec.toFixed(1)}` +
260
- `+${this.calibrateSound1000HzPostSec.toFixed(1)} s. ` +
261
- `1000 Hz at ${this.inDB} dB${sdMessage}`
262
- );
263
- this.calibrationNodes = [];
264
-
265
- // eslint-disable-next-line no-await-in-loop
266
- await sleep(2);
267
- }
268
- };
269
-
270
- /**
271
- * Simulates recording by convolving input signal with loudspeaker and microphone impulse responses
272
- *
273
- * @param {Array<number>} inputSignal - The input signal to be convolved
274
- * @param {Array<number>} loudspeakerImpulseResponse - The loudspeaker impulse response
275
- * @param {Array<number>} microphoneImpulseResponse - The microphone impulse response
276
- * @param {Function} afterRecord - Callback function to execute after recording simulation
277
- * @param {number} lCalib - Calibration parameter
278
- * @param {string} checkRec - Recording check type
279
- */
280
- simulatedVolumeCalibrationSteps = async (
281
- inputSignal,
282
- loudspeakerImpulseResponse,
283
- microphoneImpulseResponse,
284
- afterRecord = () => {},
285
- lCalib,
286
- checkSD,
287
- maxSD,
288
- maxRetry
289
- ) => {
290
- const totalSec = this.CALIBRATION_TONE_DURATION;
291
- // Convolve with loudspeaker and microphone impulse responses
292
- const convolvedSignalWithMicrophone = await this.pyServerAPI
293
- .irConvolution({
294
- input_signal: inputSignal,
295
- loudspeaker_ir: loudspeakerImpulseResponse,
296
- microphone_ir: microphoneImpulseResponse,
297
- duration: totalSec,
298
- sample_rate: this.sourceSamplingRate / this._calibrateSoundBurstDownsample,
299
- })
300
- .then(res => {
301
- console.log('res in simulatedVolumeCalibrationSteps: ', res);
302
- return res['output_signal'];
303
- });
304
-
305
- // Log details about the simulated recording
306
- const uniqueSet = new Set(convolvedSignalWithMicrophone);
307
- const numberOfUniqueValues = uniqueSet.size;
308
- const squaredValues = convolvedSignalWithMicrophone.map(value => value * value);
309
- const sum_of_squares = squaredValues.reduce((total, value) => total + value, 0);
310
- const squared_mean = sum_of_squares / convolvedSignalWithMicrophone.length;
311
- const dbLevel = 20 * Math.log10(Math.sqrt(squared_mean));
312
- const roundedDbLevel = Math.round(dbLevel * 10) / 10;
313
-
314
- console.log(
315
- '[SIMULATION] 1000-Hz recording: ' +
316
- roundedDbLevel +
317
- ' dB with ' +
318
- numberOfUniqueValues +
319
- ' unique values.'
320
- );
321
-
322
- // Save the simulated recording as if it were captured from a microphone
323
- this.saveVolumeRecording(convolvedSignalWithMicrophone);
324
-
325
- // Process the simulated recording
326
- await afterRecord(lCalib);
327
- const sd = await checkSD();
328
- let sdMessage;
329
- if (sd <= maxSD) {
330
- console.log(`SD =${sd}, less than calibrateSound1000HzMaxSD_dB=${maxSD}`);
331
- this.numCalibratingRoundsCompleted += maxRetry;
332
- sdMessage = `. SD = ${sd} dB`;
333
- } else {
334
- // if exist the maxSD do it one more time and only one more time
335
- console.log(`SD =${sd}, greater than calibrateSound1000HzMaxSD_dB=${maxSD}`);
336
- this.numCalibratingRoundsCompleted += 1;
337
- sdMessage = `. SD = ${sd} > ${this.calibrateSound1000HzMaxSD_dB} dB.`;
338
- }
339
- this.addTimeStamp(
340
- `${this.calibrateSound1000HzPreSec.toFixed(1)}` +
341
- `+${this.calibrateSound1000HzSec.toFixed(1)}` +
342
- `+${this.calibrateSound1000HzPostSec.toFixed(1)} s. ` +
343
- `1000 Hz at ${this.inDB} dB${sdMessage}`
344
- );
345
- };
346
-
347
- /**
348
- * Getter for the isCalibrating property.
349
- *
350
- * @public
351
- * @returns - True if the audio is being calibrated, false otherwise.
352
- * @example
353
- */
354
- getCalibrationStatus = () => this.isCalibrating;
355
-
356
- /** .
357
- * .
358
- * .
359
- * Set the sampling rate to the value received from the listener
360
- *
361
- * @param {*} sinkSamplingRate
362
- * @param samplingRate
363
- * @example
364
- */
365
- setSamplingRates = samplingRate => {
366
- this.sinkSamplingRate = samplingRate;
367
- this.sourceSamplingRate = samplingRate;
368
-
369
- // this.emit('update', {message: `sampling at ${samplingRate}Hz...`});
370
- };
371
-
372
- setSampleSize = sampleSize => {
373
- this.sampleSize = sampleSize;
374
- };
375
-
376
- setFlags = flags => {
377
- this.flags = flags;
378
- };
379
-
380
- sampleRatesSet = () => this.sourceSamplingRate && this.sinkSamplingRate;
381
-
382
- addCalibrationNode = node => {
383
- this.calibrationNodes.push(node);
384
- };
385
-
386
- addCalibrationNodeConvolved = node => {
387
- this.calibrationNodesConvolved.push(node);
388
- };
389
-
390
- makeNewSourceAudioContext = () => {
391
- const options = {
392
- sampleRate: this.sourceSamplingRate,
393
- };
394
-
395
- this.sourceAudioContext = new (window.AudioContext ||
396
- window.webkitAudioContext ||
397
- window.audioContext)(options);
398
-
399
- return this.sourceAudioContext;
400
- };
401
-
402
- makeNewSourceAudioContextConvolved = () => {
403
- const options = {
404
- sampleRate: this.sourceSamplingRate,
405
- };
406
-
407
- this.sourceAudioContextConvolved = new (window.AudioContext ||
408
- window.webkitAudioContext ||
409
- window.audioContext)(options);
410
-
411
- return this.sourceAudioContextConvolved;
412
- };
413
-
414
- /** .
415
- * .
416
- * .
417
- * Download the result of the calibration roudns
418
- *
419
- * @example
420
- */
421
- downloadData = () => {
422
- const recordings = this.getAllRecordedSignals();
423
- const i = recordings.length - 1;
424
- saveToCSV(recordings[i], `recordedMLSignal_${i}_unconvolved.csv`);
425
- };
426
- downloadSingleUnfilteredRecording = () => {
427
- const recordings = this.getAllUnfilteredRecordedSignals();
428
- saveToCSV(recordings[recordings.length - 1], `recordedMLSignal_unconvolved.csv`);
429
- };
430
- downloadSingleFilteredRecording = () => {
431
- const recordings = this.getAllFilteredRecordedSignals();
432
- console.log('Single filtered recording should be of length: ' + recordings[0].length);
433
- saveToCSV(recordings[0], `recordedMLSignal_convolved.csv`);
434
- };
435
- downloadUnfilteredRecordings = () => {
436
- const recordings = this.getAllRecordedSignals();
437
- console.log('unfilterd download?');
438
- for (let i = 0; i < recordings.length; i++) {
439
- console.log(i);
440
- saveToCSV(recordings[i], `recordedMLSignal_${i}_unconvolved.csv`);
441
- }
442
- };
443
- downloadFilteredRecordings = () => {
444
- const recordings = this.getAllFilteredRecordedSignals();
445
- for (let i = 0; i < recordings.length; i++) {
446
- saveToCSV(recordings[i], `recordedMLSignal_${i}_convolved.csv`);
447
- }
448
- };
449
- }
450
-
451
- export default AudioCalibrator;
1
+ /* eslint-disable no-await-in-loop */
2
+ import AudioRecorder from './audioRecorder';
3
+ import PythonServerAPI from '../server/PythonServerAPI';
4
+ import {sleep, saveToCSV} from '../utils';
5
+
6
+ /**
7
+ * .
8
+ * .
9
+ * .
10
+ * Provides methods for calibrating the user's speakers
11
+ *
12
+ * @extends AudioRecorder
13
+ */
14
+ class AudioCalibrator extends AudioRecorder {
15
+ /**
16
+ *
17
+ * @param numCaptures
18
+ * @param numMLSPerCapture
19
+ * @example
20
+ */
21
+ constructor(numCaptures = 1, numMLSPerCapture = 1) {
22
+ super();
23
+ this.numCaptures = numCaptures;
24
+ this.numMLSPerCapture = numMLSPerCapture;
25
+ this.pyServerAPI = new PythonServerAPI();
26
+ this.currentTime = 0;
27
+ }
28
+
29
+ /** @private */
30
+ isCalibrating = false;
31
+
32
+ /** @private */
33
+ sourceAudioContext;
34
+
35
+ /** @private */
36
+ sourceAudioContextConvolved;
37
+
38
+ /** @protected */
39
+ numCalibratingRounds = 1;
40
+
41
+ /** @protected */
42
+ numSuccessfulCaptured = 0;
43
+
44
+ /** @private */
45
+ sourceSamplingRate;
46
+
47
+ /** @protected */
48
+ calibrationNodes = [];
49
+
50
+ /** @protected */
51
+ calibrationNodesConvolved = [];
52
+
53
+ /** @protected */
54
+ localAudio;
55
+
56
+ /** @private */
57
+ startTime;
58
+
59
+ numCalibratingRoundsCompleted = 0;
60
+ /**
61
+ * Called when a call is received.
62
+ * Creates a local audio DOM element and attaches it to the page.
63
+ *
64
+ * @param targetElement
65
+ * @example
66
+ */
67
+ createLocalAudio = targetElement => {
68
+ this.localAudio = document.createElement('audio');
69
+ this.localAudio.setAttribute('id', 'localAudio');
70
+ targetElement.appendChild(this.localAudio);
71
+ };
72
+
73
+ addTimeStamp = taskName => {
74
+ const currentTime = new Date().getTime(); // ms
75
+ const elapsedTime = (currentTime - this.startTime) / 1000; // s
76
+ const stepDuration = elapsedTime - this.currentTime;
77
+ const startTimeSec = elapsedTime - stepDuration;
78
+
79
+ this.currentTime = elapsedTime; // for the next call
80
+
81
+ const startStr = startTimeSec.toFixed(1);
82
+ const stepStr = stepDuration.toFixed(1);
83
+
84
+ if (taskName === "Plot results") {
85
+ // first push your normal Plot‐results line
86
+ this.timeStamp.push(
87
+ `${startStr} s. ∆ ${stepStr} s. ${taskName}`
88
+ );
89
+ // *then* push the final “Done” line using the final elapsedTime
90
+ const endStr = elapsedTime.toFixed(1);
91
+ this.timeStamp.push(
92
+ `${endStr} s. Done`
93
+ );
94
+ }
95
+ else {
96
+ this.timeStamp.push(
97
+ `${startStr} s. ∆ ${stepStr} s. ${taskName}`
98
+ );
99
+ }
100
+ };
101
+
102
+
103
+ recordBackground = async (
104
+ stream,
105
+ loopCondition = () => false,
106
+ duringRecord = async () => {},
107
+ afterRecord = async () => {},
108
+ mode,
109
+ checkRec
110
+ ) => {
111
+ console.warn('before recording background noise');
112
+ // calibration loop
113
+ while (loopCondition()) {
114
+ // start recording
115
+ console.warn('startRecording');
116
+ await this.startRecording(stream);
117
+
118
+ // do something during the recording such as sleep n amount of time
119
+ console.warn('duringRecord');
120
+ await duringRecord();
121
+
122
+ // when done, stop recording
123
+ console.warn('stopRecording');
124
+ await this.stopRecording(mode, checkRec);
125
+
126
+ // do something after recording such as start processing values
127
+ console.warn('afterRecord');
128
+ await afterRecord();
129
+
130
+ // eslint-disable-next-line no-await-in-loop
131
+ await sleep(1);
132
+ }
133
+ };
134
+
135
+ /**
136
+ *
137
+ * @param {MediaStream} stream
138
+ * @param {Function} playCalibrationAudio - (async) function that plays the calibration audio
139
+ * @param {*} beforePlay - (async) function that is called before playing the audio
140
+ * @param {*} beforeRecord - (async) function that is called before recording
141
+ * @param {*} duringRecord - (async) function that is called while recording
142
+ * @param {*} afterRecord - (async) function that is called after recording
143
+ * @example
144
+ */
145
+ calibrationSteps = async (
146
+ stream,
147
+ playCalibrationAudio,
148
+ beforePlay = async () => {},
149
+ beforeRecord = async () => {},
150
+ loopCondition = () => false,
151
+ duringRecord = async () => {},
152
+ afterRecord = async () => {},
153
+ mode,
154
+ checkRec
155
+ ) => {
156
+ // if it finished 2 attempts, it move to next iteration so reset numSuccessfulCaptured
157
+ if (this.numSuccessfulCaptured >= 2) {
158
+ this.numSuccessfulCaptured = 0;
159
+ }
160
+
161
+ // do something before playing such as using the MLS to fill the buffers
162
+ console.warn('beforePlay');
163
+ await beforePlay();
164
+
165
+ // play calibration audio
166
+ console.warn('playCalibrationAudio');
167
+ playCalibrationAudio();
168
+
169
+ // do something before recording such as awaiting a certain amount of time
170
+ console.warn('beforeRecord');
171
+ await beforeRecord();
172
+ const totalSec =
173
+ this._calibrateSoundBurstPreSec +
174
+ (this.numMLSPerCapture - this.num_mls_to_skip) * this._calibrateSoundBurstSec +
175
+ this._calibrateSoundBurstPostSec;
176
+
177
+ // calibration loop
178
+ while (loopCondition()) {
179
+ if (this.isCalibrating) break;
180
+ // start recording
181
+ console.warn('startRecording');
182
+ await this.startRecording(stream);
183
+
184
+ if (this.isCalibrating) break;
185
+ // do something during the recording such as sleep n amount of time
186
+ console.warn('duringRecord');
187
+ await duringRecord();
188
+
189
+ if (this.isCalibrating) break;
190
+ // when done, stop recording
191
+ console.warn('stopRecording');
192
+ await this.stopRecording(mode, checkRec);
193
+
194
+ if (this.isCalibrating) break;
195
+ // do something after recording such as start processing values
196
+ console.warn('afterRecord');
197
+ await afterRecord();
198
+
199
+ // eslint-disable-next-line no-await-in-loop
200
+ await sleep(1);
201
+ }
202
+ };
203
+
204
+ /**
205
+ *
206
+ * @param {MediaStream} stream
207
+ * @param {Function} playCalibrationAudio - (async) function that plays the calibration audio
208
+ * @param {*} beforeRecord - (async) function that is called before recording
209
+ * @param {*} afterRecord - (async) function that is called after recording
210
+ * @param {Number} gainValue - the gain value to set the gain node to
211
+ */
212
+ volumeCalibrationSteps = async (
213
+ stream,
214
+ playCalibrationAudio,
215
+ beforeRecord = () => {},
216
+ afterRecord = () => {},
217
+ gainValue,
218
+ lCalib = 104.92978421490648,
219
+ checkRec,
220
+ checkSD,
221
+ maxSD,
222
+ maxRetry
223
+ ) => {
224
+ this.numCalibratingRoundsCompleted = 0;
225
+ console.log('maxSD in VolumeCaibrationSteps: ', maxSD, '0' >= maxSD);
226
+ // calibration loop
227
+ while (!this.isCalibrating && this.numCalibratingRoundsCompleted < maxRetry) {
228
+ if (this.isCalibrating) break;
229
+ // before recording
230
+ await beforeRecord(gainValue);
231
+ if (this.isCalibrating) break;
232
+ // start recording
233
+ await this.startRecording(stream);
234
+ if (this.isCalibrating) break;
235
+ // play calibration audio
236
+ console.log(`Calibration Round ${this.numCalibratingRoundsCompleted}`);
237
+ await playCalibrationAudio();
238
+ if (this.isCalibrating) break;
239
+ // when done, stop recording
240
+ console.log('Calibration Round Complete');
241
+ await this.stopRecording('volume', checkRec);
242
+ if (this.isCalibrating) break;
243
+ // after recording
244
+ await afterRecord(lCalib);
245
+ const sd = await checkSD();
246
+ let sdMessage;
247
+ if (sd <= maxSD) {
248
+ console.log(`SD =${sd}, less than calibrateSound1000HzMaxSD_dB=${maxSD}`);
249
+ this.numCalibratingRoundsCompleted += maxRetry;
250
+ sdMessage = `. SD = ${sd} dB`;
251
+ } else {
252
+ // if exist the maxSD do it one more time and only one more time
253
+ console.log(`SD =${sd}, greater than calibrateSound1000HzMaxSD_dB=${maxSD}`);
254
+ this.numCalibratingRoundsCompleted += 1;
255
+ sdMessage = `. SD = ${sd} > ${this.calibrateSound1000HzMaxSD_dB} dB.`;
256
+ }
257
+ this.addTimeStamp(
258
+ `${this.calibrateSound1000HzPreSec.toFixed(1)}` +
259
+ `+${this.calibrateSound1000HzSec.toFixed(1)}` +
260
+ `+${this.calibrateSound1000HzPostSec.toFixed(1)} s. ` +
261
+ `1000 Hz at ${this.inDB} dB${sdMessage}`
262
+ );
263
+ this.calibrationNodes = [];
264
+
265
+ // eslint-disable-next-line no-await-in-loop
266
+ await sleep(2);
267
+ }
268
+ };
269
+
270
+ /**
271
+ * Simulates recording by convolving input signal with loudspeaker and microphone impulse responses
272
+ *
273
+ * @param {Array<number>} inputSignal - The input signal to be convolved
274
+ * @param {Array<number>} loudspeakerImpulseResponse - The loudspeaker impulse response
275
+ * @param {Array<number>} microphoneImpulseResponse - The microphone impulse response
276
+ * @param {Function} afterRecord - Callback function to execute after recording simulation
277
+ * @param {number} lCalib - Calibration parameter
278
+ * @param {string} checkRec - Recording check type
279
+ */
280
+ simulatedVolumeCalibrationSteps = async (
281
+ inputSignal,
282
+ loudspeakerImpulseResponse,
283
+ microphoneImpulseResponse,
284
+ afterRecord = () => {},
285
+ lCalib,
286
+ checkSD,
287
+ maxSD,
288
+ maxRetry
289
+ ) => {
290
+ const totalSec = this.CALIBRATION_TONE_DURATION;
291
+ // Convolve with loudspeaker and microphone impulse responses
292
+ const convolvedSignalWithMicrophone = await this.pyServerAPI
293
+ .irConvolution({
294
+ input_signal: inputSignal,
295
+ loudspeaker_ir: loudspeakerImpulseResponse,
296
+ microphone_ir: microphoneImpulseResponse,
297
+ duration: totalSec,
298
+ sample_rate: this.sourceSamplingRate / this._calibrateSoundBurstDownsample,
299
+ })
300
+ .then(res => {
301
+ console.log('res in simulatedVolumeCalibrationSteps: ', res);
302
+ return res['output_signal'];
303
+ });
304
+
305
+ // Log details about the simulated recording
306
+ const uniqueSet = new Set(convolvedSignalWithMicrophone);
307
+ const numberOfUniqueValues = uniqueSet.size;
308
+ const squaredValues = convolvedSignalWithMicrophone.map(value => value * value);
309
+ const sum_of_squares = squaredValues.reduce((total, value) => total + value, 0);
310
+ const squared_mean = sum_of_squares / convolvedSignalWithMicrophone.length;
311
+ const dbLevel = 20 * Math.log10(Math.sqrt(squared_mean));
312
+ const roundedDbLevel = Math.round(dbLevel * 10) / 10;
313
+
314
+ console.log(
315
+ '[SIMULATION] 1000-Hz recording: ' +
316
+ roundedDbLevel +
317
+ ' dB with ' +
318
+ numberOfUniqueValues +
319
+ ' unique values.'
320
+ );
321
+
322
+ // Save the simulated recording as if it were captured from a microphone
323
+ this.saveVolumeRecording(convolvedSignalWithMicrophone);
324
+
325
+ // Process the simulated recording
326
+ await afterRecord(lCalib);
327
+ const sd = await checkSD();
328
+ let sdMessage;
329
+ if (sd <= maxSD) {
330
+ console.log(`SD =${sd}, less than calibrateSound1000HzMaxSD_dB=${maxSD}`);
331
+ this.numCalibratingRoundsCompleted += maxRetry;
332
+ sdMessage = `. SD = ${sd} dB`;
333
+ } else {
334
+ // if exist the maxSD do it one more time and only one more time
335
+ console.log(`SD =${sd}, greater than calibrateSound1000HzMaxSD_dB=${maxSD}`);
336
+ this.numCalibratingRoundsCompleted += 1;
337
+ sdMessage = `. SD = ${sd} > ${this.calibrateSound1000HzMaxSD_dB} dB.`;
338
+ }
339
+ this.addTimeStamp(
340
+ `${this.calibrateSound1000HzPreSec.toFixed(1)}` +
341
+ `+${this.calibrateSound1000HzSec.toFixed(1)}` +
342
+ `+${this.calibrateSound1000HzPostSec.toFixed(1)} s. ` +
343
+ `1000 Hz at ${this.inDB} dB${sdMessage}`
344
+ );
345
+ };
346
+
347
+ /**
348
+ * Getter for the isCalibrating property.
349
+ *
350
+ * @public
351
+ * @returns - True if the audio is being calibrated, false otherwise.
352
+ * @example
353
+ */
354
+ getCalibrationStatus = () => this.isCalibrating;
355
+
356
+ /** .
357
+ * .
358
+ * .
359
+ * Set the sampling rate to the value received from the listener
360
+ *
361
+ * @param {*} sinkSamplingRate
362
+ * @param samplingRate
363
+ * @example
364
+ */
365
+ setSamplingRates = samplingRate => {
366
+ this.sinkSamplingRate = samplingRate;
367
+ this.sourceSamplingRate = samplingRate;
368
+
369
+ // this.emit('update', {message: `sampling at ${samplingRate}Hz...`});
370
+ };
371
+
372
+ setSampleSize = sampleSize => {
373
+ this.sampleSize = sampleSize;
374
+ };
375
+
376
+ setFlags = flags => {
377
+ this.flags = flags;
378
+ };
379
+
380
+ sampleRatesSet = () => this.sourceSamplingRate && this.sinkSamplingRate;
381
+
382
+ addCalibrationNode = node => {
383
+ this.calibrationNodes.push(node);
384
+ };
385
+
386
+ addCalibrationNodeConvolved = node => {
387
+ this.calibrationNodesConvolved.push(node);
388
+ };
389
+
390
+ makeNewSourceAudioContext = () => {
391
+ const options = {
392
+ sampleRate: this.sourceSamplingRate,
393
+ };
394
+
395
+ this.sourceAudioContext = new (window.AudioContext ||
396
+ window.webkitAudioContext ||
397
+ window.audioContext)(options);
398
+
399
+ return this.sourceAudioContext;
400
+ };
401
+
402
+ makeNewSourceAudioContextConvolved = () => {
403
+ const options = {
404
+ sampleRate: this.sourceSamplingRate,
405
+ };
406
+
407
+ this.sourceAudioContextConvolved = new (window.AudioContext ||
408
+ window.webkitAudioContext ||
409
+ window.audioContext)(options);
410
+
411
+ return this.sourceAudioContextConvolved;
412
+ };
413
+
414
+ /** .
415
+ * .
416
+ * .
417
+ * Download the result of the calibration roudns
418
+ *
419
+ * @example
420
+ */
421
+ downloadData = () => {
422
+ const recordings = this.getAllRecordedSignals();
423
+ const i = recordings.length - 1;
424
+ saveToCSV(recordings[i], `recordedMLSignal_${i}_unconvolved.csv`);
425
+ };
426
+ downloadSingleUnfilteredRecording = () => {
427
+ const recordings = this.getAllUnfilteredRecordedSignals();
428
+ saveToCSV(recordings[recordings.length - 1], `recordedMLSignal_unconvolved.csv`);
429
+ };
430
+ downloadSingleFilteredRecording = () => {
431
+ const recordings = this.getAllFilteredRecordedSignals();
432
+ console.log('Single filtered recording should be of length: ' + recordings[0].length);
433
+ saveToCSV(recordings[0], `recordedMLSignal_convolved.csv`);
434
+ };
435
+ downloadUnfilteredRecordings = () => {
436
+ const recordings = this.getAllRecordedSignals();
437
+ console.log('unfilterd download?');
438
+ for (let i = 0; i < recordings.length; i++) {
439
+ console.log(i);
440
+ saveToCSV(recordings[i], `recordedMLSignal_${i}_unconvolved.csv`);
441
+ }
442
+ };
443
+ downloadFilteredRecordings = () => {
444
+ const recordings = this.getAllFilteredRecordedSignals();
445
+ for (let i = 0; i < recordings.length; i++) {
446
+ saveToCSV(recordings[i], `recordedMLSignal_${i}_convolved.csv`);
447
+ }
448
+ };
449
+ }
450
+
451
+ export default AudioCalibrator;