speaker-calibration 2.2.66 → 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 -471
  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 -2036
  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,2036 +1,2092 @@
1
- import AudioCalibrator from '../audioCalibrator';
2
-
3
- import {sleep, csvToArray, saveToCSV, saveToJSON, findMinValue, findMaxValue} from '../../utils';
4
- import database from '../../config/firebase';
5
- import {ref, set, get, child} from 'firebase/database';
6
-
7
- /**
8
- *
9
- */
10
- class Combination extends AudioCalibrator {
11
- /**
12
- * Default constructor. Creates an instance with any number of paramters passed or the default parameters defined here.
13
- *
14
- * @param {Object<boolean, number, number, number>} calibratorParams - paramter object
15
- * @param {boolean} [calibratorParams.download = false] - boolean flag to download captures
16
- * @param {number} [calibratorParams.mlsOrder = 18] - order of the MLS to be generated
17
- * @param {number} [calibratorParams.numCaptures = 5] - number of captures to perform
18
- * @param {number} [calibratorParams.numMLSPerCapture = 2] - number of bursts of MLS per capture
19
- */
20
- constructor({
21
- download = false,
22
- mlsOrder = 18,
23
- numCaptures = 3,
24
- numMLSPerCapture = 2,
25
- lowHz = 20,
26
- highHz = 10000,
27
- }) {
28
- super(numCaptures, numMLSPerCapture);
29
- this.#mlsOrder = parseInt(mlsOrder, 10);
30
- this.#P = 2 ** mlsOrder - 1;
31
- this.#download = download;
32
- this.#mls = [];
33
- this.#lowHz = lowHz;
34
- this.#highHz = highHz;
35
- }
36
-
37
- /** @private */
38
- stepNum = 0;
39
-
40
- /** @private */
41
- totalSteps = 25;
42
-
43
- /** @private */
44
- #download;
45
-
46
- /** @private */
47
- #mlsGenInterface;
48
-
49
- /** @private */
50
- #mlsBufferView;
51
-
52
- /** @private */
53
- componentInvertedImpulseResponse = null;
54
-
55
- /** @private */
56
- systemInvertedImpulseResponse = null;
57
-
58
- //averaged and subtracted ir returned from calibration used to calculated iir
59
- /** @private */
60
- ir = null;
61
-
62
- /** @private */
63
- impulseResponses = [];
64
-
65
- /** @private */
66
- #mlsOrder;
67
-
68
- /** @private */
69
- #lowHz;
70
-
71
- /** @private */
72
- #highHz;
73
-
74
- /** @private */
75
- #mls;
76
-
77
- /** @private */
78
- #P;
79
-
80
- /** @private */
81
- #audioContext;
82
-
83
- /** @private */
84
- TAPER_SECS = 5;
85
-
86
- /** @private */
87
- offsetGainNode;
88
-
89
- /** @private */
90
- componentConvolution;
91
-
92
- /** @private */
93
- testConvolution;
94
-
95
- /** @private */
96
- systemConvolution;
97
-
98
- ////////////////////////volume
99
- /** @private */
100
- #CALIBRATION_TONE_FREQUENCY = 1000; // Hz
101
-
102
- /** @private */
103
- #CALIBRATION_TONE_TYPE = 'sine';
104
-
105
- CALIBRATION_TONE_DURATION = 5; // seconds
106
- calibrateSound1000HzPreSec = 3.5;
107
- calibrateSound1000HzSec = 1.0;
108
- calibrateSound1000HzPostSec = 0.5;
109
-
110
- /** @private */
111
- outDBSPL = null;
112
- THD = null;
113
- outDBSPL1000 = null;
114
-
115
- /** @private */
116
- TAPER_SECS = 0.01; // seconds
117
-
118
- /** @private */
119
- status_denominator = 8;
120
-
121
- /** @private */
122
- status_numerator = 0;
123
-
124
- /** @private */
125
- percent_complete = 0;
126
-
127
- /** @private */
128
- status = ``;
129
-
130
- /**@private */
131
- status_literal = `<div style="display: flex; justify-content: center;"><div style="width: 200px; height: 20px; border: 2px solid #000; border-radius: 10px;"><div style="width: ${this.percent_complete}%; height: 100%; background-color: #00aaff; border-radius: 8px;"></div></div></div>`;
132
-
133
- /**@private */
134
- componentIR = null;
135
-
136
- /**@private */
137
- oldComponentIR = null;
138
-
139
- /**@private */
140
- systemIR = null;
141
-
142
- /**@private */
143
- _calibrateSoundCheck = '';
144
-
145
- deviceType = null;
146
-
147
- deviceName = null;
148
-
149
- deviceInfo = null;
150
-
151
- desired_time_per_mls = 0;
152
-
153
- num_mls_to_skip = 0;
154
-
155
- desired_sampling_rate = 0;
156
-
157
- #currentConvolution = [];
158
-
159
- mode = 'unfiltered';
160
-
161
- sourceNode;
162
-
163
- autocorrelations = [];
164
-
165
- iirLength = 0;
166
-
167
- componentInvertedImpulseResponseNoBandpass = [];
168
-
169
- systemInvertedImpulseResponseNoBandpass = [];
170
-
171
- _calibrateSoundBackgroundSecs;
172
-
173
- background_noise = {};
174
-
175
- numSuccessfulBackgroundCaptured;
176
-
177
- _calibrateSoundBurstDb;
178
-
179
- filteredMLSRange = {
180
- component: {
181
- Min: null,
182
- Max: null,
183
- },
184
- system: {
185
- Min: null,
186
- Max: null,
187
- },
188
- };
189
-
190
- /**generate string template that gets reevaluated as variable increases */
191
- generateTemplate = () => {
192
- if (this.percent_complete > 100) {
193
- this.percent_complete = 100;
194
- }
195
- const template = `<div style="display: flex; justify-content: center; margin-top:12px;"><div style="width: 800px; height: 20px; border: 2px solid #000; border-radius: 10px;"><div style="width: ${this.percent_complete}%; height: 100%; background-color: #00aaff; border-radius: 8px;"></div></div></div>`;
196
- return template;
197
- };
198
-
199
- /** increment numerator and percent for status bar */
200
- incrementStatusBar = () => {
201
- this.status_numerator += 1;
202
- this.percent_complete = (this.status_numerator / this.status_denominator) * 100;
203
- };
204
-
205
- setDeviceType = deviceType => {
206
- this.deviceType = deviceType;
207
- };
208
-
209
- setDeviceName = deviceName => {
210
- this.deviceName = deviceName;
211
- };
212
-
213
- setDeviceInfo = deviceInfo => {
214
- this.deviceInfo = deviceInfo;
215
- };
216
-
217
- /** .
218
- * .
219
- * .
220
- * Sends all the computed impulse responses to the backend server for processing
221
- *
222
- * @returns sets the resulting inverted impulse response to the class property
223
- * @example
224
- */
225
- sendSystemImpulseResponsesToServerForProcessing = async () => {
226
- const computedIRs = await Promise.all(this.impulseResponses);
227
- const filteredComputedIRs = computedIRs.filter(element => {
228
- return element != undefined;
229
- }); //log any errors that are found in this step
230
- const mls = this.#mls;
231
- const lowHz = this.#lowHz; //gain of 1 below cutoff, need gain of 0
232
- const highHz = this.#highHz; //check error for anything other than 10 kHz
233
- const iirLength = this.iirLength;
234
- const num_periods = this.numMLSPerCapture + this.num_mls_to_skip;
235
- this.stepNum += 1;
236
- console.log('send impulse responses to server: ' + this.stepNum);
237
- this.status =
238
- `All Hz Calibration: computing the IIR...`.toString() + this.generateTemplate().toString();
239
- this.emit('update', {message: this.status});
240
- return this.pyServerAPI
241
- .getSystemInverseImpulseResponseWithRetry({
242
- payload: filteredComputedIRs.slice(0, this.numCaptures),
243
- mls,
244
- lowHz,
245
- highHz,
246
- iirLength,
247
- num_periods,
248
- sampleRate: this.sourceSamplingRate || 96000,
249
- calibrateSoundBurstDb: this._calibrateSoundBurstDb,
250
- })
251
- .then(res => {
252
- console.log(res);
253
- this.stepNum += 1;
254
- console.log('got impulse response ' + this.stepNum);
255
- this.incrementStatusBar();
256
- this.status =
257
- `All Hz Calibration: done computing the IIR...`.toString() +
258
- this.generateTemplate().toString();
259
- this.emit('update', {message: this.status});
260
- this.systemInvertedImpulseResponse = res['iir'];
261
- this.systemIR = res['ir'];
262
- this.systemConvolution = res['convolution'];
263
- this.systemInvertedImpulseResponseNoBandpass = res['iirNoBandpass'];
264
- })
265
- .catch(err => {
266
- console.error(err);
267
- });
268
- };
269
-
270
- /** .
271
- * .
272
- * .
273
- * Sends all the computed impulse responses to the backend server for processing
274
- *
275
- * @returns sets the resulting inverted impulse response to the class property
276
- * @example
277
- */
278
- sendComponentImpulseResponsesToServerForProcessing = async () => {
279
- const computedIRs = await Promise.all(this.impulseResponses);
280
- const filteredComputedIRs = computedIRs.filter(element => {
281
- return element != undefined;
282
- });
283
- const componentIRGains = this.componentIR['Gain'];
284
- const componentIRFreqs = this.componentIR['Freq'];
285
- const mls = this.#mls;
286
- const lowHz = this.#lowHz;
287
- const iirLength = this.iirLength;
288
- const num_periods = this.numMLSPerCapture + this.num_mls_to_skip;
289
- const highHz = this.#highHz;
290
- this.stepNum += 1;
291
- console.log('send impulse responses to server: ' + this.stepNum);
292
- this.status =
293
- `All Hz Calibration: computing the IIR...`.toString() + this.generateTemplate().toString();
294
- this.emit('update', {message: this.status});
295
- return this.pyServerAPI
296
- .getComponentInverseImpulseResponseWithRetry({
297
- payload: filteredComputedIRs.slice(0, this.numCaptures),
298
- mls,
299
- lowHz,
300
- highHz,
301
- iirLength,
302
- componentIRGains,
303
- componentIRFreqs,
304
- num_periods,
305
- sampleRate: this.sourceSamplingRate || 96000,
306
- calibrateSoundBurstDb: this._calibrateSoundBurstDb,
307
- })
308
- .then(res => {
309
- console.log(res);
310
- this.stepNum += 1;
311
- console.log('got impulse response ' + this.stepNum);
312
- this.incrementStatusBar();
313
- this.status =
314
- `All Hz Calibration: done computing the IIR...`.toString() +
315
- this.generateTemplate().toString();
316
- this.emit('update', {message: this.status});
317
- this.componentInvertedImpulseResponse = res['iir'];
318
- this.componentIR['Gain'] = res['ir'];
319
- this.componentIR['Freq'] = res['frequencies'];
320
- this.componentConvolution = res['convolution'];
321
- this.testConvolution = res['convolutionTest'];
322
- this.componentInvertedImpulseResponseNoBandpass = res['iirNoBandpass'];
323
- })
324
- .catch(err => {
325
- // this.emit('InvertedImpulseResponse', {res: false});
326
- console.error(err);
327
- });
328
- };
329
-
330
- sendBackgroundRecording = () => {
331
- const allSignals = this.getAllBackgroundRecordings();
332
- const numSignals = allSignals.length;
333
- const background_rec_whole = allSignals[numSignals - 1];
334
- const fraction = 0.5 / (this._calibrateSoundBackgroundSecs + 0.5);
335
- // Calculate the starting index for slicing the array
336
- const startIndex = Math.round(fraction * background_rec_whole.length);
337
- // Slice the array from the calculated start index to the end of the array
338
- const background_rec = background_rec_whole.slice(startIndex);
339
- console.log('Sending background recording to server for processing');
340
- this.pyServerAPI
341
- .getBackgroundNoisePSDWithRetry({
342
- background_rec,
343
- sampleRate: this.sourceSamplingRate || 96000,
344
- })
345
- .then(res => {
346
- if (this.numSuccessfulBackgroundCaptured < 1) {
347
- this.numSuccessfulBackgroundCaptured += 1;
348
- //storing all background data in background_psd object
349
- this.background_noise['x_background'] = res['x_background'];
350
- this.background_noise['y_background'] = res['y_background'];
351
- this.background_noise['recording'] = background_rec;
352
- }
353
- })
354
- .catch(err => {
355
- console.error(err);
356
- });
357
- };
358
-
359
- /** .
360
- * .
361
- * .
362
- * Sends the recorded signal, or a given csv string of a signal, to the back end server for processing
363
- *
364
- * @param {<array>String} signalCsv - Optional csv string of a previously recorded signal, if given, this signal will be processed
365
- * @example
366
- */
367
- sendRecordingToServerForProcessing = signalCsv => {
368
- const allSignals = this.getAllUnfilteredRecordedSignals();
369
- console.log(
370
- 'Obtaining last all hz unfiltered recording from #allHzUnfilteredRecordings to send to server for processing'
371
- );
372
- const numSignals = allSignals.length;
373
- const mls = this.#mlsBufferView;
374
- const payload =
375
- signalCsv && signalCsv.length > 0 ? csvToArray(signalCsv) : allSignals[numSignals - 1];
376
- console.log('sending rec');
377
- this.stepNum += 1;
378
- console.log('send rec ' + this.stepNum);
379
- this.status =
380
- `All Hz Calibration Step: computing the IR of the last recording...`.toString() +
381
- this.generateTemplate().toString();
382
- this.emit('update', {message: this.status});
383
- this.impulseResponses.push(
384
- this.pyServerAPI
385
- .getImpulseResponse({
386
- sampleRate: this.sourceSamplingRate || 96000,
387
- payload,
388
- mls,
389
- P: this.#P, //get rid of this
390
- numPeriods: this.numMLSPerCapture,
391
- })
392
- .then(res => {
393
- if (this.numSuccessfulCaptured < this.numCaptures) {
394
- this.numSuccessfulCaptured += 1;
395
- console.log('num succ capt: ' + this.numSuccessfulCaptured);
396
- this.stepNum += 1;
397
- console.log('got impulse response ' + this.stepNum);
398
- this.incrementStatusBar();
399
- this.status =
400
- `All Hz Calibration: ${this.numSuccessfulCaptured}/${this.numCaptures} IRs computed...`.toString() +
401
- this.generateTemplate().toString();
402
- this.emit('update', {
403
- message: this.status,
404
- });
405
- this.autocorrelations.push(res['autocorrelation']);
406
- return res['ir'];
407
- }
408
- })
409
- .catch(err => {
410
- console.error(err);
411
- })
412
- );
413
- };
414
-
415
- /**
416
- * Passed to the calibration steps function, awaits the desired amount of seconds to capture the desired number
417
- * of MLS periods defined in the constructor.
418
- *
419
- * @example
420
- */
421
- #awaitDesiredMLSLength = async () => {
422
- // seconds per MLS = P / SR
423
- // await N * P / SR
424
- this.stepNum += 1;
425
- console.log('await desired length ' + this.stepNum);
426
- this.status =
427
- `All Hz Calibration: sampling the calibration signal...`.toString() +
428
- `\niteration ${this.stepNum}` +
429
- this.generateTemplate();
430
- this.emit('update', {
431
- message: this.status,
432
- });
433
- let time_to_wait = 0;
434
- if (this.mode === 'unfiltered') {
435
- //unfiltered
436
- time_to_wait = (this.#mls.length / this.sourceSamplingRate) * this.numMLSPerCapture;
437
- time_to_wait = time_to_wait * 1.1;
438
- } else if (this.mode === 'filtered') {
439
- //filtered
440
- // time_to_wait =
441
- // (this.#currentConvolution.length / this.sourceSamplingRate) *
442
- // (this.numMLSPerCapture / (this.num_mls_to_skip + this.numMLSPerCapture));
443
- time_to_wait =
444
- (this.#currentConvolution.length / this.sourceSamplingRate) * this.numMLSPerCapture;
445
- time_to_wait = time_to_wait * 1.1;
446
- } else {
447
- throw new Error('Mode broke in awaitDesiredMLSLength');
448
- }
449
-
450
- await sleep(time_to_wait);
451
- };
452
-
453
- /**
454
- * Passed to the background noise recording function, awaits the desired amount of seconds to capture the desired number
455
- * of seconds of background noise
456
- *
457
- * @example
458
- */
459
- #awaitBackgroundNoiseRecording = async () => {
460
- console.log(
461
- 'Waiting ' + this._calibrateSoundBackgroundSecs + ' second(s) to record background noise'
462
- );
463
- let time_to_wait = this._calibrateSoundBackgroundSecs + 0.5;
464
- await sleep(time_to_wait);
465
- };
466
-
467
- /** .
468
- * .
469
- * .
470
- * Passed to the calibration steps function, awaits the onset of the signal to ensure a steady state
471
- *
472
- * @example
473
- */
474
- #awaitSignalOnset = async () => {
475
- this.stepNum += 1;
476
- console.log('await signal onset ' + this.stepNum);
477
- this.status =
478
- `All Hz Calibration: waiting for the signal to stabilize...`.toString() +
479
- this.generateTemplate();
480
- this.emit('update', {
481
- message: this.status,
482
- });
483
- let number_of_bursts_to_skip = this.num_mls_to_skip;
484
- let time_to_sleep = 0;
485
- if (this.mode === 'unfiltered') {
486
- time_to_sleep = (this.#mls.length / this.sourceSamplingRate) * number_of_bursts_to_skip;
487
- } else if (this.mode === 'filtered') {
488
- console.log(this.#currentConvolution.length);
489
- // time_to_sleep =
490
- // (this.#currentConvolution.length / this.sourceSamplingRate) *
491
- // (number_of_bursts_to_skip / (number_of_bursts_to_skip + this.numMLSPerCapture));
492
- time_to_sleep =
493
- (this.#currentConvolution.length / this.sourceSamplingRate) * number_of_bursts_to_skip;
494
- } else {
495
- throw new Error('Mode broke in awaitSignalOnset');
496
- }
497
- await sleep(time_to_sleep);
498
- };
499
-
500
- /**
501
- * Called immediately after a recording is captured. Used to process the resulting signal
502
- * whether by sending the result to a server or by computing a result locally.
503
- *
504
- * @example
505
- */
506
- #afterMLSRecord = () => {
507
- console.log('after record');
508
- this.sendRecordingToServerForProcessing();
509
- };
510
-
511
- #afterMLSwIIRRecord = () => {
512
- if (this.numSuccessfulCaptured < 1) {
513
- this.numSuccessfulCaptured += 1;
514
- this.stepNum += 1;
515
- this.incrementStatusBar();
516
- console.log('after mls w iir record for some reason add numSucc capt ' + this.stepNum);
517
- this.status =
518
- `All Hz Calibration: ${this.numSuccessfulCaptured} recording of convolved MLS captured`.toString() +
519
- this.generateTemplate().toString();
520
- this.emit('update', {
521
- message: this.status,
522
- });
523
- }
524
- };
525
-
526
- /** .
527
- * .
528
- * .
529
- * Created an S Curver Buffer to taper the signal onset
530
- *
531
- * @param {*} length
532
- * @param {*} phase
533
- * @returns
534
- * @example
535
- */
536
- static createSCurveBuffer = (length, phase) => {
537
- const curve = new Float32Array(length);
538
- let i;
539
- for (i = 0; i < length; i += 1) {
540
- // scale the curve to be between 0-1
541
- curve[i] = Math.sin((Math.PI * i) / length - phase) / 2 + 0.5;
542
- }
543
- return curve;
544
- };
545
-
546
- static createInverseSCurveBuffer = (length, phase) => {
547
- const curve = new Float32Array(length);
548
- let i;
549
- let j = length - 1;
550
- for (i = 0; i < length; i += 1) {
551
- // scale the curve to be between 0-1
552
- curve[i] = Math.sin((Math.PI * j) / length - phase) / 2 + 0.5;
553
- j -= 1;
554
- }
555
- return curve;
556
- };
557
-
558
- /**
559
- * Construct a Calibration Node with the calibration parameters.
560
- *
561
- * @param dataBuffer
562
- * @private
563
- * @example
564
- */
565
- #createCalibrationNodeFromBuffer = dataBuffer => {
566
- console.log('length databuffer');
567
- console.log(dataBuffer.length);
568
- if (!this.sourceAudioContext) {
569
- this.makeNewSourceAudioContext();
570
- }
571
-
572
- const buffer = this.sourceAudioContext.createBuffer(
573
- 1, // number of channels
574
- dataBuffer.length,
575
- this.sourceAudioContext.sampleRate // sample rate
576
- );
577
-
578
- const data = buffer.getChannelData(0); // get data
579
-
580
- // fill the buffer with our data
581
- try {
582
- for (let i = 0; i < dataBuffer.length; i += 1) {
583
- data[i] = dataBuffer[i];
584
- }
585
- } catch (error) {
586
- console.error(error);
587
- }
588
-
589
- this.sourceNode = this.sourceAudioContext.createBufferSource();
590
-
591
- this.sourceNode.buffer = buffer;
592
-
593
- if (this.mode === 'filtered') {
594
- //used to not loop filtered
595
- this.sourceNode.loop = true;
596
- } else {
597
- this.sourceNode.loop = true;
598
- }
599
-
600
- this.sourceNode.connect(this.sourceAudioContext.destination);
601
-
602
- this.addCalibrationNode(this.sourceNode);
603
- };
604
-
605
- /**
606
- * Given a data buffer, creates the required calibration node
607
- *
608
- * @param {*} dataBufferArray
609
- * @example
610
- */
611
- #setCalibrationNodesFromBuffer = (dataBufferArray = [this.#mlsBufferView]) => {
612
- if (dataBufferArray.length === 1) {
613
- this.#createCalibrationNodeFromBuffer(dataBufferArray[0]);
614
- } else {
615
- throw new Error('The length of the data buffer array must be 1');
616
- }
617
- };
618
-
619
- /**
620
- * Creates an audio context and plays it for a few seconds.
621
- *
622
- * @private
623
- * @returns - Resolves when the audio is done playing.
624
- * @example
625
- */
626
- #playCalibrationAudio = () => {
627
- this.calibrationNodes[0].start(0);
628
- this.status = ``;
629
- if (this.mode === 'unfiltered') {
630
- console.log('mls', this.#mls); // before multiplied by calibrateSoundBurstDb
631
- console.log('mls buffer view', this.#mlsBufferView); // after multiplied by calibrateSoundBurstDb
632
- console.log('play calibration audio ' + this.stepNum);
633
- this.status =
634
- `All Hz Calibration: playing the calibration tone...`.toString() +
635
- this.generateTemplate().toString();
636
- } else if (this.mode === 'filtered') {
637
- console.log('play convolved audio ' + this.stepNum);
638
- this.status =
639
- `All Hz Calibration: playing the convolved calibration tone...`.toString() +
640
- this.generateTemplate().toString();
641
- } else {
642
- throw new Error('Mode is incorrect');
643
- }
644
- this.emit('update', {message: this.status});
645
- this.stepNum += 1;
646
- console.log('sink sampling rate');
647
- console.log(this.sinkSamplingRate);
648
- console.log('source sampling rate');
649
- console.log(this.sourceSamplingRate);
650
- console.log('sample size');
651
- console.log(this.sampleSize);
652
- };
653
-
654
- /** .
655
- * .
656
- * .
657
- * Stops the audio with tapered offset
658
- *
659
- * @example
660
- */
661
- #stopCalibrationAudio = () => {
662
- this.calibrationNodes[0].stop(0);
663
- this.calibrationNodes = [];
664
- this.sourceNode.disconnect();
665
- this.stepNum += 1;
666
- console.log('stop calibration audio ' + this.stepNum);
667
- this.status =
668
- `All Hz Calibration: stopping the calibration tone...`.toString() +
669
- this.generateTemplate().toString();
670
- this.emit('update', {message: this.status});
671
- };
672
-
673
- playMLSwithIIR = async (stream, convolution) => {
674
- let checkRec = false;
675
- this.mode = 'filtered';
676
- console.log('play mls with iir');
677
- //this.invertedImpulseResponse = iir
678
-
679
- await this.calibrationSteps(
680
- stream,
681
- this.#playCalibrationAudio, // play audio func (required)
682
- this.#createCalibrationNodeFromBuffer(convolution), // before play func
683
- this.#awaitSignalOnset, // before record
684
- () => this.numSuccessfulCaptured < 1,
685
- this.#awaitDesiredMLSLength, // during record
686
- this.#afterMLSwIIRRecord, // after record
687
- this.mode,
688
- checkRec
689
- );
690
- };
691
-
692
- bothSoundCheck = async stream => {
693
- let iir_ir_and_plots;
694
- this.#currentConvolution = this.componentConvolution;
695
- this.filteredMLSRange.component.Min = findMinValue(this.#currentConvolution);
696
- this.filteredMLSRange.component.Max = findMaxValue(this.#currentConvolution);
697
- await this.playMLSwithIIR(stream, this.#currentConvolution);
698
- this.#stopCalibrationAudio();
699
- let component_conv_recs = this.getAllFilteredRecordedSignals();
700
- let return_component_conv_rec = component_conv_recs[0];
701
- this.clearAllFilteredRecordedSignals();
702
-
703
- this.#currentConvolution = this.testConvolution;
704
- await this.playMLSwithIIR(stream, this.#currentConvolution);
705
- this.#stopCalibrationAudio();
706
- let test_conv_recs = this.getAllFilteredRecordedSignals();
707
- let return_test_conv_rec = test_conv_recs[0];
708
- this.clearAllFilteredRecordedSignals();
709
-
710
- this.#currentConvolution = this.systemConvolution;
711
- this.filteredMLSRange.system.Min = findMinValue(this.#currentConvolution);
712
- this.filteredMLSRange.system.Max = findMaxValue(this.#currentConvolution);
713
- await this.playMLSwithIIR(stream, this.#currentConvolution);
714
- this.#stopCalibrationAudio();
715
- let system_conv_recs = this.getAllFilteredRecordedSignals();
716
- let return_system_conv_rec = system_conv_recs[0];
717
-
718
- this.clearAllFilteredRecordedSignals();
719
-
720
- this.sourceAudioContext.close();
721
- let recs = this.getAllUnfilteredRecordedSignals();
722
- let unconv_rec = recs[0];
723
- let return_unconv_rec = unconv_rec;
724
- let conv_rec = component_conv_recs[0];
725
-
726
- //psd of component
727
- let knownGain = this.oldComponentIR.Gain;
728
- let knownFreq = this.oldComponentIR.Freq;
729
- let sampleRate = this.sourceSamplingRate || 96000;
730
- let component_unconv_rec_psd = await this.pyServerAPI
731
- .getSubtractedPSDWithRetry(unconv_rec, knownGain, knownFreq, sampleRate)
732
- .then(res => {
733
- this.incrementStatusBar();
734
- this.status =
735
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
736
- this.generateTemplate().toString();
737
- this.emit('update', {message: this.status});
738
- return res;
739
- })
740
- .catch(err => {
741
- console.error(err);
742
- });
743
-
744
- let component_conv_rec_psd = await this.pyServerAPI
745
- .getSubtractedPSDWithRetry(conv_rec, knownGain, knownFreq, sampleRate)
746
- .then(res => {
747
- this.incrementStatusBar();
748
- this.status =
749
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
750
- this.generateTemplate().toString();
751
- this.emit('update', {message: this.status});
752
- return res;
753
- })
754
- .catch(err => {
755
- console.error(err);
756
- });
757
-
758
- let test_conv_rec_psd = await this.pyServerAPI
759
- .getSubtractedPSDWithRetry(return_test_conv_rec, knownGain, knownFreq, sampleRate)
760
- .then(res => {
761
- this.incrementStatusBar();
762
- this.status =
763
- `All Hz Calibration: done computing testing PSD graphs...`.toString() +
764
- this.generateTemplate().toString();
765
- this.emit('update', {message: this.status});
766
- return res;
767
- })
768
- .catch(err => {
769
- console.error(err);
770
- });
771
-
772
- conv_rec = system_conv_recs[0];
773
- //psd of system
774
- let system_recs_psd = await this.pyServerAPI
775
- .getPSDWithRetry({
776
- unconv_rec,
777
- conv_rec,
778
- sampleRate: this.sourceSamplingRate || 96000,
779
- })
780
- .then(res => {
781
- this.incrementStatusBar();
782
- this.status =
783
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
784
- this.generateTemplate().toString();
785
- this.emit('update', {message: this.status});
786
- return res;
787
- })
788
- .catch(err => {
789
- console.error(err);
790
- });
791
-
792
- //iir w/ and without bandpass psd. done
793
- unconv_rec = this.componentInvertedImpulseResponseNoBandpass;
794
- conv_rec = this.componentInvertedImpulseResponse;
795
- let component_iir_psd = await this.pyServerAPI
796
- .getPSDWithRetry({
797
- unconv_rec,
798
- conv_rec,
799
- sampleRate: this.sourceSamplingRate || 96000,
800
- })
801
- .then(res => {
802
- this.incrementStatusBar();
803
- this.status =
804
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
805
- this.generateTemplate().toString();
806
- this.emit('update', {message: this.status});
807
- return res;
808
- })
809
- .catch(err => {
810
- console.error(err);
811
- });
812
- unconv_rec = this.systemInvertedImpulseResponseNoBandpass;
813
- conv_rec = this.systemInvertedImpulseResponse;
814
- let system_iir_psd = await this.pyServerAPI
815
- .getPSDWithRetry({
816
- unconv_rec,
817
- conv_rec,
818
- sampleRate: this.sourceSamplingRate || 96000,
819
- })
820
- .then(res => {
821
- this.incrementStatusBar();
822
- this.status =
823
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
824
- this.generateTemplate().toString();
825
- this.emit('update', {message: this.status});
826
- return res;
827
- })
828
- .catch(err => {
829
- console.error(err);
830
- });
831
-
832
- let mls_psd = await this.pyServerAPI
833
- .getMLSPSDWithRetry({mls: this.#mlsBufferView, sampleRate: this.sourceSamplingRate || 96000})
834
- .then(res => {
835
- this.incrementStatusBar();
836
- this.status =
837
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
838
- this.generateTemplate().toString();
839
- this.emit('update', {message: this.status});
840
- return res;
841
- })
842
- .catch(err => {
843
- console.error(err);
844
- });
845
-
846
- let system_filtered_mls_psd = await this.pyServerAPI
847
- .getMLSPSDWithRetry({
848
- mls: this.systemConvolution,
849
- sampleRate: this.sourceSamplingRate || 96000,
850
- })
851
- .then(res => {
852
- this.incrementStatusBar();
853
- this.status =
854
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
855
- this.generateTemplate().toString();
856
- this.emit('update', {message: this.status});
857
- return res;
858
- })
859
- .catch(err => {
860
- console.error(err);
861
- });
862
-
863
- let component_filtered_mls_psd = await this.pyServerAPI
864
- .getMLSPSDWithRetry({
865
- mls: this.componentConvolution,
866
- sampleRate: this.sourceSamplingRate || 96000,
867
- })
868
- .then(res => {
869
- this.incrementStatusBar();
870
- this.status =
871
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
872
- this.generateTemplate().toString();
873
- this.emit('update', {message: this.status});
874
- return res;
875
- })
876
- .catch(err => {
877
- console.error(err);
878
- });
879
-
880
- iir_ir_and_plots = {
881
- filtered_recording: {
882
- component: return_component_conv_rec,
883
- system: return_system_conv_rec,
884
- },
885
- unfiltered_recording: this.getAllUnfilteredRecordedSignals()[0],
886
- system: {
887
- iir: this.systemInvertedImpulseResponse,
888
- ir: this.systemIR,
889
- iir_psd: {
890
- y: system_iir_psd['y_conv'],
891
- x: system_iir_psd['x_conv'],
892
- y_no_bandpass: system_iir_psd['y_unconv'],
893
- x_no_bandpass: system_iir_psd['x_unconv'],
894
- },
895
- filtered_mls_psd: {
896
- x: system_filtered_mls_psd['x_mls'],
897
- y: system_filtered_mls_psd['y_mls'],
898
- },
899
- convolution: this.systemConvolution,
900
- psd: {
901
- unconv: {
902
- x: system_recs_psd['x_unconv'],
903
- y: system_recs_psd['y_unconv'],
904
- },
905
- conv: {
906
- x: system_recs_psd['x_conv'],
907
- y: system_recs_psd['y_conv'],
908
- },
909
- },
910
- },
911
- component: {
912
- iir: this.componentInvertedImpulseResponse,
913
- ir: this.componentIR,
914
- iir_psd: {
915
- y: component_iir_psd['y_conv'],
916
- x: component_iir_psd['x_conv'],
917
- y_no_bandpass: component_iir_psd['y_unconv'],
918
- x_no_bandpass: component_iir_psd['x_unconv'],
919
- },
920
- filtered_mls_psd: {
921
- x: component_filtered_mls_psd['x_mls'],
922
- y: component_filtered_mls_psd['y_mls'],
923
- },
924
- convolution: this.componentConvolution,
925
- psd: {
926
- unconv: {
927
- x: component_unconv_rec_psd['x'],
928
- y: component_unconv_rec_psd['y'],
929
- },
930
- conv: {
931
- x: component_conv_rec_psd['x'],
932
- y: component_conv_rec_psd['y'],
933
- },
934
- test: {
935
- x: test_conv_rec_psd['x'],
936
- y: test_conv_rec_psd['y'],
937
- },
938
- },
939
- },
940
- mls: this.#mlsBufferView,
941
- mls_psd: {
942
- x: mls_psd['x_mls'],
943
- y: mls_psd['y_mls'],
944
- },
945
- autocorrelations: this.autocorrelations,
946
- impulseResponses: [],
947
- };
948
-
949
- return iir_ir_and_plots;
950
- };
951
-
952
- singleSoundCheck = async stream => {
953
- let iir_ir_and_plots;
954
- if (this._calibrateSoundCheck != 'system') {
955
- this.#currentConvolution = this.componentConvolution;
956
- this.filteredMLSRange.component.Min = findMinValue(this.#currentConvolution);
957
- this.filteredMLSRange.component.Max = findMaxValue(this.#currentConvolution);
958
- } else {
959
- this.#currentConvolution = this.systemConvolution;
960
- this.filteredMLSRange.system.Min = findMinValue(this.#currentConvolution);
961
- this.filteredMLSRange.system.Max = findMaxValue(this.#currentConvolution);
962
- }
963
- await this.playMLSwithIIR(stream, this.#currentConvolution);
964
- this.#stopCalibrationAudio();
965
- let conv_recs = this.getAllFilteredRecordedSignals();
966
- let recs = this.getAllUnfilteredRecordedSignals();
967
- this.clearAllFilteredRecordedSignals();
968
- console.log('Obtaining unfiltered recording from #allHzUnfilteredRecordings to calculate PSD');
969
- console.log('Obtaining filtered recording from #allHzFilteredRecordings to calculate PSD');
970
- let unconv_rec = recs[0];
971
- let return_unconv_rec = unconv_rec;
972
- let conv_rec = conv_recs[0];
973
- let return_conv_rec = conv_rec;
974
- if (this._calibrateSoundCheck != 'system') {
975
- this.#currentConvolution = this.testConvolution;
976
- await this.playMLSwithIIR(stream, this.#currentConvolution);
977
- this.#stopCalibrationAudio();
978
- }
979
- let test_conv_recs = this.getAllFilteredRecordedSignals();
980
- let return_test_conv_rec = test_conv_recs[0];
981
- this.sourceAudioContext.close();
982
- if (this._calibrateSoundCheck != 'system') {
983
- let knownGain = this.oldComponentIR.Gain;
984
- let knownFreq = this.oldComponentIR.Freq;
985
- let sampleRate = this.sourceSamplingRate || 96000;
986
- let unconv_results = await this.pyServerAPI
987
- .getSubtractedPSDWithRetry(unconv_rec, knownGain, knownFreq, sampleRate)
988
- .then(res => {
989
- this.incrementStatusBar();
990
- this.status =
991
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
992
- this.generateTemplate().toString();
993
- this.emit('update', {message: this.status});
994
- return res;
995
- })
996
- .catch(err => {
997
- console.error(err);
998
- });
999
-
1000
- let conv_results = await this.pyServerAPI
1001
- .getSubtractedPSDWithRetry(conv_rec, knownGain, knownFreq, sampleRate)
1002
- .then(res => {
1003
- this.incrementStatusBar();
1004
- this.status =
1005
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
1006
- this.generateTemplate().toString();
1007
- this.emit('update', {message: this.status});
1008
- return res;
1009
- })
1010
- .catch(err => {
1011
- console.error(err);
1012
- });
1013
-
1014
- let test_conv_results = await this.pyServerAPI
1015
- .getSubtractedPSDWithRetry(return_test_conv_rec, knownGain, knownFreq, sampleRate)
1016
- .then(res => {
1017
- this.incrementStatusBar();
1018
- this.status =
1019
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
1020
- this.generateTemplate().toString();
1021
- this.emit('update', {message: this.status});
1022
- return res;
1023
- })
1024
- .catch(err => {
1025
- console.error(err);
1026
- });
1027
-
1028
- unconv_rec = this.componentInvertedImpulseResponseNoBandpass;
1029
- conv_rec = this.componentInvertedImpulseResponse;
1030
- let component_iir_psd = await this.pyServerAPI
1031
- .getPSDWithRetry({
1032
- unconv_rec,
1033
- conv_rec,
1034
- sampleRate: this.sourceSamplingRate || 96000,
1035
- })
1036
- .then(res => {
1037
- this.incrementStatusBar();
1038
- this.status =
1039
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
1040
- this.generateTemplate().toString();
1041
- this.emit('update', {message: this.status});
1042
- return res;
1043
- })
1044
- .catch(err => {
1045
- console.error(err);
1046
- });
1047
- unconv_rec = this.systemInvertedImpulseResponseNoBandpass;
1048
- conv_rec = this.systemInvertedImpulseResponse;
1049
- let system_iir_psd = await this.pyServerAPI
1050
- .getPSDWithRetry({
1051
- unconv_rec,
1052
- conv_rec,
1053
- sampleRate: this.sourceSamplingRate || 96000,
1054
- })
1055
- .then(res => {
1056
- this.incrementStatusBar();
1057
- this.status =
1058
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
1059
- this.generateTemplate().toString();
1060
- this.emit('update', {message: this.status});
1061
- return res;
1062
- })
1063
- .catch(err => {
1064
- console.error(err);
1065
- });
1066
-
1067
- let mls_psd = await this.pyServerAPI
1068
- .getMLSPSDWithRetry({
1069
- mls: this.#mlsBufferView,
1070
- sampleRate: this.sourceSamplingRate || 96000,
1071
- })
1072
- .then(res => {
1073
- this.incrementStatusBar();
1074
- this.status =
1075
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
1076
- this.generateTemplate().toString();
1077
- this.emit('update', {message: this.status});
1078
- return res;
1079
- })
1080
- .catch(err => {
1081
- console.error(err);
1082
- });
1083
-
1084
- let filtered_mls_psd = await this.pyServerAPI
1085
- .getMLSPSDWithRetry({
1086
- mls: this.componentConvolution,
1087
- sampleRate: this.sourceSamplingRate || 96000,
1088
- })
1089
- .then(res => {
1090
- this.incrementStatusBar();
1091
- this.status =
1092
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
1093
- this.generateTemplate().toString();
1094
- this.emit('update', {message: this.status});
1095
- return res;
1096
- })
1097
- .catch(err => {
1098
- console.error(err);
1099
- });
1100
-
1101
- iir_ir_and_plots = {
1102
- unfiltered_recording: return_unconv_rec,
1103
- filtered_recording: return_conv_rec,
1104
- system: {
1105
- iir: this.systemInvertedImpulseResponse,
1106
- ir: this.systemIR,
1107
- iir_psd: {
1108
- y: system_iir_psd['y_conv'],
1109
- x: system_iir_psd['y_conv'],
1110
- y_no_bandpass: system_iir_psd['y_unconv'],
1111
- x_no_bandpass: system_iir_psd['x_unconv'],
1112
- },
1113
- filtered_recording: [],
1114
- filtered_mls_psd: {},
1115
- convolution: this.systemConvolution,
1116
- psd: {
1117
- unconv: {
1118
- x: [],
1119
- y: [],
1120
- },
1121
- conv: {
1122
- x: [],
1123
- y: [],
1124
- },
1125
- },
1126
- },
1127
- component: {
1128
- iir: this.componentInvertedImpulseResponse,
1129
- ir: this.componentIR,
1130
- iir_psd: {
1131
- y: component_iir_psd['y_conv'],
1132
- x: component_iir_psd['x_conv'],
1133
- y_no_bandpass: component_iir_psd['y_unconv'],
1134
- x_no_bandpass: component_iir_psd['x_unconv'],
1135
- },
1136
- filtered_mls_psd: {
1137
- x: filtered_mls_psd['x_mls'],
1138
- y: filtered_mls_psd['y_mls'],
1139
- },
1140
- convolution: this.componentConvolution,
1141
- psd: {
1142
- unconv: {
1143
- x: unconv_results['x'],
1144
- y: unconv_results['y'],
1145
- },
1146
- conv: {
1147
- x: conv_results['x'],
1148
- y: conv_results['y'],
1149
- },
1150
- test: {
1151
- x: test_conv_results['x'],
1152
- y: test_conv_results['y'],
1153
- },
1154
- },
1155
- },
1156
- mls: this.#mlsBufferView,
1157
- mls_psd: {
1158
- x: mls_psd['x_mls'],
1159
- y: mls_psd['y_mls'],
1160
- },
1161
- autocorrelations: this.autocorrelations,
1162
- impulseResponses: [],
1163
- };
1164
- } else {
1165
- let results = await this.pyServerAPI
1166
- .getPSDWithRetry({
1167
- unconv_rec,
1168
- conv_rec,
1169
- sampleRate: this.sourceSamplingRate || 96000,
1170
- })
1171
- .then(res => {
1172
- this.incrementStatusBar();
1173
- this.status =
1174
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
1175
- this.generateTemplate().toString();
1176
- this.emit('update', {message: this.status});
1177
- return res;
1178
- })
1179
- .catch(err => {
1180
- console.error(err);
1181
- });
1182
-
1183
- //iir w/ and without bandpass psd
1184
- unconv_rec = this.componentInvertedImpulseResponseNoBandpass;
1185
- conv_rec = this.componentInvertedImpulseResponse;
1186
- let component_iir_psd = await this.pyServerAPI
1187
- .getPSDWithRetry({
1188
- unconv_rec,
1189
- conv_rec,
1190
- sampleRate: this.sourceSamplingRate || 96000,
1191
- })
1192
- .then(res => {
1193
- this.incrementStatusBar();
1194
- this.status =
1195
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
1196
- this.generateTemplate().toString();
1197
- this.emit('update', {message: this.status});
1198
- return res;
1199
- })
1200
- .catch(err => {
1201
- console.error(err);
1202
- });
1203
- unconv_rec = this.systemInvertedImpulseResponseNoBandpass;
1204
- conv_rec = this.systemInvertedImpulseResponse;
1205
- let system_iir_psd = await this.pyServerAPI
1206
- .getPSDWithRetry({
1207
- unconv_rec,
1208
- conv_rec,
1209
- sampleRate: this.sourceSamplingRate || 96000,
1210
- })
1211
- .then(res => {
1212
- this.incrementStatusBar();
1213
- this.status =
1214
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
1215
- this.generateTemplate().toString();
1216
- this.emit('update', {message: this.status});
1217
- return res;
1218
- })
1219
- .catch(err => {
1220
- console.error(err);
1221
- });
1222
-
1223
- let mls_psd = await this.pyServerAPI
1224
- .getMLSPSDWithRetry({
1225
- mls: this.#mlsBufferView,
1226
- sampleRate: this.sourceSamplingRate || 96000,
1227
- })
1228
- .then(res => {
1229
- this.incrementStatusBar();
1230
- this.status =
1231
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
1232
- this.generateTemplate().toString();
1233
- this.emit('update', {message: this.status});
1234
- return res;
1235
- })
1236
- .catch(err => {
1237
- console.error(err);
1238
- });
1239
-
1240
- let filtered_mls_psd = await this.pyServerAPI
1241
- .getMLSPSDWithRetry({
1242
- mls: this.systemConvolution,
1243
- sampleRate: this.sourceSamplingRate || 96000,
1244
- })
1245
- .then(res => {
1246
- this.incrementStatusBar();
1247
- this.status =
1248
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
1249
- this.generateTemplate().toString();
1250
- this.emit('update', {message: this.status});
1251
- return res;
1252
- })
1253
- .catch(err => {
1254
- console.error(err);
1255
- });
1256
-
1257
- iir_ir_and_plots = {
1258
- unfiltered_recording: return_unconv_rec,
1259
- filtered_recording: return_conv_rec,
1260
- system: {
1261
- iir: this.systemInvertedImpulseResponse,
1262
- ir: this.systemIR,
1263
- iir_psd: {
1264
- y: system_iir_psd['y_conv'],
1265
- x: system_iir_psd['y_conv'],
1266
- y_no_bandpass: system_iir_psd['y_unconv'],
1267
- x_no_bandpass: system_iir_psd['x_unconv'],
1268
- },
1269
- filtered_recording: [],
1270
- filtered_mls_psd: {
1271
- x: filtered_mls_psd['x_mls'],
1272
- y: filtered_mls_psd['y_mls'],
1273
- },
1274
- convolution: this.systemConvolution,
1275
- psd: {
1276
- unconv: {
1277
- x: results['x_unconv'],
1278
- y: results['y_unconv'],
1279
- },
1280
- conv: {
1281
- x: results['x_conv'],
1282
- y: results['y_conv'],
1283
- },
1284
- },
1285
- },
1286
- component: {
1287
- iir: this.componentInvertedImpulseResponse,
1288
- ir: this.componentIR,
1289
- iir_psd: {
1290
- y: component_iir_psd['y_conv'],
1291
- x: component_iir_psd['x_conv'],
1292
- y_no_bandpass: component_iir_psd['y_unconv'],
1293
- x_no_bandpass: component_iir_psd['x_unconv'],
1294
- },
1295
- filtered_mls_psd: {},
1296
- convolution: this.componentConvolution,
1297
- psd: {
1298
- unconv: {
1299
- x: [],
1300
- y: [],
1301
- },
1302
- conv: {
1303
- x: [],
1304
- y: [],
1305
- },
1306
- test: {
1307
- x: [],
1308
- y: [],
1309
- },
1310
- },
1311
- },
1312
- mls: this.#mlsBufferView,
1313
- mls_psd: {
1314
- x: mls_psd['x_mls'],
1315
- y: mls_psd['y_mls'],
1316
- },
1317
- autocorrelations: this.autocorrelations,
1318
- impulseResponses: [],
1319
- };
1320
- }
1321
- await Promise.all(this.impulseResponses).then(res => {
1322
- for (let i = 0; i < res.length; i++) {
1323
- if (res[i] != undefined) {
1324
- iir_ir_and_plots['impulseResponses'].push(res[i]);
1325
- }
1326
- }
1327
- });
1328
-
1329
- if (this.#download) {
1330
- this.downloadSingleUnfilteredRecording();
1331
- this.downloadSingleFilteredRecording();
1332
- saveToCSV(this.#mls, 'MLS.csv');
1333
- saveToCSV(this.componentConvolution, 'python_component_convolution_mls_iir.csv');
1334
- saveToCSV(this.systemConvolution, 'python_system_convolution_mls_iir.csv');
1335
- saveToCSV(this.componentInvertedImpulseResponse, 'componentIIR.csv');
1336
- saveToCSV(this.systemInvertedImpulseResponse, 'systemIIR.csv');
1337
- for (let i = 0; i < this.autocorrelations.length; i++) {
1338
- saveToCSV(this.autocorrelations[i], `autocorrelation_${i}`);
1339
- }
1340
- const computedIRagain = await Promise.all(this.impulseResponses).then(res => {
1341
- for (let i = 0; i < res.length; i++) {
1342
- if (res[i] != undefined) {
1343
- saveToCSV(res[i], `IR_${i}`);
1344
- }
1345
- }
1346
- });
1347
- }
1348
-
1349
- return iir_ir_and_plots;
1350
- };
1351
-
1352
- /**
1353
- * Public method to start the calibration process. Objects intialized from webassembly allocate new memory
1354
- * and must be manually freed. This function is responsible for intializing the MlsGenInterface,
1355
- * and wrapping the calibration steps with a garbage collection safe gaurd.
1356
- *
1357
- * @public
1358
- * @param stream - The stream of audio from the Listener.
1359
- * @example
1360
- */
1361
- startCalibrationImpulseResponse = async stream => {
1362
- let desired_time = this.desired_time_per_mls;
1363
- let checkRec = 'allhz';
1364
-
1365
- console.log('MLS sequence should be of length: ' + this.sourceSamplingRate * desired_time);
1366
-
1367
- length = this.sourceSamplingRate * desired_time;
1368
- //get mls here
1369
- const calibrateSoundBurstDb = this._calibrateSoundBurstDb;
1370
- await this.pyServerAPI
1371
- .getMLSWithRetry({length, calibrateSoundBurstDb})
1372
- .then(res => {
1373
- console.log(res);
1374
- this.#mlsBufferView = res['mls'];
1375
- this.#mls = res['unscaledMLS'];
1376
- })
1377
- .catch(err => {
1378
- // this.emit('InvertedImpulseResponse', {res: false});
1379
- console.error(err);
1380
- });
1381
- this.numSuccessfulBackgroundCaptured = 0;
1382
- if (this._calibrateSoundBackgroundSecs > 0) {
1383
- this.mode = 'background';
1384
- this.status =
1385
- `All Hz Calibration: sampling the background noise...`.toString() +
1386
- this.generateTemplate().toString();
1387
- this.emit('update', {message: this.status});
1388
- await this.recordBackground(
1389
- stream, //stream
1390
- () => this.numSuccessfulBackgroundCaptured < 1, //loop condition
1391
- this.#awaitBackgroundNoiseRecording, //sleep to record
1392
- this.sendBackgroundRecording, //send to get PSD
1393
- this.mode,
1394
- checkRec
1395
- );
1396
- this.incrementStatusBar();
1397
- }
1398
- this.mode = 'unfiltered';
1399
- this.numSuccessfulCaptured = 0;
1400
-
1401
- await this.calibrationSteps(
1402
- stream,
1403
- this.#playCalibrationAudio, // play audio func (required)
1404
- this.#createCalibrationNodeFromBuffer(this.#mlsBufferView), // before play func
1405
- this.#awaitSignalOnset, // before record
1406
- () => this.numSuccessfulCaptured < this.numCaptures, // loop while true
1407
- this.#awaitDesiredMLSLength, // during record
1408
- this.#afterMLSRecord, // after record
1409
- this.mode,
1410
- checkRec
1411
- ),
1412
- this.#stopCalibrationAudio();
1413
- checkRec = false;
1414
-
1415
- // at this stage we've captured all the required signals,
1416
- // and have received IRs for each one
1417
- // so let's send all the IRs to the server to be converted to a single IIR
1418
- await this.sendSystemImpulseResponsesToServerForProcessing();
1419
- await this.sendComponentImpulseResponsesToServerForProcessing();
1420
-
1421
- this.numSuccessfulCaptured = 0;
1422
-
1423
- let iir_ir_and_plots;
1424
- if (this._calibrateSoundCheck != 'none') {
1425
- //do single check
1426
- if (this._calibrateSoundCheck == 'goal' || this._calibrateSoundCheck == 'system') {
1427
- iir_ir_and_plots = await this.singleSoundCheck(stream);
1428
- } else {
1429
- //both
1430
- iir_ir_and_plots = await this.bothSoundCheck(stream);
1431
- }
1432
- } else {
1433
- let unconv_rec = this.componentInvertedImpulseResponseNoBandpass;
1434
- let conv_rec = this.componentInvertedImpulseResponse;
1435
- let component_iir_psd = await this.pyServerAPI
1436
- .getPSDWithRetry({
1437
- unconv_rec,
1438
- conv_rec,
1439
- sampleRate: this.sourceSamplingRate || 96000,
1440
- })
1441
- .then(res => {
1442
- this.incrementStatusBar();
1443
- this.status =
1444
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
1445
- this.generateTemplate().toString();
1446
- this.emit('update', {message: this.status});
1447
- return res;
1448
- })
1449
- .catch(err => {
1450
- console.error(err);
1451
- });
1452
- unconv_rec = this.systemInvertedImpulseResponseNoBandpass;
1453
- conv_rec = this.systemInvertedImpulseResponse;
1454
- let system_iir_psd = await this.pyServerAPI
1455
- .getPSDWithRetry({
1456
- unconv_rec,
1457
- conv_rec,
1458
- sampleRate: this.sourceSamplingRate || 96000,
1459
- })
1460
- .then(res => {
1461
- this.incrementStatusBar();
1462
- this.status =
1463
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
1464
- this.generateTemplate().toString();
1465
- this.emit('update', {message: this.status});
1466
- return res;
1467
- })
1468
- .catch(err => {
1469
- console.error(err);
1470
- });
1471
-
1472
- iir_ir_and_plots = {
1473
- unfiltered_recording: return_unconv_rec,
1474
- filtered_recording: return_conv_rec,
1475
- system: {
1476
- iir: this.systemInvertedImpulseResponse,
1477
- ir: this.systemIR,
1478
- iir_psd: {
1479
- y: system_iir_psd['y_conv'],
1480
- x: system_iir_psd['y_conv'],
1481
- y_no_bandpass: system_iir_psd['y_unconv'],
1482
- x_no_bandpass: system_iir_psd['x_unconv'],
1483
- },
1484
- filtered_recording: [],
1485
- convolution: this.systemConvolution,
1486
- psd: {
1487
- unconv: {
1488
- x: [],
1489
- y: [],
1490
- },
1491
- conv: {
1492
- x: [],
1493
- y: [],
1494
- },
1495
- },
1496
- },
1497
- component: {
1498
- iir: this.componentInvertedImpulseResponse,
1499
- ir: this.componentIR,
1500
- iir_psd: {
1501
- y: component_iir_psd['y_conv'],
1502
- x: component_iir_psd['x_conv'],
1503
- y_no_bandpass: component_iir_psd['y_unconv'],
1504
- x_no_bandpass: component_iir_psd['x_unconv'],
1505
- },
1506
- convolution: this.componentConvolution,
1507
- psd: {
1508
- unconv: {
1509
- x: [],
1510
- y: [],
1511
- },
1512
- conv: {
1513
- x: [],
1514
- y: [],
1515
- },
1516
- },
1517
- },
1518
- mls: this.#mlsBufferView,
1519
- autocorrelations: this.autocorrelations,
1520
- impulseResponses: [],
1521
- };
1522
- await Promise.all(this.impulseResponses).then(res => {
1523
- for (let i = 0; i < res.length; i++) {
1524
- if (res[i] != undefined) {
1525
- iir_ir_and_plots['impulseResponses'].push(res[i]);
1526
- }
1527
- }
1528
- });
1529
-
1530
- if (this.#download) {
1531
- saveToCSV(this.#mls, 'MLS.csv');
1532
- saveToCSV(this.componentConvolution, 'python_component_convolution_mls_iir.csv');
1533
- saveToCSV(this.systemConvolution, 'python_system_convolution_mls_iir.csv');
1534
- saveToCSV(this.componentInvertedImpulseResponse, 'componentIIR.csv');
1535
- saveToCSV(this.systemInvertedImpulseResponse, 'systemIIR.csv');
1536
- for (let i = 0; i < this.autocorrelations.length; i++) {
1537
- saveToCSV(this.autocorrelations[i], `autocorrelation_${i}`);
1538
- }
1539
- const computedIRagain = await Promise.all(this.impulseResponses).then(res => {
1540
- for (let i = 0; i < res.length; i++) {
1541
- if (res[i] != undefined) {
1542
- saveToCSV(res[i], `IR_${i}`);
1543
- }
1544
- }
1545
- });
1546
- }
1547
- }
1548
-
1549
- this.percent_complete = 100;
1550
-
1551
- this.status = `All Hz Calibration: Finished`.toString() + this.generateTemplate().toString();
1552
- this.emit('update', {message: this.status});
1553
-
1554
- //here after calibration we have the component calibration (either loudspeaker or microphone) in the same form as the componentIR
1555
- //that was used to calibrate
1556
- // saveToJSON(iir_ir_and_plots);
1557
- return iir_ir_and_plots;
1558
- };
1559
-
1560
- //////////////////////volume
1561
-
1562
- handleIncomingData = data => {
1563
- console.log('Received data: ', data);
1564
- if (data.type === 'soundGainDBSPL') {
1565
- this.soundGainDBSPL = data.value;
1566
- } else {
1567
- throw new Error(`Unknown data type: ${data.type}`);
1568
- }
1569
- };
1570
- createSCurveBuffer = (onSetBool = true) => {
1571
- const curve = new Float32Array(this.TAPER_SECS * this.sourceSamplingRate + 1);
1572
- const frequency = 1 / (4 * this.TAPER_SECS);
1573
- let j = 0;
1574
- for (let i = 0; i < this.TAPER_SECS * this.sourceSamplingRate + 1; i += 1) {
1575
- const phase = 2 * Math.PI * frequency * j;
1576
- const onsetTaper = Math.pow(Math.sin(phase), 2);
1577
- const offsetTaper = Math.pow(Math.cos(phase), 2);
1578
- curve[i] = onSetBool ? onsetTaper : offsetTaper;
1579
- j += 1 / this.sourceSamplingRate;
1580
- }
1581
- return curve;
1582
- };
1583
-
1584
- #getTruncatedSignal = (left = 3.5, right = 4.5) => {
1585
- const start = Math.floor(left * this.sourceSamplingRate);
1586
- const end = Math.floor(right * this.sourceSamplingRate);
1587
- const result = Array.from(this.getLastVolumeRecordedSignal().slice(start, end));
1588
- console.log(
1589
- 'Obtaining last 1000 hz recording from #allVolumeRecordings to send for processing'
1590
- );
1591
- /**
1592
- * function to check that capture was properly made
1593
- * @param {*} list
1594
- */
1595
- const checkResult = list => {
1596
- const setItem = new Set(list);
1597
- if (setItem.size === 1 && setItem.has(0)) {
1598
- console.warn(
1599
- 'The last capture failed, all recorded signal is zero',
1600
- this.getAllVolumeRecordedSignals()
1601
- );
1602
- }
1603
- if (setItem.size === 0) {
1604
- console.warn('The last capture failed, no recorded signal');
1605
- }
1606
- };
1607
- checkResult(result);
1608
- return result;
1609
- };
1610
-
1611
- /**
1612
- *
1613
- *
1614
- Construct a calibration Node with the calibration parameters and given gain value
1615
- * @param {*} gainValue
1616
- * */
1617
- #createCalibrationToneWithGainValue = gainValue => {
1618
- const audioContext = this.makeNewSourceAudioContext();
1619
- const oscilator = audioContext.createOscillator();
1620
- const gainNode = audioContext.createGain();
1621
- const taperGainNode = audioContext.createGain();
1622
- const offsetGainNode = audioContext.createGain();
1623
- const totalDuration = this.CALIBRATION_TONE_DURATION * 1.2;
1624
-
1625
- oscilator.frequency.value = this.#CALIBRATION_TONE_FREQUENCY;
1626
- oscilator.type = this.#CALIBRATION_TONE_TYPE;
1627
- gainNode.gain.value = gainValue;
1628
-
1629
- oscilator.connect(gainNode);
1630
- gainNode.connect(taperGainNode);
1631
- const onsetCurve = this.createSCurveBuffer();
1632
- taperGainNode.gain.setValueCurveAtTime(onsetCurve, 0, this.TAPER_SECS);
1633
- taperGainNode.connect(offsetGainNode);
1634
- const offsetCurve = this.createSCurveBuffer(false);
1635
- offsetGainNode.gain.setValueCurveAtTime(
1636
- offsetCurve,
1637
- totalDuration - this.TAPER_SECS,
1638
- this.TAPER_SECS
1639
- );
1640
- offsetGainNode.connect(audioContext.destination);
1641
-
1642
- this.addCalibrationNode(oscilator);
1643
- };
1644
-
1645
- /**
1646
- * Construct a Calibration Node with the calibration parameters.
1647
- *
1648
- * @private
1649
- * @example
1650
- */
1651
- #createCalibrationNode = () => {
1652
- const audioContext = this.makeNewSourceAudioContext();
1653
- const oscilator = audioContext.createOscillator();
1654
- const gainNode = audioContext.createGain();
1655
-
1656
- oscilator.frequency.value = this.#CALIBRATION_TONE_FREQUENCY;
1657
- oscilator.type = this.#CALIBRATION_TONE_TYPE;
1658
- gainNode.gain.value = 0.04;
1659
-
1660
- oscilator.connect(gainNode);
1661
- gainNode.connect(audioContext.destination);
1662
-
1663
- this.addCalibrationNode(oscilator);
1664
- };
1665
-
1666
- #playCalibrationAudioVolume = async () => {
1667
- const totalDuration = this.CALIBRATION_TONE_DURATION * 1.2;
1668
-
1669
- this.calibrationNodes[0].start(0);
1670
- this.calibrationNodes[0].stop(totalDuration);
1671
- console.log(`Playing a buffer of ${this.CALIBRATION_TONE_DURATION} seconds of audio`);
1672
- console.log(`Waiting a total of ${totalDuration} seconds`);
1673
- await sleep(totalDuration);
1674
- };
1675
-
1676
- #sendToServerForProcessing = lCalib => {
1677
- console.log('Sending data to server');
1678
- let left = this.calibrateSound1000HzPreSec;
1679
- let right = this.calibrateSound1000HzPreSec + this.calibrateSound1000HzSec;
1680
- this.pyServerAPI
1681
- .getVolumeCalibration({
1682
- sampleRate: this.sourceSamplingRate,
1683
- payload: this.#getTruncatedSignal(left, right),
1684
- lCalib: lCalib,
1685
- })
1686
- .then(res => {
1687
- if (this.outDBSPL === null) {
1688
- this.incrementStatusBar();
1689
- this.outDBSPL = res['outDbSPL'];
1690
- this.outDBSPL1000 = res['outDbSPL1000'];
1691
- this.THD = res['thd'];
1692
- }
1693
- })
1694
- .catch(err => {
1695
- console.warn(err);
1696
- });
1697
- };
1698
-
1699
- startCalibrationVolume = async (stream, gainValues, lCalib, componentGainDBSPL) => {
1700
- const trialIterations = gainValues.length;
1701
- this.status_denominator += trialIterations;
1702
- const thdValues = [];
1703
- const inDBValues = [];
1704
- let inDB = 0;
1705
- const outDBSPLValues = [];
1706
- const outDBSPL1000Values = [];
1707
- let checkRec = false;
1708
-
1709
- // do one calibration that will be discarded
1710
- const soundLevelToDiscard = -60;
1711
- const gainToDiscard = Math.pow(10, soundLevelToDiscard / 20);
1712
- this.status =
1713
- `1000 Hz Calibration: Sound Level ${soundLevelToDiscard} dB`.toString() +
1714
- this.generateTemplate().toString();
1715
- //this.emit('update', {message: `1000 Hz Calibration: Sound Level ${soundLevelToDiscard} dB`});
1716
- this.emit('update', {message: this.status});
1717
-
1718
- do {
1719
- // eslint-disable-next-line no-await-in-loop
1720
- await this.volumeCalibrationSteps(
1721
- stream,
1722
- this.#playCalibrationAudioVolume,
1723
- this.#createCalibrationToneWithGainValue,
1724
- this.#sendToServerForProcessing,
1725
- gainToDiscard,
1726
- lCalib, //todo make this a class parameter
1727
- checkRec
1728
- );
1729
- } while (this.outDBSPL === null);
1730
- //reset the values
1731
- //this.incrementStatusBar();
1732
-
1733
- this.outDBSPL = null;
1734
- this.outDBSPL = null;
1735
- this.outDBSPL1000 = null;
1736
- this.THD = null;
1737
-
1738
- // run the calibration at different gain values provided by the user
1739
- for (let i = 0; i < trialIterations; i++) {
1740
- //convert gain to DB and add to inDB
1741
- if (i == trialIterations - 1) {
1742
- checkRec = 'loudest';
1743
- }
1744
- inDB = Math.log10(gainValues[i]) * 20;
1745
- // precision to 1 decimal place
1746
- inDB = Math.round(inDB * 10) / 10;
1747
- inDBValues.push(inDB);
1748
- console.log('next update');
1749
- this.status =
1750
- `1000 Hz Calibration: Sound Level ${inDB} dB`.toString() +
1751
- this.generateTemplate().toString();
1752
- this.emit('update', {message: this.status});
1753
- do {
1754
- // eslint-disable-next-line no-await-in-loop
1755
- await this.volumeCalibrationSteps(
1756
- stream,
1757
- this.#playCalibrationAudioVolume,
1758
- this.#createCalibrationToneWithGainValue,
1759
- this.#sendToServerForProcessing,
1760
- gainValues[i],
1761
- lCalib, //todo make this a class parameter
1762
- checkRec
1763
- );
1764
- } while (this.outDBSPL === null);
1765
- outDBSPL1000Values.push(this.outDBSPL1000);
1766
- thdValues.push(this.THD);
1767
- outDBSPLValues.push(this.outDBSPL);
1768
-
1769
- this.outDBSPL = null;
1770
- this.outDBSPL1000 = null;
1771
- this.THD = null;
1772
- }
1773
-
1774
- // get the volume calibration parameters from the server
1775
- const parameters = await this.pyServerAPI
1776
- .getVolumeCalibrationParameters({
1777
- inDBValues: inDBValues,
1778
- outDBSPLValues: outDBSPL1000Values,
1779
- lCalib: lCalib,
1780
- componentGainDBSPL,
1781
- })
1782
- .then(res => {
1783
- this.incrementStatusBar();
1784
- return res;
1785
- });
1786
- const result = {
1787
- parameters: parameters,
1788
- inDBValues: inDBValues,
1789
- outDBSPLValues: outDBSPLValues,
1790
- outDBSPL1000Values: outDBSPL1000Values,
1791
- thdValues: thdValues,
1792
- };
1793
-
1794
- return result;
1795
- };
1796
-
1797
- // function to write frq and gain to firebase database given speakerID
1798
- writeFrqGain = async (speakerID, frq, gain, OEM) => {
1799
- // freq and gain are too large to take samples 1 in every 100 samples
1800
-
1801
- const sampledFrq = [];
1802
- const sampledGain = [];
1803
- for (let i = 0; i < frq.length; i += 100) {
1804
- sampledFrq.push(frq[i]);
1805
- sampledGain.push(gain[i]);
1806
- }
1807
-
1808
- const data = {Freq: sampledFrq, Gain: sampledGain};
1809
-
1810
- await set(ref(database, `Microphone2/${OEM}/${speakerID}/linear`), data);
1811
- };
1812
-
1813
- // Function to Read frq and gain from firebase database given speakerID
1814
- // returns an array of frq and gain if speakerID exists, returns null otherwise
1815
-
1816
- readFrqGain = async (speakerID, OEM) => {
1817
- const dbRef = ref(database);
1818
- const snapshot = await get(child(dbRef, `Microphone2/${OEM}/${speakerID}/linear`));
1819
- if (snapshot.exists()) {
1820
- return snapshot.val();
1821
- }
1822
- return null;
1823
- };
1824
-
1825
- readGainat1000Hz = async (speakerID, OEM) => {
1826
- const dbRef = ref(database);
1827
- const snapshot = await get(child(dbRef, `Microphone2/${OEM}/${speakerID}/Gain1000`));
1828
- if (snapshot.exists()) {
1829
- return snapshot.val();
1830
- }
1831
- return null;
1832
- };
1833
-
1834
- writeGainat1000Hz = async (speakerID, gain, OEM) => {
1835
- const data = {Gain: gain};
1836
- await set(ref(database, `Microphone2/${OEM}/${speakerID}/Gain1000`), gain);
1837
- };
1838
-
1839
- writeIsSmartPhone = async (speakerID, isSmartPhone, OEM) => {
1840
- const data = {isSmartPhone: isSmartPhone};
1841
- await set(ref(database, `Microphone2/${OEM}/${speakerID}/isSmartPhone`), isSmartPhone);
1842
- };
1843
-
1844
- doesMicrophoneExist = async (speakerID, OEM) => {
1845
- const dbRef = ref(database);
1846
- const snapshot = await get(child(dbRef, `Microphone2/${OEM}/${speakerID}`));
1847
- if (snapshot.exists()) {
1848
- return true;
1849
- }
1850
- return false;
1851
- };
1852
-
1853
- addMicrophoneInfo = async (speakerID, OEM, micInfo) => {
1854
- // add to database if /info does not exist
1855
- const dbRef = ref(database);
1856
- const snapshot = await get(child(dbRef, `Microphone2/${OEM}/${speakerID}/info`));
1857
- if (!snapshot.exists()) {
1858
- await set(ref(database, `Microphone2/${OEM}/${speakerID}/info`), micInfo);
1859
- }
1860
- };
1861
-
1862
- convertToDB = gain => {
1863
- return Math.log10(gain) * 20;
1864
- };
1865
-
1866
- // Function to perform linear interpolation between two points
1867
- interpolate(x, x0, y0, x1, y1) {
1868
- return y0 + ((x - x0) * (y1 - y0)) / (x1 - x0);
1869
- }
1870
-
1871
- findGainatFrequency = (frequencies, gains, targetFrequency) => {
1872
- // Find the index of the first frequency in the array greater than the target frequency
1873
- let index = 0;
1874
- while (index < frequencies.length && frequencies[index] < targetFrequency) {
1875
- index++;
1876
- }
1877
-
1878
- // Handle cases when the target frequency is outside the range of the given data
1879
- if (index === 0) {
1880
- return gains[0];
1881
- } else if (index === frequencies.length) {
1882
- return gains[gains.length - 1];
1883
- } else {
1884
- // Interpolate the gain based on the surrounding frequencies
1885
- const x0 = frequencies[index - 1];
1886
- const y0 = gains[index - 1];
1887
- const x1 = frequencies[index];
1888
- const y1 = gains[index];
1889
- return this.interpolate(targetFrequency, x0, y0, x1, y1);
1890
- }
1891
- };
1892
-
1893
- // Example of how to use the writeFrqGain and readFrqGain functions
1894
- // writeFrqGain('speaker1', [1, 2, 3], [4, 5, 6]);
1895
- // Speaker1 is the speakerID you want to write to in the database
1896
- // readFrqGain('MiniDSPUMIK_1').then(data => console.log(data));
1897
- // MiniDSPUMIK_1 is the speakerID with some Data in the database
1898
- //adding gainDBSPL
1899
- startCalibration = async (
1900
- stream,
1901
- gainValues,
1902
- lCalib = 104.92978421490648,
1903
- componentIR = null,
1904
- microphoneName = 'MiniDSP-UMIK1-711-4754-vertical',
1905
- _calibrateSoundCheck = 'goal', //GOAL PASSed in by default
1906
- isSmartPhone = false,
1907
- _calibrateSoundBurstDb = 0.33,
1908
- _calibrateSoundBurstRepeats = 3,
1909
- _calibrateSoundBurstSec = 1,
1910
- _calibrateSoundBurstsWarmup = 1,
1911
- _calibrateSoundHz = 48000,
1912
- _calibrateSoundIIRSec = 0.2,
1913
- calibrateSound1000HzPreSec = 3.5,
1914
- calibrateSound1000HzSec = 1.0,
1915
- calibrateSound1000HzPostSec = 0.5,
1916
- _calibrateSoundBackgroundSecs = 0,
1917
- micManufacturer = '',
1918
- micSerialNumber = '',
1919
- micModelNumber = '',
1920
- micModelName = ''
1921
- ) => {
1922
- this._calibrateSoundBurstDb = _calibrateSoundBurstDb;
1923
- this.CALIBRATION_TONE_DURATION =
1924
- calibrateSound1000HzPreSec + calibrateSound1000HzSec + calibrateSound1000HzPostSec;
1925
- this.calibrateSound1000HzPreSec = calibrateSound1000HzPreSec;
1926
- this.calibrateSound1000HzSec = calibrateSound1000HzSec;
1927
- this.calibrateSound1000HzPostSec = calibrateSound1000HzPostSec;
1928
- this.iirLength = Math.floor(_calibrateSoundIIRSec * this.sourceSamplingRate);
1929
- console.log('device info:', this.deviceInfo);
1930
- this.numMLSPerCapture = _calibrateSoundBurstRepeats;
1931
- this.desired_time_per_mls = _calibrateSoundBurstSec;
1932
- this.num_mls_to_skip = _calibrateSoundBurstsWarmup;
1933
- this.desired_sampling_rate = _calibrateSoundHz;
1934
- this._calibrateSoundBackgroundSecs = _calibrateSoundBackgroundSecs;
1935
-
1936
- if (isSmartPhone) {
1937
- micModelName = this.deviceInfo.deviceInfoFromUser.modelName;
1938
- micModelNumber = this.deviceInfo.deviceInfoFromUser.modelNumber;
1939
- }
1940
-
1941
- //feed calibration goal here
1942
- this._calibrateSoundCheck = _calibrateSoundCheck;
1943
- //check if a componentIR was given to the system, if it isn't check for the microphone. using dummy data here bc we need to
1944
- //check the db based on the microphone currently connected
1945
-
1946
- //new lCalib found at top of calibration files *1000hz, make sure to correct
1947
- //based on zeroing of 1000hz, search for "*1000Hz"
1948
- const ID = isSmartPhone ? micModelNumber : micSerialNumber;
1949
- const OEM = isSmartPhone
1950
- ? this.deviceInfo.OEM.toLowerCase().split(' ').join('')
1951
- : micManufacturer;
1952
- // const ID = "711-4754";
1953
- // const OEM = "MiniDSP";
1954
- const micInfo = {
1955
- micModelName: isSmartPhone ? micModelName : microphoneName,
1956
- OEM: OEM,
1957
- ID: ID,
1958
- HardwareName: isSmartPhone ? this.deviceInfo.hardwarename : microphoneName,
1959
- hardwareFamily: isSmartPhone ? this.deviceInfo.hardwarefamily : microphoneName,
1960
- HardwareModel: isSmartPhone ? this.deviceInfo.hardwaremodel : microphoneName,
1961
- PlatformName: isSmartPhone ? this.deviceInfo.platformname : 'N/A',
1962
- PlatformVersion: isSmartPhone ? this.deviceInfo.platformversion : 'N/A',
1963
- DeviceType: isSmartPhone ? this.deviceInfo.devicetype : 'N/A',
1964
- };
1965
- // if undefined in micInfo, set to empty string
1966
- for (const [key, value] of Object.entries(micInfo)) {
1967
- if (value === undefined) {
1968
- micInfo[key] = '';
1969
- }
1970
- }
1971
-
1972
- this.addMicrophoneInfo(ID, OEM, micInfo);
1973
- if (componentIR == null) {
1974
- //mode 'ir'
1975
- //global variable this.componentIR must be set
1976
- this.componentIR = await this.readFrqGain(ID, OEM).then(data => {
1977
- return data;
1978
- });
1979
-
1980
- lCalib = await this.readGainat1000Hz(ID, OEM);
1981
- micInfo['gainDBSPL'] = lCalib;
1982
- // this.componentGainDBSPL = this.convertToDB(lCalib);
1983
- this.componentGainDBSPL = lCalib;
1984
- //TODO: if this call to database is unknown, cannot perform experiment => return false
1985
- if (this.componentIR == null) {
1986
- this.status =
1987
- `Microphone (${OEM},${ID}) is not found in the database. Please add it to the database.`.toString();
1988
- this.emit('update', {message: this.status});
1989
- return false;
1990
- }
1991
- } else {
1992
- this.componentIR = componentIR;
1993
- lCalib = this.findGainatFrequency(this.componentIR.Freq, this.componentIR.Gain, 1000);
1994
- // this.componentGainDBSPL = this.convertToDB(lCalib);
1995
- this.componentGainDBSPL = lCalib;
1996
- await this.writeIsSmartPhone(ID, isSmartPhone, OEM);
1997
- }
1998
-
1999
- this.oldComponentIR = this.componentIR;
2000
-
2001
- let volumeResults = await this.startCalibrationVolume(
2002
- stream,
2003
- gainValues,
2004
- lCalib,
2005
- this.componentGainDBSPL
2006
- );
2007
-
2008
- let impulseResponseResults = await this.startCalibrationImpulseResponse(stream);
2009
- impulseResponseResults['background_noise'] = this.background_noise;
2010
- if (componentIR != null) {
2011
- //insert Freq and Gain from this.componentIR into db
2012
- await this.writeFrqGain(
2013
- ID,
2014
- impulseResponseResults.componentIR.Freq,
2015
- impulseResponseResults.componentIR.Gain,
2016
- OEM
2017
- );
2018
- micInfo['gainDBSPL'] = impulseResponseResults.parameters.gainDBSPL;
2019
- await this.writeGainat1000Hz(ID, micInfo['gainDBSPL'], OEM);
2020
- }
2021
-
2022
- const total_results = {...volumeResults, ...impulseResponseResults};
2023
- total_results['filteredMLSRange'] = this.filteredMLSRange;
2024
- total_results['micInfo'] = micInfo;
2025
- total_results['audioInfo'] = {};
2026
- total_results['audioInfo']['sinkSampleRate'] = this.sinkSamplingRate;
2027
- total_results['audioInfo']['sourceSampleRate'] = this.sourceSamplingRate;
2028
- total_results['audioInfo']['bitsPerSample'] = this.sampleSize;
2029
-
2030
- console.log('total results');
2031
- console.log(total_results);
2032
- return total_results;
2033
- };
2034
- }
2035
-
2036
- export default Combination;
1
+ import AudioCalibrator from '../audioCalibrator';
2
+
3
+ import {sleep, csvToArray, saveToCSV, saveToJSON, findMinValue, findMaxValue} from '../../utils';
4
+ import database from '../../config/firebase';
5
+ import {ref, set, get, child} from 'firebase/database';
6
+
7
+ /**
8
+ *
9
+ */
10
+ class Combination extends AudioCalibrator {
11
+ /**
12
+ * Default constructor. Creates an instance with any number of paramters passed or the default parameters defined here.
13
+ *
14
+ * @param {Object<boolean, number, number, number>} calibratorParams - paramter object
15
+ * @param {boolean} [calibratorParams.download = false] - boolean flag to download captures
16
+ * @param {number} [calibratorParams.mlsOrder = 18] - order of the MLS to be generated
17
+ * @param {number} [calibratorParams.numCaptures = 5] - number of captures to perform
18
+ * @param {number} [calibratorParams.numMLSPerCapture = 2] - number of bursts of MLS per capture
19
+ */
20
+ constructor({
21
+ download = false,
22
+ mlsOrder = 18,
23
+ numCaptures = 3,
24
+ numMLSPerCapture = 2,
25
+ lowHz = 20,
26
+ highHz = 10000,
27
+ }) {
28
+ super(numCaptures, numMLSPerCapture);
29
+ this.#mlsOrder = parseInt(mlsOrder, 10);
30
+ this.#P = 2 ** mlsOrder - 1;
31
+ this.#download = download;
32
+ this.#mls = [];
33
+ this.#lowHz = lowHz;
34
+ this.#highHz = highHz;
35
+ }
36
+
37
+ /** @private */
38
+ stepNum = 0;
39
+
40
+ /** @private */
41
+ totalSteps = 25;
42
+
43
+ /** @private */
44
+ #download;
45
+
46
+ /** @private */
47
+ #mlsGenInterface;
48
+
49
+ /** @private */
50
+ #mlsBufferView;
51
+
52
+ /** @private */
53
+ componentInvertedImpulseResponse = null;
54
+
55
+ /** @private */
56
+ systemInvertedImpulseResponse = null;
57
+
58
+ //averaged and subtracted ir returned from calibration used to calculated iir
59
+ /** @private */
60
+ ir = null;
61
+
62
+ /** @private */
63
+ impulseResponses = [];
64
+
65
+ /** @private */
66
+ #mlsOrder;
67
+
68
+ /** @private */
69
+ #lowHz;
70
+
71
+ /** @private */
72
+ #highHz;
73
+
74
+ /** @private */
75
+ #mls;
76
+
77
+ /** @private */
78
+ #P;
79
+
80
+ /** @private */
81
+ #audioContext;
82
+
83
+ /** @private */
84
+ TAPER_SECS = 5;
85
+
86
+ /** @private */
87
+ offsetGainNode;
88
+
89
+ /** @private */
90
+ componentConvolution;
91
+
92
+ /** @private */
93
+ testConvolution;
94
+
95
+ /** @private */
96
+ systemConvolution;
97
+
98
+ ////////////////////////volume
99
+ /** @private */
100
+ #CALIBRATION_TONE_FREQUENCY = 1000; // Hz
101
+
102
+ /** @private */
103
+ #CALIBRATION_TONE_TYPE = 'sine';
104
+
105
+ CALIBRATION_TONE_DURATION = 5; // seconds
106
+ calibrateSound1000HzPreSec = 3.5;
107
+ calibrateSound1000HzSec = 1.0;
108
+ calibrateSound1000HzPostSec = 0.5;
109
+
110
+ /** @private */
111
+ outDBSPL = null;
112
+ THD = null;
113
+ outDBSPL1000 = null;
114
+
115
+ /** @private */
116
+ TAPER_SECS = 0.01; // seconds
117
+
118
+ /** @private */
119
+ status_denominator = 8;
120
+
121
+ /** @private */
122
+ status_numerator = 0;
123
+
124
+ /** @private */
125
+ percent_complete = 0;
126
+
127
+ /** @private */
128
+ status = ``;
129
+
130
+ /**@private */
131
+ status_literal = `<div style="display: flex; justify-content: center;"><div style="width: 200px; height: 20px; border: 2px solid #000; border-radius: 10px;"><div style="width: ${this.percent_complete}%; height: 100%; background-color: #00aaff; border-radius: 8px;"></div></div></div>`;
132
+
133
+ /**@private */
134
+ componentIR = null;
135
+
136
+ /**@private */
137
+ oldComponentIR = null;
138
+
139
+ /**@private */
140
+ systemIR = null;
141
+
142
+ /**@private */
143
+ _calibrateSoundCheck = '';
144
+
145
+ deviceType = null;
146
+
147
+ deviceName = null;
148
+
149
+ deviceInfo = null;
150
+
151
+ desired_time_per_mls = 0;
152
+
153
+ num_mls_to_skip = 0;
154
+
155
+ desired_sampling_rate = 0;
156
+
157
+ #currentConvolution = [];
158
+
159
+ mode = 'unfiltered';
160
+
161
+ sourceNode;
162
+
163
+ autocorrelations = [];
164
+
165
+ iirLength = 0;
166
+
167
+ componentInvertedImpulseResponseNoBandpass = [];
168
+
169
+ systemInvertedImpulseResponseNoBandpass = [];
170
+
171
+ _calibrateSoundBackgroundSecs;
172
+
173
+ background_noise = {};
174
+
175
+ numSuccessfulBackgroundCaptured;
176
+
177
+ _calibrateSoundBurstDb;
178
+
179
+ filteredMLSRange = {
180
+ component: {
181
+ Min: null,
182
+ Max: null,
183
+ },
184
+ system: {
185
+ Min: null,
186
+ Max: null,
187
+ },
188
+ };
189
+
190
+ /** @private */
191
+ timeStamp = [];
192
+
193
+ /** @private */
194
+ startTime;
195
+
196
+ /**generate string template that gets reevaluated as variable increases */
197
+ generateTemplate = () => {
198
+ if (this.percent_complete > 100) {
199
+ this.percent_complete = 100;
200
+ }
201
+ const template = `<div style="display: flex; justify-content: center; margin-top:12px;"><div style="width: 800px; height: 20px; border: 2px solid #000; border-radius: 10px;"><div style="width: ${this.percent_complete}%; height: 100%; background-color: #00aaff; border-radius: 8px;"></div></div></div>`;
202
+ return template;
203
+ };
204
+
205
+ /** increment numerator and percent for status bar */
206
+ incrementStatusBar = () => {
207
+ this.status_numerator += 1;
208
+ this.percent_complete = (this.status_numerator / this.status_denominator) * 100;
209
+ };
210
+
211
+ setDeviceType = deviceType => {
212
+ this.deviceType = deviceType;
213
+ };
214
+
215
+ setDeviceName = deviceName => {
216
+ this.deviceName = deviceName;
217
+ };
218
+
219
+ setDeviceInfo = deviceInfo => {
220
+ this.deviceInfo = deviceInfo;
221
+ };
222
+
223
+ /** .
224
+ * .
225
+ * .
226
+ * Sends all the computed impulse responses to the backend server for processing
227
+ *
228
+ * @returns sets the resulting inverted impulse response to the class property
229
+ * @example
230
+ */
231
+ sendSystemImpulseResponsesToServerForProcessing = async () => {
232
+ this.addTimeStamp("Get system iir");
233
+ const computedIRs = await Promise.all(this.impulseResponses);
234
+ const filteredComputedIRs = computedIRs.filter(element => {
235
+ return element != undefined;
236
+ }); //log any errors that are found in this step
237
+ const mls = this.#mls;
238
+ const lowHz = this.#lowHz; //gain of 1 below cutoff, need gain of 0
239
+ const highHz = this.#highHz; //check error for anything other than 10 kHz
240
+ const iirLength = this.iirLength;
241
+ const num_periods = this.numMLSPerCapture + this.num_mls_to_skip;
242
+ this.stepNum += 1;
243
+ console.log('send impulse responses to server: ' + this.stepNum);
244
+ this.status =
245
+ `All Hz Calibration: computing the IIR...`.toString() + this.generateTemplate().toString();
246
+ this.emit('update', {message: this.status});
247
+ return this.pyServerAPI
248
+ .getSystemInverseImpulseResponseWithRetry({
249
+ payload: filteredComputedIRs.slice(0, this.numCaptures),
250
+ mls,
251
+ lowHz,
252
+ highHz,
253
+ iirLength,
254
+ num_periods,
255
+ sampleRate: this.sourceSamplingRate || 96000,
256
+ calibrateSoundBurstDb: this._calibrateSoundBurstDb,
257
+ })
258
+ .then(res => {
259
+ console.log(res);
260
+ this.stepNum += 1;
261
+ console.log('got impulse response ' + this.stepNum);
262
+ this.incrementStatusBar();
263
+ this.status =
264
+ `All Hz Calibration: done computing the IIR...`.toString() +
265
+ this.generateTemplate().toString();
266
+ this.emit('update', {message: this.status});
267
+ this.systemInvertedImpulseResponse = res['iir'];
268
+ this.systemIR = res['ir'];
269
+ this.systemConvolution = res['convolution'];
270
+ this.systemInvertedImpulseResponseNoBandpass = res['iirNoBandpass'];
271
+ })
272
+ .catch(err => {
273
+ console.error(err);
274
+ });
275
+ };
276
+
277
+ /** .
278
+ * .
279
+ * .
280
+ * Sends all the computed impulse responses to the backend server for processing
281
+ *
282
+ * @returns sets the resulting inverted impulse response to the class property
283
+ * @example
284
+ */
285
+ sendComponentImpulseResponsesToServerForProcessing = async () => {
286
+ this.addTimeStamp("Get component iir");
287
+ const computedIRs = await Promise.all(this.impulseResponses);
288
+ const filteredComputedIRs = computedIRs.filter(element => {
289
+ return element != undefined;
290
+ });
291
+ const componentIRGains = this.componentIR['Gain'];
292
+ const componentIRFreqs = this.componentIR['Freq'];
293
+ const mls = this.#mls;
294
+ const lowHz = this.#lowHz;
295
+ const iirLength = this.iirLength;
296
+ const num_periods = this.numMLSPerCapture + this.num_mls_to_skip;
297
+ const highHz = this.#highHz;
298
+ this.stepNum += 1;
299
+ console.log('send impulse responses to server: ' + this.stepNum);
300
+ this.status =
301
+ `All Hz Calibration: computing the IIR...`.toString() + this.generateTemplate().toString();
302
+ this.emit('update', {message: this.status});
303
+ return this.pyServerAPI
304
+ .getComponentInverseImpulseResponseWithRetry({
305
+ payload: filteredComputedIRs.slice(0, this.numCaptures),
306
+ mls,
307
+ lowHz,
308
+ highHz,
309
+ iirLength,
310
+ componentIRGains,
311
+ componentIRFreqs,
312
+ num_periods,
313
+ sampleRate: this.sourceSamplingRate || 96000,
314
+ calibrateSoundBurstDb: this._calibrateSoundBurstDb,
315
+ })
316
+ .then(res => {
317
+ console.log(res);
318
+ this.stepNum += 1;
319
+ console.log('got impulse response ' + this.stepNum);
320
+ this.incrementStatusBar();
321
+ this.status =
322
+ `All Hz Calibration: done computing the IIR...`.toString() +
323
+ this.generateTemplate().toString();
324
+ this.emit('update', {message: this.status});
325
+ this.componentInvertedImpulseResponse = res['iir'];
326
+ this.componentIR['Gain'] = res['ir'];
327
+ this.componentIR['Freq'] = res['frequencies'];
328
+ this.componentConvolution = res['convolution'];
329
+ this.testConvolution = res['convolutionTest'];
330
+ this.componentInvertedImpulseResponseNoBandpass = res['iirNoBandpass'];
331
+ })
332
+ .catch(err => {
333
+ // this.emit('InvertedImpulseResponse', {res: false});
334
+ console.error(err);
335
+ });
336
+ };
337
+
338
+ sendBackgroundRecording = () => {
339
+ const allSignals = this.getAllBackgroundRecordings();
340
+ const numSignals = allSignals.length;
341
+ const background_rec_whole = allSignals[numSignals - 1];
342
+ const fraction = 0.5 / (this._calibrateSoundBackgroundSecs + 0.5);
343
+ // Calculate the starting index for slicing the array
344
+ const startIndex = Math.round(fraction * background_rec_whole.length);
345
+ // Slice the array from the calculated start index to the end of the array
346
+ const background_rec = background_rec_whole.slice(startIndex);
347
+ console.log('Sending background recording to server for processing');
348
+ this.addTimeStamp("Get background PSD");
349
+ this.pyServerAPI
350
+ .getBackgroundNoisePSDWithRetry({
351
+ background_rec,
352
+ sampleRate: this.sourceSamplingRate || 96000,
353
+ })
354
+ .then(res => {
355
+ if (this.numSuccessfulBackgroundCaptured < 1) {
356
+ this.numSuccessfulBackgroundCaptured += 1;
357
+ //storing all background data in background_psd object
358
+ this.background_noise['x_background'] = res['x_background'];
359
+ this.background_noise['y_background'] = res['y_background'];
360
+ this.background_noise['recording'] = background_rec;
361
+ }
362
+ })
363
+ .catch(err => {
364
+ console.error(err);
365
+ });
366
+ };
367
+
368
+ /** .
369
+ * .
370
+ * .
371
+ * Sends the recorded signal, or a given csv string of a signal, to the back end server for processing
372
+ *
373
+ * @param {<array>String} signalCsv - Optional csv string of a previously recorded signal, if given, this signal will be processed
374
+ * @example
375
+ */
376
+ sendRecordingToServerForProcessing = signalCsv => {
377
+ const allSignals = this.getAllUnfilteredRecordedSignals();
378
+ console.log(
379
+ 'Obtaining last all hz unfiltered recording from #allHzUnfilteredRecordings to send to server for processing'
380
+ );
381
+ const numSignals = allSignals.length;
382
+ const mls = this.#mlsBufferView;
383
+ const payload =
384
+ signalCsv && signalCsv.length > 0 ? csvToArray(signalCsv) : allSignals[numSignals - 1];
385
+ console.log('sending rec');
386
+ this.stepNum += 1;
387
+ console.log('send rec ' + this.stepNum);
388
+ this.status =
389
+ `All Hz Calibration Step: computing the IR of the last recording...`.toString() +
390
+ this.generateTemplate().toString();
391
+ this.emit('update', {message: this.status});
392
+ this.impulseResponses.push(
393
+ this.pyServerAPI
394
+ .getImpulseResponse({
395
+ sampleRate: this.sourceSamplingRate || 96000,
396
+ payload,
397
+ mls,
398
+ P: this.#P, //get rid of this
399
+ numPeriods: this.numMLSPerCapture,
400
+ })
401
+ .then(res => {
402
+ if (this.numSuccessfulCaptured < this.numCaptures) {
403
+ this.numSuccessfulCaptured += 1;
404
+ console.log('num succ capt: ' + this.numSuccessfulCaptured);
405
+ this.stepNum += 1;
406
+ console.log('got impulse response ' + this.stepNum);
407
+ this.incrementStatusBar();
408
+ this.status =
409
+ `All Hz Calibration: ${this.numSuccessfulCaptured}/${this.numCaptures} IRs computed...`.toString() +
410
+ this.generateTemplate().toString();
411
+ this.emit('update', {
412
+ message: this.status,
413
+ });
414
+ this.autocorrelations.push(res['autocorrelation']);
415
+ return res['ir'];
416
+ }
417
+ })
418
+ .catch(err => {
419
+ console.error(err);
420
+ })
421
+ );
422
+ };
423
+
424
+ /**
425
+ * Passed to the calibration steps function, awaits the desired amount of seconds to capture the desired number
426
+ * of MLS periods defined in the constructor.
427
+ *
428
+ * @example
429
+ */
430
+ #awaitDesiredMLSLength = async () => {
431
+ // seconds per MLS = P / SR
432
+ // await N * P / SR
433
+ this.stepNum += 1;
434
+ console.log('await desired length ' + this.stepNum);
435
+ this.status =
436
+ `All Hz Calibration: sampling the calibration signal...`.toString() +
437
+ `\niteration ${this.stepNum}` +
438
+ this.generateTemplate();
439
+ this.emit('update', {
440
+ message: this.status,
441
+ });
442
+ let time_to_wait = 0;
443
+ if (this.mode === 'unfiltered') {
444
+ //unfiltered
445
+ time_to_wait = (this.#mls.length / this.sourceSamplingRate) * this.numMLSPerCapture;
446
+ time_to_wait = time_to_wait * 1.1;
447
+ } else if (this.mode === 'filtered') {
448
+ //filtered
449
+ // time_to_wait =
450
+ // (this.#currentConvolution.length / this.sourceSamplingRate) *
451
+ // (this.numMLSPerCapture / (this.num_mls_to_skip + this.numMLSPerCapture));
452
+ time_to_wait =
453
+ (this.#currentConvolution.length / this.sourceSamplingRate) * this.numMLSPerCapture;
454
+ time_to_wait = time_to_wait * 1.1;
455
+ } else {
456
+ throw new Error('Mode broke in awaitDesiredMLSLength');
457
+ }
458
+
459
+ await sleep(time_to_wait);
460
+ };
461
+
462
+ /**
463
+ * Passed to the background noise recording function, awaits the desired amount of seconds to capture the desired number
464
+ * of seconds of background noise
465
+ *
466
+ * @example
467
+ */
468
+ #awaitBackgroundNoiseRecording = async () => {
469
+ console.log(
470
+ 'Waiting ' + this._calibrateSoundBackgroundSecs + ' second(s) to record background noise'
471
+ );
472
+ let time_to_wait = this._calibrateSoundBackgroundSecs + 0.5;
473
+ await sleep(time_to_wait);
474
+ };
475
+
476
+ /** .
477
+ * .
478
+ * .
479
+ * Passed to the calibration steps function, awaits the onset of the signal to ensure a steady state
480
+ *
481
+ * @example
482
+ */
483
+ #awaitSignalOnset = async () => {
484
+ this.stepNum += 1;
485
+ console.log('await signal onset ' + this.stepNum);
486
+ this.status =
487
+ `All Hz Calibration: waiting for the signal to stabilize...`.toString() +
488
+ this.generateTemplate();
489
+ this.emit('update', {
490
+ message: this.status,
491
+ });
492
+ let number_of_bursts_to_skip = this.num_mls_to_skip;
493
+ let time_to_sleep = 0;
494
+ if (this.mode === 'unfiltered') {
495
+ time_to_sleep = (this.#mls.length / this.sourceSamplingRate) * number_of_bursts_to_skip;
496
+ } else if (this.mode === 'filtered') {
497
+ console.log(this.#currentConvolution.length);
498
+ // time_to_sleep =
499
+ // (this.#currentConvolution.length / this.sourceSamplingRate) *
500
+ // (number_of_bursts_to_skip / (number_of_bursts_to_skip + this.numMLSPerCapture));
501
+ time_to_sleep =
502
+ (this.#currentConvolution.length / this.sourceSamplingRate) * number_of_bursts_to_skip;
503
+ } else {
504
+ throw new Error('Mode broke in awaitSignalOnset');
505
+ }
506
+ await sleep(time_to_sleep);
507
+ };
508
+
509
+ /**
510
+ * Called immediately after a recording is captured. Used to process the resulting signal
511
+ * whether by sending the result to a server or by computing a result locally.
512
+ *
513
+ * @example
514
+ */
515
+ #afterMLSRecord = () => {
516
+ console.log('after record');
517
+ this.sendRecordingToServerForProcessing();
518
+ };
519
+
520
+ #afterMLSwIIRRecord = () => {
521
+ if (this.numSuccessfulCaptured < 1) {
522
+ this.numSuccessfulCaptured += 1;
523
+ this.stepNum += 1;
524
+ this.incrementStatusBar();
525
+ console.log('after mls w iir record for some reason add numSucc capt ' + this.stepNum);
526
+ this.status =
527
+ `All Hz Calibration: ${this.numSuccessfulCaptured} recording of convolved MLS captured`.toString() +
528
+ this.generateTemplate().toString();
529
+ this.emit('update', {
530
+ message: this.status,
531
+ });
532
+ }
533
+ };
534
+
535
+ /** .
536
+ * .
537
+ * .
538
+ * Created an S Curver Buffer to taper the signal onset
539
+ *
540
+ * @param {*} length
541
+ * @param {*} phase
542
+ * @returns
543
+ * @example
544
+ */
545
+ static createSCurveBuffer = (length, phase) => {
546
+ const curve = new Float32Array(length);
547
+ let i;
548
+ for (i = 0; i < length; i += 1) {
549
+ // scale the curve to be between 0-1
550
+ curve[i] = Math.sin((Math.PI * i) / length - phase) / 2 + 0.5;
551
+ }
552
+ return curve;
553
+ };
554
+
555
+ static createInverseSCurveBuffer = (length, phase) => {
556
+ const curve = new Float32Array(length);
557
+ let i;
558
+ let j = length - 1;
559
+ for (i = 0; i < length; i += 1) {
560
+ // scale the curve to be between 0-1
561
+ curve[i] = Math.sin((Math.PI * j) / length - phase) / 2 + 0.5;
562
+ j -= 1;
563
+ }
564
+ return curve;
565
+ };
566
+
567
+ /**
568
+ * Construct a Calibration Node with the calibration parameters.
569
+ *
570
+ * @param dataBuffer
571
+ * @private
572
+ * @example
573
+ */
574
+ #createCalibrationNodeFromBuffer = dataBuffer => {
575
+ console.log('length databuffer');
576
+ console.log(dataBuffer.length);
577
+ if (!this.sourceAudioContext) {
578
+ this.makeNewSourceAudioContext();
579
+ }
580
+
581
+ const buffer = this.sourceAudioContext.createBuffer(
582
+ 1, // number of channels
583
+ dataBuffer.length,
584
+ this.sourceAudioContext.sampleRate // sample rate
585
+ );
586
+
587
+ const data = buffer.getChannelData(0); // get data
588
+
589
+ // fill the buffer with our data
590
+ try {
591
+ for (let i = 0; i < dataBuffer.length; i += 1) {
592
+ data[i] = dataBuffer[i];
593
+ }
594
+ } catch (error) {
595
+ console.error(error);
596
+ }
597
+
598
+ this.sourceNode = this.sourceAudioContext.createBufferSource();
599
+
600
+ this.sourceNode.buffer = buffer;
601
+
602
+ if (this.mode === 'filtered') {
603
+ //used to not loop filtered
604
+ this.sourceNode.loop = true;
605
+ } else {
606
+ this.sourceNode.loop = true;
607
+ }
608
+
609
+ this.sourceNode.connect(this.sourceAudioContext.destination);
610
+
611
+ this.addCalibrationNode(this.sourceNode);
612
+ };
613
+
614
+ /**
615
+ * Given a data buffer, creates the required calibration node
616
+ *
617
+ * @param {*} dataBufferArray
618
+ * @example
619
+ */
620
+ #setCalibrationNodesFromBuffer = (dataBufferArray = [this.#mlsBufferView]) => {
621
+ if (dataBufferArray.length === 1) {
622
+ this.#createCalibrationNodeFromBuffer(dataBufferArray[0]);
623
+ } else {
624
+ throw new Error('The length of the data buffer array must be 1');
625
+ }
626
+ };
627
+
628
+ /**
629
+ * Creates an audio context and plays it for a few seconds.
630
+ *
631
+ * @private
632
+ * @returns - Resolves when the audio is done playing.
633
+ * @example
634
+ */
635
+ #playCalibrationAudio = () => {
636
+ this.calibrationNodes[0].start(0);
637
+ this.status = ``;
638
+ if (this.mode === 'unfiltered') {
639
+ console.log('mls', this.#mls); // before multiplied by calibrateSoundBurstDb
640
+ console.log('mls buffer view', this.#mlsBufferView); // after multiplied by calibrateSoundBurstDb
641
+ console.log('play calibration audio ' + this.stepNum);
642
+ this.status =
643
+ `All Hz Calibration: playing the calibration tone...`.toString() +
644
+ this.generateTemplate().toString();
645
+ } else if (this.mode === 'filtered') {
646
+ console.log('play convolved audio ' + this.stepNum);
647
+ this.status =
648
+ `All Hz Calibration: playing the convolved calibration tone...`.toString() +
649
+ this.generateTemplate().toString();
650
+ } else {
651
+ throw new Error('Mode is incorrect');
652
+ }
653
+ this.emit('update', {message: this.status});
654
+ this.stepNum += 1;
655
+ console.log('sink sampling rate');
656
+ console.log(this.sinkSamplingRate);
657
+ console.log('source sampling rate');
658
+ console.log(this.sourceSamplingRate);
659
+ console.log('sample size');
660
+ console.log(this.sampleSize);
661
+ };
662
+
663
+ /** .
664
+ * .
665
+ * .
666
+ * Stops the audio with tapered offset
667
+ *
668
+ * @example
669
+ */
670
+ #stopCalibrationAudio = () => {
671
+ this.calibrationNodes[0].stop(0);
672
+ this.calibrationNodes = [];
673
+ this.sourceNode.disconnect();
674
+ this.stepNum += 1;
675
+ console.log('stop calibration audio ' + this.stepNum);
676
+ this.status =
677
+ `All Hz Calibration: stopping the calibration tone...`.toString() +
678
+ this.generateTemplate().toString();
679
+ this.emit('update', {message: this.status});
680
+ };
681
+
682
+ playMLSwithIIR = async (stream, convolution) => {
683
+ let checkRec = false;
684
+ this.mode = 'filtered';
685
+ console.log('play mls with iir');
686
+ //this.invertedImpulseResponse = iir
687
+
688
+ await this.calibrationSteps(
689
+ stream,
690
+ this.#playCalibrationAudio, // play audio func (required)
691
+ this.#createCalibrationNodeFromBuffer(convolution), // before play func
692
+ this.#awaitSignalOnset, // before record
693
+ () => this.numSuccessfulCaptured < 1,
694
+ this.#awaitDesiredMLSLength, // during record
695
+ this.#afterMLSwIIRRecord, // after record
696
+ this.mode,
697
+ checkRec
698
+ );
699
+ };
700
+
701
+ bothSoundCheck = async stream => {
702
+ let iir_ir_and_plots;
703
+ this.#currentConvolution = this.componentConvolution;
704
+ this.filteredMLSRange.component.Min = findMinValue(this.#currentConvolution);
705
+ this.filteredMLSRange.component.Max = findMaxValue(this.#currentConvolution);
706
+ this.addTimeStamp("Play MLS with component IIR");
707
+ await this.playMLSwithIIR(stream, this.#currentConvolution);
708
+ this.#stopCalibrationAudio();
709
+ let component_conv_recs = this.getAllFilteredRecordedSignals();
710
+ let return_component_conv_rec = component_conv_recs[0];
711
+ this.clearAllFilteredRecordedSignals();
712
+
713
+ this.#currentConvolution = this.testConvolution;
714
+ this.addTimeStamp("Play MLS with test IIR");
715
+ await this.playMLSwithIIR(stream, this.#currentConvolution);
716
+ this.#stopCalibrationAudio();
717
+ let test_conv_recs = this.getAllFilteredRecordedSignals();
718
+ let return_test_conv_rec = test_conv_recs[0];
719
+ this.clearAllFilteredRecordedSignals();
720
+
721
+ this.#currentConvolution = this.systemConvolution;
722
+ this.filteredMLSRange.system.Min = findMinValue(this.#currentConvolution);
723
+ this.filteredMLSRange.system.Max = findMaxValue(this.#currentConvolution);
724
+ this.addTimeStamp("Play MLS with system IIR");
725
+ await this.playMLSwithIIR(stream, this.#currentConvolution);
726
+ this.#stopCalibrationAudio();
727
+ let system_conv_recs = this.getAllFilteredRecordedSignals();
728
+ let return_system_conv_rec = system_conv_recs[0];
729
+
730
+ this.clearAllFilteredRecordedSignals();
731
+
732
+ this.sourceAudioContext.close();
733
+ let recs = this.getAllUnfilteredRecordedSignals();
734
+ let unconv_rec = recs[0];
735
+ let return_unconv_rec = unconv_rec;
736
+ let conv_rec = component_conv_recs[0];
737
+
738
+ //psd of component
739
+ let knownGain = this.oldComponentIR.Gain;
740
+ let knownFreq = this.oldComponentIR.Freq;
741
+ let sampleRate = this.sourceSamplingRate || 96000;
742
+ this.addTimeStamp("Get PSD of mls recording");
743
+ let component_unconv_rec_psd = await this.pyServerAPI
744
+ .getSubtractedPSDWithRetry(unconv_rec, knownGain, knownFreq, sampleRate)
745
+ .then(res => {
746
+ this.incrementStatusBar();
747
+ this.status =
748
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
749
+ this.generateTemplate().toString();
750
+ this.emit('update', {message: this.status});
751
+ return res;
752
+ })
753
+ .catch(err => {
754
+ console.error(err);
755
+ });
756
+
757
+ this.addTimeStamp("Get PSD of filtered recording (component)");
758
+ let component_conv_rec_psd = await this.pyServerAPI
759
+ .getSubtractedPSDWithRetry(conv_rec, knownGain, knownFreq, sampleRate)
760
+ .then(res => {
761
+ this.incrementStatusBar();
762
+ this.status =
763
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
764
+ this.generateTemplate().toString();
765
+ this.emit('update', {message: this.status});
766
+ return res;
767
+ })
768
+ .catch(err => {
769
+ console.error(err);
770
+ });
771
+
772
+ this.addTimeStamp("Get PSD of filtered recording (test component)");
773
+ let test_conv_rec_psd = await this.pyServerAPI
774
+ .getSubtractedPSDWithRetry(return_test_conv_rec, knownGain, knownFreq, sampleRate)
775
+ .then(res => {
776
+ this.incrementStatusBar();
777
+ this.status =
778
+ `All Hz Calibration: done computing testing PSD graphs...`.toString() +
779
+ this.generateTemplate().toString();
780
+ this.emit('update', {message: this.status});
781
+ return res;
782
+ })
783
+ .catch(err => {
784
+ console.error(err);
785
+ });
786
+
787
+ conv_rec = system_conv_recs[0];
788
+ //psd of system
789
+ this.addTimeStamp("Get PSD of filtered recording (system) and unfiltered recording");
790
+ let system_recs_psd = await this.pyServerAPI
791
+ .getPSDWithRetry({
792
+ unconv_rec,
793
+ conv_rec,
794
+ sampleRate: this.sourceSamplingRate || 96000,
795
+ })
796
+ .then(res => {
797
+ this.incrementStatusBar();
798
+ this.status =
799
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
800
+ this.generateTemplate().toString();
801
+ this.emit('update', {message: this.status});
802
+ return res;
803
+ })
804
+ .catch(err => {
805
+ console.error(err);
806
+ });
807
+
808
+ //iir w/ and without bandpass psd. done
809
+ unconv_rec = this.componentInvertedImpulseResponseNoBandpass;
810
+ conv_rec = this.componentInvertedImpulseResponse;
811
+ this.addTimeStamp("Get PSD of component iir and component iir no band pass");
812
+ let component_iir_psd = await this.pyServerAPI
813
+ .getPSDWithRetry({
814
+ unconv_rec,
815
+ conv_rec,
816
+ sampleRate: this.sourceSamplingRate || 96000,
817
+ })
818
+ .then(res => {
819
+ this.incrementStatusBar();
820
+ this.status =
821
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
822
+ this.generateTemplate().toString();
823
+ this.emit('update', {message: this.status});
824
+ return res;
825
+ })
826
+ .catch(err => {
827
+ console.error(err);
828
+ });
829
+ unconv_rec = this.systemInvertedImpulseResponseNoBandpass;
830
+ conv_rec = this.systemInvertedImpulseResponse;
831
+ this.addTimeStamp("Get PSD of system iir and system iir no band pass");
832
+ let system_iir_psd = await this.pyServerAPI
833
+ .getPSDWithRetry({
834
+ unconv_rec,
835
+ conv_rec,
836
+ sampleRate: this.sourceSamplingRate || 96000,
837
+ })
838
+ .then(res => {
839
+ this.incrementStatusBar();
840
+ this.status =
841
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
842
+ this.generateTemplate().toString();
843
+ this.emit('update', {message: this.status});
844
+ return res;
845
+ })
846
+ .catch(err => {
847
+ console.error(err);
848
+ });
849
+
850
+ this.addTimeStamp("Get PSD of mls sequence");
851
+ let mls_psd = await this.pyServerAPI
852
+ .getMLSPSDWithRetry({mls: this.#mlsBufferView, sampleRate: this.sourceSamplingRate || 96000})
853
+ .then(res => {
854
+ this.incrementStatusBar();
855
+ this.status =
856
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
857
+ this.generateTemplate().toString();
858
+ this.emit('update', {message: this.status});
859
+ return res;
860
+ })
861
+ .catch(err => {
862
+ console.error(err);
863
+ });
864
+
865
+
866
+ this.addTimeStamp("Get PSD of filered mls (system)");
867
+ let system_filtered_mls_psd = await this.pyServerAPI
868
+ .getMLSPSDWithRetry({
869
+ mls: this.systemConvolution,
870
+ sampleRate: this.sourceSamplingRate || 96000,
871
+ })
872
+ .then(res => {
873
+ this.incrementStatusBar();
874
+ this.status =
875
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
876
+ this.generateTemplate().toString();
877
+ this.emit('update', {message: this.status});
878
+ return res;
879
+ })
880
+ .catch(err => {
881
+ console.error(err);
882
+ });
883
+
884
+
885
+ this.addTimeStamp("Get PSD of filered mls (component)");
886
+ let component_filtered_mls_psd = await this.pyServerAPI
887
+ .getMLSPSDWithRetry({
888
+ mls: this.componentConvolution,
889
+ sampleRate: this.sourceSamplingRate || 96000,
890
+ })
891
+ .then(res => {
892
+ this.incrementStatusBar();
893
+ this.status =
894
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
895
+ this.generateTemplate().toString();
896
+ this.emit('update', {message: this.status});
897
+ return res;
898
+ })
899
+ .catch(err => {
900
+ console.error(err);
901
+ });
902
+
903
+ iir_ir_and_plots = {
904
+ filtered_recording: {
905
+ component: return_component_conv_rec,
906
+ system: return_system_conv_rec,
907
+ },
908
+ unfiltered_recording: this.getAllUnfilteredRecordedSignals()[0],
909
+ system: {
910
+ iir: this.systemInvertedImpulseResponse,
911
+ ir: this.systemIR,
912
+ iir_psd: {
913
+ y: system_iir_psd['y_conv'],
914
+ x: system_iir_psd['x_conv'],
915
+ y_no_bandpass: system_iir_psd['y_unconv'],
916
+ x_no_bandpass: system_iir_psd['x_unconv'],
917
+ },
918
+ filtered_mls_psd: {
919
+ x: system_filtered_mls_psd['x_mls'],
920
+ y: system_filtered_mls_psd['y_mls'],
921
+ },
922
+ convolution: this.systemConvolution,
923
+ psd: {
924
+ unconv: {
925
+ x: system_recs_psd['x_unconv'],
926
+ y: system_recs_psd['y_unconv'],
927
+ },
928
+ conv: {
929
+ x: system_recs_psd['x_conv'],
930
+ y: system_recs_psd['y_conv'],
931
+ },
932
+ },
933
+ },
934
+ component: {
935
+ iir: this.componentInvertedImpulseResponse,
936
+ ir: this.componentIR,
937
+ iir_psd: {
938
+ y: component_iir_psd['y_conv'],
939
+ x: component_iir_psd['x_conv'],
940
+ y_no_bandpass: component_iir_psd['y_unconv'],
941
+ x_no_bandpass: component_iir_psd['x_unconv'],
942
+ },
943
+ filtered_mls_psd: {
944
+ x: component_filtered_mls_psd['x_mls'],
945
+ y: component_filtered_mls_psd['y_mls'],
946
+ },
947
+ convolution: this.componentConvolution,
948
+ psd: {
949
+ unconv: {
950
+ x: component_unconv_rec_psd['x'],
951
+ y: component_unconv_rec_psd['y'],
952
+ },
953
+ conv: {
954
+ x: component_conv_rec_psd['x'],
955
+ y: component_conv_rec_psd['y'],
956
+ },
957
+ test: {
958
+ x: test_conv_rec_psd['x'],
959
+ y: test_conv_rec_psd['y'],
960
+ },
961
+ },
962
+ },
963
+ mls: this.#mlsBufferView,
964
+ mls_psd: {
965
+ x: mls_psd['x_mls'],
966
+ y: mls_psd['y_mls'],
967
+ },
968
+ autocorrelations: this.autocorrelations,
969
+ impulseResponses: [],
970
+ };
971
+
972
+ return iir_ir_and_plots;
973
+ };
974
+
975
+ singleSoundCheck = async stream => {
976
+ let iir_ir_and_plots;
977
+ if (this._calibrateSoundCheck != 'system') {
978
+ this.#currentConvolution = this.componentConvolution;
979
+ this.filteredMLSRange.component.Min = findMinValue(this.#currentConvolution);
980
+ this.filteredMLSRange.component.Max = findMaxValue(this.#currentConvolution);
981
+ this.addTimeStamp("Play MLS with component IIR");
982
+ } else {
983
+ this.#currentConvolution = this.systemConvolution;
984
+ this.filteredMLSRange.system.Min = findMinValue(this.#currentConvolution);
985
+ this.filteredMLSRange.system.Max = findMaxValue(this.#currentConvolution);
986
+ this.addTimeStamp("Play MLS with system IIR");
987
+ }
988
+ await this.playMLSwithIIR(stream, this.#currentConvolution);
989
+ this.#stopCalibrationAudio();
990
+ let conv_recs = this.getAllFilteredRecordedSignals();
991
+ let recs = this.getAllUnfilteredRecordedSignals();
992
+ this.clearAllFilteredRecordedSignals();
993
+ console.log('Obtaining unfiltered recording from #allHzUnfilteredRecordings to calculate PSD');
994
+ console.log('Obtaining filtered recording from #allHzFilteredRecordings to calculate PSD');
995
+ let unconv_rec = recs[0];
996
+ let return_unconv_rec = unconv_rec;
997
+ let conv_rec = conv_recs[0];
998
+ let return_conv_rec = conv_rec;
999
+ if (this._calibrateSoundCheck != 'system') {
1000
+ this.addTimeStamp("Play MLS with test IIR");
1001
+ this.#currentConvolution = this.testConvolution;
1002
+ await this.playMLSwithIIR(stream, this.#currentConvolution);
1003
+ this.#stopCalibrationAudio();
1004
+ }
1005
+ let test_conv_recs = this.getAllFilteredRecordedSignals();
1006
+ let return_test_conv_rec = test_conv_recs[0];
1007
+ this.sourceAudioContext.close();
1008
+ if (this._calibrateSoundCheck != 'system') {
1009
+ let knownGain = this.oldComponentIR.Gain;
1010
+ let knownFreq = this.oldComponentIR.Freq;
1011
+ let sampleRate = this.sourceSamplingRate || 96000;
1012
+ this.addTimeStamp("Get PSD of mls recording");
1013
+ let unconv_results = await this.pyServerAPI
1014
+ .getSubtractedPSDWithRetry(unconv_rec, knownGain, knownFreq, sampleRate)
1015
+ .then(res => {
1016
+ this.incrementStatusBar();
1017
+ this.status =
1018
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
1019
+ this.generateTemplate().toString();
1020
+ this.emit('update', {message: this.status});
1021
+ return res;
1022
+ })
1023
+ .catch(err => {
1024
+ console.error(err);
1025
+ });
1026
+
1027
+ this.addTimeStamp("Get PSD recording of filtered recording (component)");
1028
+ let conv_results = await this.pyServerAPI
1029
+ .getSubtractedPSDWithRetry(conv_rec, knownGain, knownFreq, sampleRate)
1030
+ .then(res => {
1031
+ this.incrementStatusBar();
1032
+ this.status =
1033
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
1034
+ this.generateTemplate().toString();
1035
+ this.emit('update', {message: this.status});
1036
+ return res;
1037
+ })
1038
+ .catch(err => {
1039
+ console.error(err);
1040
+ });
1041
+
1042
+ this.addTimeStamp("Get PSD of filtered recording (test component)");
1043
+ let test_conv_results = await this.pyServerAPI
1044
+ .getSubtractedPSDWithRetry(return_test_conv_rec, knownGain, knownFreq, sampleRate)
1045
+ .then(res => {
1046
+ this.incrementStatusBar();
1047
+ this.status =
1048
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
1049
+ this.generateTemplate().toString();
1050
+ this.emit('update', {message: this.status});
1051
+ return res;
1052
+ })
1053
+ .catch(err => {
1054
+ console.error(err);
1055
+ });
1056
+
1057
+ unconv_rec = this.componentInvertedImpulseResponseNoBandpass;
1058
+ conv_rec = this.componentInvertedImpulseResponse;
1059
+ this.addTimeStamp("Get PSD of component iir and component iir no bandpass");
1060
+ let component_iir_psd = await this.pyServerAPI
1061
+ .getPSDWithRetry({
1062
+ unconv_rec,
1063
+ conv_rec,
1064
+ sampleRate: this.sourceSamplingRate || 96000,
1065
+ })
1066
+ .then(res => {
1067
+ this.incrementStatusBar();
1068
+ this.status =
1069
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
1070
+ this.generateTemplate().toString();
1071
+ this.emit('update', {message: this.status});
1072
+ return res;
1073
+ })
1074
+ .catch(err => {
1075
+ console.error(err);
1076
+ });
1077
+ unconv_rec = this.systemInvertedImpulseResponseNoBandpass;
1078
+ conv_rec = this.systemInvertedImpulseResponse;
1079
+ this.addTimeStamp("Get PSD of system iir and system iir no bandpass");
1080
+ let system_iir_psd = await this.pyServerAPI
1081
+ .getPSDWithRetry({
1082
+ unconv_rec,
1083
+ conv_rec,
1084
+ sampleRate: this.sourceSamplingRate || 96000,
1085
+ })
1086
+ .then(res => {
1087
+ this.incrementStatusBar();
1088
+ this.status =
1089
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
1090
+ this.generateTemplate().toString();
1091
+ this.emit('update', {message: this.status});
1092
+ return res;
1093
+ })
1094
+ .catch(err => {
1095
+ console.error(err);
1096
+ });
1097
+
1098
+ this.addTimeStamp("Get PSD of mls sequence");
1099
+ let mls_psd = await this.pyServerAPI
1100
+ .getMLSPSDWithRetry({
1101
+ mls: this.#mlsBufferView,
1102
+ sampleRate: this.sourceSamplingRate || 96000,
1103
+ })
1104
+ .then(res => {
1105
+ this.incrementStatusBar();
1106
+ this.status =
1107
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
1108
+ this.generateTemplate().toString();
1109
+ this.emit('update', {message: this.status});
1110
+ return res;
1111
+ })
1112
+ .catch(err => {
1113
+ console.error(err);
1114
+ });
1115
+
1116
+ this.addTimeStamp("Get PSD of filtered mls (component)");
1117
+ let filtered_mls_psd = await this.pyServerAPI
1118
+ .getMLSPSDWithRetry({
1119
+ mls: this.componentConvolution,
1120
+ sampleRate: this.sourceSamplingRate || 96000,
1121
+ })
1122
+ .then(res => {
1123
+ this.incrementStatusBar();
1124
+ this.status =
1125
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
1126
+ this.generateTemplate().toString();
1127
+ this.emit('update', {message: this.status});
1128
+ return res;
1129
+ })
1130
+ .catch(err => {
1131
+ console.error(err);
1132
+ });
1133
+
1134
+ iir_ir_and_plots = {
1135
+ unfiltered_recording: return_unconv_rec,
1136
+ filtered_recording: return_conv_rec,
1137
+ system: {
1138
+ iir: this.systemInvertedImpulseResponse,
1139
+ ir: this.systemIR,
1140
+ iir_psd: {
1141
+ y: system_iir_psd['y_conv'],
1142
+ x: system_iir_psd['y_conv'],
1143
+ y_no_bandpass: system_iir_psd['y_unconv'],
1144
+ x_no_bandpass: system_iir_psd['x_unconv'],
1145
+ },
1146
+ filtered_recording: [],
1147
+ filtered_mls_psd: {},
1148
+ convolution: this.systemConvolution,
1149
+ psd: {
1150
+ unconv: {
1151
+ x: [],
1152
+ y: [],
1153
+ },
1154
+ conv: {
1155
+ x: [],
1156
+ y: [],
1157
+ },
1158
+ },
1159
+ },
1160
+ component: {
1161
+ iir: this.componentInvertedImpulseResponse,
1162
+ ir: this.componentIR,
1163
+ iir_psd: {
1164
+ y: component_iir_psd['y_conv'],
1165
+ x: component_iir_psd['x_conv'],
1166
+ y_no_bandpass: component_iir_psd['y_unconv'],
1167
+ x_no_bandpass: component_iir_psd['x_unconv'],
1168
+ },
1169
+ filtered_mls_psd: {
1170
+ x: filtered_mls_psd['x_mls'],
1171
+ y: filtered_mls_psd['y_mls'],
1172
+ },
1173
+ convolution: this.componentConvolution,
1174
+ psd: {
1175
+ unconv: {
1176
+ x: unconv_results['x'],
1177
+ y: unconv_results['y'],
1178
+ },
1179
+ conv: {
1180
+ x: conv_results['x'],
1181
+ y: conv_results['y'],
1182
+ },
1183
+ test: {
1184
+ x: test_conv_results['x'],
1185
+ y: test_conv_results['y'],
1186
+ },
1187
+ },
1188
+ },
1189
+ mls: this.#mlsBufferView,
1190
+ mls_psd: {
1191
+ x: mls_psd['x_mls'],
1192
+ y: mls_psd['y_mls'],
1193
+ },
1194
+ autocorrelations: this.autocorrelations,
1195
+ impulseResponses: [],
1196
+ };
1197
+ } else {
1198
+ this.addTimeStamp("Get PSD of filtered recording (system) and unfiltered recording");
1199
+ let results = await this.pyServerAPI
1200
+ .getPSDWithRetry({
1201
+ unconv_rec,
1202
+ conv_rec,
1203
+ sampleRate: this.sourceSamplingRate || 96000,
1204
+ })
1205
+ .then(res => {
1206
+ this.incrementStatusBar();
1207
+ this.status =
1208
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
1209
+ this.generateTemplate().toString();
1210
+ this.emit('update', {message: this.status});
1211
+ return res;
1212
+ })
1213
+ .catch(err => {
1214
+ console.error(err);
1215
+ });
1216
+
1217
+ //iir w/ and without bandpass psd
1218
+ unconv_rec = this.componentInvertedImpulseResponseNoBandpass;
1219
+ conv_rec = this.componentInvertedImpulseResponse;
1220
+ this.addTimeStamp("Get PSD of component iir and component iir no band pass");
1221
+ let component_iir_psd = await this.pyServerAPI
1222
+ .getPSDWithRetry({
1223
+ unconv_rec,
1224
+ conv_rec,
1225
+ sampleRate: this.sourceSamplingRate || 96000,
1226
+ })
1227
+ .then(res => {
1228
+ this.incrementStatusBar();
1229
+ this.status =
1230
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
1231
+ this.generateTemplate().toString();
1232
+ this.emit('update', {message: this.status});
1233
+ return res;
1234
+ })
1235
+ .catch(err => {
1236
+ console.error(err);
1237
+ });
1238
+ unconv_rec = this.systemInvertedImpulseResponseNoBandpass;
1239
+ conv_rec = this.systemInvertedImpulseResponse;
1240
+ this.addTimeStamp("Get PSD of system iir and system iir no band pass");
1241
+ let system_iir_psd = await this.pyServerAPI
1242
+ .getPSDWithRetry({
1243
+ unconv_rec,
1244
+ conv_rec,
1245
+ sampleRate: this.sourceSamplingRate || 96000,
1246
+ })
1247
+ .then(res => {
1248
+ this.incrementStatusBar();
1249
+ this.status =
1250
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
1251
+ this.generateTemplate().toString();
1252
+ this.emit('update', {message: this.status});
1253
+ return res;
1254
+ })
1255
+ .catch(err => {
1256
+ console.error(err);
1257
+ });
1258
+
1259
+ this.addTimeStamp("Get PSD of mls sequence");
1260
+ let mls_psd = await this.pyServerAPI
1261
+ .getMLSPSDWithRetry({
1262
+ mls: this.#mlsBufferView,
1263
+ sampleRate: this.sourceSamplingRate || 96000,
1264
+ })
1265
+ .then(res => {
1266
+ this.incrementStatusBar();
1267
+ this.status =
1268
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
1269
+ this.generateTemplate().toString();
1270
+ this.emit('update', {message: this.status});
1271
+ return res;
1272
+ })
1273
+ .catch(err => {
1274
+ console.error(err);
1275
+ });
1276
+
1277
+
1278
+ this.addTimeStamp("Get PSD of filtered mls (system)");
1279
+ let filtered_mls_psd = await this.pyServerAPI
1280
+ .getMLSPSDWithRetry({
1281
+ mls: this.systemConvolution,
1282
+ sampleRate: this.sourceSamplingRate || 96000,
1283
+ })
1284
+ .then(res => {
1285
+ this.incrementStatusBar();
1286
+ this.status =
1287
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
1288
+ this.generateTemplate().toString();
1289
+ this.emit('update', {message: this.status});
1290
+ return res;
1291
+ })
1292
+ .catch(err => {
1293
+ console.error(err);
1294
+ });
1295
+
1296
+ iir_ir_and_plots = {
1297
+ unfiltered_recording: return_unconv_rec,
1298
+ filtered_recording: return_conv_rec,
1299
+ system: {
1300
+ iir: this.systemInvertedImpulseResponse,
1301
+ ir: this.systemIR,
1302
+ iir_psd: {
1303
+ y: system_iir_psd['y_conv'],
1304
+ x: system_iir_psd['y_conv'],
1305
+ y_no_bandpass: system_iir_psd['y_unconv'],
1306
+ x_no_bandpass: system_iir_psd['x_unconv'],
1307
+ },
1308
+ filtered_recording: [],
1309
+ filtered_mls_psd: {
1310
+ x: filtered_mls_psd['x_mls'],
1311
+ y: filtered_mls_psd['y_mls'],
1312
+ },
1313
+ convolution: this.systemConvolution,
1314
+ psd: {
1315
+ unconv: {
1316
+ x: results['x_unconv'],
1317
+ y: results['y_unconv'],
1318
+ },
1319
+ conv: {
1320
+ x: results['x_conv'],
1321
+ y: results['y_conv'],
1322
+ },
1323
+ },
1324
+ },
1325
+ component: {
1326
+ iir: this.componentInvertedImpulseResponse,
1327
+ ir: this.componentIR,
1328
+ iir_psd: {
1329
+ y: component_iir_psd['y_conv'],
1330
+ x: component_iir_psd['x_conv'],
1331
+ y_no_bandpass: component_iir_psd['y_unconv'],
1332
+ x_no_bandpass: component_iir_psd['x_unconv'],
1333
+ },
1334
+ filtered_mls_psd: {},
1335
+ convolution: this.componentConvolution,
1336
+ psd: {
1337
+ unconv: {
1338
+ x: [],
1339
+ y: [],
1340
+ },
1341
+ conv: {
1342
+ x: [],
1343
+ y: [],
1344
+ },
1345
+ test: {
1346
+ x: [],
1347
+ y: [],
1348
+ },
1349
+ },
1350
+ },
1351
+ mls: this.#mlsBufferView,
1352
+ mls_psd: {
1353
+ x: mls_psd['x_mls'],
1354
+ y: mls_psd['y_mls'],
1355
+ },
1356
+ autocorrelations: this.autocorrelations,
1357
+ impulseResponses: [],
1358
+ };
1359
+ }
1360
+ await Promise.all(this.impulseResponses).then(res => {
1361
+ for (let i = 0; i < res.length; i++) {
1362
+ if (res[i] != undefined) {
1363
+ iir_ir_and_plots['impulseResponses'].push(res[i]);
1364
+ }
1365
+ }
1366
+ });
1367
+
1368
+ if (this.#download) {
1369
+ this.downloadSingleUnfilteredRecording();
1370
+ this.downloadSingleFilteredRecording();
1371
+ saveToCSV(this.#mls, 'MLS.csv');
1372
+ saveToCSV(this.componentConvolution, 'python_component_convolution_mls_iir.csv');
1373
+ saveToCSV(this.systemConvolution, 'python_system_convolution_mls_iir.csv');
1374
+ saveToCSV(this.componentInvertedImpulseResponse, 'componentIIR.csv');
1375
+ saveToCSV(this.systemInvertedImpulseResponse, 'systemIIR.csv');
1376
+ for (let i = 0; i < this.autocorrelations.length; i++) {
1377
+ saveToCSV(this.autocorrelations[i], `autocorrelation_${i}`);
1378
+ }
1379
+ const computedIRagain = await Promise.all(this.impulseResponses).then(res => {
1380
+ for (let i = 0; i < res.length; i++) {
1381
+ if (res[i] != undefined) {
1382
+ saveToCSV(res[i], `IR_${i}`);
1383
+ }
1384
+ }
1385
+ });
1386
+ }
1387
+
1388
+ return iir_ir_and_plots;
1389
+ };
1390
+
1391
+ /**
1392
+ * Public method to start the calibration process. Objects intialized from webassembly allocate new memory
1393
+ * and must be manually freed. This function is responsible for intializing the MlsGenInterface,
1394
+ * and wrapping the calibration steps with a garbage collection safe gaurd.
1395
+ *
1396
+ * @public
1397
+ * @param stream - The stream of audio from the Listener.
1398
+ * @example
1399
+ */
1400
+ startCalibrationImpulseResponse = async stream => {
1401
+
1402
+
1403
+ let desired_time = this.desired_time_per_mls;
1404
+ let checkRec = 'allhz';
1405
+
1406
+ console.log('MLS sequence should be of length: ' + this.sourceSamplingRate * desired_time);
1407
+
1408
+ length = this.sourceSamplingRate * desired_time;
1409
+ //get mls here
1410
+ const calibrateSoundBurstDb = this._calibrateSoundBurstDb;
1411
+ this.addTimeStamp("Get MLS sequence");
1412
+ await this.pyServerAPI
1413
+ .getMLSWithRetry({length, calibrateSoundBurstDb})
1414
+ .then(res => {
1415
+ console.log(res);
1416
+ this.#mlsBufferView = res['mls'];
1417
+ this.#mls = res['unscaledMLS'];
1418
+ })
1419
+ .catch(err => {
1420
+ // this.emit('InvertedImpulseResponse', {res: false});
1421
+ console.error(err);
1422
+ });
1423
+ this.numSuccessfulBackgroundCaptured = 0;
1424
+ if (this._calibrateSoundBackgroundSecs > 0) {
1425
+ this.mode = 'background';
1426
+ this.status =
1427
+ `All Hz Calibration: sampling the background noise...`.toString() +
1428
+ this.generateTemplate().toString();
1429
+ this.emit('update', {message: this.status});
1430
+ await this.recordBackground(
1431
+ stream, //stream
1432
+ () => this.numSuccessfulBackgroundCaptured < 1, //loop condition
1433
+ this.#awaitBackgroundNoiseRecording, //sleep to record
1434
+ this.sendBackgroundRecording, //send to get PSD
1435
+ this.mode,
1436
+ checkRec
1437
+ );
1438
+ this.incrementStatusBar();
1439
+ }
1440
+ this.mode = 'unfiltered';
1441
+ this.numSuccessfulCaptured = 0;
1442
+
1443
+ await this.calibrationSteps(
1444
+ stream,
1445
+ this.#playCalibrationAudio, // play audio func (required)
1446
+ this.#createCalibrationNodeFromBuffer(this.#mlsBufferView), // before play func
1447
+ this.#awaitSignalOnset, // before record
1448
+ () => this.numSuccessfulCaptured < this.numCaptures, // loop while true
1449
+ this.#awaitDesiredMLSLength, // during record
1450
+ this.#afterMLSRecord, // after record
1451
+ this.mode,
1452
+ checkRec
1453
+ ),
1454
+ this.#stopCalibrationAudio();
1455
+ checkRec = false;
1456
+
1457
+ // at this stage we've captured all the required signals,
1458
+ // and have received IRs for each one
1459
+ // so let's send all the IRs to the server to be converted to a single IIR
1460
+ await this.sendSystemImpulseResponsesToServerForProcessing();
1461
+ await this.sendComponentImpulseResponsesToServerForProcessing();
1462
+
1463
+ this.numSuccessfulCaptured = 0;
1464
+
1465
+ let iir_ir_and_plots;
1466
+ if (this._calibrateSoundCheck != 'none') {
1467
+ //do single check
1468
+ if (this._calibrateSoundCheck == 'goal' || this._calibrateSoundCheck == 'system') {
1469
+ iir_ir_and_plots = await this.singleSoundCheck(stream);
1470
+ } else {
1471
+ //both
1472
+ iir_ir_and_plots = await this.bothSoundCheck(stream);
1473
+ }
1474
+ } else {
1475
+ let unconv_rec = this.componentInvertedImpulseResponseNoBandpass;
1476
+ let conv_rec = this.componentInvertedImpulseResponse;
1477
+ let component_iir_psd = await this.pyServerAPI
1478
+ .getPSDWithRetry({
1479
+ unconv_rec,
1480
+ conv_rec,
1481
+ sampleRate: this.sourceSamplingRate || 96000,
1482
+ })
1483
+ .then(res => {
1484
+ this.incrementStatusBar();
1485
+ this.status =
1486
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
1487
+ this.generateTemplate().toString();
1488
+ this.emit('update', {message: this.status});
1489
+ return res;
1490
+ })
1491
+ .catch(err => {
1492
+ console.error(err);
1493
+ });
1494
+ unconv_rec = this.systemInvertedImpulseResponseNoBandpass;
1495
+ conv_rec = this.systemInvertedImpulseResponse;
1496
+ let system_iir_psd = await this.pyServerAPI
1497
+ .getPSDWithRetry({
1498
+ unconv_rec,
1499
+ conv_rec,
1500
+ sampleRate: this.sourceSamplingRate || 96000,
1501
+ })
1502
+ .then(res => {
1503
+ this.incrementStatusBar();
1504
+ this.status =
1505
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
1506
+ this.generateTemplate().toString();
1507
+ this.emit('update', {message: this.status});
1508
+ return res;
1509
+ })
1510
+ .catch(err => {
1511
+ console.error(err);
1512
+ });
1513
+
1514
+ iir_ir_and_plots = {
1515
+ unfiltered_recording: return_unconv_rec,
1516
+ filtered_recording: return_conv_rec,
1517
+ system: {
1518
+ iir: this.systemInvertedImpulseResponse,
1519
+ ir: this.systemIR,
1520
+ iir_psd: {
1521
+ y: system_iir_psd['y_conv'],
1522
+ x: system_iir_psd['y_conv'],
1523
+ y_no_bandpass: system_iir_psd['y_unconv'],
1524
+ x_no_bandpass: system_iir_psd['x_unconv'],
1525
+ },
1526
+ filtered_recording: [],
1527
+ convolution: this.systemConvolution,
1528
+ psd: {
1529
+ unconv: {
1530
+ x: [],
1531
+ y: [],
1532
+ },
1533
+ conv: {
1534
+ x: [],
1535
+ y: [],
1536
+ },
1537
+ },
1538
+ },
1539
+ component: {
1540
+ iir: this.componentInvertedImpulseResponse,
1541
+ ir: this.componentIR,
1542
+ iir_psd: {
1543
+ y: component_iir_psd['y_conv'],
1544
+ x: component_iir_psd['x_conv'],
1545
+ y_no_bandpass: component_iir_psd['y_unconv'],
1546
+ x_no_bandpass: component_iir_psd['x_unconv'],
1547
+ },
1548
+ convolution: this.componentConvolution,
1549
+ psd: {
1550
+ unconv: {
1551
+ x: [],
1552
+ y: [],
1553
+ },
1554
+ conv: {
1555
+ x: [],
1556
+ y: [],
1557
+ },
1558
+ },
1559
+ },
1560
+ mls: this.#mlsBufferView,
1561
+ autocorrelations: this.autocorrelations,
1562
+ impulseResponses: [],
1563
+ };
1564
+ await Promise.all(this.impulseResponses).then(res => {
1565
+ for (let i = 0; i < res.length; i++) {
1566
+ if (res[i] != undefined) {
1567
+ iir_ir_and_plots['impulseResponses'].push(res[i]);
1568
+ }
1569
+ }
1570
+ });
1571
+
1572
+ if (this.#download) {
1573
+ saveToCSV(this.#mls, 'MLS.csv');
1574
+ saveToCSV(this.componentConvolution, 'python_component_convolution_mls_iir.csv');
1575
+ saveToCSV(this.systemConvolution, 'python_system_convolution_mls_iir.csv');
1576
+ saveToCSV(this.componentInvertedImpulseResponse, 'componentIIR.csv');
1577
+ saveToCSV(this.systemInvertedImpulseResponse, 'systemIIR.csv');
1578
+ for (let i = 0; i < this.autocorrelations.length; i++) {
1579
+ saveToCSV(this.autocorrelations[i], `autocorrelation_${i}`);
1580
+ }
1581
+ const computedIRagain = await Promise.all(this.impulseResponses).then(res => {
1582
+ for (let i = 0; i < res.length; i++) {
1583
+ if (res[i] != undefined) {
1584
+ saveToCSV(res[i], `IR_${i}`);
1585
+ }
1586
+ }
1587
+ });
1588
+ }
1589
+ }
1590
+
1591
+ this.percent_complete = 100;
1592
+
1593
+ this.status = `All Hz Calibration: Finished`.toString() + this.generateTemplate().toString();
1594
+ this.emit('update', {message: this.status});
1595
+
1596
+ //here after calibration we have the component calibration (either loudspeaker or microphone) in the same form as the componentIR
1597
+ //that was used to calibrate
1598
+ // saveToJSON(iir_ir_and_plots);
1599
+ return iir_ir_and_plots;
1600
+ };
1601
+
1602
+ //////////////////////volume
1603
+
1604
+ handleIncomingData = data => {
1605
+ console.log('Received data: ', data);
1606
+ if (data.type === 'soundGainDBSPL') {
1607
+ this.soundGainDBSPL = data.value;
1608
+ } else {
1609
+ throw new Error(`Unknown data type: ${data.type}`);
1610
+ }
1611
+ };
1612
+ createSCurveBuffer = (onSetBool = true) => {
1613
+ const curve = new Float32Array(this.TAPER_SECS * this.sourceSamplingRate + 1);
1614
+ const frequency = 1 / (4 * this.TAPER_SECS);
1615
+ let j = 0;
1616
+ for (let i = 0; i < this.TAPER_SECS * this.sourceSamplingRate + 1; i += 1) {
1617
+ const phase = 2 * Math.PI * frequency * j;
1618
+ const onsetTaper = Math.pow(Math.sin(phase), 2);
1619
+ const offsetTaper = Math.pow(Math.cos(phase), 2);
1620
+ curve[i] = onSetBool ? onsetTaper : offsetTaper;
1621
+ j += 1 / this.sourceSamplingRate;
1622
+ }
1623
+ return curve;
1624
+ };
1625
+
1626
+ #getTruncatedSignal = (left = 3.5, right = 4.5) => {
1627
+ const start = Math.floor(left * this.sourceSamplingRate);
1628
+ const end = Math.floor(right * this.sourceSamplingRate);
1629
+ const result = Array.from(this.getLastVolumeRecordedSignal().slice(start, end));
1630
+ console.log(
1631
+ 'Obtaining last 1000 hz recording from #allVolumeRecordings to send for processing'
1632
+ );
1633
+ /**
1634
+ * function to check that capture was properly made
1635
+ * @param {*} list
1636
+ */
1637
+ const checkResult = list => {
1638
+ const setItem = new Set(list);
1639
+ if (setItem.size === 1 && setItem.has(0)) {
1640
+ console.warn(
1641
+ 'The last capture failed, all recorded signal is zero',
1642
+ this.getAllVolumeRecordedSignals()
1643
+ );
1644
+ }
1645
+ if (setItem.size === 0) {
1646
+ console.warn('The last capture failed, no recorded signal');
1647
+ }
1648
+ };
1649
+ checkResult(result);
1650
+ return result;
1651
+ };
1652
+
1653
+ /**
1654
+ *
1655
+ *
1656
+ Construct a calibration Node with the calibration parameters and given gain value
1657
+ * @param {*} gainValue
1658
+ * */
1659
+ #createCalibrationToneWithGainValue = gainValue => {
1660
+ const audioContext = this.makeNewSourceAudioContext();
1661
+ const oscilator = audioContext.createOscillator();
1662
+ const gainNode = audioContext.createGain();
1663
+ const taperGainNode = audioContext.createGain();
1664
+ const offsetGainNode = audioContext.createGain();
1665
+ const totalDuration = this.CALIBRATION_TONE_DURATION * 1.2;
1666
+
1667
+ oscilator.frequency.value = this.#CALIBRATION_TONE_FREQUENCY;
1668
+ oscilator.type = this.#CALIBRATION_TONE_TYPE;
1669
+ gainNode.gain.value = gainValue;
1670
+
1671
+ oscilator.connect(gainNode);
1672
+ gainNode.connect(taperGainNode);
1673
+ const onsetCurve = this.createSCurveBuffer();
1674
+ taperGainNode.gain.setValueCurveAtTime(onsetCurve, 0, this.TAPER_SECS);
1675
+ taperGainNode.connect(offsetGainNode);
1676
+ const offsetCurve = this.createSCurveBuffer(false);
1677
+ offsetGainNode.gain.setValueCurveAtTime(
1678
+ offsetCurve,
1679
+ totalDuration - this.TAPER_SECS,
1680
+ this.TAPER_SECS
1681
+ );
1682
+ offsetGainNode.connect(audioContext.destination);
1683
+
1684
+ this.addCalibrationNode(oscilator);
1685
+ };
1686
+
1687
+ /**
1688
+ * Construct a Calibration Node with the calibration parameters.
1689
+ *
1690
+ * @private
1691
+ * @example
1692
+ */
1693
+ #createCalibrationNode = () => {
1694
+ const audioContext = this.makeNewSourceAudioContext();
1695
+ const oscilator = audioContext.createOscillator();
1696
+ const gainNode = audioContext.createGain();
1697
+
1698
+ oscilator.frequency.value = this.#CALIBRATION_TONE_FREQUENCY;
1699
+ oscilator.type = this.#CALIBRATION_TONE_TYPE;
1700
+ gainNode.gain.value = 0.04;
1701
+
1702
+ oscilator.connect(gainNode);
1703
+ gainNode.connect(audioContext.destination);
1704
+
1705
+ this.addCalibrationNode(oscilator);
1706
+ };
1707
+
1708
+ #playCalibrationAudioVolume = async () => {
1709
+ const totalDuration = this.CALIBRATION_TONE_DURATION * 1.2;
1710
+
1711
+ this.calibrationNodes[0].start(0);
1712
+ this.calibrationNodes[0].stop(totalDuration);
1713
+ console.log(`Playing a buffer of ${this.CALIBRATION_TONE_DURATION} seconds of audio`);
1714
+ console.log(`Waiting a total of ${totalDuration} seconds`);
1715
+ await sleep(totalDuration);
1716
+ };
1717
+
1718
+ #sendToServerForProcessing = lCalib => {
1719
+ console.log('Sending data to server');
1720
+ this.addTimeStamp("Send volume data to server");
1721
+ let left = this.calibrateSound1000HzPreSec;
1722
+ let right = this.calibrateSound1000HzPreSec + this.calibrateSound1000HzSec;
1723
+ this.pyServerAPI
1724
+ .getVolumeCalibration({
1725
+ sampleRate: this.sourceSamplingRate,
1726
+ payload: this.#getTruncatedSignal(left, right),
1727
+ lCalib: lCalib,
1728
+ })
1729
+ .then(res => {
1730
+ if (this.outDBSPL === null) {
1731
+ this.incrementStatusBar();
1732
+ this.outDBSPL = res['outDbSPL'];
1733
+ this.outDBSPL1000 = res['outDbSPL1000'];
1734
+ this.THD = res['thd'];
1735
+ }
1736
+ })
1737
+ .catch(err => {
1738
+ console.warn(err);
1739
+ });
1740
+ };
1741
+
1742
+ startCalibrationVolume = async (stream, gainValues, lCalib, componentGainDBSPL) => {
1743
+ const trialIterations = gainValues.length;
1744
+ this.status_denominator += trialIterations;
1745
+ const thdValues = [];
1746
+ const inDBValues = [];
1747
+ let inDB = 0;
1748
+ const outDBSPLValues = [];
1749
+ const outDBSPL1000Values = [];
1750
+ let checkRec = false;
1751
+
1752
+ // do one calibration that will be discarded
1753
+ const soundLevelToDiscard = -60;
1754
+ const gainToDiscard = Math.pow(10, soundLevelToDiscard / 20);
1755
+ this.status =
1756
+ `1000 Hz Calibration: Sound Level ${soundLevelToDiscard} dB`.toString() +
1757
+ this.generateTemplate().toString();
1758
+ //this.emit('update', {message: `1000 Hz Calibration: Sound Level ${soundLevelToDiscard} dB`});
1759
+ this.emit('update', {message: this.status});
1760
+ this.startTime = new Date().getTime();
1761
+
1762
+ do {
1763
+ // eslint-disable-next-line no-await-in-loop
1764
+ await this.volumeCalibrationSteps(
1765
+ stream,
1766
+ this.#playCalibrationAudioVolume,
1767
+ this.#createCalibrationToneWithGainValue,
1768
+ this.#sendToServerForProcessing,
1769
+ gainToDiscard,
1770
+ lCalib, //todo make this a class parameter
1771
+ checkRec
1772
+ );
1773
+ } while (this.outDBSPL === null);
1774
+ //reset the values
1775
+ //this.incrementStatusBar();
1776
+
1777
+ this.outDBSPL = null;
1778
+ this.outDBSPL = null;
1779
+ this.outDBSPL1000 = null;
1780
+ this.THD = null;
1781
+
1782
+ // run the calibration at different gain values provided by the user
1783
+ for (let i = 0; i < trialIterations; i++) {
1784
+ //convert gain to DB and add to inDB
1785
+ if (i == trialIterations - 1) {
1786
+ checkRec = 'loudest';
1787
+ }
1788
+ inDB = Math.log10(gainValues[i]) * 20;
1789
+ // precision to 1 decimal place
1790
+ inDB = Math.round(inDB * 10) / 10;
1791
+ inDBValues.push(inDB);
1792
+ console.log('next update');
1793
+ this.status =
1794
+ `1000 Hz Calibration: Sound Level ${inDB} dB`.toString() +
1795
+ this.generateTemplate().toString();
1796
+ this.emit('update', {message: this.status});
1797
+ do {
1798
+ // eslint-disable-next-line no-await-in-loop
1799
+ await this.volumeCalibrationSteps(
1800
+ stream,
1801
+ this.#playCalibrationAudioVolume,
1802
+ this.#createCalibrationToneWithGainValue,
1803
+ this.#sendToServerForProcessing,
1804
+ gainValues[i],
1805
+ lCalib, //todo make this a class parameter
1806
+ checkRec
1807
+ );
1808
+ } while (this.outDBSPL === null);
1809
+ outDBSPL1000Values.push(this.outDBSPL1000);
1810
+ thdValues.push(this.THD);
1811
+ outDBSPLValues.push(this.outDBSPL);
1812
+
1813
+ this.outDBSPL = null;
1814
+ this.outDBSPL1000 = null;
1815
+ this.THD = null;
1816
+ }
1817
+
1818
+ // get the volume calibration parameters from the server
1819
+ this.addTimeStamp("Get Volume Calibration Parameters");
1820
+
1821
+ const parameters = await this.pyServerAPI
1822
+ .getVolumeCalibrationParameters({
1823
+ inDBValues: inDBValues,
1824
+ outDBSPLValues: outDBSPL1000Values,
1825
+ lCalib: lCalib,
1826
+ componentGainDBSPL,
1827
+ })
1828
+ .then(res => {
1829
+ this.incrementStatusBar();
1830
+ return res;
1831
+ });
1832
+ const result = {
1833
+ parameters: parameters,
1834
+ inDBValues: inDBValues,
1835
+ outDBSPLValues: outDBSPLValues,
1836
+ outDBSPL1000Values: outDBSPL1000Values,
1837
+ thdValues: thdValues,
1838
+ };
1839
+
1840
+ return result;
1841
+ };
1842
+
1843
+ // function to write frq and gain to firebase database given speakerID
1844
+ writeFrqGain = async (speakerID, frq, gain, OEM) => {
1845
+ // freq and gain are too large to take samples 1 in every 100 samples
1846
+
1847
+ const sampledFrq = [];
1848
+ const sampledGain = [];
1849
+ for (let i = 0; i < frq.length; i += 100) {
1850
+ sampledFrq.push(frq[i]);
1851
+ sampledGain.push(gain[i]);
1852
+ }
1853
+
1854
+ const data = {Freq: sampledFrq, Gain: sampledGain};
1855
+
1856
+ await set(ref(database, `Microphone2/${OEM}/${speakerID}/linear`), data);
1857
+ };
1858
+
1859
+ // Function to Read frq and gain from firebase database given speakerID
1860
+ // returns an array of frq and gain if speakerID exists, returns null otherwise
1861
+
1862
+ readFrqGain = async (speakerID, OEM) => {
1863
+ const dbRef = ref(database);
1864
+ const snapshot = await get(child(dbRef, `Microphone2/${OEM}/${speakerID}/linear`));
1865
+ if (snapshot.exists()) {
1866
+ return snapshot.val();
1867
+ }
1868
+ return null;
1869
+ };
1870
+
1871
+ readGainat1000Hz = async (speakerID, OEM) => {
1872
+ const dbRef = ref(database);
1873
+ const snapshot = await get(child(dbRef, `Microphone2/${OEM}/${speakerID}/Gain1000`));
1874
+ if (snapshot.exists()) {
1875
+ return snapshot.val();
1876
+ }
1877
+ return null;
1878
+ };
1879
+
1880
+ writeGainat1000Hz = async (speakerID, gain, OEM) => {
1881
+ const data = {Gain: gain};
1882
+ await set(ref(database, `Microphone2/${OEM}/${speakerID}/Gain1000`), gain);
1883
+ };
1884
+
1885
+ writeIsSmartPhone = async (speakerID, isSmartPhone, OEM) => {
1886
+ const data = {isSmartPhone: isSmartPhone};
1887
+ await set(ref(database, `Microphone2/${OEM}/${speakerID}/isSmartPhone`), isSmartPhone);
1888
+ };
1889
+
1890
+ doesMicrophoneExist = async (speakerID, OEM) => {
1891
+ const dbRef = ref(database);
1892
+ const snapshot = await get(child(dbRef, `Microphone2/${OEM}/${speakerID}`));
1893
+ if (snapshot.exists()) {
1894
+ return true;
1895
+ }
1896
+ return false;
1897
+ };
1898
+
1899
+ addMicrophoneInfo = async (speakerID, OEM, micInfo) => {
1900
+ // add to database if /info does not exist
1901
+ const dbRef = ref(database);
1902
+ const snapshot = await get(child(dbRef, `Microphone2/${OEM}/${speakerID}/info`));
1903
+ if (!snapshot.exists()) {
1904
+ await set(ref(database, `Microphone2/${OEM}/${speakerID}/info`), micInfo);
1905
+ }
1906
+ };
1907
+
1908
+ convertToDB = gain => {
1909
+ return Math.log10(gain) * 20;
1910
+ };
1911
+
1912
+ // Function to perform linear interpolation between two points
1913
+ interpolate(x, x0, y0, x1, y1) {
1914
+ return y0 + ((x - x0) * (y1 - y0)) / (x1 - x0);
1915
+ }
1916
+
1917
+ findGainatFrequency = (frequencies, gains, targetFrequency) => {
1918
+ // Find the index of the first frequency in the array greater than the target frequency
1919
+ let index = 0;
1920
+ while (index < frequencies.length && frequencies[index] < targetFrequency) {
1921
+ index++;
1922
+ }
1923
+
1924
+ // Handle cases when the target frequency is outside the range of the given data
1925
+ if (index === 0) {
1926
+ return gains[0];
1927
+ } else if (index === frequencies.length) {
1928
+ return gains[gains.length - 1];
1929
+ } else {
1930
+ // Interpolate the gain based on the surrounding frequencies
1931
+ const x0 = frequencies[index - 1];
1932
+ const y0 = gains[index - 1];
1933
+ const x1 = frequencies[index];
1934
+ const y1 = gains[index];
1935
+ return this.interpolate(targetFrequency, x0, y0, x1, y1);
1936
+ }
1937
+ };
1938
+
1939
+ // add time stamp
1940
+ addTimeStamp = (taskName) => {
1941
+ let startTaskTime = (new Date().getTime() - this.startTime)/1000;
1942
+ this.timeStamp.push(`SOUND ${Number(startTaskTime.toFixed(1))} s. ${taskName}`);
1943
+ }
1944
+
1945
+ // Example of how to use the writeFrqGain and readFrqGain functions
1946
+ // writeFrqGain('speaker1', [1, 2, 3], [4, 5, 6]);
1947
+ // Speaker1 is the speakerID you want to write to in the database
1948
+ // readFrqGain('MiniDSPUMIK_1').then(data => console.log(data));
1949
+ // MiniDSPUMIK_1 is the speakerID with some Data in the database
1950
+ //adding gainDBSPL
1951
+ startCalibration = async (
1952
+ stream,
1953
+ gainValues,
1954
+ lCalib = 104.92978421490648,
1955
+ componentIR = null,
1956
+ microphoneName = 'MiniDSP-UMIK1-711-4754-vertical',
1957
+ _calibrateSoundCheck = 'goal', //GOAL PASSed in by default
1958
+ isSmartPhone = false,
1959
+ _calibrateSoundBurstDb = 0.33,
1960
+ _calibrateSoundBurstRepeats = 3,
1961
+ _calibrateSoundBurstSec = 1,
1962
+ _calibrateSoundBurstsWarmup = 1,
1963
+ _calibrateSoundHz = 48000,
1964
+ _calibrateSoundIIRSec = 0.2,
1965
+ calibrateSound1000HzPreSec = 3.5,
1966
+ calibrateSound1000HzSec = 1.0,
1967
+ calibrateSound1000HzPostSec = 0.5,
1968
+ _calibrateSoundBackgroundSecs = 0,
1969
+ micManufacturer = '',
1970
+ micSerialNumber = '',
1971
+ micModelNumber = '',
1972
+ micModelName = ''
1973
+ ) => {
1974
+ this._calibrateSoundBurstDb = _calibrateSoundBurstDb;
1975
+ this.CALIBRATION_TONE_DURATION =
1976
+ calibrateSound1000HzPreSec + calibrateSound1000HzSec + calibrateSound1000HzPostSec;
1977
+ this.calibrateSound1000HzPreSec = calibrateSound1000HzPreSec;
1978
+ this.calibrateSound1000HzSec = calibrateSound1000HzSec;
1979
+ this.calibrateSound1000HzPostSec = calibrateSound1000HzPostSec;
1980
+ this.iirLength = Math.floor(_calibrateSoundIIRSec * this.sourceSamplingRate);
1981
+ console.log('device info:', this.deviceInfo);
1982
+ this.numMLSPerCapture = _calibrateSoundBurstRepeats;
1983
+ this.desired_time_per_mls = _calibrateSoundBurstSec;
1984
+ this.num_mls_to_skip = _calibrateSoundBurstsWarmup;
1985
+ this.desired_sampling_rate = _calibrateSoundHz;
1986
+ this._calibrateSoundBackgroundSecs = _calibrateSoundBackgroundSecs;
1987
+
1988
+ if (isSmartPhone) {
1989
+ micModelName = this.deviceInfo.deviceInfoFromUser.modelName;
1990
+ micModelNumber = this.deviceInfo.deviceInfoFromUser.modelNumber;
1991
+ }
1992
+
1993
+ //feed calibration goal here
1994
+ this._calibrateSoundCheck = _calibrateSoundCheck;
1995
+ //check if a componentIR was given to the system, if it isn't check for the microphone. using dummy data here bc we need to
1996
+ //check the db based on the microphone currently connected
1997
+
1998
+ //new lCalib found at top of calibration files *1000hz, make sure to correct
1999
+ //based on zeroing of 1000hz, search for "*1000Hz"
2000
+ const ID = isSmartPhone ? micModelNumber : micSerialNumber;
2001
+ const OEM = isSmartPhone
2002
+ ? this.deviceInfo.OEM.toLowerCase().split(' ').join('')
2003
+ : micManufacturer;
2004
+ // const ID = "711-4754";
2005
+ // const OEM = "minidsp";
2006
+ const micInfo = {
2007
+ micModelName: isSmartPhone ? micModelName : microphoneName,
2008
+ OEM: OEM,
2009
+ ID: ID,
2010
+ HardwareName: isSmartPhone ? this.deviceInfo.hardwarename : microphoneName,
2011
+ hardwareFamily: isSmartPhone ? this.deviceInfo.hardwarefamily : microphoneName,
2012
+ HardwareModel: isSmartPhone ? this.deviceInfo.hardwaremodel : microphoneName,
2013
+ PlatformName: isSmartPhone ? this.deviceInfo.platformname : 'N/A',
2014
+ PlatformVersion: isSmartPhone ? this.deviceInfo.platformversion : 'N/A',
2015
+ DeviceType: isSmartPhone ? this.deviceInfo.devicetype : 'N/A',
2016
+ };
2017
+ // if undefined in micInfo, set to empty string
2018
+ for (const [key, value] of Object.entries(micInfo)) {
2019
+ if (value === undefined) {
2020
+ micInfo[key] = '';
2021
+ }
2022
+ }
2023
+
2024
+ this.addMicrophoneInfo(ID, OEM, micInfo);
2025
+ if (componentIR == null) {
2026
+ //mode 'ir'
2027
+ //global variable this.componentIR must be set
2028
+ this.componentIR = await this.readFrqGain(ID, OEM).then(data => {
2029
+ return data;
2030
+ });
2031
+
2032
+ lCalib = await this.readGainat1000Hz(ID, OEM);
2033
+ micInfo['gainDBSPL'] = lCalib;
2034
+ // this.componentGainDBSPL = this.convertToDB(lCalib);
2035
+ this.componentGainDBSPL = lCalib;
2036
+ //TODO: if this call to database is unknown, cannot perform experiment => return false
2037
+ if (this.componentIR == null) {
2038
+ this.status =
2039
+ `Microphone (${OEM},${ID}) is not found in the database. Please add it to the database.`.toString();
2040
+ this.emit('update', {message: this.status});
2041
+ return false;
2042
+ }
2043
+ } else {
2044
+ this.componentIR = componentIR;
2045
+ lCalib = this.findGainatFrequency(this.componentIR.Freq, this.componentIR.Gain, 1000);
2046
+ // this.componentGainDBSPL = this.convertToDB(lCalib);
2047
+ this.componentGainDBSPL = lCalib;
2048
+ await this.writeIsSmartPhone(ID, isSmartPhone, OEM);
2049
+ }
2050
+
2051
+ this.oldComponentIR = this.componentIR;
2052
+
2053
+ let volumeResults = await this.startCalibrationVolume(
2054
+ stream,
2055
+ gainValues,
2056
+ lCalib,
2057
+ this.componentGainDBSPL
2058
+ );
2059
+
2060
+ let impulseResponseResults = await this.startCalibrationImpulseResponse(stream);
2061
+ impulseResponseResults['background_noise'] = this.background_noise;
2062
+ if (componentIR != null) {
2063
+ //insert Freq and Gain from this.componentIR into db
2064
+ await this.writeFrqGain(
2065
+ ID,
2066
+ impulseResponseResults.componentIR.Freq,
2067
+ impulseResponseResults.componentIR.Gain,
2068
+ OEM
2069
+ );
2070
+ micInfo['gainDBSPL'] = impulseResponseResults.parameters.gainDBSPL;
2071
+ await this.writeGainat1000Hz(ID, micInfo['gainDBSPL'], OEM);
2072
+ }
2073
+
2074
+ const total_results = {...volumeResults, ...impulseResponseResults};
2075
+ total_results['filteredMLSRange'] = this.filteredMLSRange;
2076
+ total_results['micInfo'] = micInfo;
2077
+ total_results['audioInfo'] = {};
2078
+ total_results['audioInfo']['sinkSampleRate'] = this.sinkSamplingRate;
2079
+ total_results['audioInfo']['sourceSampleRate'] = this.sourceSamplingRate;
2080
+ total_results['audioInfo']['bitsPerSample'] = this.sampleSize;
2081
+ const timeStampresult = [...this.timeStamp].join('\n');
2082
+ total_results["timeStamps"] = timeStampresult;
2083
+ console.log('total results');
2084
+ console.log(total_results);
2085
+ console.log('Time Stamps');
2086
+ console.log(timeStampresult);
2087
+
2088
+ return total_results;
2089
+ };
2090
+ }
2091
+
2092
+ export default Combination;