speaker-calibration 2.2.74 → 2.2.76

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