speaker-calibration 2.2.29 → 2.2.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/.eslintignore +71 -71
  2. package/.eslintrc.json +40 -40
  3. package/.gitignore +81 -0
  4. package/.prettierignore +69 -69
  5. package/.prettierrc +14 -14
  6. package/LICENSE +20 -20
  7. package/README.md +133 -133
  8. package/__mocks__/fileMock.js +1 -1
  9. package/__mocks__/styleMock.js +1 -1
  10. package/babel.config.js +3 -3
  11. package/coverage/clover.xml +71 -71
  12. package/coverage/coverage-final.json +224 -224
  13. package/coverage/lcov-report/PythonServerInterface.js.html +265 -265
  14. package/coverage/lcov-report/base.css +354 -354
  15. package/coverage/lcov-report/block-navigation.js +82 -82
  16. package/coverage/lcov-report/index.html +123 -123
  17. package/coverage/lcov-report/prettify.css +101 -101
  18. package/coverage/lcov-report/prettify.js +937 -937
  19. package/coverage/lcov-report/sorter.js +189 -189
  20. package/coverage/lcov-report/src/index.html +121 -121
  21. package/coverage/lcov-report/src/server/PythonServerInterface.js.html +268 -268
  22. package/coverage/lcov-report/src/server/index.html +123 -123
  23. package/coverage/lcov-report/src/tasks/audioCalibrator.js.html +499 -499
  24. package/coverage/lcov-report/src/tasks/audioRecorder.js.html +412 -412
  25. package/coverage/lcov-report/src/tasks/index.html +143 -143
  26. package/coverage/lcov-report/src/tasks/volume/index.html +123 -123
  27. package/coverage/lcov-report/src/tasks/volume/volume.js.html +409 -409
  28. package/coverage/lcov-report/src/utils.js.html +172 -172
  29. package/coverage/lcov.info +91 -91
  30. package/dist/example/fetch-languages-sheets.js +77 -77
  31. package/dist/example/i18n.js +6822 -6822
  32. package/dist/example/index.html +47 -47
  33. package/dist/example/listener.html +61 -61
  34. package/dist/example/listener.js +97 -97
  35. package/dist/example/server.js +51 -51
  36. package/dist/example/speaker.html +138 -138
  37. package/dist/example/speakerUI.js +269 -269
  38. package/dist/example/styles.css +63 -63
  39. package/dist/main.js +17 -17
  40. package/dist/mlsGen.js +6814 -6814
  41. package/dist/mlsGen.wasm +0 -0
  42. package/dist/package-lock.json +1018 -1018
  43. package/dist/package.json +18 -18
  44. package/doc/AudioCalibrator.html +417 -417
  45. package/doc/AudioPeer.html +251 -251
  46. package/doc/AudioRecorder.html +195 -195
  47. package/doc/ImpulseResponse.html +215 -215
  48. package/doc/Listener.html +308 -308
  49. package/doc/MlsGenInterface.html +226 -226
  50. package/doc/MyEventEmitter.html +274 -274
  51. package/doc/PythonServerAPI.html +109 -109
  52. package/doc/Speaker.html +276 -276
  53. package/doc/Takes%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +128 -128
  54. package/doc/Takes%20the%20url%20of%20the%20current%20site%0Aand%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +138 -138
  55. package/doc/Takes%20the%20url%20of%20the%20current%20site%20and%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +137 -137
  56. package/doc/Volume.html +88 -88
  57. package/doc/audioCalibrator.js.html +179 -179
  58. package/doc/audioPeer.js.html +175 -175
  59. package/doc/audioRecorder.js.html +163 -163
  60. package/doc/creates%20a%20new%20AudioRecorder%20instance.%20%0ASets%20up%20the%20audio%20context%20and%20file%20reader..html +114 -114
  61. package/doc/fonts/OpenSans-Bold-webfont.svg +1829 -1829
  62. package/doc/fonts/OpenSans-BoldItalic-webfont.svg +1829 -1829
  63. package/doc/fonts/OpenSans-Italic-webfont.svg +1829 -1829
  64. package/doc/fonts/OpenSans-Light-webfont.svg +1830 -1830
  65. package/doc/fonts/OpenSans-LightItalic-webfont.svg +1834 -1834
  66. package/doc/fonts/OpenSans-Regular-webfont.svg +1830 -1830
  67. package/doc/global.html +308 -308
  68. package/doc/index.html +58 -58
  69. package/doc/listener.js.html +170 -170
  70. package/doc/mlsGen_mlsGenInterface.js.html +117 -117
  71. package/doc/myEventEmitter.js.html +124 -124
  72. package/doc/peer-connection_audioPeer.js.html +188 -188
  73. package/doc/peer-connection_listener.js.html +311 -311
  74. package/doc/peer-connection_speaker.js.html +381 -381
  75. package/doc/scripts/linenumber.js +25 -25
  76. package/doc/scripts/prettify/Apache-License-2.0.txt +202 -202
  77. package/doc/scripts/prettify/lang-css.js +24 -24
  78. package/doc/scripts/prettify/prettify.js +640 -640
  79. package/doc/server_PythonServerAPI.js.html +160 -160
  80. package/doc/speaker.js.html +248 -248
  81. package/doc/styles/jsdoc-default.css +371 -371
  82. package/doc/styles/prettify-jsdoc.css +111 -111
  83. package/doc/styles/prettify-tomorrow.css +163 -163
  84. package/doc/tasks_audioCalibrator.js.html +207 -207
  85. package/doc/tasks_audioRecorder.js.html +190 -190
  86. package/doc/tasks_impulse-response_impulseResponse.js.html +442 -442
  87. package/doc/tasks_impulse-response_mlsGen_mlsGenInterface.js.html +175 -175
  88. package/doc/tasks_volume_volume.js.html +185 -185
  89. package/doc/utils.js.html +105 -105
  90. package/jest.config.js +173 -173
  91. package/netlify.toml +26 -26
  92. package/package.json +69 -69
  93. package/src/config/firebase.js +25 -25
  94. package/src/index.html +21 -21
  95. package/src/main.js +23 -23
  96. package/src/myEventEmitter.js +83 -83
  97. package/src/peer-connection/audioPeer.js +151 -151
  98. package/src/peer-connection/listener.js +288 -288
  99. package/src/peer-connection/peerErrors.js +25 -25
  100. package/src/peer-connection/speaker.js +436 -436
  101. package/src/server/PythonServerAPI.js +415 -415
  102. package/src/tasks/audioCalibrator.js +271 -271
  103. package/src/tasks/audioRecorder.js +252 -252
  104. package/src/tasks/combination/combination.js +1390 -1243
  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 +55 -55
  118. package/webpack.config.js +37 -37
  119. package/makefile +0 -74
@@ -1,1243 +1,1390 @@
1
- import AudioCalibrator from '../audioCalibrator';
2
-
3
- import {sleep, csvToArray, saveToCSV} 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 = 4] - number of bursts of MLS per capture
19
- */
20
- constructor({
21
- download = false,
22
- mlsOrder = 18,
23
- numCaptures = 3,
24
- numMLSPerCapture = 4,
25
- lowHz = 20,
26
- highHz = 10000,
27
- }) {
28
- super(numCaptures, numMLSPerCapture);
29
- this.#mlsOrder = parseInt(mlsOrder, 10);
30
- this.#P = 2 ** mlsOrder - 1;
31
- this.#download = download;
32
- this.#mls = [];
33
- this.#lowHz = lowHz;
34
- this.#highHz = highHz;
35
- }
36
-
37
- /** @private */
38
- stepNum = 0;
39
-
40
- /** @private */
41
- totalSteps = 25;
42
-
43
- /** @private */
44
- #download;
45
-
46
- /** @private */
47
- #mlsGenInterface;
48
-
49
- /** @private */
50
- #mlsBufferView;
51
-
52
- /** @private */
53
- componentInvertedImpulseResponse = null;
54
-
55
- /** @private */
56
- systemInvertedImpulseResponse = null;
57
-
58
- //averaged and subtracted ir returned from calibration used to calculated iir
59
- /** @private */
60
- ir = null;
61
-
62
- /** @private */
63
- impulseResponses = [];
64
-
65
- /** @private */
66
- #mlsOrder;
67
-
68
- /** @private */
69
- #lowHz;
70
-
71
- /** @private */
72
- #highHz;
73
-
74
- /** @private */
75
- #mls;
76
-
77
- /** @private */
78
- #P;
79
-
80
- /** @private */
81
- #audioContext;
82
-
83
- /** @private */
84
- TAPER_SECS = 5;
85
-
86
- /** @private */
87
- offsetGainNode;
88
-
89
- /** @private */
90
- componentConvolution;
91
-
92
- /** @private */
93
- systemConvolution;
94
-
95
- ////////////////////////volume
96
- /** @private */
97
- #CALIBRATION_TONE_FREQUENCY = 1000; // Hz
98
-
99
- /** @private */
100
- #CALIBRATION_TONE_TYPE = 'sine';
101
-
102
- CALIBRATION_TONE_DURATION = 5; // seconds
103
-
104
- /** @private */
105
- outDBSPL = null;
106
- THD = null;
107
- outDBSPL1000 = null;
108
-
109
- /** @private */
110
- TAPER_SECS = 0.01; // seconds
111
-
112
- /** @private */
113
- status_denominator = 8;
114
-
115
- /** @private */
116
- status_numerator = 0;
117
-
118
- /** @private */
119
- percent_complete = 0;
120
-
121
- /** @private */
122
- status = ``;
123
-
124
- /**@private */
125
- 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>`;
126
-
127
- /**@private */
128
- componentIR = null;
129
-
130
- /**@private */
131
- oldComponentIR = null;
132
-
133
- /**@private */
134
- systemIR = null;
135
-
136
- /**@private */
137
- _calibrateSoundCheck = '';
138
-
139
- deviceType = null;
140
-
141
- deviceName = null;
142
-
143
- deviceInfo = null;
144
-
145
- desired_time_per_mls = 0;
146
-
147
- num_mls_to_skip = 0;
148
-
149
- desired_sampling_rate = 0;
150
-
151
- #currentConvolution = [];
152
-
153
- mode = 'unfiltered';
154
-
155
- sourceNode;
156
-
157
- autocorrelations = [];
158
-
159
- iirLength = 0;
160
-
161
- /**generate string template that gets reevaluated as variable increases */
162
- generateTemplate = () => {
163
- if (this.percent_complete > 100) {
164
- this.percent_complete = 100;
165
- }
166
- const template = `<div style="display: flex; justify-content: center;"><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>`;
167
- return template;
168
- };
169
-
170
- /** increment numerator and percent for status bar */
171
- incrementStatusBar = () => {
172
- this.status_numerator += 1;
173
- this.percent_complete = (this.status_numerator / this.status_denominator) * 100;
174
- };
175
-
176
- setDeviceType = deviceType => {
177
- this.deviceType = deviceType;
178
- };
179
-
180
- setDeviceName = deviceName => {
181
- this.deviceName = deviceName;
182
- };
183
-
184
- setDeviceInfo = deviceInfo => {
185
- this.deviceInfo = deviceInfo;
186
- };
187
-
188
- /** .
189
- * .
190
- * .
191
- * Sends all the computed impulse responses to the backend server for processing
192
- *
193
- * @returns sets the resulting inverted impulse response to the class property
194
- * @example
195
- */
196
- sendSystemImpulseResponsesToServerForProcessing = async () => {
197
- const computedIRs = await Promise.all(this.impulseResponses);
198
- const filteredComputedIRs = computedIRs.filter(element => {
199
- return element != undefined;
200
- });
201
- const mls = this.#mls;
202
- const lowHz = this.#lowHz;
203
- const highHz = this.#highHz;
204
- const iirLength = this.iirLength;
205
- const num_periods = this.numMLSPerCapture + this.num_mls_to_skip;
206
- this.stepNum += 1;
207
- console.log('send impulse responses to server: ' + this.stepNum);
208
- this.status =
209
- `All Hz Calibration: computing the IIR...`.toString() + this.generateTemplate().toString();
210
- this.emit('update', {message: this.status});
211
- return this.pyServerAPI
212
- .getSystemInverseImpulseResponseWithRetry({
213
- payload: filteredComputedIRs.slice(0, this.numCaptures),
214
- mls,
215
- lowHz,
216
- highHz,
217
- iirLength,
218
- num_periods,
219
- sampleRate: this.sourceSamplingRate || 96000,
220
- })
221
- .then(res => {
222
- console.log(res);
223
- this.stepNum += 1;
224
- console.log('got impulse response ' + this.stepNum);
225
- this.incrementStatusBar();
226
- this.status =
227
- `All Hz Calibration: done computing the IIR...`.toString() +
228
- this.generateTemplate().toString();
229
- this.emit('update', {message: this.status});
230
- this.systemInvertedImpulseResponse = res['iir'];
231
- this.systemIR = res['ir'];
232
- this.systemConvolution = res['convolution'];
233
- })
234
- .catch(err => {
235
- console.error(err);
236
- });
237
- };
238
-
239
- /** .
240
- * .
241
- * .
242
- * Sends all the computed impulse responses to the backend server for processing
243
- *
244
- * @returns sets the resulting inverted impulse response to the class property
245
- * @example
246
- */
247
- sendComponentImpulseResponsesToServerForProcessing = async () => {
248
- const computedIRs = await Promise.all(this.impulseResponses);
249
- const filteredComputedIRs = computedIRs.filter(element => {
250
- return element != undefined;
251
- });
252
- const componentIRGains = this.componentIR['Gain'];
253
- const componentIRFreqs = this.componentIR['Freq'];
254
- const mls = this.#mls;
255
- const lowHz = this.#lowHz;
256
- const iirLength = this.iirLength;
257
- const num_periods = this.numMLSPerCapture + this.num_mls_to_skip;
258
- const highHz = this.#highHz;
259
- this.stepNum += 1;
260
- console.log('send impulse responses to server: ' + this.stepNum);
261
- this.status =
262
- `All Hz Calibration: computing the IIR...`.toString() + this.generateTemplate().toString();
263
- this.emit('update', {message: this.status});
264
- return this.pyServerAPI
265
- .getComponentInverseImpulseResponseWithRetry({
266
- payload: filteredComputedIRs.slice(0, this.numCaptures),
267
- mls,
268
- lowHz,
269
- highHz,
270
- iirLength,
271
- componentIRGains,
272
- componentIRFreqs,
273
- num_periods,
274
- sampleRate: this.sourceSamplingRate || 96000,
275
- })
276
- .then(res => {
277
- console.log(res);
278
- this.stepNum += 1;
279
- console.log('got impulse response ' + this.stepNum);
280
- this.incrementStatusBar();
281
- this.status =
282
- `All Hz Calibration: done computing the IIR...`.toString() +
283
- this.generateTemplate().toString();
284
- this.emit('update', {message: this.status});
285
- this.componentInvertedImpulseResponse = res['iir'];
286
- this.componentIR['Gain'] = res['ir'];
287
- this.componentIR['Freq'] = res['frequencies'];
288
- this.componentConvolution = res['convolution'];
289
- })
290
- .catch(err => {
291
- // this.emit('InvertedImpulseResponse', {res: false});
292
- console.error(err);
293
- });
294
- };
295
-
296
- /** .
297
- * .
298
- * .
299
- * Sends the recorded signal, or a given csv string of a signal, to the back end server for processing
300
- *
301
- * @param {<array>String} signalCsv - Optional csv string of a previously recorded signal, if given, this signal will be processed
302
- * @example
303
- */
304
- sendRecordingToServerForProcessing = signalCsv => {
305
- const allSignals = this.getAllUnfilteredRecordedSignals();
306
- console.log(
307
- 'Obtaining last all hz unfiltered recording from #allHzUnfilteredRecordings to send to server for processing'
308
- );
309
- const numSignals = allSignals.length;
310
- const mls = this.#mls;
311
- const payload =
312
- signalCsv && signalCsv.length > 0 ? csvToArray(signalCsv) : allSignals[numSignals - 1];
313
- console.log('sending rec');
314
- this.stepNum += 1;
315
- console.log('send rec ' + this.stepNum);
316
- this.status =
317
- `All Hz Calibration Step: computing the IR of the last recording...`.toString() +
318
- this.generateTemplate().toString();
319
- this.emit('update', {message: this.status});
320
- this.impulseResponses.push(
321
- this.pyServerAPI
322
- .getImpulseResponse({
323
- sampleRate: this.sourceSamplingRate || 96000,
324
- payload,
325
- mls,
326
- P: this.#P,
327
- numPeriods: this.numMLSPerCapture,
328
- })
329
- .then(res => {
330
- if (this.numSuccessfulCaptured < this.numCaptures) {
331
- this.numSuccessfulCaptured += 1;
332
- console.log('num succ capt: ' + this.numSuccessfulCaptured);
333
- this.stepNum += 1;
334
- console.log('got impulse response ' + this.stepNum);
335
- this.incrementStatusBar();
336
- this.status =
337
- `All Hz Calibration: ${this.numSuccessfulCaptured}/${this.numCaptures} IRs computed...`.toString() +
338
- this.generateTemplate().toString();
339
- this.emit('update', {
340
- message: this.status,
341
- });
342
- this.autocorrelations.push(res['autocorrelation']);
343
- return res['ir'];
344
- }
345
- })
346
- .catch(err => {
347
- console.error(err);
348
- })
349
- );
350
- };
351
-
352
- /**
353
- * Passed to the calibration steps function, awaits the desired amount of seconds to capture the desired number
354
- * of MLS periods defined in the constructor.
355
- *
356
- * @example
357
- */
358
- #awaitDesiredMLSLength = async () => {
359
- // seconds per MLS = P / SR
360
- // await N * P / SR
361
- this.stepNum += 1;
362
- console.log('await desired length ' + this.stepNum);
363
- this.status =
364
- `All Hz Calibration: sampling the calibration signal...`.toString() +
365
- `\niteration ${this.stepNum}` +
366
- this.generateTemplate();
367
- this.emit('update', {
368
- message: this.status,
369
- });
370
- let time_to_wait = 0;
371
- if (this.mode === 'unfiltered') {
372
- time_to_wait = (this.#mls.length / this.sourceSamplingRate) * this.numMLSPerCapture;
373
- time_to_wait = time_to_wait * 1.1;
374
- } else if (this.mode === 'filtered') {
375
- time_to_wait =
376
- (this.#currentConvolution.length / this.sourceSamplingRate) *
377
- (this.numMLSPerCapture / (this.num_mls_to_skip + this.numMLSPerCapture));
378
- } else {
379
- throw new Error('Mode broke in awaitDesiredMLSLength');
380
- }
381
-
382
- await sleep(time_to_wait);
383
- };
384
-
385
- /** .
386
- * .
387
- * .
388
- * Passed to the calibration steps function, awaits the onset of the signal to ensure a steady state
389
- *
390
- * @example
391
- */
392
- #awaitSignalOnset = async () => {
393
- this.stepNum += 1;
394
- console.log('await signal onset ' + this.stepNum);
395
- this.status =
396
- `All Hz Calibration: waiting for the signal to stabilize...`.toString() +
397
- this.generateTemplate();
398
- this.emit('update', {
399
- message: this.status,
400
- });
401
- let number_of_bursts_to_skip = this.num_mls_to_skip;
402
- let time_to_sleep = 0;
403
- if (this.mode === 'unfiltered') {
404
- time_to_sleep = (this.#mls.length / this.sourceSamplingRate) * number_of_bursts_to_skip;
405
- } else if (this.mode === 'filtered') {
406
- console.log(this.#currentConvolution.length);
407
- time_to_sleep =
408
- (this.#currentConvolution.length / this.sourceSamplingRate) *
409
- (number_of_bursts_to_skip / (number_of_bursts_to_skip + this.numMLSPerCapture));
410
- } else {
411
- throw new Error('Mode broke in awaitSignalOnset');
412
- }
413
- await sleep(time_to_sleep);
414
- };
415
-
416
- /**
417
- * Called immediately after a recording is captured. Used to process the resulting signal
418
- * whether by sending the result to a server or by computing a result locally.
419
- *
420
- * @example
421
- */
422
- #afterMLSRecord = () => {
423
- console.log('after record');
424
- this.sendRecordingToServerForProcessing();
425
- };
426
-
427
- #afterMLSwIIRRecord = () => {
428
- if (this.numSuccessfulCaptured < 1) {
429
- this.numSuccessfulCaptured += 1;
430
- this.stepNum += 1;
431
- this.incrementStatusBar();
432
- console.log('after mls w iir record for some reason add numSucc capt ' + this.stepNum);
433
- this.status =
434
- `All Hz Calibration: ${this.numSuccessfulCaptured} recording of convolved MLS captured`.toString() +
435
- this.generateTemplate().toString();
436
- this.emit('update', {
437
- message: this.status,
438
- });
439
- }
440
- };
441
-
442
- /** .
443
- * .
444
- * .
445
- * Created an S Curver Buffer to taper the signal onset
446
- *
447
- * @param {*} length
448
- * @param {*} phase
449
- * @returns
450
- * @example
451
- */
452
- static createSCurveBuffer = (length, phase) => {
453
- const curve = new Float32Array(length);
454
- let i;
455
- for (i = 0; i < length; i += 1) {
456
- // scale the curve to be between 0-1
457
- curve[i] = Math.sin((Math.PI * i) / length - phase) / 2 + 0.5;
458
- }
459
- return curve;
460
- };
461
-
462
- static createInverseSCurveBuffer = (length, phase) => {
463
- const curve = new Float32Array(length);
464
- let i;
465
- let j = length - 1;
466
- for (i = 0; i < length; i += 1) {
467
- // scale the curve to be between 0-1
468
- curve[i] = Math.sin((Math.PI * j) / length - phase) / 2 + 0.5;
469
- j -= 1;
470
- }
471
- return curve;
472
- };
473
-
474
- /**
475
- * Construct a Calibration Node with the calibration parameters.
476
- *
477
- * @param dataBuffer
478
- * @private
479
- * @example
480
- */
481
- #createCalibrationNodeFromBuffer = dataBuffer => {
482
- console.log('databuffer');
483
- console.log(dataBuffer);
484
- if (!this.sourceAudioContext) {
485
- this.makeNewSourceAudioContext();
486
- }
487
-
488
- const buffer = this.sourceAudioContext.createBuffer(
489
- 1, // number of channels
490
- dataBuffer.length,
491
- this.sourceAudioContext.sampleRate // sample rate
492
- );
493
-
494
- const data = buffer.getChannelData(0); // get data
495
- // fill the buffer with our data
496
- try {
497
- for (let i = 0; i < dataBuffer.length; i += 1) {
498
- data[i] = dataBuffer[i] * 0.1;
499
- }
500
- } catch (error) {
501
- console.error(error);
502
- }
503
-
504
- this.sourceNode = this.sourceAudioContext.createBufferSource();
505
-
506
- this.sourceNode.buffer = buffer;
507
- if (this.mode === 'filtered') {
508
- this.sourceNode.loop = false;
509
- } else {
510
- this.sourceNode.loop = true;
511
- }
512
-
513
- this.sourceNode.connect(this.sourceAudioContext.destination);
514
-
515
- this.addCalibrationNode(this.sourceNode);
516
- };
517
-
518
- /**
519
- * Given a data buffer, creates the required calibration node
520
- *
521
- * @param {*} dataBufferArray
522
- * @example
523
- */
524
- #setCalibrationNodesFromBuffer = (dataBufferArray = [this.#mlsBufferView]) => {
525
- if (dataBufferArray.length === 1) {
526
- this.#createCalibrationNodeFromBuffer(dataBufferArray[0]);
527
- } else {
528
- throw new Error('The length of the data buffer array must be 1');
529
- }
530
- };
531
-
532
- /**
533
- * Creates an audio context and plays it for a few seconds.
534
- *
535
- * @private
536
- * @returns - Resolves when the audio is done playing.
537
- * @example
538
- */
539
- #playCalibrationAudio = () => {
540
- this.calibrationNodes[0].start(0);
541
- this.status = ``;
542
- if (this.mode === 'unfiltered') {
543
- this.#mls = this.calibrationNodes[0].buffer.getChannelData(0);
544
- console.log('play calibration audio ' + this.stepNum);
545
- this.status =
546
- `All Hz Calibration: playing the calibration tone...`.toString() +
547
- this.generateTemplate().toString();
548
- } else if (this.mode === 'filtered') {
549
- console.log('play convolved audio ' + this.stepNum);
550
- this.status =
551
- `All Hz Calibration: playing the convolved calibration tone...`.toString() +
552
- this.generateTemplate().toString();
553
- } else {
554
- throw new Error('Mode is incorrect');
555
- }
556
- this.emit('update', {message: this.status});
557
- this.stepNum += 1;
558
- console.log('sink sampling rate');
559
- console.log(this.sinkSamplingRate);
560
- console.log('source sampling rate');
561
- console.log(this.sourceSamplingRate);
562
- };
563
-
564
- /** .
565
- * .
566
- * .
567
- * Stops the audio with tapered offset
568
- *
569
- * @example
570
- */
571
- #stopCalibrationAudio = () => {
572
- this.calibrationNodes[0].stop(0);
573
- this.calibrationNodes = [];
574
- this.sourceNode.disconnect();
575
- this.stepNum += 1;
576
- console.log('stop calibration audio ' + this.stepNum);
577
- this.status =
578
- `All Hz Calibration: stopping the calibration tone...`.toString() +
579
- this.generateTemplate().toString();
580
- this.emit('update', {message: this.status});
581
- };
582
-
583
- playMLSwithIIR = async (stream, iir) => {
584
- let checkRec = false;
585
- this.mode = 'filtered';
586
- console.log('play mls with iir');
587
- this.invertedImpulseResponse = iir;
588
-
589
- await this.calibrationSteps(
590
- stream,
591
- this.#playCalibrationAudio, // play audio func (required)
592
- this.#createCalibrationNodeFromBuffer(this.#currentConvolution), // before play func
593
- this.#awaitSignalOnset, // before record
594
- () => this.numSuccessfulCaptured < 1,
595
- this.#awaitDesiredMLSLength, // during record
596
- this.#afterMLSwIIRRecord, // after record
597
- this.mode,
598
- checkRec
599
- );
600
- };
601
-
602
- /**
603
- * Public method to start the calibration process. Objects intialized from webassembly allocate new memory
604
- * and must be manually freed. This function is responsible for intializing the MlsGenInterface,
605
- * and wrapping the calibration steps with a garbage collection safe gaurd.
606
- *
607
- * @public
608
- * @param stream - The stream of audio from the Listener.
609
- * @example
610
- */
611
- startCalibrationImpulseResponse = async stream => {
612
- let desired_time = this.desired_time_per_mls;
613
- let checkRec = 'allhz';
614
-
615
- console.log('MLS sequence should be of length: ' + this.sourceSamplingRate * desired_time);
616
-
617
- length = this.sourceSamplingRate * desired_time;
618
- //get mls here
619
- await this.pyServerAPI
620
- .getMLSWithRetry(length)
621
- .then(res => {
622
- console.log(res);
623
- this.#mlsBufferView = res['mls'];
624
- })
625
- .catch(err => {
626
- // this.emit('InvertedImpulseResponse', {res: false});
627
- console.error(err);
628
- });
629
- await this.calibrationSteps(
630
- stream,
631
- this.#playCalibrationAudio, // play audio func (required)
632
- this.#createCalibrationNodeFromBuffer(this.#mlsBufferView), // before play func
633
- this.#awaitSignalOnset, // before record
634
- () => this.numSuccessfulCaptured < this.numCaptures, // loop while true
635
- this.#awaitDesiredMLSLength, // during record
636
- this.#afterMLSRecord, // after record
637
- this.mode,
638
- checkRec
639
- ),
640
- this.#stopCalibrationAudio();
641
- checkRec = false;
642
-
643
- // at this stage we've captured all the required signals,
644
- // and have received IRs for each one
645
- // so let's send all the IRs to the server to be converted to a single IIR
646
- await this.sendSystemImpulseResponsesToServerForProcessing();
647
- await this.sendComponentImpulseResponsesToServerForProcessing();
648
-
649
- this.numSuccessfulCaptured = 0;
650
-
651
- let iir_ir_and_plots;
652
- if (this._calibrateSoundCheck != 'none') {
653
- if (this._calibrateSoundCheck != 'system') {
654
- this.#currentConvolution = this.componentConvolution;
655
- } else {
656
- this.#currentConvolution = this.systemConvolution;
657
- }
658
- await this.playMLSwithIIR(stream, this.invertedImpulseResponse);
659
- this.#stopCalibrationAudio();
660
- this.sourceAudioContext.close();
661
- let conv_recs = this.getAllFilteredRecordedSignals();
662
- let recs = this.getAllUnfilteredRecordedSignals();
663
- console.log(
664
- 'Obtaining unfiltered recording from #allHzUnfilteredRecordings to calculate PSD'
665
- );
666
- console.log('Obtaining filtered recording from #allHzFilteredRecordings to calculate PSD');
667
- let unconv_rec = recs[0];
668
- let conv_rec = conv_recs[0];
669
- if (this._calibrateSoundCheck != 'system') {
670
- let knownGain = this.oldComponentIR.Gain;
671
- let knownFreq = this.oldComponentIR.Freq;
672
- let sampleRate = this.sourceSamplingRate || 96000;
673
- let unconv_results = await this.pyServerAPI
674
- .getSubtractedPSDWithRetry(unconv_rec, knownGain, knownFreq, sampleRate)
675
- .then(res => {
676
- this.incrementStatusBar();
677
- this.status =
678
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
679
- this.generateTemplate().toString();
680
- this.emit('update', {message: this.status});
681
- return res;
682
- })
683
- .catch(err => {
684
- console.error(err);
685
- });
686
-
687
- let conv_results = await this.pyServerAPI
688
- .getSubtractedPSDWithRetry(conv_rec, knownGain, knownFreq, sampleRate)
689
- .then(res => {
690
- this.incrementStatusBar();
691
- this.status =
692
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
693
- this.generateTemplate().toString();
694
- this.emit('update', {message: this.status});
695
- return res;
696
- })
697
- .catch(err => {
698
- console.error(err);
699
- });
700
- iir_ir_and_plots = {
701
- systemIIR: this.systemInvertedImpulseResponse,
702
- componentIIR: this.componentInvertedImpulseResponse,
703
- x_unconv: unconv_results['x'],
704
- y_unconv: unconv_results['y'],
705
- x_conv: conv_results['x'],
706
- y_conv: conv_results['y'],
707
- componentIR: this.componentIR,
708
- systemIR: this.systemIR,
709
- };
710
- } else {
711
- let results = await this.pyServerAPI
712
- .getPSDWithRetry({
713
- unconv_rec,
714
- conv_rec,
715
- sampleRate: this.sourceSamplingRate || 96000,
716
- })
717
- .then(res => {
718
- this.incrementStatusBar();
719
- this.status =
720
- `All Hz Calibration: done computing the PSD graphs...`.toString() +
721
- this.generateTemplate().toString();
722
- this.emit('update', {message: this.status});
723
- return res;
724
- })
725
- .catch(err => {
726
- console.error(err);
727
- });
728
- iir_ir_and_plots = {
729
- systemIIR: this.systemInvertedImpulseResponse,
730
- componentIIR: this.componentInvertedImpulseResponse,
731
- x_unconv: results['x_unconv'],
732
- y_unconv: results['y_unconv'],
733
- x_conv: results['x_conv'],
734
- y_conv: results['y_conv'],
735
- componentIR: this.componentIR,
736
- systemIR: this.systemIR,
737
- };
738
- }
739
-
740
- if (this.#download) {
741
- this.downloadSingleUnfilteredRecording();
742
- this.downloadSingleFilteredRecording();
743
- saveToCSV(this.#mls, 'MLS.csv');
744
- saveToCSV(this.componentConvolution, 'python_component_convolution_mls_iir.csv');
745
- saveToCSV(this.systemConvolution, 'python_system_convolution_mls_iir.csv');
746
- saveToCSV(this.componentInvertedImpulseResponse, 'componentIIR.csv');
747
- saveToCSV(this.systemInvertedImpulseResponse, 'systemIIR.csv');
748
- for (let i = 0; i < this.autocorrelations.length; i++) {
749
- saveToCSV(this.autocorrelations[i], `autocorrelation_${i}`);
750
- }
751
- const computedIRagain = await Promise.all(this.impulseResponses).then(res => {
752
- for (let i = 0; i < res.length; i++) {
753
- if (res[i] != undefined) {
754
- saveToCSV(res[i], `IR_${i}`);
755
- }
756
- }
757
- });
758
- }
759
- } else {
760
- iir_ir_and_plots = {
761
- systemIIR: this.systemInvertedImpulseResponse,
762
- componentIIR: this.componentInvertedImpulseResponse,
763
- x_unconv: [],
764
- y_unconv: [],
765
- x_conv: [],
766
- y_conv: [],
767
- componentIR: this.componentIR,
768
- systemIR: this.systemIR,
769
- };
770
- if (this.#download) {
771
- saveToCSV(this.#mls, 'MLS.csv');
772
- saveToCSV(this.componentConvolution, 'python_component_convolution_mls_iir.csv');
773
- saveToCSV(this.systemConvolution, 'python_system_convolution_mls_iir.csv');
774
- saveToCSV(this.componentInvertedImpulseResponse, 'componentIIR.csv');
775
- saveToCSV(this.systemInvertedImpulseResponse, 'systemIIR.csv');
776
- for (let i = 0; i < this.autocorrelations.length; i++) {
777
- saveToCSV(this.autocorrelations[i], `autocorrelation_${i}`);
778
- }
779
- const computedIRagain = await Promise.all(this.impulseResponses).then(res => {
780
- for (let i = 0; i < res.length; i++) {
781
- if (res[i] != undefined) {
782
- saveToCSV(res[i], `IR_${i}`);
783
- }
784
- }
785
- });
786
- }
787
- }
788
-
789
- this.percent_complete = 100;
790
-
791
- this.status = `All Hz Calibration: Finished`.toString() + this.generateTemplate().toString();
792
- this.emit('update', {message: this.status});
793
-
794
- //here after calibration we have the component calibration (either loudspeaker or microphone) in the same form as the componentIR
795
- //that was used to calibrate
796
-
797
- return iir_ir_and_plots;
798
- };
799
-
800
- //////////////////////volume
801
-
802
- handleIncomingData = data => {
803
- console.log('Received data: ', data);
804
- if (data.type === 'soundGainDBSPL') {
805
- this.soundGainDBSPL = data.value;
806
- } else {
807
- throw new Error(`Unknown data type: ${data.type}`);
808
- }
809
- };
810
- createSCurveBuffer = (onSetBool = true) => {
811
- const curve = new Float32Array(this.TAPER_SECS * this.sourceSamplingRate + 1);
812
- const frequency = 1 / (4 * this.TAPER_SECS);
813
- let j = 0;
814
- for (let i = 0; i < this.TAPER_SECS * this.sourceSamplingRate + 1; i += 1) {
815
- const phase = 2 * Math.PI * frequency * j;
816
- const onsetTaper = Math.pow(Math.sin(phase), 2);
817
- const offsetTaper = Math.pow(Math.cos(phase), 2);
818
- curve[i] = onSetBool ? onsetTaper : offsetTaper;
819
- j += 1 / this.sourceSamplingRate;
820
- }
821
- return curve;
822
- };
823
-
824
- #getTruncatedSignal = (left = 3.5, right = 4.5) => {
825
- const start = Math.floor(left * this.sourceSamplingRate);
826
- const end = Math.floor(right * this.sourceSamplingRate);
827
- const result = Array.from(this.getLastVolumeRecordedSignal().slice(start, end));
828
- console.log(
829
- 'Obtaining last 1000 hz recording from #allVolumeRecordings to send for processing'
830
- );
831
- /**
832
- * function to check that capture was properly made
833
- * @param {*} list
834
- */
835
- const checkResult = list => {
836
- const setItem = new Set(list);
837
- if (setItem.size === 1 && setItem.has(0)) {
838
- console.warn(
839
- 'The last capture failed, all recorded signal is zero',
840
- this.getAllVolumeRecordedSignals()
841
- );
842
- }
843
- if (setItem.size === 0) {
844
- console.warn('The last capture failed, no recorded signal');
845
- }
846
- };
847
- checkResult(result);
848
- return result;
849
- };
850
-
851
- /**
852
- *
853
- *
854
- Construct a calibration Node with the calibration parameters and given gain value
855
- * @param {*} gainValue
856
- * */
857
- #createCalibrationToneWithGainValue = gainValue => {
858
- const audioContext = this.makeNewSourceAudioContext();
859
- const oscilator = audioContext.createOscillator();
860
- const gainNode = audioContext.createGain();
861
- const taperGainNode = audioContext.createGain();
862
- const offsetGainNode = audioContext.createGain();
863
- const totalDuration = this.CALIBRATION_TONE_DURATION * 1.2;
864
-
865
- oscilator.frequency.value = this.#CALIBRATION_TONE_FREQUENCY;
866
- oscilator.type = this.#CALIBRATION_TONE_TYPE;
867
- gainNode.gain.value = gainValue;
868
-
869
- oscilator.connect(gainNode);
870
- gainNode.connect(taperGainNode);
871
- const onsetCurve = this.createSCurveBuffer();
872
- taperGainNode.gain.setValueCurveAtTime(onsetCurve, 0, this.TAPER_SECS);
873
- taperGainNode.connect(offsetGainNode);
874
- const offsetCurve = this.createSCurveBuffer(false);
875
- offsetGainNode.gain.setValueCurveAtTime(
876
- offsetCurve,
877
- totalDuration - this.TAPER_SECS,
878
- this.TAPER_SECS
879
- );
880
- offsetGainNode.connect(audioContext.destination);
881
-
882
- this.addCalibrationNode(oscilator);
883
- };
884
-
885
- /**
886
- * Construct a Calibration Node with the calibration parameters.
887
- *
888
- * @private
889
- * @example
890
- */
891
- #createCalibrationNode = () => {
892
- const audioContext = this.makeNewSourceAudioContext();
893
- const oscilator = audioContext.createOscillator();
894
- const gainNode = audioContext.createGain();
895
-
896
- oscilator.frequency.value = this.#CALIBRATION_TONE_FREQUENCY;
897
- oscilator.type = this.#CALIBRATION_TONE_TYPE;
898
- gainNode.gain.value = 0.04;
899
-
900
- oscilator.connect(gainNode);
901
- gainNode.connect(audioContext.destination);
902
-
903
- this.addCalibrationNode(oscilator);
904
- };
905
-
906
- #playCalibrationAudioVolume = async () => {
907
- const totalDuration = this.CALIBRATION_TONE_DURATION * 1.2;
908
-
909
- this.calibrationNodes[0].start(0);
910
- this.calibrationNodes[0].stop(totalDuration);
911
- console.log(`Playing a buffer of ${this.CALIBRATION_TONE_DURATION} seconds of audio`);
912
- console.log(`Waiting a total of ${totalDuration} seconds`);
913
- await sleep(totalDuration);
914
- };
915
-
916
- #sendToServerForProcessing = lCalib => {
917
- console.log('Sending data to server');
918
- this.pyServerAPI
919
- .getVolumeCalibration({
920
- sampleRate: this.sourceSamplingRate,
921
- payload: this.#getTruncatedSignal(),
922
- lCalib: lCalib,
923
- })
924
- .then(res => {
925
- if (this.outDBSPL === null) {
926
- this.incrementStatusBar();
927
- this.outDBSPL = res['outDbSPL'];
928
- this.outDBSPL1000 = res['outDbSPL1000'];
929
- this.THD = res['thd'];
930
- }
931
- })
932
- .catch(err => {
933
- console.warn(err);
934
- });
935
- };
936
-
937
- startCalibrationVolume = async (stream, gainValues, lCalib, componentGainDBSPL) => {
938
- const trialIterations = gainValues.length;
939
- this.status_denominator += trialIterations;
940
- const thdValues = [];
941
- const inDBValues = [];
942
- let inDB = 0;
943
- const outDBSPLValues = [];
944
- const outDBSPL1000Values = [];
945
- let checkRec = false;
946
-
947
- // do one calibration that will be discarded
948
- const soundLevelToDiscard = -60;
949
- const gainToDiscard = Math.pow(10, soundLevelToDiscard / 20);
950
- this.status =
951
- `1000 Hz Calibration: Sound Level ${soundLevelToDiscard} dB`.toString() +
952
- this.generateTemplate().toString();
953
- //this.emit('update', {message: `1000 Hz Calibration: Sound Level ${soundLevelToDiscard} dB`});
954
- this.emit('update', {message: this.status});
955
-
956
- do {
957
- // eslint-disable-next-line no-await-in-loop
958
- await this.volumeCalibrationSteps(
959
- stream,
960
- this.#playCalibrationAudioVolume,
961
- this.#createCalibrationToneWithGainValue,
962
- this.#sendToServerForProcessing,
963
- gainToDiscard,
964
- lCalib, //todo make this a class parameter
965
- checkRec
966
- );
967
- } while (this.outDBSPL === null);
968
- //reset the values
969
- //this.incrementStatusBar();
970
-
971
- this.outDBSPL = null;
972
- this.outDBSPL = null;
973
- this.outDBSPL1000 = null;
974
- this.THD = null;
975
-
976
- // run the calibration at different gain values provided by the user
977
- for (let i = 0; i < trialIterations; i++) {
978
- //convert gain to DB and add to inDB
979
- if (i == trialIterations - 1) {
980
- checkRec = 'loudest';
981
- }
982
- inDB = Math.log10(gainValues[i]) * 20;
983
- // precision to 1 decimal place
984
- inDB = Math.round(inDB * 10) / 10;
985
- inDBValues.push(inDB);
986
- console.log('next update');
987
- this.status =
988
- `1000 Hz Calibration: Sound Level ${inDB} dB`.toString() +
989
- this.generateTemplate().toString();
990
- this.emit('update', {message: this.status});
991
- do {
992
- // eslint-disable-next-line no-await-in-loop
993
- await this.volumeCalibrationSteps(
994
- stream,
995
- this.#playCalibrationAudioVolume,
996
- this.#createCalibrationToneWithGainValue,
997
- this.#sendToServerForProcessing,
998
- gainValues[i],
999
- lCalib, //todo make this a class parameter
1000
- checkRec
1001
- );
1002
- } while (this.outDBSPL === null);
1003
- outDBSPL1000Values.push(this.outDBSPL1000);
1004
- thdValues.push(this.THD);
1005
- outDBSPLValues.push(this.outDBSPL);
1006
-
1007
- this.outDBSPL = null;
1008
- this.outDBSPL1000 = null;
1009
- this.THD = null;
1010
- }
1011
-
1012
- // get the volume calibration parameters from the server
1013
- const parameters = await this.pyServerAPI
1014
- .getVolumeCalibrationParameters({
1015
- inDBValues: inDBValues,
1016
- outDBSPLValues: outDBSPL1000Values,
1017
- lCalib: lCalib,
1018
- componentGainDBSPL,
1019
- })
1020
- .then(res => {
1021
- this.incrementStatusBar();
1022
- return res;
1023
- });
1024
- const result = {
1025
- parameters: parameters,
1026
- inDBValues: inDBValues,
1027
- outDBSPLValues: outDBSPLValues,
1028
- outDBSPL1000Values: outDBSPL1000Values,
1029
- thdValues: thdValues,
1030
- };
1031
-
1032
- return result;
1033
- };
1034
-
1035
- // function to write frq and gain to firebase database given speakerID
1036
- writeFrqGain = async (speakerID, frq, gain, OEM) => {
1037
- // freq and gain are too large to take samples 1 in every 100 samples
1038
-
1039
- const sampledFrq = [];
1040
- const sampledGain = [];
1041
- for (let i = 0; i < frq.length; i += 100) {
1042
- sampledFrq.push(frq[i]);
1043
- sampledGain.push(gain[i]);
1044
- }
1045
-
1046
- const data = {Freq: sampledFrq, Gain: sampledGain};
1047
-
1048
- await set(ref(database, `Microphone2/${OEM}/${speakerID}/linear`), data);
1049
- };
1050
-
1051
- // Function to Read frq and gain from firebase database given speakerID
1052
- // returns an array of frq and gain if speakerID exists, returns null otherwise
1053
-
1054
- readFrqGain = async (speakerID, OEM) => {
1055
- const dbRef = ref(database);
1056
- const snapshot = await get(child(dbRef, `Microphone2/${OEM}/${speakerID}/linear`));
1057
- if (snapshot.exists()) {
1058
- return snapshot.val();
1059
- }
1060
- return null;
1061
- };
1062
-
1063
- readGainat1000Hz = async (speakerID, OEM) => {
1064
- const dbRef = ref(database);
1065
- const snapshot = await get(child(dbRef, `Microphone2/${OEM}/${speakerID}/Gain1000`));
1066
- if (snapshot.exists()) {
1067
- return snapshot.val();
1068
- }
1069
- return null;
1070
- };
1071
-
1072
- writeGainat1000Hz = async (speakerID, gain, OEM) => {
1073
- const data = {Gain: gain};
1074
- await set(ref(database, `Microphone2/${OEM}/${speakerID}/Gain1000`), gain);
1075
- };
1076
-
1077
- writeIsSmartPhone = async (speakerID, isSmartPhone, OEM) => {
1078
- const data = {isSmartPhone: isSmartPhone};
1079
- await set(ref(database, `Microphone2/${OEM}/${speakerID}/isSmartPhone`), isSmartPhone);
1080
- };
1081
-
1082
- doesMicrophoneExist = async (speakerID, OEM) => {
1083
- const dbRef = ref(database);
1084
- const snapshot = await get(child(dbRef, `Microphone2/${OEM}/${speakerID}`));
1085
- if (snapshot.exists()) {
1086
- return true;
1087
- }
1088
- return false;
1089
- };
1090
-
1091
- addMicrophoneInfo = async (speakerID, OEM, micInfo) => {
1092
- // add to database if /info does not exist
1093
- const dbRef = ref(database);
1094
- const snapshot = await get(child(dbRef, `Microphone2/${OEM}/${speakerID}/info`));
1095
- if (!snapshot.exists()) {
1096
- await set(ref(database, `Microphone2/${OEM}/${speakerID}/info`), micInfo);
1097
- }
1098
- };
1099
-
1100
- convertToDB = gain => {
1101
- return Math.log10(gain) * 20;
1102
- };
1103
-
1104
- // Function to perform linear interpolation between two points
1105
- interpolate(x, x0, y0, x1, y1) {
1106
- return y0 + ((x - x0) * (y1 - y0)) / (x1 - x0);
1107
- }
1108
-
1109
- findGainatFrequency = (frequencies, gains, targetFrequency) => {
1110
- // Find the index of the first frequency in the array greater than the target frequency
1111
- let index = 0;
1112
- while (index < frequencies.length && frequencies[index] < targetFrequency) {
1113
- index++;
1114
- }
1115
-
1116
- // Handle cases when the target frequency is outside the range of the given data
1117
- if (index === 0) {
1118
- return gains[0];
1119
- } else if (index === frequencies.length) {
1120
- return gains[gains.length - 1];
1121
- } else {
1122
- // Interpolate the gain based on the surrounding frequencies
1123
- const x0 = frequencies[index - 1];
1124
- const y0 = gains[index - 1];
1125
- const x1 = frequencies[index];
1126
- const y1 = gains[index];
1127
- return this.interpolate(targetFrequency, x0, y0, x1, y1);
1128
- }
1129
- };
1130
-
1131
- // Example of how to use the writeFrqGain and readFrqGain functions
1132
- // writeFrqGain('speaker1', [1, 2, 3], [4, 5, 6]);
1133
- // Speaker1 is the speakerID you want to write to in the database
1134
- // readFrqGain('MiniDSPUMIK_1').then(data => console.log(data));
1135
- // MiniDSPUMIK_1 is the speakerID with some Data in the database
1136
- //adding gainDBSPL
1137
- startCalibration = async (
1138
- stream,
1139
- gainValues,
1140
- lCalib = 104.92978421490648,
1141
- componentIR = null,
1142
- microphoneName = 'MiniDSP-UMIK1-711-4754-vertical',
1143
- _calibrateSoundCheck = 'goal', //GOAL PASSed in by default
1144
- isSmartPhone = false,
1145
- _calibrateSoundBurstRepeats = 4,
1146
- _calibrateSoundBurstSec = 1,
1147
- _calibrateSoundBurstsWarmup = 1,
1148
- _calibrateSoundHz = 48000,
1149
- _calibrateSoundIIRSec = 0.2,
1150
- calibrateSound1000HzSec = 5,
1151
- micManufacturer = '',
1152
- micSerialNumber = '',
1153
- micModelNumber = '',
1154
- micModelName = ''
1155
- ) => {
1156
- this.CALIBRATION_TONE_DURATION = calibrateSound1000HzSec;
1157
- this.iirLength = Math.floor(_calibrateSoundIIRSec * this.sourceSamplingRate);
1158
- console.log('device info:', this.deviceInfo);
1159
- this.numMLSPerCapture = _calibrateSoundBurstRepeats;
1160
- this.desired_time_per_mls = _calibrateSoundBurstSec;
1161
- this.num_mls_to_skip = _calibrateSoundBurstsWarmup;
1162
- this.desired_sampling_rate = _calibrateSoundHz;
1163
-
1164
- //feed calibration goal here
1165
- this._calibrateSoundCheck = _calibrateSoundCheck;
1166
- //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
1167
- //check the db based on the microphone currently connected
1168
-
1169
- //new lCalib found at top of calibration files *1000hz, make sure to correct
1170
- //based on zeroing of 1000hz, search for "*1000Hz"
1171
- const ID = isSmartPhone ? micModelNumber : micSerialNumber;
1172
- const OEM = isSmartPhone ? this.deviceInfo.OEM : micManufacturer;
1173
- const micInfo = {
1174
- micModelName: isSmartPhone ? micModelName : microphoneName,
1175
- OEM: OEM,
1176
- ID: ID,
1177
- HardwareName: isSmartPhone ? this.deviceInfo.hardwarename : microphoneName,
1178
- hardwareFamily: isSmartPhone ? this.deviceInfo.hardwarefamily : microphoneName,
1179
- HardwareModel: isSmartPhone ? this.deviceInfo.hardwaremodel : microphoneName,
1180
- PlatformName: isSmartPhone ? this.deviceInfo.platformname : 'N/A',
1181
- PlatformVersion: isSmartPhone ? this.deviceInfo.platformversion : 'N/A',
1182
- DeviceType: isSmartPhone ? this.deviceInfo.devicetype : 'N/A',
1183
- };
1184
- this.addMicrophoneInfo(ID, OEM, micInfo);
1185
- if (componentIR == null) {
1186
- //mode 'ir'
1187
- //global variable this.componentIR must be set
1188
- this.componentIR = await this.readFrqGain(ID, OEM).then(data => {
1189
- return data;
1190
- });
1191
-
1192
- lCalib = await this.readGainat1000Hz(ID, OEM);
1193
- micInfo['gainDBSPL'] = lCalib;
1194
- // this.componentGainDBSPL = this.convertToDB(lCalib);
1195
- this.componentGainDBSPL = lCalib;
1196
- //TODO: if this call to database is unknown, cannot perform experiment => return false
1197
- if (this.componentIR == null) {
1198
- this.status =
1199
- `Microphone (${OEM},${ID}) is not found in the database. Please add it to the database.`.toString();
1200
- this.emit('update', {message: this.status});
1201
- return false;
1202
- }
1203
- } else {
1204
- this.componentIR = componentIR;
1205
- lCalib = this.findGainatFrequency(this.componentIR.Freq, this.componentIR.Gain, 1000);
1206
- // this.componentGainDBSPL = this.convertToDB(lCalib);
1207
- this.componentGainDBSPL = lCalib;
1208
- await this.writeGainat1000Hz(ID, lCalib, OEM);
1209
- await this.writeIsSmartPhone(ID, isSmartPhone, OEM);
1210
- micInfo['gainDBSPL'] = lCalib;
1211
- }
1212
-
1213
- this.oldComponentIR = this.componentIR;
1214
-
1215
- let volumeResults = await this.startCalibrationVolume(
1216
- stream,
1217
- gainValues,
1218
- lCalib,
1219
- this.componentGainDBSPL
1220
- );
1221
-
1222
- let impulseResponseResults = await this.startCalibrationImpulseResponse(stream);
1223
-
1224
- if (componentIR != null) {
1225
- //insert Freq and Gain from this.componentIR into db
1226
- await this.writeFrqGain(
1227
- ID,
1228
- impulseResponseResults.componentIR.Freq,
1229
- impulseResponseResults.componentIR.Gain,
1230
- OEM
1231
- );
1232
- }
1233
-
1234
- const total_results = {...volumeResults, ...impulseResponseResults};
1235
-
1236
- total_results['micInfo'] = micInfo;
1237
- console.log('total results');
1238
- console.log(total_results);
1239
- return total_results;
1240
- };
1241
- }
1242
-
1243
- export default Combination;
1
+ import AudioCalibrator from '../audioCalibrator';
2
+
3
+ import {sleep, csvToArray, saveToCSV} 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 = 4] - number of bursts of MLS per capture
19
+ */
20
+ constructor({
21
+ download = false,
22
+ mlsOrder = 18,
23
+ numCaptures = 3,
24
+ numMLSPerCapture = 4,
25
+ lowHz = 20,
26
+ highHz = 10000,
27
+ }) {
28
+ super(numCaptures, numMLSPerCapture);
29
+ this.#mlsOrder = parseInt(mlsOrder, 10);
30
+ this.#P = 2 ** mlsOrder - 1;
31
+ this.#download = download;
32
+ this.#mls = [];
33
+ this.#lowHz = lowHz;
34
+ this.#highHz = highHz;
35
+ }
36
+
37
+ /** @private */
38
+ stepNum = 0;
39
+
40
+ /** @private */
41
+ totalSteps = 25;
42
+
43
+ /** @private */
44
+ #download;
45
+
46
+ /** @private */
47
+ #mlsGenInterface;
48
+
49
+ /** @private */
50
+ #mlsBufferView;
51
+
52
+ /** @private */
53
+ componentInvertedImpulseResponse = null;
54
+
55
+ /** @private */
56
+ systemInvertedImpulseResponse = null;
57
+
58
+ //averaged and subtracted ir returned from calibration used to calculated iir
59
+ /** @private */
60
+ ir = null;
61
+
62
+ /** @private */
63
+ impulseResponses = [];
64
+
65
+ /** @private */
66
+ #mlsOrder;
67
+
68
+ /** @private */
69
+ #lowHz;
70
+
71
+ /** @private */
72
+ #highHz;
73
+
74
+ /** @private */
75
+ #mls;
76
+
77
+ /** @private */
78
+ #P;
79
+
80
+ /** @private */
81
+ #audioContext;
82
+
83
+ /** @private */
84
+ TAPER_SECS = 5;
85
+
86
+ /** @private */
87
+ offsetGainNode;
88
+
89
+ /** @private */
90
+ componentConvolution;
91
+
92
+ /** @private */
93
+ systemConvolution;
94
+
95
+ ////////////////////////volume
96
+ /** @private */
97
+ #CALIBRATION_TONE_FREQUENCY = 1000; // Hz
98
+
99
+ /** @private */
100
+ #CALIBRATION_TONE_TYPE = 'sine';
101
+
102
+ CALIBRATION_TONE_DURATION = 5; // seconds
103
+
104
+ /** @private */
105
+ outDBSPL = null;
106
+ THD = null;
107
+ outDBSPL1000 = null;
108
+
109
+ /** @private */
110
+ TAPER_SECS = 0.01; // seconds
111
+
112
+ /** @private */
113
+ status_denominator = 8;
114
+
115
+ /** @private */
116
+ status_numerator = 0;
117
+
118
+ /** @private */
119
+ percent_complete = 0;
120
+
121
+ /** @private */
122
+ status = ``;
123
+
124
+ /**@private */
125
+ 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>`;
126
+
127
+ /**@private */
128
+ componentIR = null;
129
+
130
+ /**@private */
131
+ oldComponentIR = null;
132
+
133
+ /**@private */
134
+ systemIR = null;
135
+
136
+ /**@private */
137
+ _calibrateSoundCheck = '';
138
+
139
+ deviceType = null;
140
+
141
+ deviceName = null;
142
+
143
+ deviceInfo = null;
144
+
145
+ desired_time_per_mls = 0;
146
+
147
+ num_mls_to_skip = 0;
148
+
149
+ desired_sampling_rate = 0;
150
+
151
+ #currentConvolution = [];
152
+
153
+ mode = 'unfiltered';
154
+
155
+ sourceNode;
156
+
157
+ autocorrelations = [];
158
+
159
+ iirLength = 0;
160
+
161
+ componentInvertedImpulseResponseNoBandpass = [];
162
+
163
+ systemInvertedImpulseResponseNoBandpass = [];
164
+
165
+ /**generate string template that gets reevaluated as variable increases */
166
+ generateTemplate = () => {
167
+ if (this.percent_complete > 100) {
168
+ this.percent_complete = 100;
169
+ }
170
+ const template = `<div style="display: flex; justify-content: center;"><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>`;
171
+ return template;
172
+ };
173
+
174
+ /** increment numerator and percent for status bar */
175
+ incrementStatusBar = () => {
176
+ this.status_numerator += 1;
177
+ this.percent_complete = (this.status_numerator / this.status_denominator) * 100;
178
+ };
179
+
180
+ setDeviceType = deviceType => {
181
+ this.deviceType = deviceType;
182
+ };
183
+
184
+ setDeviceName = deviceName => {
185
+ this.deviceName = deviceName;
186
+ };
187
+
188
+ setDeviceInfo = deviceInfo => {
189
+ this.deviceInfo = deviceInfo;
190
+ };
191
+
192
+ /** .
193
+ * .
194
+ * .
195
+ * Sends all the computed impulse responses to the backend server for processing
196
+ *
197
+ * @returns sets the resulting inverted impulse response to the class property
198
+ * @example
199
+ */
200
+ sendSystemImpulseResponsesToServerForProcessing = async () => {
201
+ const computedIRs = await Promise.all(this.impulseResponses);
202
+ const filteredComputedIRs = computedIRs.filter(element => {
203
+ return element != undefined;
204
+ }); //log any errors that are found in this step
205
+ const mls = this.#mls;
206
+ const lowHz = this.#lowHz; //gain of 1 below cutoff, need gain of 0
207
+ const highHz = this.#highHz; //check error for anything other than 10 kHz
208
+ const iirLength = this.iirLength;
209
+ const num_periods = this.numMLSPerCapture + this.num_mls_to_skip;
210
+ this.stepNum += 1;
211
+ console.log('send impulse responses to server: ' + this.stepNum);
212
+ this.status =
213
+ `All Hz Calibration: computing the IIR...`.toString() + this.generateTemplate().toString();
214
+ this.emit('update', {message: this.status});
215
+ return this.pyServerAPI
216
+ .getSystemInverseImpulseResponseWithRetry({
217
+ payload: filteredComputedIRs.slice(0, this.numCaptures),
218
+ mls,
219
+ lowHz,
220
+ highHz,
221
+ iirLength,
222
+ num_periods,
223
+ sampleRate: this.sourceSamplingRate || 96000,
224
+ })
225
+ .then(res => {
226
+ console.log(res);
227
+ this.stepNum += 1;
228
+ console.log('got impulse response ' + this.stepNum);
229
+ this.incrementStatusBar();
230
+ this.status =
231
+ `All Hz Calibration: done computing the IIR...`.toString() +
232
+ this.generateTemplate().toString();
233
+ this.emit('update', {message: this.status});
234
+ this.systemInvertedImpulseResponse = res['iir'];
235
+ this.systemIR = res['ir'];
236
+ this.systemConvolution = res['convolution'];
237
+ this.systemInvertedImpulseResponseNoBandpass = res['iirNoBandpass'];
238
+ })
239
+ .catch(err => {
240
+ console.error(err);
241
+ });
242
+ };
243
+
244
+ /** .
245
+ * .
246
+ * .
247
+ * Sends all the computed impulse responses to the backend server for processing
248
+ *
249
+ * @returns sets the resulting inverted impulse response to the class property
250
+ * @example
251
+ */
252
+ sendComponentImpulseResponsesToServerForProcessing = async () => {
253
+ const computedIRs = await Promise.all(this.impulseResponses);
254
+ const filteredComputedIRs = computedIRs.filter(element => {
255
+ return element != undefined;
256
+ });
257
+ const componentIRGains = this.componentIR['Gain'];
258
+ const componentIRFreqs = this.componentIR['Freq'];
259
+ const mls = this.#mls;
260
+ const lowHz = this.#lowHz;
261
+ const iirLength = this.iirLength;
262
+ const num_periods = this.numMLSPerCapture + this.num_mls_to_skip;
263
+ const highHz = this.#highHz;
264
+ this.stepNum += 1;
265
+ console.log('send impulse responses to server: ' + this.stepNum);
266
+ this.status =
267
+ `All Hz Calibration: computing the IIR...`.toString() + this.generateTemplate().toString();
268
+ this.emit('update', {message: this.status});
269
+ return this.pyServerAPI
270
+ .getComponentInverseImpulseResponseWithRetry({
271
+ payload: filteredComputedIRs.slice(0, this.numCaptures),
272
+ mls,
273
+ lowHz,
274
+ highHz,
275
+ iirLength,
276
+ componentIRGains,
277
+ componentIRFreqs,
278
+ num_periods,
279
+ sampleRate: this.sourceSamplingRate || 96000,
280
+ })
281
+ .then(res => {
282
+ console.log(res);
283
+ this.stepNum += 1;
284
+ console.log('got impulse response ' + this.stepNum);
285
+ this.incrementStatusBar();
286
+ this.status =
287
+ `All Hz Calibration: done computing the IIR...`.toString() +
288
+ this.generateTemplate().toString();
289
+ this.emit('update', {message: this.status});
290
+ this.componentInvertedImpulseResponse = res['iir'];
291
+ this.componentIR['Gain'] = res['ir'];
292
+ this.componentIR['Freq'] = res['frequencies'];
293
+ this.componentConvolution = res['convolution'];
294
+ this.componentInvertedImpulseResponseNoBandpass = res['iirNoBandpass'];
295
+ })
296
+ .catch(err => {
297
+ // this.emit('InvertedImpulseResponse', {res: false});
298
+ console.error(err);
299
+ });
300
+ };
301
+
302
+ /** .
303
+ * .
304
+ * .
305
+ * Sends the recorded signal, or a given csv string of a signal, to the back end server for processing
306
+ *
307
+ * @param {<array>String} signalCsv - Optional csv string of a previously recorded signal, if given, this signal will be processed
308
+ * @example
309
+ */
310
+ sendRecordingToServerForProcessing = signalCsv => {
311
+ const allSignals = this.getAllUnfilteredRecordedSignals();
312
+ console.log(
313
+ 'Obtaining last all hz unfiltered recording from #allHzUnfilteredRecordings to send to server for processing'
314
+ );
315
+ const numSignals = allSignals.length;
316
+ const mls = this.#mls;
317
+ const payload =
318
+ signalCsv && signalCsv.length > 0 ? csvToArray(signalCsv) : allSignals[numSignals - 1];
319
+ console.log('sending rec');
320
+ this.stepNum += 1;
321
+ console.log('send rec ' + this.stepNum);
322
+ this.status =
323
+ `All Hz Calibration Step: computing the IR of the last recording...`.toString() +
324
+ this.generateTemplate().toString();
325
+ this.emit('update', {message: this.status});
326
+ this.impulseResponses.push(
327
+ this.pyServerAPI
328
+ .getImpulseResponse({
329
+ sampleRate: this.sourceSamplingRate || 96000,
330
+ payload,
331
+ mls,
332
+ P: this.#P, //get rid of this
333
+ numPeriods: this.numMLSPerCapture,
334
+ })
335
+ .then(res => {
336
+ if (this.numSuccessfulCaptured < this.numCaptures) {
337
+ this.numSuccessfulCaptured += 1;
338
+ console.log('num succ capt: ' + this.numSuccessfulCaptured);
339
+ this.stepNum += 1;
340
+ console.log('got impulse response ' + this.stepNum);
341
+ this.incrementStatusBar();
342
+ this.status =
343
+ `All Hz Calibration: ${this.numSuccessfulCaptured}/${this.numCaptures} IRs computed...`.toString() +
344
+ this.generateTemplate().toString();
345
+ this.emit('update', {
346
+ message: this.status,
347
+ });
348
+ this.autocorrelations.push(res['autocorrelation']);
349
+ return res['ir'];
350
+ }
351
+ })
352
+ .catch(err => {
353
+ console.error(err);
354
+ })
355
+ );
356
+ };
357
+
358
+ /**
359
+ * Passed to the calibration steps function, awaits the desired amount of seconds to capture the desired number
360
+ * of MLS periods defined in the constructor.
361
+ *
362
+ * @example
363
+ */
364
+ #awaitDesiredMLSLength = async () => {
365
+ // seconds per MLS = P / SR
366
+ // await N * P / SR
367
+ this.stepNum += 1;
368
+ console.log('await desired length ' + this.stepNum);
369
+ this.status =
370
+ `All Hz Calibration: sampling the calibration signal...`.toString() +
371
+ `\niteration ${this.stepNum}` +
372
+ this.generateTemplate();
373
+ this.emit('update', {
374
+ message: this.status,
375
+ });
376
+ let time_to_wait = 0;
377
+ if (this.mode === 'unfiltered') {
378
+ time_to_wait = (this.#mls.length / this.sourceSamplingRate) * this.numMLSPerCapture;
379
+ time_to_wait = time_to_wait * 1.1;
380
+ } else if (this.mode === 'filtered') {
381
+ time_to_wait =
382
+ (this.#currentConvolution.length / this.sourceSamplingRate) *
383
+ (this.numMLSPerCapture / (this.num_mls_to_skip + this.numMLSPerCapture));
384
+ } else {
385
+ throw new Error('Mode broke in awaitDesiredMLSLength');
386
+ }
387
+
388
+ await sleep(time_to_wait);
389
+ };
390
+
391
+ /** .
392
+ * .
393
+ * .
394
+ * Passed to the calibration steps function, awaits the onset of the signal to ensure a steady state
395
+ *
396
+ * @example
397
+ */
398
+ #awaitSignalOnset = async () => {
399
+ this.stepNum += 1;
400
+ console.log('await signal onset ' + this.stepNum);
401
+ this.status =
402
+ `All Hz Calibration: waiting for the signal to stabilize...`.toString() +
403
+ this.generateTemplate();
404
+ this.emit('update', {
405
+ message: this.status,
406
+ });
407
+ let number_of_bursts_to_skip = this.num_mls_to_skip;
408
+ let time_to_sleep = 0;
409
+ if (this.mode === 'unfiltered') {
410
+ time_to_sleep = (this.#mls.length / this.sourceSamplingRate) * number_of_bursts_to_skip;
411
+ } else if (this.mode === 'filtered') {
412
+ console.log(this.#currentConvolution.length);
413
+ time_to_sleep =
414
+ (this.#currentConvolution.length / this.sourceSamplingRate) *
415
+ (number_of_bursts_to_skip / (number_of_bursts_to_skip + this.numMLSPerCapture));
416
+ } else {
417
+ throw new Error('Mode broke in awaitSignalOnset');
418
+ }
419
+ await sleep(time_to_sleep);
420
+ };
421
+
422
+ /**
423
+ * Called immediately after a recording is captured. Used to process the resulting signal
424
+ * whether by sending the result to a server or by computing a result locally.
425
+ *
426
+ * @example
427
+ */
428
+ #afterMLSRecord = () => {
429
+ console.log('after record');
430
+ this.sendRecordingToServerForProcessing();
431
+ };
432
+
433
+ #afterMLSwIIRRecord = () => {
434
+ if (this.numSuccessfulCaptured < 1) {
435
+ this.numSuccessfulCaptured += 1;
436
+ this.stepNum += 1;
437
+ this.incrementStatusBar();
438
+ console.log('after mls w iir record for some reason add numSucc capt ' + this.stepNum);
439
+ this.status =
440
+ `All Hz Calibration: ${this.numSuccessfulCaptured} recording of convolved MLS captured`.toString() +
441
+ this.generateTemplate().toString();
442
+ this.emit('update', {
443
+ message: this.status,
444
+ });
445
+ }
446
+ };
447
+
448
+ /** .
449
+ * .
450
+ * .
451
+ * Created an S Curver Buffer to taper the signal onset
452
+ *
453
+ * @param {*} length
454
+ * @param {*} phase
455
+ * @returns
456
+ * @example
457
+ */
458
+ static createSCurveBuffer = (length, phase) => {
459
+ const curve = new Float32Array(length);
460
+ let i;
461
+ for (i = 0; i < length; i += 1) {
462
+ // scale the curve to be between 0-1
463
+ curve[i] = Math.sin((Math.PI * i) / length - phase) / 2 + 0.5;
464
+ }
465
+ return curve;
466
+ };
467
+
468
+ static createInverseSCurveBuffer = (length, phase) => {
469
+ const curve = new Float32Array(length);
470
+ let i;
471
+ let j = length - 1;
472
+ for (i = 0; i < length; i += 1) {
473
+ // scale the curve to be between 0-1
474
+ curve[i] = Math.sin((Math.PI * j) / length - phase) / 2 + 0.5;
475
+ j -= 1;
476
+ }
477
+ return curve;
478
+ };
479
+
480
+ /**
481
+ * Construct a Calibration Node with the calibration parameters.
482
+ *
483
+ * @param dataBuffer
484
+ * @private
485
+ * @example
486
+ */
487
+ #createCalibrationNodeFromBuffer = dataBuffer => {
488
+ console.log('databuffer');
489
+ console.log(dataBuffer);
490
+ if (!this.sourceAudioContext) {
491
+ this.makeNewSourceAudioContext();
492
+ }
493
+
494
+ const buffer = this.sourceAudioContext.createBuffer(
495
+ 1, // number of channels
496
+ dataBuffer.length,
497
+ this.sourceAudioContext.sampleRate // sample rate
498
+ );
499
+
500
+ const data = buffer.getChannelData(0); // get data
501
+ // fill the buffer with our data
502
+ try {
503
+ for (let i = 0; i < dataBuffer.length; i += 1) {
504
+ data[i] = dataBuffer[i] * 0.1;
505
+ }
506
+ } catch (error) {
507
+ console.error(error);
508
+ }
509
+
510
+ this.sourceNode = this.sourceAudioContext.createBufferSource();
511
+
512
+ this.sourceNode.buffer = buffer;
513
+ if (this.mode === 'filtered') {
514
+ this.sourceNode.loop = false;
515
+ } else {
516
+ this.sourceNode.loop = true;
517
+ }
518
+
519
+ this.sourceNode.connect(this.sourceAudioContext.destination);
520
+
521
+ this.addCalibrationNode(this.sourceNode);
522
+ };
523
+
524
+ /**
525
+ * Given a data buffer, creates the required calibration node
526
+ *
527
+ * @param {*} dataBufferArray
528
+ * @example
529
+ */
530
+ #setCalibrationNodesFromBuffer = (dataBufferArray = [this.#mlsBufferView]) => {
531
+ if (dataBufferArray.length === 1) {
532
+ this.#createCalibrationNodeFromBuffer(dataBufferArray[0]);
533
+ } else {
534
+ throw new Error('The length of the data buffer array must be 1');
535
+ }
536
+ };
537
+
538
+ /**
539
+ * Creates an audio context and plays it for a few seconds.
540
+ *
541
+ * @private
542
+ * @returns - Resolves when the audio is done playing.
543
+ * @example
544
+ */
545
+ #playCalibrationAudio = () => {
546
+ this.calibrationNodes[0].start(0);
547
+ this.status = ``;
548
+ if (this.mode === 'unfiltered') {
549
+ this.#mls = this.calibrationNodes[0].buffer.getChannelData(0);
550
+ console.log('play calibration audio ' + this.stepNum);
551
+ this.status =
552
+ `All Hz Calibration: playing the calibration tone...`.toString() +
553
+ this.generateTemplate().toString();
554
+ } else if (this.mode === 'filtered') {
555
+ console.log('play convolved audio ' + this.stepNum);
556
+ this.status =
557
+ `All Hz Calibration: playing the convolved calibration tone...`.toString() +
558
+ this.generateTemplate().toString();
559
+ } else {
560
+ throw new Error('Mode is incorrect');
561
+ }
562
+ this.emit('update', {message: this.status});
563
+ this.stepNum += 1;
564
+ console.log('sink sampling rate');
565
+ console.log(this.sinkSamplingRate);
566
+ console.log('source sampling rate');
567
+ console.log(this.sourceSamplingRate);
568
+ };
569
+
570
+ /** .
571
+ * .
572
+ * .
573
+ * Stops the audio with tapered offset
574
+ *
575
+ * @example
576
+ */
577
+ #stopCalibrationAudio = () => {
578
+ this.calibrationNodes[0].stop(0);
579
+ this.calibrationNodes = [];
580
+ this.sourceNode.disconnect();
581
+ this.stepNum += 1;
582
+ console.log('stop calibration audio ' + this.stepNum);
583
+ this.status =
584
+ `All Hz Calibration: stopping the calibration tone...`.toString() +
585
+ this.generateTemplate().toString();
586
+ this.emit('update', {message: this.status});
587
+ };
588
+
589
+ playMLSwithIIR = async (stream, iir) => {
590
+ let checkRec = false;
591
+ this.mode = 'filtered';
592
+ console.log('play mls with iir');
593
+ this.invertedImpulseResponse = iir;
594
+
595
+ await this.calibrationSteps(
596
+ stream,
597
+ this.#playCalibrationAudio, // play audio func (required)
598
+ this.#createCalibrationNodeFromBuffer(this.#currentConvolution), // before play func
599
+ this.#awaitSignalOnset, // before record
600
+ () => this.numSuccessfulCaptured < 1,
601
+ this.#awaitDesiredMLSLength, // during record
602
+ this.#afterMLSwIIRRecord, // after record
603
+ this.mode,
604
+ checkRec
605
+ );
606
+ };
607
+
608
+ /**
609
+ * Public method to start the calibration process. Objects intialized from webassembly allocate new memory
610
+ * and must be manually freed. This function is responsible for intializing the MlsGenInterface,
611
+ * and wrapping the calibration steps with a garbage collection safe gaurd.
612
+ *
613
+ * @public
614
+ * @param stream - The stream of audio from the Listener.
615
+ * @example
616
+ */
617
+ startCalibrationImpulseResponse = async stream => {
618
+ let desired_time = this.desired_time_per_mls;
619
+ let checkRec = 'allhz';
620
+
621
+ console.log('MLS sequence should be of length: ' + this.sourceSamplingRate * desired_time);
622
+
623
+ length = this.sourceSamplingRate * desired_time;
624
+ //get mls here
625
+ await this.pyServerAPI
626
+ .getMLSWithRetry(length)
627
+ .then(res => {
628
+ console.log(res);
629
+ this.#mlsBufferView = res['mls'];
630
+ })
631
+ .catch(err => {
632
+ // this.emit('InvertedImpulseResponse', {res: false});
633
+ console.error(err);
634
+ });
635
+ await this.calibrationSteps(
636
+ stream,
637
+ this.#playCalibrationAudio, // play audio func (required)
638
+ this.#createCalibrationNodeFromBuffer(this.#mlsBufferView), // before play func
639
+ this.#awaitSignalOnset, // before record
640
+ () => this.numSuccessfulCaptured < this.numCaptures, // loop while true
641
+ this.#awaitDesiredMLSLength, // during record
642
+ this.#afterMLSRecord, // after record
643
+ this.mode,
644
+ checkRec
645
+ ),
646
+ this.#stopCalibrationAudio();
647
+ checkRec = false;
648
+
649
+ // at this stage we've captured all the required signals,
650
+ // and have received IRs for each one
651
+ // so let's send all the IRs to the server to be converted to a single IIR
652
+ await this.sendSystemImpulseResponsesToServerForProcessing();
653
+ await this.sendComponentImpulseResponsesToServerForProcessing();
654
+
655
+ this.numSuccessfulCaptured = 0;
656
+
657
+ let iir_ir_and_plots;
658
+ if (this._calibrateSoundCheck != 'none') {
659
+ if (this._calibrateSoundCheck != 'system') {
660
+ this.#currentConvolution = this.componentConvolution;
661
+ } else {
662
+ this.#currentConvolution = this.systemConvolution;
663
+ }
664
+ await this.playMLSwithIIR(stream, this.invertedImpulseResponse);
665
+ this.#stopCalibrationAudio();
666
+ this.sourceAudioContext.close();
667
+ let conv_recs = this.getAllFilteredRecordedSignals();
668
+ let recs = this.getAllUnfilteredRecordedSignals();
669
+ console.log(
670
+ 'Obtaining unfiltered recording from #allHzUnfilteredRecordings to calculate PSD'
671
+ );
672
+ console.log('Obtaining filtered recording from #allHzFilteredRecordings to calculate PSD');
673
+ let unconv_rec = recs[0];
674
+ let conv_rec = conv_recs[0];
675
+ if (this._calibrateSoundCheck != 'system') {
676
+ let knownGain = this.oldComponentIR.Gain;
677
+ let knownFreq = this.oldComponentIR.Freq;
678
+ let sampleRate = this.sourceSamplingRate || 96000;
679
+ let unconv_results = await this.pyServerAPI
680
+ .getSubtractedPSDWithRetry(unconv_rec, knownGain, knownFreq, sampleRate)
681
+ .then(res => {
682
+ this.incrementStatusBar();
683
+ this.status =
684
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
685
+ this.generateTemplate().toString();
686
+ this.emit('update', {message: this.status});
687
+ return res;
688
+ })
689
+ .catch(err => {
690
+ console.error(err);
691
+ });
692
+
693
+ let conv_results = await this.pyServerAPI
694
+ .getSubtractedPSDWithRetry(conv_rec, knownGain, knownFreq, sampleRate)
695
+ .then(res => {
696
+ this.incrementStatusBar();
697
+ this.status =
698
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
699
+ this.generateTemplate().toString();
700
+ this.emit('update', {message: this.status});
701
+ return res;
702
+ })
703
+ .catch(err => {
704
+ console.error(err);
705
+ });
706
+ unconv_rec = this.componentInvertedImpulseResponseNoBandpass;
707
+ conv_rec = this.componentInvertedImpulseResponse;
708
+ let component_iir_psd = await this.pyServerAPI
709
+ .getPSDWithRetry({
710
+ unconv_rec,
711
+ conv_rec,
712
+ sampleRate: this.sourceSamplingRate || 96000,
713
+ })
714
+ .then(res => {
715
+ this.incrementStatusBar();
716
+ this.status =
717
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
718
+ this.generateTemplate().toString();
719
+ this.emit('update', {message: this.status});
720
+ return res;
721
+ })
722
+ .catch(err => {
723
+ console.error(err);
724
+ });
725
+ unconv_rec = this.systemInvertedImpulseResponseNoBandpass;
726
+ conv_rec = this.systemInvertedImpulseResponse;
727
+ let system_iir_psd = await this.pyServerAPI
728
+ .getPSDWithRetry({
729
+ unconv_rec,
730
+ conv_rec,
731
+ sampleRate: this.sourceSamplingRate || 96000,
732
+ })
733
+ .then(res => {
734
+ this.incrementStatusBar();
735
+ this.status =
736
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
737
+ this.generateTemplate().toString();
738
+ this.emit('update', {message: this.status});
739
+ return res;
740
+ })
741
+ .catch(err => {
742
+ console.error(err);
743
+ });
744
+ iir_ir_and_plots = {
745
+ systemIIR: this.systemInvertedImpulseResponse,
746
+ componentIIR: this.componentInvertedImpulseResponse,
747
+ x_unconv: unconv_results['x'],
748
+ y_unconv: unconv_results['y'],
749
+ x_conv: conv_results['x'],
750
+ y_conv: conv_results['y'],
751
+ componentIR: this.componentIR,
752
+ systemIR: this.systemIR,
753
+ x_system_iir: system_iir_psd['x_conv'],
754
+ y_system_iir: system_iir_psd['y_conv'],
755
+ x_system_iir_no_bandpass: system_iir_psd['x_unconv'],
756
+ y_system_iir_no_bandpass: system_iir_psd['y_unconv'],
757
+ x_component_iir: component_iir_psd['x_conv'],
758
+ y_component_iir: component_iir_psd['y_conv'],
759
+ x_component_iir_no_bandpass: component_iir_psd['x_unconv'],
760
+ y_component_iir_no_bandpass: component_iir_psd['y_unconv'],
761
+ };
762
+ } else {
763
+ let results = await this.pyServerAPI
764
+ .getPSDWithRetry({
765
+ unconv_rec,
766
+ conv_rec,
767
+ sampleRate: this.sourceSamplingRate || 96000,
768
+ })
769
+ .then(res => {
770
+ this.incrementStatusBar();
771
+ this.status =
772
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
773
+ this.generateTemplate().toString();
774
+ this.emit('update', {message: this.status});
775
+ return res;
776
+ })
777
+ .catch(err => {
778
+ console.error(err);
779
+ });
780
+
781
+ //iir w/ and without bandpass psd
782
+ unconv_rec = this.componentInvertedImpulseResponseNoBandpass;
783
+ conv_rec = this.componentInvertedImpulseResponse;
784
+ let component_iir_psd = await this.pyServerAPI
785
+ .getPSDWithRetry({
786
+ unconv_rec,
787
+ conv_rec,
788
+ sampleRate: this.sourceSamplingRate || 96000,
789
+ })
790
+ .then(res => {
791
+ this.incrementStatusBar();
792
+ this.status =
793
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
794
+ this.generateTemplate().toString();
795
+ this.emit('update', {message: this.status});
796
+ return res;
797
+ })
798
+ .catch(err => {
799
+ console.error(err);
800
+ });
801
+ unconv_rec = this.systemInvertedImpulseResponseNoBandpass;
802
+ conv_rec = this.systemInvertedImpulseResponse;
803
+ let system_iir_psd = await this.pyServerAPI
804
+ .getPSDWithRetry({
805
+ unconv_rec,
806
+ conv_rec,
807
+ sampleRate: this.sourceSamplingRate || 96000,
808
+ })
809
+ .then(res => {
810
+ this.incrementStatusBar();
811
+ this.status =
812
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
813
+ this.generateTemplate().toString();
814
+ this.emit('update', {message: this.status});
815
+ return res;
816
+ })
817
+ .catch(err => {
818
+ console.error(err);
819
+ });
820
+
821
+ iir_ir_and_plots = {
822
+ systemIIR: this.systemInvertedImpulseResponse,
823
+ componentIIR: this.componentInvertedImpulseResponse,
824
+ x_unconv: results['x_unconv'],
825
+ y_unconv: results['y_unconv'],
826
+ x_conv: results['x_conv'],
827
+ y_conv: results['y_conv'],
828
+ componentIR: this.componentIR,
829
+ systemIR: this.systemIR,
830
+ x_system_iir: system_iir_psd['x_conv'],
831
+ y_system_iir: system_iir_psd['y_conv'],
832
+ x_system_iir_no_bandpass: system_iir_psd['x_unconv'],
833
+ y_system_iir_no_bandpass: system_iir_psd['y_unconv'],
834
+ x_component_iir: component_iir_psd['x_conv'],
835
+ y_component_iir: component_iir_psd['y_conv'],
836
+ x_component_iir_no_bandpass: component_iir_psd['x_unconv'],
837
+ y_component_iir_no_bandpass: component_iir_psd['y_unconv'],
838
+ };
839
+ }
840
+
841
+ if (this.#download) {
842
+ this.downloadSingleUnfilteredRecording();
843
+ this.downloadSingleFilteredRecording();
844
+ saveToCSV(this.#mls, 'MLS.csv');
845
+ saveToCSV(this.componentConvolution, 'python_component_convolution_mls_iir.csv');
846
+ saveToCSV(this.systemConvolution, 'python_system_convolution_mls_iir.csv');
847
+ saveToCSV(this.componentInvertedImpulseResponse, 'componentIIR.csv');
848
+ saveToCSV(this.systemInvertedImpulseResponse, 'systemIIR.csv');
849
+ for (let i = 0; i < this.autocorrelations.length; i++) {
850
+ saveToCSV(this.autocorrelations[i], `autocorrelation_${i}`);
851
+ }
852
+ const computedIRagain = await Promise.all(this.impulseResponses).then(res => {
853
+ for (let i = 0; i < res.length; i++) {
854
+ if (res[i] != undefined) {
855
+ saveToCSV(res[i], `IR_${i}`);
856
+ }
857
+ }
858
+ });
859
+ }
860
+ } else {
861
+ let unconv_rec = this.componentInvertedImpulseResponseNoBandpass;
862
+ let conv_rec = this.componentInvertedImpulseResponse;
863
+ let component_iir_psd = await this.pyServerAPI
864
+ .getPSDWithRetry({
865
+ unconv_rec,
866
+ conv_rec,
867
+ sampleRate: this.sourceSamplingRate || 96000,
868
+ })
869
+ .then(res => {
870
+ this.incrementStatusBar();
871
+ this.status =
872
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
873
+ this.generateTemplate().toString();
874
+ this.emit('update', {message: this.status});
875
+ return res;
876
+ })
877
+ .catch(err => {
878
+ console.error(err);
879
+ });
880
+ unconv_rec = this.systemInvertedImpulseResponseNoBandpass;
881
+ conv_rec = this.systemInvertedImpulseResponse;
882
+ let system_iir_psd = await this.pyServerAPI
883
+ .getPSDWithRetry({
884
+ unconv_rec,
885
+ conv_rec,
886
+ sampleRate: this.sourceSamplingRate || 96000,
887
+ })
888
+ .then(res => {
889
+ this.incrementStatusBar();
890
+ this.status =
891
+ `All Hz Calibration: done computing the PSD graphs...`.toString() +
892
+ this.generateTemplate().toString();
893
+ this.emit('update', {message: this.status});
894
+ return res;
895
+ })
896
+ .catch(err => {
897
+ console.error(err);
898
+ });
899
+ iir_ir_and_plots = {
900
+ systemIIR: this.systemInvertedImpulseResponse,
901
+ componentIIR: this.componentInvertedImpulseResponse,
902
+ x_unconv: [],
903
+ y_unconv: [],
904
+ x_conv: [],
905
+ y_conv: [],
906
+ componentIR: this.componentIR,
907
+ systemIR: this.systemIR,
908
+ x_system_iir: system_iir_psd['x_conv'],
909
+ y_system_iir: system_iir_psd['y_conv'],
910
+ x_system_iir_no_bandpass: system_iir_psd['x_unconv'],
911
+ y_system_iir_no_bandpass: system_iir_psd['y_unconv'],
912
+ x_component_iir: component_iir_psd['x_conv'],
913
+ y_component_iir: component_iir_psd['y_conv'],
914
+ x_component_iir_no_bandpass: component_iir_psd['x_unconv'],
915
+ y_component_iir_no_bandpass: component_iir_psd['y_unconv'],
916
+ };
917
+ if (this.#download) {
918
+ saveToCSV(this.#mls, 'MLS.csv');
919
+ saveToCSV(this.componentConvolution, 'python_component_convolution_mls_iir.csv');
920
+ saveToCSV(this.systemConvolution, 'python_system_convolution_mls_iir.csv');
921
+ saveToCSV(this.componentInvertedImpulseResponse, 'componentIIR.csv');
922
+ saveToCSV(this.systemInvertedImpulseResponse, 'systemIIR.csv');
923
+ for (let i = 0; i < this.autocorrelations.length; i++) {
924
+ saveToCSV(this.autocorrelations[i], `autocorrelation_${i}`);
925
+ }
926
+ const computedIRagain = await Promise.all(this.impulseResponses).then(res => {
927
+ for (let i = 0; i < res.length; i++) {
928
+ if (res[i] != undefined) {
929
+ saveToCSV(res[i], `IR_${i}`);
930
+ }
931
+ }
932
+ });
933
+ }
934
+ }
935
+
936
+ this.percent_complete = 100;
937
+
938
+ this.status = `All Hz Calibration: Finished`.toString() + this.generateTemplate().toString();
939
+ this.emit('update', {message: this.status});
940
+
941
+ //here after calibration we have the component calibration (either loudspeaker or microphone) in the same form as the componentIR
942
+ //that was used to calibrate
943
+
944
+ return iir_ir_and_plots;
945
+ };
946
+
947
+ //////////////////////volume
948
+
949
+ handleIncomingData = data => {
950
+ console.log('Received data: ', data);
951
+ if (data.type === 'soundGainDBSPL') {
952
+ this.soundGainDBSPL = data.value;
953
+ } else {
954
+ throw new Error(`Unknown data type: ${data.type}`);
955
+ }
956
+ };
957
+ createSCurveBuffer = (onSetBool = true) => {
958
+ const curve = new Float32Array(this.TAPER_SECS * this.sourceSamplingRate + 1);
959
+ const frequency = 1 / (4 * this.TAPER_SECS);
960
+ let j = 0;
961
+ for (let i = 0; i < this.TAPER_SECS * this.sourceSamplingRate + 1; i += 1) {
962
+ const phase = 2 * Math.PI * frequency * j;
963
+ const onsetTaper = Math.pow(Math.sin(phase), 2);
964
+ const offsetTaper = Math.pow(Math.cos(phase), 2);
965
+ curve[i] = onSetBool ? onsetTaper : offsetTaper;
966
+ j += 1 / this.sourceSamplingRate;
967
+ }
968
+ return curve;
969
+ };
970
+
971
+ #getTruncatedSignal = (left = 3.5, right = 4.5) => {
972
+ const start = Math.floor(left * this.sourceSamplingRate);
973
+ const end = Math.floor(right * this.sourceSamplingRate);
974
+ const result = Array.from(this.getLastVolumeRecordedSignal().slice(start, end));
975
+ console.log(
976
+ 'Obtaining last 1000 hz recording from #allVolumeRecordings to send for processing'
977
+ );
978
+ /**
979
+ * function to check that capture was properly made
980
+ * @param {*} list
981
+ */
982
+ const checkResult = list => {
983
+ const setItem = new Set(list);
984
+ if (setItem.size === 1 && setItem.has(0)) {
985
+ console.warn(
986
+ 'The last capture failed, all recorded signal is zero',
987
+ this.getAllVolumeRecordedSignals()
988
+ );
989
+ }
990
+ if (setItem.size === 0) {
991
+ console.warn('The last capture failed, no recorded signal');
992
+ }
993
+ };
994
+ checkResult(result);
995
+ return result;
996
+ };
997
+
998
+ /**
999
+ *
1000
+ *
1001
+ Construct a calibration Node with the calibration parameters and given gain value
1002
+ * @param {*} gainValue
1003
+ * */
1004
+ #createCalibrationToneWithGainValue = gainValue => {
1005
+ const audioContext = this.makeNewSourceAudioContext();
1006
+ const oscilator = audioContext.createOscillator();
1007
+ const gainNode = audioContext.createGain();
1008
+ const taperGainNode = audioContext.createGain();
1009
+ const offsetGainNode = audioContext.createGain();
1010
+ const totalDuration = this.CALIBRATION_TONE_DURATION * 1.2;
1011
+
1012
+ oscilator.frequency.value = this.#CALIBRATION_TONE_FREQUENCY;
1013
+ oscilator.type = this.#CALIBRATION_TONE_TYPE;
1014
+ gainNode.gain.value = gainValue;
1015
+
1016
+ oscilator.connect(gainNode);
1017
+ gainNode.connect(taperGainNode);
1018
+ const onsetCurve = this.createSCurveBuffer();
1019
+ taperGainNode.gain.setValueCurveAtTime(onsetCurve, 0, this.TAPER_SECS);
1020
+ taperGainNode.connect(offsetGainNode);
1021
+ const offsetCurve = this.createSCurveBuffer(false);
1022
+ offsetGainNode.gain.setValueCurveAtTime(
1023
+ offsetCurve,
1024
+ totalDuration - this.TAPER_SECS,
1025
+ this.TAPER_SECS
1026
+ );
1027
+ offsetGainNode.connect(audioContext.destination);
1028
+
1029
+ this.addCalibrationNode(oscilator);
1030
+ };
1031
+
1032
+ /**
1033
+ * Construct a Calibration Node with the calibration parameters.
1034
+ *
1035
+ * @private
1036
+ * @example
1037
+ */
1038
+ #createCalibrationNode = () => {
1039
+ const audioContext = this.makeNewSourceAudioContext();
1040
+ const oscilator = audioContext.createOscillator();
1041
+ const gainNode = audioContext.createGain();
1042
+
1043
+ oscilator.frequency.value = this.#CALIBRATION_TONE_FREQUENCY;
1044
+ oscilator.type = this.#CALIBRATION_TONE_TYPE;
1045
+ gainNode.gain.value = 0.04;
1046
+
1047
+ oscilator.connect(gainNode);
1048
+ gainNode.connect(audioContext.destination);
1049
+
1050
+ this.addCalibrationNode(oscilator);
1051
+ };
1052
+
1053
+ #playCalibrationAudioVolume = async () => {
1054
+ const totalDuration = this.CALIBRATION_TONE_DURATION * 1.2;
1055
+
1056
+ this.calibrationNodes[0].start(0);
1057
+ this.calibrationNodes[0].stop(totalDuration);
1058
+ console.log(`Playing a buffer of ${this.CALIBRATION_TONE_DURATION} seconds of audio`);
1059
+ console.log(`Waiting a total of ${totalDuration} seconds`);
1060
+ await sleep(totalDuration);
1061
+ };
1062
+
1063
+ #sendToServerForProcessing = lCalib => {
1064
+ console.log('Sending data to server');
1065
+ this.pyServerAPI
1066
+ .getVolumeCalibration({
1067
+ sampleRate: this.sourceSamplingRate,
1068
+ payload: this.#getTruncatedSignal(),
1069
+ lCalib: lCalib,
1070
+ })
1071
+ .then(res => {
1072
+ if (this.outDBSPL === null) {
1073
+ this.incrementStatusBar();
1074
+ this.outDBSPL = res['outDbSPL'];
1075
+ this.outDBSPL1000 = res['outDbSPL1000'];
1076
+ this.THD = res['thd'];
1077
+ }
1078
+ })
1079
+ .catch(err => {
1080
+ console.warn(err);
1081
+ });
1082
+ };
1083
+
1084
+ startCalibrationVolume = async (stream, gainValues, lCalib, componentGainDBSPL) => {
1085
+ const trialIterations = gainValues.length;
1086
+ this.status_denominator += trialIterations;
1087
+ const thdValues = [];
1088
+ const inDBValues = [];
1089
+ let inDB = 0;
1090
+ const outDBSPLValues = [];
1091
+ const outDBSPL1000Values = [];
1092
+ let checkRec = false;
1093
+
1094
+ // do one calibration that will be discarded
1095
+ const soundLevelToDiscard = -60;
1096
+ const gainToDiscard = Math.pow(10, soundLevelToDiscard / 20);
1097
+ this.status =
1098
+ `1000 Hz Calibration: Sound Level ${soundLevelToDiscard} dB`.toString() +
1099
+ this.generateTemplate().toString();
1100
+ //this.emit('update', {message: `1000 Hz Calibration: Sound Level ${soundLevelToDiscard} dB`});
1101
+ this.emit('update', {message: this.status});
1102
+
1103
+ do {
1104
+ // eslint-disable-next-line no-await-in-loop
1105
+ await this.volumeCalibrationSteps(
1106
+ stream,
1107
+ this.#playCalibrationAudioVolume,
1108
+ this.#createCalibrationToneWithGainValue,
1109
+ this.#sendToServerForProcessing,
1110
+ gainToDiscard,
1111
+ lCalib, //todo make this a class parameter
1112
+ checkRec
1113
+ );
1114
+ } while (this.outDBSPL === null);
1115
+ //reset the values
1116
+ //this.incrementStatusBar();
1117
+
1118
+ this.outDBSPL = null;
1119
+ this.outDBSPL = null;
1120
+ this.outDBSPL1000 = null;
1121
+ this.THD = null;
1122
+
1123
+ // run the calibration at different gain values provided by the user
1124
+ for (let i = 0; i < trialIterations; i++) {
1125
+ //convert gain to DB and add to inDB
1126
+ if (i == trialIterations - 1) {
1127
+ checkRec = 'loudest';
1128
+ }
1129
+ inDB = Math.log10(gainValues[i]) * 20;
1130
+ // precision to 1 decimal place
1131
+ inDB = Math.round(inDB * 10) / 10;
1132
+ inDBValues.push(inDB);
1133
+ console.log('next update');
1134
+ this.status =
1135
+ `1000 Hz Calibration: Sound Level ${inDB} dB`.toString() +
1136
+ this.generateTemplate().toString();
1137
+ this.emit('update', {message: this.status});
1138
+ do {
1139
+ // eslint-disable-next-line no-await-in-loop
1140
+ await this.volumeCalibrationSteps(
1141
+ stream,
1142
+ this.#playCalibrationAudioVolume,
1143
+ this.#createCalibrationToneWithGainValue,
1144
+ this.#sendToServerForProcessing,
1145
+ gainValues[i],
1146
+ lCalib, //todo make this a class parameter
1147
+ checkRec
1148
+ );
1149
+ } while (this.outDBSPL === null);
1150
+ outDBSPL1000Values.push(this.outDBSPL1000);
1151
+ thdValues.push(this.THD);
1152
+ outDBSPLValues.push(this.outDBSPL);
1153
+
1154
+ this.outDBSPL = null;
1155
+ this.outDBSPL1000 = null;
1156
+ this.THD = null;
1157
+ }
1158
+
1159
+ // get the volume calibration parameters from the server
1160
+ const parameters = await this.pyServerAPI
1161
+ .getVolumeCalibrationParameters({
1162
+ inDBValues: inDBValues,
1163
+ outDBSPLValues: outDBSPL1000Values,
1164
+ lCalib: lCalib,
1165
+ componentGainDBSPL,
1166
+ })
1167
+ .then(res => {
1168
+ this.incrementStatusBar();
1169
+ return res;
1170
+ });
1171
+ const result = {
1172
+ parameters: parameters,
1173
+ inDBValues: inDBValues,
1174
+ outDBSPLValues: outDBSPLValues,
1175
+ outDBSPL1000Values: outDBSPL1000Values,
1176
+ thdValues: thdValues,
1177
+ };
1178
+
1179
+ return result;
1180
+ };
1181
+
1182
+ // function to write frq and gain to firebase database given speakerID
1183
+ writeFrqGain = async (speakerID, frq, gain, OEM) => {
1184
+ // freq and gain are too large to take samples 1 in every 100 samples
1185
+
1186
+ const sampledFrq = [];
1187
+ const sampledGain = [];
1188
+ for (let i = 0; i < frq.length; i += 100) {
1189
+ sampledFrq.push(frq[i]);
1190
+ sampledGain.push(gain[i]);
1191
+ }
1192
+
1193
+ const data = {Freq: sampledFrq, Gain: sampledGain};
1194
+
1195
+ await set(ref(database, `Microphone2/${OEM}/${speakerID}/linear`), data);
1196
+ };
1197
+
1198
+ // Function to Read frq and gain from firebase database given speakerID
1199
+ // returns an array of frq and gain if speakerID exists, returns null otherwise
1200
+
1201
+ readFrqGain = async (speakerID, OEM) => {
1202
+ const dbRef = ref(database);
1203
+ const snapshot = await get(child(dbRef, `Microphone2/${OEM}/${speakerID}/linear`));
1204
+ if (snapshot.exists()) {
1205
+ return snapshot.val();
1206
+ }
1207
+ return null;
1208
+ };
1209
+
1210
+ readGainat1000Hz = async (speakerID, OEM) => {
1211
+ const dbRef = ref(database);
1212
+ const snapshot = await get(child(dbRef, `Microphone2/${OEM}/${speakerID}/Gain1000`));
1213
+ if (snapshot.exists()) {
1214
+ return snapshot.val();
1215
+ }
1216
+ return null;
1217
+ };
1218
+
1219
+ writeGainat1000Hz = async (speakerID, gain, OEM) => {
1220
+ const data = {Gain: gain};
1221
+ await set(ref(database, `Microphone2/${OEM}/${speakerID}/Gain1000`), gain);
1222
+ };
1223
+
1224
+ writeIsSmartPhone = async (speakerID, isSmartPhone, OEM) => {
1225
+ const data = {isSmartPhone: isSmartPhone};
1226
+ await set(ref(database, `Microphone2/${OEM}/${speakerID}/isSmartPhone`), isSmartPhone);
1227
+ };
1228
+
1229
+ doesMicrophoneExist = async (speakerID, OEM) => {
1230
+ const dbRef = ref(database);
1231
+ const snapshot = await get(child(dbRef, `Microphone2/${OEM}/${speakerID}`));
1232
+ if (snapshot.exists()) {
1233
+ return true;
1234
+ }
1235
+ return false;
1236
+ };
1237
+
1238
+ addMicrophoneInfo = async (speakerID, OEM, micInfo) => {
1239
+ // add to database if /info does not exist
1240
+ const dbRef = ref(database);
1241
+ const snapshot = await get(child(dbRef, `Microphone2/${OEM}/${speakerID}/info`));
1242
+ if (!snapshot.exists()) {
1243
+ await set(ref(database, `Microphone2/${OEM}/${speakerID}/info`), micInfo);
1244
+ }
1245
+ };
1246
+
1247
+ convertToDB = gain => {
1248
+ return Math.log10(gain) * 20;
1249
+ };
1250
+
1251
+ // Function to perform linear interpolation between two points
1252
+ interpolate(x, x0, y0, x1, y1) {
1253
+ return y0 + ((x - x0) * (y1 - y0)) / (x1 - x0);
1254
+ }
1255
+
1256
+ findGainatFrequency = (frequencies, gains, targetFrequency) => {
1257
+ // Find the index of the first frequency in the array greater than the target frequency
1258
+ let index = 0;
1259
+ while (index < frequencies.length && frequencies[index] < targetFrequency) {
1260
+ index++;
1261
+ }
1262
+
1263
+ // Handle cases when the target frequency is outside the range of the given data
1264
+ if (index === 0) {
1265
+ return gains[0];
1266
+ } else if (index === frequencies.length) {
1267
+ return gains[gains.length - 1];
1268
+ } else {
1269
+ // Interpolate the gain based on the surrounding frequencies
1270
+ const x0 = frequencies[index - 1];
1271
+ const y0 = gains[index - 1];
1272
+ const x1 = frequencies[index];
1273
+ const y1 = gains[index];
1274
+ return this.interpolate(targetFrequency, x0, y0, x1, y1);
1275
+ }
1276
+ };
1277
+
1278
+ // Example of how to use the writeFrqGain and readFrqGain functions
1279
+ // writeFrqGain('speaker1', [1, 2, 3], [4, 5, 6]);
1280
+ // Speaker1 is the speakerID you want to write to in the database
1281
+ // readFrqGain('MiniDSPUMIK_1').then(data => console.log(data));
1282
+ // MiniDSPUMIK_1 is the speakerID with some Data in the database
1283
+ //adding gainDBSPL
1284
+ startCalibration = async (
1285
+ stream,
1286
+ gainValues,
1287
+ lCalib = 104.92978421490648,
1288
+ componentIR = null,
1289
+ microphoneName = 'MiniDSP-UMIK1-711-4754-vertical',
1290
+ _calibrateSoundCheck = 'goal', //GOAL PASSed in by default
1291
+ isSmartPhone = false,
1292
+ _calibrateSoundBurstRepeats = 4,
1293
+ _calibrateSoundBurstSec = 1,
1294
+ _calibrateSoundBurstsWarmup = 1,
1295
+ _calibrateSoundHz = 48000,
1296
+ _calibrateSoundIIRSec = 0.2,
1297
+ calibrateSound1000HzSec = 5,
1298
+ micManufacturer = '',
1299
+ micSerialNumber = '',
1300
+ micModelNumber = '',
1301
+ micModelName = ''
1302
+ ) => {
1303
+ this.CALIBRATION_TONE_DURATION = calibrateSound1000HzSec;
1304
+ this.iirLength = Math.floor(_calibrateSoundIIRSec * this.sourceSamplingRate);
1305
+ console.log('device info:', this.deviceInfo);
1306
+ this.numMLSPerCapture = _calibrateSoundBurstRepeats;
1307
+ this.desired_time_per_mls = _calibrateSoundBurstSec;
1308
+ this.num_mls_to_skip = _calibrateSoundBurstsWarmup;
1309
+ this.desired_sampling_rate = _calibrateSoundHz;
1310
+
1311
+ //feed calibration goal here
1312
+ this._calibrateSoundCheck = _calibrateSoundCheck;
1313
+ //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
1314
+ //check the db based on the microphone currently connected
1315
+
1316
+ //new lCalib found at top of calibration files *1000hz, make sure to correct
1317
+ //based on zeroing of 1000hz, search for "*1000Hz"
1318
+ const ID = isSmartPhone ? micModelNumber : micSerialNumber;
1319
+ const OEM = isSmartPhone ? this.deviceInfo.OEM : micManufacturer;
1320
+ const micInfo = {
1321
+ micModelName: isSmartPhone ? micModelName : microphoneName,
1322
+ OEM: OEM,
1323
+ ID: ID,
1324
+ HardwareName: isSmartPhone ? this.deviceInfo.hardwarename : microphoneName,
1325
+ hardwareFamily: isSmartPhone ? this.deviceInfo.hardwarefamily : microphoneName,
1326
+ HardwareModel: isSmartPhone ? this.deviceInfo.hardwaremodel : microphoneName,
1327
+ PlatformName: isSmartPhone ? this.deviceInfo.platformname : 'N/A',
1328
+ PlatformVersion: isSmartPhone ? this.deviceInfo.platformversion : 'N/A',
1329
+ DeviceType: isSmartPhone ? this.deviceInfo.devicetype : 'N/A',
1330
+ };
1331
+ this.addMicrophoneInfo(ID, OEM, micInfo);
1332
+ if (componentIR == null) {
1333
+ //mode 'ir'
1334
+ //global variable this.componentIR must be set
1335
+ this.componentIR = await this.readFrqGain(ID, OEM).then(data => {
1336
+ return data;
1337
+ });
1338
+
1339
+ lCalib = await this.readGainat1000Hz(ID, OEM);
1340
+ micInfo['gainDBSPL'] = lCalib;
1341
+ // this.componentGainDBSPL = this.convertToDB(lCalib);
1342
+ this.componentGainDBSPL = lCalib;
1343
+ //TODO: if this call to database is unknown, cannot perform experiment => return false
1344
+ if (this.componentIR == null) {
1345
+ this.status =
1346
+ `Microphone (${OEM},${ID}) is not found in the database. Please add it to the database.`.toString();
1347
+ this.emit('update', {message: this.status});
1348
+ return false;
1349
+ }
1350
+ } else {
1351
+ this.componentIR = componentIR;
1352
+ lCalib = this.findGainatFrequency(this.componentIR.Freq, this.componentIR.Gain, 1000);
1353
+ // this.componentGainDBSPL = this.convertToDB(lCalib);
1354
+ this.componentGainDBSPL = lCalib;
1355
+ await this.writeIsSmartPhone(ID, isSmartPhone, OEM);
1356
+ }
1357
+
1358
+ this.oldComponentIR = this.componentIR;
1359
+
1360
+ let volumeResults = await this.startCalibrationVolume(
1361
+ stream,
1362
+ gainValues,
1363
+ lCalib,
1364
+ this.componentGainDBSPL
1365
+ );
1366
+
1367
+ let impulseResponseResults = await this.startCalibrationImpulseResponse(stream);
1368
+
1369
+ if (componentIR != null) {
1370
+ //insert Freq and Gain from this.componentIR into db
1371
+ await this.writeFrqGain(
1372
+ ID,
1373
+ impulseResponseResults.componentIR.Freq,
1374
+ impulseResponseResults.componentIR.Gain,
1375
+ OEM
1376
+ );
1377
+ micInfo['gainDBSPL'] = impulseResponseResults.parameters.gainDBSPL;
1378
+ await this.writeGainat1000Hz(ID, micInfo['gainDBSPL'], OEM);
1379
+ }
1380
+
1381
+ const total_results = {...volumeResults, ...impulseResponseResults};
1382
+
1383
+ total_results['micInfo'] = micInfo;
1384
+ console.log('total results');
1385
+ console.log(total_results);
1386
+ return total_results;
1387
+ };
1388
+ }
1389
+
1390
+ export default Combination;