speaker-calibration 2.2.253 → 2.2.255

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 (124) 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/NoSleep.min.js +1 -1
  31. package/dist/example/fetch-languages-sheets.js +77 -77
  32. package/dist/example/i18n.js +29654 -29654
  33. package/dist/example/index.html +47 -47
  34. package/dist/example/listener.html +81 -81
  35. package/dist/example/server.js +51 -51
  36. package/dist/example/speaker.html +145 -145
  37. package/dist/example/speakerUI.js +273 -273
  38. package/dist/example/styles.css +152 -152
  39. package/dist/listener.js +3 -3
  40. package/dist/main.js +10 -10
  41. package/dist/mlsGen.js +6814 -6814
  42. package/dist/mlsGen.wasm +0 -0
  43. package/dist/package-lock.json +1018 -1018
  44. package/dist/package.json +18 -18
  45. package/dist/phonePeer.js +3 -3
  46. package/doc/AudioCalibrator.html +417 -417
  47. package/doc/AudioPeer.html +251 -251
  48. package/doc/AudioRecorder.html +195 -195
  49. package/doc/ImpulseResponse.html +215 -215
  50. package/doc/Listener.html +308 -308
  51. package/doc/MlsGenInterface.html +226 -226
  52. package/doc/MyEventEmitter.html +274 -274
  53. package/doc/PythonServerAPI.html +109 -109
  54. package/doc/Speaker.html +276 -276
  55. package/doc/Takes%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +128 -128
  56. package/doc/Takes%20the%20url%20of%20the%20current%20site%0Aand%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +138 -138
  57. package/doc/Takes%20the%20url%20of%20the%20current%20site%20and%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +137 -137
  58. package/doc/Volume.html +88 -88
  59. package/doc/audioCalibrator.js.html +179 -179
  60. package/doc/audioPeer.js.html +175 -175
  61. package/doc/audioRecorder.js.html +163 -163
  62. package/doc/creates%20a%20new%20AudioRecorder%20instance.%20%0ASets%20up%20the%20audio%20context%20and%20file%20reader..html +114 -114
  63. package/doc/fonts/OpenSans-Bold-webfont.svg +1829 -1829
  64. package/doc/fonts/OpenSans-BoldItalic-webfont.svg +1829 -1829
  65. package/doc/fonts/OpenSans-Italic-webfont.svg +1829 -1829
  66. package/doc/fonts/OpenSans-Light-webfont.svg +1830 -1830
  67. package/doc/fonts/OpenSans-LightItalic-webfont.svg +1834 -1834
  68. package/doc/fonts/OpenSans-Regular-webfont.svg +1830 -1830
  69. package/doc/global.html +308 -308
  70. package/doc/index.html +58 -58
  71. package/doc/listener.js.html +170 -170
  72. package/doc/mlsGen_mlsGenInterface.js.html +117 -117
  73. package/doc/myEventEmitter.js.html +124 -124
  74. package/doc/peer-connection_audioPeer.js.html +188 -188
  75. package/doc/peer-connection_listener.js.html +311 -311
  76. package/doc/peer-connection_speaker.js.html +381 -381
  77. package/doc/scripts/linenumber.js +25 -25
  78. package/doc/scripts/prettify/Apache-License-2.0.txt +202 -202
  79. package/doc/scripts/prettify/lang-css.js +24 -24
  80. package/doc/scripts/prettify/prettify.js +640 -640
  81. package/doc/server_PythonServerAPI.js.html +160 -160
  82. package/doc/speaker.js.html +248 -248
  83. package/doc/styles/jsdoc-default.css +371 -371
  84. package/doc/styles/prettify-jsdoc.css +111 -111
  85. package/doc/styles/prettify-tomorrow.css +163 -163
  86. package/doc/tasks_audioCalibrator.js.html +207 -207
  87. package/doc/tasks_audioRecorder.js.html +190 -190
  88. package/doc/tasks_impulse-response_impulseResponse.js.html +442 -442
  89. package/doc/tasks_impulse-response_mlsGen_mlsGenInterface.js.html +175 -175
  90. package/doc/tasks_volume_volume.js.html +185 -185
  91. package/doc/utils.js.html +105 -105
  92. package/jest.config.js +173 -173
  93. package/netlify.toml +26 -26
  94. package/package.json +78 -78
  95. package/src/config/firebase.js +26 -26
  96. package/src/index.html +21 -21
  97. package/src/listener-app/PhonePeer.js +499 -499
  98. package/src/listener-app/listener.js +380 -380
  99. package/src/main.js +22 -22
  100. package/src/myEventEmitter.js +83 -83
  101. package/src/peer-connection/audioPeer.js +100 -100
  102. package/src/peer-connection/listener.js +298 -298
  103. package/src/peer-connection/peerErrors.js +25 -25
  104. package/src/peer-connection/speaker.js +975 -975
  105. package/src/powerCheck.js +110 -110
  106. package/src/server/PythonServerAPI.js +1001 -999
  107. package/src/tasks/audioCalibrator.js +3 -0
  108. package/src/tasks/combination/combination.js +3906 -3758
  109. package/src/tasks/combination/mlsGen/mlsGen.cpp +98 -98
  110. package/src/tasks/combination/mlsGen/mlsGen.hpp +303 -303
  111. package/src/tasks/combination/mlsGen/mlsGenInterface.js +131 -131
  112. package/src/tasks/combination/mlsGen/mlsGenTest.cpp +180 -180
  113. package/src/tasks/impulse-response/impulseResponse.js +610 -610
  114. package/src/tasks/impulse-response/mlsGen/mlsGen.cpp +98 -98
  115. package/src/tasks/impulse-response/mlsGen/mlsGen.hpp +303 -303
  116. package/src/tasks/impulse-response/mlsGen/mlsGenInterface.js +131 -131
  117. package/src/tasks/impulse-response/mlsGen/mlsGenTest.cpp +180 -180
  118. package/src/tasks/volume/volume.cpp +2 -2
  119. package/src/tasks/volume/volume.hpp +22 -22
  120. package/src/tasks/volume/volume.js +279 -279
  121. package/src/utils.js +205 -205
  122. package/webpack.config.js +65 -65
  123. package/.github/workflows/update-phrases.yml +0 -37
  124. package/makefile +0 -74
@@ -1,999 +1,1001 @@
1
- import axios from 'axios';
2
- import {sleep} from '../utils';
3
- /**
4
- *
5
- */
6
- class PythonServerAPI {
7
- static PYTHON_SERVER_URL = 'https://easyeyes-python-flask-server.herokuapp.com';
8
-
9
- static TEST_SERVER_URL = 'http://127.0.0.1:5000';
10
- // static PYTHON_SERVER_URL = 'http://127.0.0.1:5000';
11
-
12
- /** @private */
13
- MAX_RETRY_COUNT = 3;
14
- /** @private */
15
- RETRY_DELAY_MS = 1000;
16
- /**
17
- * @param data- -
18
- * g = inverted impulse response, when convolved with the impulse
19
- * reponse, they cancel out.
20
- * @param data.payload
21
- * @param data.sampleRate
22
- * @param data.P
23
- * @param data-.payload
24
- * @param data-.sampleRate
25
- * @param data-.P
26
- * @returns
27
- * @example
28
- */
29
- getImpulseResponse = async ({mls, sampleRate, numPeriods, sig, fs2, L_new_n, dL_n}) => {
30
- const task = 'impulse-response';
31
- let res = null;
32
-
33
- const data = JSON.stringify({
34
- task,
35
- 'sample-rate': sampleRate,
36
- mls,
37
- numPeriods,
38
- sig,
39
- fs2,
40
- L_new_n,
41
- dL_n,
42
- });
43
-
44
- await axios({
45
- method: 'post',
46
- baseURL: PythonServerAPI.PYTHON_SERVER_URL,
47
- url: `/task/${task}`,
48
- headers: {
49
- 'Content-Type': 'application/json',
50
- },
51
- data,
52
- })
53
- .then(response => {
54
- res = response;
55
- })
56
- .catch(error => {
57
- throw error;
58
- });
59
- return res.data[task];
60
- };
61
-
62
- getAutocorrelation = async ({mls, payload, sampleRate, numPeriods}) => {
63
- const task = 'autocorrelation';
64
- let res = null;
65
-
66
- const data = JSON.stringify({
67
- task,
68
- payload: payload,
69
- 'sample-rate': sampleRate,
70
- mls,
71
- numPeriods,
72
- });
73
-
74
- await axios({
75
- method: 'post',
76
- baseURL: PythonServerAPI.PYTHON_SERVER_URL,
77
- url: `/task/${task}`,
78
- headers: {
79
- 'Content-Type': 'application/json',
80
- },
81
- data,
82
- })
83
- .then(response => {
84
- res = response;
85
- })
86
- .catch(error => {
87
- throw error;
88
- });
89
- return res.data[task];
90
- };
91
-
92
- getConvolution = async ({
93
- mls,
94
- inverse_response,
95
- inverse_response_no_bandpass,
96
- attenuatorGain_dB,
97
- mls_amplitude,
98
- }) => {
99
- const task = 'convolution';
100
- let res = null;
101
-
102
- const data = JSON.stringify({
103
- task,
104
- mls,
105
- inverse_response,
106
- inverse_response_no_bandpass,
107
- attenuatorGain_dB,
108
- mls_amplitude,
109
- });
110
-
111
- await axios({
112
- method: 'post',
113
- baseURL: PythonServerAPI.PYTHON_SERVER_URL,
114
- url: `/task/${task}`,
115
- headers: {
116
- 'Content-Type': 'application/json',
117
- },
118
- data,
119
- })
120
- .then(response => {
121
- res = response;
122
- })
123
- .catch(error => {
124
- throw error;
125
- });
126
- return res.data[task];
127
- };
128
-
129
- getImpulseResponseFromFrequencyResponse = async ({
130
- frequencies,
131
- gains,
132
- sample_rate,
133
- iir_length,
134
- calibrateSoundIIRPhase,
135
- totalDuration,
136
- totalDuration1000Hz,
137
- }) => {
138
- const task = 'frequency-response-to-impulse-response';
139
- let res = null;
140
-
141
- const data = JSON.stringify({
142
- task,
143
- frequencies,
144
- gains,
145
- sample_rate,
146
- iir_length,
147
- calibrateSoundIIRPhase,
148
- total_duration: totalDuration,
149
- total_duration_1000hz: totalDuration1000Hz,
150
- });
151
-
152
- await axios({
153
- method: 'post',
154
- baseURL: PythonServerAPI.PYTHON_SERVER_URL,
155
- url: `/task/${task}`,
156
- headers: {
157
- 'Content-Type': 'application/json',
158
- },
159
- data,
160
- })
161
- .then(response => {
162
- res = response;
163
- })
164
- .catch(error => {
165
- throw error;
166
- });
167
-
168
- return res.data[task];
169
- };
170
-
171
- getFrequencyResponseFromImpulseResponse = async ({
172
- impulseResponse,
173
- sampleRate,
174
- timeArray,
175
- totalDuration,
176
- totalDuration1000Hz,
177
- }) => {
178
- const task = 'frequency-response';
179
- let res = null;
180
-
181
- const data = JSON.stringify({
182
- task,
183
- impulse_response: impulseResponse,
184
- sample_rate: sampleRate,
185
- time_array: timeArray,
186
- total_duration: totalDuration,
187
- total_duration_1000hz: totalDuration1000Hz,
188
- });
189
-
190
- await axios({
191
- method: 'post',
192
- baseURL: PythonServerAPI.PYTHON_SERVER_URL,
193
- url: `/task/${task}`,
194
- headers: {
195
- 'Content-Type': 'application/json',
196
- },
197
- data,
198
- })
199
- .then(response => {
200
- res = response;
201
- })
202
- .catch(error => {
203
- throw error;
204
- });
205
- return res.data[task];
206
- };
207
-
208
- getMLS = async ({
209
- length,
210
- amplitude,
211
- calibrateSoundBurstMLSVersions,
212
- calibrateSoundBurstDownsample,
213
- }) => {
214
- const task = 'mls';
215
- let res = null;
216
-
217
- const data = JSON.stringify({
218
- task,
219
- length: length,
220
- amplitude: amplitude,
221
- calibrateSoundBurstMLSVersions: calibrateSoundBurstMLSVersions,
222
- calibrateSoundBurstDownsample: calibrateSoundBurstDownsample,
223
- });
224
-
225
- await axios({
226
- method: 'post',
227
- baseURL: PythonServerAPI.PYTHON_SERVER_URL,
228
- url: `/task/${task}`,
229
- headers: {
230
- 'Content-Type': 'application/json',
231
- },
232
- data,
233
- })
234
- .then(response => {
235
- res = response;
236
- })
237
- .catch(error => {
238
- throw error;
239
- });
240
-
241
- return res.data[task];
242
- };
243
-
244
- getShortURL = async originalURL => {
245
- const task = 'url';
246
- let res = null;
247
- console.log(originalURL);
248
- const data = JSON.stringify({
249
- URL: originalURL,
250
- });
251
-
252
- console.log(data);
253
-
254
- await axios({
255
- method: 'post',
256
- baseURL: PythonServerAPI.PYTHON_SERVER_URL,
257
- url: `/task/${task}`,
258
- headers: {
259
- 'Content-Type': 'application/json',
260
- },
261
- data,
262
- })
263
- .then(response => {
264
- res = response;
265
- console.log(res.data[task]);
266
- })
267
- .catch(error => {
268
- throw error;
269
- });
270
-
271
- return res.data[task];
272
- };
273
-
274
- getMemory = async () => {
275
- let res;
276
- await axios({
277
- method: 'post',
278
- baseURL: PythonServerAPI.PYTHON_SERVER_URL,
279
- url: `/memory`,
280
- headers: {
281
- 'Content-Type': 'application/json',
282
- },
283
- })
284
- .then(response => {
285
- console.log('memory used:', Math.round(response.data['memory']), 'mb');
286
- res = response.data['memory'];
287
- })
288
- .catch(error => {
289
- throw error;
290
- });
291
- return res;
292
- };
293
-
294
- checkMemory = async () => {
295
- console.log('wait for memory under 500 mb to continue calibration');
296
- await this.getMemory();
297
- // let memory = await this.getMemory();
298
- // while (memory >= 500) {
299
- // console.log("sleep 30s");
300
- // await sleep(30);
301
- // memory = await this.getMemory();
302
- // }
303
- };
304
-
305
- getMLSWithRetry = async ({
306
- length,
307
- amplitude,
308
- calibrateSoundBurstMLSVersions,
309
- calibrateSoundBurstDownsample,
310
- }) => {
311
- let retryCount = 0;
312
- let response = null;
313
-
314
- while (retryCount < this.MAX_RETRY_COUNT) {
315
- try {
316
- response = await this.getMLS({
317
- length,
318
- amplitude,
319
- calibrateSoundBurstMLSVersions,
320
- calibrateSoundBurstDownsample,
321
- });
322
- // If the request is successful, break out of the loop
323
- break;
324
- } catch (error) {
325
- console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
326
- retryCount++;
327
- await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
328
- }
329
- }
330
-
331
- if (response) {
332
- return response;
333
- } else {
334
- throw new Error(`Failed to get MLS after ${this.MAX_RETRY_COUNT} attempts.`);
335
- }
336
- };
337
-
338
- getPSD = async ({unconv_rec, conv_rec, sampleRate, downsample}) => {
339
- const task = 'psd';
340
- let res = null;
341
-
342
- const data = JSON.stringify({
343
- task,
344
- unconv_rec,
345
- conv_rec,
346
- sampleRate,
347
- downsample,
348
- });
349
-
350
- await axios({
351
- method: 'post',
352
- baseURL: PythonServerAPI.PYTHON_SERVER_URL,
353
- url: `/task/${task}`,
354
- headers: {
355
- 'Content-Type': 'application/json',
356
- },
357
- data,
358
- })
359
- .then(response => {
360
- res = response;
361
- })
362
- .catch(error => {
363
- throw error;
364
- });
365
- return res.data[task];
366
- };
367
-
368
- getBackgroundNoisePSD = async ({background_rec, sampleRate}) => {
369
- const task = 'background-psd';
370
- let res = null;
371
-
372
- const data = JSON.stringify({
373
- task,
374
- background_rec,
375
- sampleRate,
376
- });
377
-
378
- await axios({
379
- method: 'post',
380
- baseURL: PythonServerAPI.PYTHON_SERVER_URL,
381
- url: `/task/${task}`,
382
- headers: {
383
- 'Content-Type': 'application/json',
384
- },
385
- data,
386
- })
387
- .then(response => {
388
- res = response;
389
- })
390
- .catch(error => {
391
- throw error;
392
- });
393
- return res.data[task];
394
- };
395
-
396
- getBackgroundNoisePSDWithRetry = async ({background_rec, sampleRate}) => {
397
- let retryCount = 0;
398
- let response = null;
399
-
400
- while (retryCount < this.MAX_RETRY_COUNT) {
401
- try {
402
- response = await this.getBackgroundNoisePSD({background_rec, sampleRate});
403
- // If the request is successful, break out of the loop
404
- break;
405
- } catch (error) {
406
- console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
407
- retryCount++;
408
- await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
409
- }
410
- }
411
-
412
- if (response) {
413
- return response;
414
- } else {
415
- throw new Error(`Failed to get PSD after ${this.MAX_RETRY_COUNT} attempts.`);
416
- }
417
- };
418
-
419
- getSubtractedPSD = async (rec, knownGains, knownFrequencies, sampleRate, downsample) => {
420
- const task = 'subtracted-psd';
421
- let res = null;
422
-
423
- const data = JSON.stringify({
424
- task,
425
- rec,
426
- knownGains,
427
- knownFrequencies,
428
- sampleRate,
429
- downsample,
430
- });
431
-
432
- await axios({
433
- method: 'post',
434
- baseURL: PythonServerAPI.PYTHON_SERVER_URL,
435
- url: `/task/${task}`,
436
- headers: {
437
- 'Content-Type': 'application/json',
438
- },
439
- data,
440
- })
441
- .then(response => {
442
- res = response;
443
- })
444
- .catch(error => {
445
- throw error;
446
- });
447
- return res.data[task];
448
- };
449
-
450
- getSubtractedPSDWithRetry = async (rec, knownGains, knownFrequencies, sampleRate, downsample) => {
451
- let retryCount = 0;
452
- let response = null;
453
-
454
- while (retryCount < this.MAX_RETRY_COUNT) {
455
- try {
456
- response = await this.getSubtractedPSD(
457
- rec,
458
- knownGains,
459
- knownFrequencies,
460
- sampleRate,
461
- downsample
462
- );
463
- // If the request is successful, break out of the loop
464
- break;
465
- } catch (error) {
466
- console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
467
- retryCount++;
468
- await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
469
- }
470
- }
471
-
472
- if (response) {
473
- return response;
474
- } else {
475
- throw new Error(`Failed to get PSD after ${this.MAX_RETRY_COUNT} attempts.`);
476
- }
477
- };
478
-
479
- getPSDWithRetry = async ({unconv_rec, conv_rec, sampleRate, downsample}) => {
480
- let retryCount = 0;
481
- let response = null;
482
-
483
- while (retryCount < this.MAX_RETRY_COUNT) {
484
- try {
485
- response = await this.getPSD({unconv_rec, conv_rec, sampleRate, downsample});
486
- // If the request is successful, break out of the loop
487
- break;
488
- } catch (error) {
489
- console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
490
- retryCount++;
491
- await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
492
- }
493
- }
494
-
495
- if (response) {
496
- return response;
497
- } else {
498
- throw new Error(`Failed to get PSD after ${this.MAX_RETRY_COUNT} attempts.`);
499
- }
500
- };
501
-
502
- getComponentInverseImpulseResponse = async ({
503
- payload,
504
- mls,
505
- lowHz,
506
- highHz,
507
- componentIRGains,
508
- iirLength,
509
- componentIRFreqs,
510
- sampleRate,
511
- mlsAmplitude,
512
- irLength,
513
- calibrateSoundSmoothOctaves,
514
- calibrateSoundSmoothMinBandwidthHz,
515
- calibrateSoundBurstFilteredExtraDb,
516
- calibrateSoundIIRPhase,
517
- downsample,
518
- }) => {
519
- const task = 'component-inverse-impulse-response';
520
- let res = null;
521
-
522
- const data = JSON.stringify({
523
- task,
524
- payload,
525
- mls,
526
- lowHz,
527
- highHz,
528
- iirLength,
529
- componentIRGains,
530
- componentIRFreqs,
531
- sampleRate,
532
- mlsAmplitude,
533
- irLength,
534
- calibrateSoundSmoothOctaves,
535
- calibrateSoundSmoothMinBandwidthHz,
536
- calibrateSoundBurstFilteredExtraDb,
537
- calibrateSoundIIRPhase,
538
- downsample,
539
- });
540
-
541
- await axios({
542
- method: 'post',
543
- baseURL: PythonServerAPI.PYTHON_SERVER_URL,
544
- url: `/task/${task}`,
545
- headers: {
546
- 'Content-Type': 'application/json',
547
- },
548
- data,
549
- })
550
- .then(response => {
551
- res = response;
552
- })
553
- .catch(error => {
554
- throw error;
555
- });
556
-
557
- return res.data[task];
558
- };
559
- getSystemInverseImpulseResponse = async ({
560
- payload,
561
- mls,
562
- lowHz,
563
- highHz,
564
- iirLength,
565
- sampleRate,
566
- mlsAmplitude,
567
- calibrateSoundBurstFilteredExtraDb,
568
- calibrateSoundIIRPhase,
569
- downsample,
570
- }) => {
571
- const task = 'system-inverse-impulse-response';
572
- let res = null;
573
-
574
- const data = JSON.stringify({
575
- task,
576
- payload,
577
- mls,
578
- lowHz,
579
- iirLength,
580
- highHz,
581
- sampleRate,
582
- mlsAmplitude,
583
- calibrateSoundBurstFilteredExtraDb,
584
- calibrateSoundIIRPhase,
585
- downsample,
586
- });
587
-
588
- await axios({
589
- method: 'post',
590
- baseURL: PythonServerAPI.PYTHON_SERVER_URL,
591
- url: `/task/${task}`,
592
- headers: {
593
- 'Content-Type': 'application/json',
594
- },
595
- data,
596
- })
597
- .then(response => {
598
- res = response;
599
- })
600
- .catch(error => {
601
- throw error;
602
- });
603
-
604
- return res.data[task];
605
- };
606
-
607
- getMLSPSD = async ({mls, sampleRate, downsample}) => {
608
- const task = 'mls-psd';
609
- let res = null;
610
-
611
- const data = JSON.stringify({
612
- task,
613
- mls,
614
- sampleRate,
615
- downsample,
616
- });
617
-
618
- await axios({
619
- method: 'post',
620
- baseURL: PythonServerAPI.PYTHON_SERVER_URL,
621
- url: `/task/${task}`,
622
- headers: {
623
- 'Content-Type': 'application/json',
624
- },
625
- data,
626
- })
627
- .then(response => {
628
- res = response;
629
- })
630
- .catch(error => {
631
- throw error;
632
- });
633
-
634
- return res.data[task];
635
- };
636
-
637
- getMLSPSDWithRetry = async ({mls, sampleRate, downsample}) => {
638
- let retryCount = 0;
639
- let response = null;
640
-
641
- while (retryCount < this.MAX_RETRY_COUNT) {
642
- try {
643
- response = await this.getMLSPSD({mls, sampleRate, downsample});
644
- // If the request is successful, break out of the loop
645
- break;
646
- } catch (error) {
647
- console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
648
- retryCount++;
649
- await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
650
- }
651
- }
652
-
653
- if (response) {
654
- return response;
655
- } else {
656
- throw new Error(
657
- `Failed to get inverse impulse response after ${this.MAX_RETRY_COUNT} attempts.`
658
- );
659
- }
660
- };
661
-
662
- getComponentInverseImpulseResponseWithRetry = async ({
663
- payload,
664
- mls,
665
- lowHz,
666
- highHz,
667
- componentIRGains,
668
- iirLength,
669
- componentIRFreqs,
670
- sampleRate,
671
- mlsAmplitude,
672
- irLength,
673
- calibrateSoundSmoothOctaves,
674
- calibrateSoundSmoothMinBandwidthHz,
675
- calibrateSoundBurstFilteredExtraDb,
676
- calibrateSoundIIRPhase,
677
- downsample,
678
- }) => {
679
- let retryCount = 0;
680
- let response = null;
681
-
682
- while (retryCount < this.MAX_RETRY_COUNT) {
683
- try {
684
- response = await this.getComponentInverseImpulseResponse({
685
- payload,
686
- mls,
687
- lowHz,
688
- highHz,
689
- componentIRGains,
690
- iirLength,
691
- componentIRFreqs,
692
- sampleRate,
693
- mlsAmplitude,
694
- irLength,
695
- calibrateSoundSmoothOctaves,
696
- calibrateSoundSmoothMinBandwidthHz,
697
- calibrateSoundBurstFilteredExtraDb,
698
- calibrateSoundIIRPhase,
699
- downsample,
700
- });
701
- // If the request is successful, break out of the loop
702
- break;
703
- } catch (error) {
704
- console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
705
- retryCount++;
706
- await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
707
- }
708
- }
709
-
710
- if (response) {
711
- return response;
712
- } else {
713
- throw new Error(
714
- `Failed to get inverse impulse response after ${this.MAX_RETRY_COUNT} attempts.`
715
- );
716
- }
717
- };
718
-
719
- getSystemInverseImpulseResponseWithRetry = async ({
720
- payload,
721
- mls,
722
- lowHz,
723
- highHz,
724
- iirLength,
725
- sampleRate,
726
- mlsAmplitude,
727
- calibrateSoundBurstFilteredExtraDb,
728
- calibrateSoundIIRPhase,
729
- downsample,
730
- }) => {
731
- let retryCount = 0;
732
- let response = null;
733
-
734
- while (retryCount < this.MAX_RETRY_COUNT) {
735
- try {
736
- response = await this.getSystemInverseImpulseResponse({
737
- payload,
738
- mls,
739
- lowHz,
740
- highHz,
741
- iirLength,
742
- sampleRate,
743
- mlsAmplitude,
744
- calibrateSoundBurstFilteredExtraDb,
745
- calibrateSoundIIRPhase,
746
- downsample,
747
- });
748
- // If the request is successful, break out of the loop
749
- break;
750
- } catch (error) {
751
- console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
752
- retryCount++;
753
- await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
754
- }
755
- }
756
-
757
- if (response) {
758
- return response;
759
- } else {
760
- throw new Error(
761
- `Failed to get inverse impulse response after ${this.MAX_RETRY_COUNT} attempts.`
762
- );
763
- }
764
- };
765
-
766
- getVolumeCalibration = async ({payload, sampleRate, lCalib}) => {
767
- const task = 'volume';
768
- let res = null;
769
-
770
- console.log({payload});
771
-
772
- const data = JSON.stringify({
773
- task,
774
- payload,
775
- 'sample-rate': sampleRate,
776
- lCalib,
777
- });
778
-
779
- const response = await axios({
780
- method: 'post',
781
- baseURL: PythonServerAPI.PYTHON_SERVER_URL,
782
- url: `/task/${task}`,
783
- headers: {
784
- 'Content-Type': 'application/json',
785
- },
786
- data,
787
- })
788
- .then(response => {
789
- // if response.data is a string, parse it
790
- if (typeof response.data === 'string') {
791
- // response.data = response.data.replaceAll('Infinity', 99999999);
792
- // response.data = JSON.parse(response.data);
793
- //if there is Infinity in the string, throw an error
794
- if (response.data.includes('Infinity')) {
795
- throw new Error(
796
- 'Server returned Infinity. Please make sure the microphone is recording correctly'
797
- );
798
- }
799
- response.data = JSON.parse(response.data);
800
- }
801
- return response.data[task];
802
- })
803
- .catch(error => {
804
- alert(
805
- 'Invalid data. Please make sure the microphone is recording correctly and press on "Re-Record"'
806
- );
807
- throw error;
808
- });
809
-
810
- console.log(response);
811
- return response;
812
- };
813
-
814
- getVolumeCalibrationParameters = async ({
815
- inDBValues,
816
- outDBSPLValues,
817
- lCalib,
818
- componentGainDBSPL,
819
- }) => {
820
- const task = 'volume-parameters';
821
- let res = null;
822
-
823
- const data = JSON.stringify({
824
- task,
825
- inDBValues,
826
- outDBSPLValues,
827
- lCalib,
828
- componentGainDBSPL,
829
- });
830
-
831
- await axios({
832
- method: 'post',
833
- baseURL: PythonServerAPI.PYTHON_SERVER_URL, //server
834
- url: `/task/${task}`,
835
- headers: {
836
- 'Content-Type': 'application/json',
837
- },
838
- data,
839
- })
840
- .then(response => {
841
- res = response;
842
- console.log('data', data, 'res', res.data[task]);
843
- })
844
- .catch(error => {
845
- throw error;
846
- });
847
-
848
- // console.log(res.data[task]);
849
- //below is an example of res.data[task]
850
- //{
851
- // R: 16.56981076554259,
852
- // RMSError: 1.9289162528535229
853
- // T: -47.79799120884434,
854
- // W: 61.0485247483732,
855
- // backgroundDBSPL: 43.88233142069752,
856
- // gainDBSPL: -128.24742161208985
857
- //}
858
- return res.data[task];
859
- };
860
-
861
- irConvolution = async ({input_signal, microphone_ir, loudspeaker_ir}) => {
862
- const task = 'ir-convolution';
863
- let res = null;
864
-
865
- const data = JSON.stringify({
866
- input_signal,
867
- microphone_ir,
868
- loudspeaker_ir,
869
- });
870
-
871
- await axios({
872
- method: 'post',
873
- baseURL: PythonServerAPI.PYTHON_SERVER_URL, //server
874
- url: `/task/${task}`,
875
- headers: {
876
- 'Content-Type': 'application/json',
877
- },
878
- data,
879
- })
880
- .then(response => {
881
- res = response;
882
- })
883
- .catch(error => {
884
- throw error;
885
- });
886
- console.log('res in irConvolution: ', res);
887
- return res.data[task];
888
- };
889
-
890
- allHzPowerCheck = async ({
891
- payload,
892
- sampleRate,
893
- binDesiredSec,
894
- burstSec,
895
- repeats,
896
- warmUp,
897
- downsample,
898
- }) => {
899
- const task = 'all-hz-check';
900
- let res = null;
901
-
902
- console.log({payload, sampleRate, binDesiredSec, burstSec, repeats, warmUp});
903
-
904
- const data = JSON.stringify({
905
- payload,
906
- sampleRate,
907
- binDesiredSec,
908
- burstSec,
909
- repeats,
910
- warmUp,
911
- downsample,
912
- });
913
-
914
- await axios({
915
- method: 'post',
916
- baseURL: PythonServerAPI.PYTHON_SERVER_URL, //server
917
- url: `/task/${task}`,
918
- headers: {
919
- 'Content-Type': 'application/json',
920
- },
921
- data,
922
- })
923
- .then(response => {
924
- res = response;
925
- if (typeof res.data === 'string') {
926
- //if there is NaN in the string, replace it with 0
927
- res.data = res.data.replace('NaN', '0');
928
- res.data = JSON.parse(res.data);
929
- }
930
- console.log('res', res);
931
- })
932
- .catch(error => {
933
- throw error;
934
- });
935
- return res.data[task];
936
- };
937
-
938
- volumePowerCheck = async ({payload, sampleRate, preSec, Sec, binDesiredSec}) => {
939
- const task = 'volume-check';
940
- let res = null;
941
-
942
- const data = JSON.stringify({
943
- payload,
944
- sampleRate,
945
- preSec,
946
- Sec,
947
- binDesiredSec,
948
- });
949
-
950
- await axios({
951
- method: 'post',
952
- baseURL: PythonServerAPI.PYTHON_SERVER_URL, //server
953
- url: `/task/${task}`,
954
- headers: {
955
- 'Content-Type': 'application/json',
956
- },
957
- data,
958
- })
959
- .then(response => {
960
- res = response;
961
- console.log(res.data[task]);
962
- })
963
- .catch(error => {
964
- throw error;
965
- });
966
- return res.data[task];
967
- };
968
-
969
- volumePowerCheckWithRetry = async ({payload, sampleRate, preSec, Sec, binDesiredSec}) => {
970
- let retryCount = 0;
971
- let response = null;
972
-
973
- while (retryCount < this.MAX_RETRY_COUNT) {
974
- try {
975
- response = await this.volumePowerCheck({
976
- payload,
977
- sampleRate,
978
- preSec,
979
- Sec,
980
- binDesiredSec,
981
- });
982
- // If the request is successful, break out of the loop
983
- break;
984
- } catch (error) {
985
- console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
986
- retryCount++;
987
- await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
988
- }
989
- }
990
-
991
- if (response) {
992
- return response;
993
- } else {
994
- throw new Error(`Failed to get volume power check after ${this.MAX_RETRY_COUNT} attempts.`);
995
- }
996
- };
997
- }
998
-
999
- export default PythonServerAPI;
1
+ import axios from 'axios';
2
+ import {sleep} from '../utils';
3
+ /**
4
+ *
5
+ */
6
+ class PythonServerAPI {
7
+ static PYTHON_SERVER_URL = 'https://easyeyes-python-flask-server.herokuapp.com';
8
+
9
+ static TEST_SERVER_URL = 'http://127.0.0.1:5000';
10
+ // static PYTHON_SERVER_URL = 'http://127.0.0.1:5000';
11
+
12
+ /** @private */
13
+ MAX_RETRY_COUNT = 3;
14
+ /** @private */
15
+ RETRY_DELAY_MS = 1000;
16
+ /**
17
+ * @param data- -
18
+ * g = inverted impulse response, when convolved with the impulse
19
+ * reponse, they cancel out.
20
+ * @param data.payload
21
+ * @param data.sampleRate
22
+ * @param data.P
23
+ * @param data-.payload
24
+ * @param data-.sampleRate
25
+ * @param data-.P
26
+ * @returns
27
+ * @example
28
+ */
29
+ getImpulseResponse = async ({mls, sampleRate, numPeriods, sig, fs2, L_new_n, dL_n}) => {
30
+ const task = 'impulse-response';
31
+ let res = null;
32
+
33
+ const data = JSON.stringify({
34
+ task,
35
+ 'sample-rate': sampleRate,
36
+ mls,
37
+ numPeriods,
38
+ sig,
39
+ fs2,
40
+ L_new_n,
41
+ dL_n,
42
+ });
43
+
44
+ await axios({
45
+ method: 'post',
46
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL,
47
+ url: `/task/${task}`,
48
+ headers: {
49
+ 'Content-Type': 'application/json',
50
+ },
51
+ data,
52
+ })
53
+ .then(response => {
54
+ res = response;
55
+ })
56
+ .catch(error => {
57
+ throw error;
58
+ });
59
+ return res.data[task];
60
+ };
61
+
62
+ getAutocorrelation = async ({mls, payload, sampleRate, numPeriods}) => {
63
+ const task = 'autocorrelation';
64
+ let res = null;
65
+
66
+ const data = JSON.stringify({
67
+ task,
68
+ payload: payload,
69
+ 'sample-rate': sampleRate,
70
+ mls,
71
+ numPeriods,
72
+ });
73
+
74
+ await axios({
75
+ method: 'post',
76
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL,
77
+ url: `/task/${task}`,
78
+ headers: {
79
+ 'Content-Type': 'application/json',
80
+ },
81
+ data,
82
+ })
83
+ .then(response => {
84
+ res = response;
85
+ })
86
+ .catch(error => {
87
+ throw error;
88
+ });
89
+ return res.data[task];
90
+ };
91
+
92
+ getConvolution = async ({
93
+ mls,
94
+ inverse_response,
95
+ inverse_response_no_bandpass,
96
+ attenuatorGain_dB,
97
+ mls_amplitude,
98
+ }) => {
99
+ const task = 'convolution';
100
+ let res = null;
101
+
102
+ const data = JSON.stringify({
103
+ task,
104
+ mls,
105
+ inverse_response,
106
+ inverse_response_no_bandpass,
107
+ attenuatorGain_dB,
108
+ mls_amplitude,
109
+ });
110
+
111
+ await axios({
112
+ method: 'post',
113
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL,
114
+ url: `/task/${task}`,
115
+ headers: {
116
+ 'Content-Type': 'application/json',
117
+ },
118
+ data,
119
+ })
120
+ .then(response => {
121
+ res = response;
122
+ })
123
+ .catch(error => {
124
+ throw error;
125
+ });
126
+ return res.data[task];
127
+ };
128
+
129
+ getImpulseResponseFromFrequencyResponse = async ({
130
+ frequencies,
131
+ gains,
132
+ sample_rate,
133
+ iir_length,
134
+ calibrateSoundIIRPhase,
135
+ totalDuration,
136
+ totalDuration1000Hz,
137
+ }) => {
138
+ const task = 'frequency-response-to-impulse-response';
139
+ let res = null;
140
+
141
+ const data = JSON.stringify({
142
+ task,
143
+ frequencies,
144
+ gains,
145
+ sample_rate,
146
+ iir_length,
147
+ calibrateSoundIIRPhase,
148
+ total_duration: totalDuration,
149
+ total_duration_1000hz: totalDuration1000Hz,
150
+ });
151
+
152
+ await axios({
153
+ method: 'post',
154
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL,
155
+ url: `/task/${task}`,
156
+ headers: {
157
+ 'Content-Type': 'application/json',
158
+ },
159
+ data,
160
+ })
161
+ .then(response => {
162
+ res = response;
163
+ })
164
+ .catch(error => {
165
+ throw error;
166
+ });
167
+
168
+ return res.data[task];
169
+ };
170
+
171
+ getFrequencyResponseFromImpulseResponse = async ({
172
+ impulseResponse,
173
+ sampleRate,
174
+ timeArray,
175
+ totalDuration,
176
+ totalDuration1000Hz,
177
+ }) => {
178
+ const task = 'frequency-response';
179
+ let res = null;
180
+
181
+ const data = JSON.stringify({
182
+ task,
183
+ impulse_response: impulseResponse,
184
+ sample_rate: sampleRate,
185
+ time_array: timeArray,
186
+ total_duration: totalDuration,
187
+ total_duration_1000hz: totalDuration1000Hz,
188
+ });
189
+
190
+ await axios({
191
+ method: 'post',
192
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL,
193
+ url: `/task/${task}`,
194
+ headers: {
195
+ 'Content-Type': 'application/json',
196
+ },
197
+ data,
198
+ })
199
+ .then(response => {
200
+ res = response;
201
+ })
202
+ .catch(error => {
203
+ throw error;
204
+ });
205
+ return res.data[task];
206
+ };
207
+
208
+ getMLS = async ({
209
+ length,
210
+ amplitude,
211
+ calibrateSoundBurstMLSVersions,
212
+ calibrateSoundBurstDownsample,
213
+ }) => {
214
+ const task = 'mls';
215
+ let res = null;
216
+
217
+ const data = JSON.stringify({
218
+ task,
219
+ length: length,
220
+ amplitude: amplitude,
221
+ calibrateSoundBurstMLSVersions: calibrateSoundBurstMLSVersions,
222
+ calibrateSoundBurstDownsample: calibrateSoundBurstDownsample,
223
+ });
224
+
225
+ await axios({
226
+ method: 'post',
227
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL,
228
+ url: `/task/${task}`,
229
+ headers: {
230
+ 'Content-Type': 'application/json',
231
+ },
232
+ data,
233
+ })
234
+ .then(response => {
235
+ res = response;
236
+ })
237
+ .catch(error => {
238
+ throw error;
239
+ });
240
+
241
+ return res.data[task];
242
+ };
243
+
244
+ getShortURL = async originalURL => {
245
+ const task = 'url';
246
+ let res = null;
247
+ console.log(originalURL);
248
+ const data = JSON.stringify({
249
+ URL: originalURL,
250
+ });
251
+
252
+ console.log(data);
253
+
254
+ await axios({
255
+ method: 'post',
256
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL,
257
+ url: `/task/${task}`,
258
+ headers: {
259
+ 'Content-Type': 'application/json',
260
+ },
261
+ data,
262
+ })
263
+ .then(response => {
264
+ res = response;
265
+ console.log(res.data[task]);
266
+ })
267
+ .catch(error => {
268
+ throw error;
269
+ });
270
+
271
+ return res.data[task];
272
+ };
273
+
274
+ getMemory = async () => {
275
+ let res;
276
+ await axios({
277
+ method: 'post',
278
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL,
279
+ url: `/memory`,
280
+ headers: {
281
+ 'Content-Type': 'application/json',
282
+ },
283
+ })
284
+ .then(response => {
285
+ console.log('memory used:', Math.round(response.data['memory']), 'mb');
286
+ res = response.data['memory'];
287
+ })
288
+ .catch(error => {
289
+ throw error;
290
+ });
291
+ return res;
292
+ };
293
+
294
+ checkMemory = async () => {
295
+ console.log('wait for memory under 500 mb to continue calibration');
296
+ await this.getMemory();
297
+ // let memory = await this.getMemory();
298
+ // while (memory >= 500) {
299
+ // console.log("sleep 30s");
300
+ // await sleep(30);
301
+ // memory = await this.getMemory();
302
+ // }
303
+ };
304
+
305
+ getMLSWithRetry = async ({
306
+ length,
307
+ amplitude,
308
+ calibrateSoundBurstMLSVersions,
309
+ calibrateSoundBurstDownsample,
310
+ }) => {
311
+ let retryCount = 0;
312
+ let response = null;
313
+
314
+ while (retryCount < this.MAX_RETRY_COUNT) {
315
+ try {
316
+ response = await this.getMLS({
317
+ length,
318
+ amplitude,
319
+ calibrateSoundBurstMLSVersions,
320
+ calibrateSoundBurstDownsample,
321
+ });
322
+ // If the request is successful, break out of the loop
323
+ break;
324
+ } catch (error) {
325
+ console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
326
+ retryCount++;
327
+ await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
328
+ }
329
+ }
330
+
331
+ if (response) {
332
+ return response;
333
+ } else {
334
+ throw new Error(`Failed to get MLS after ${this.MAX_RETRY_COUNT} attempts.`);
335
+ }
336
+ };
337
+
338
+ getPSD = async ({unconv_rec, conv_rec, sampleRate, downsample}) => {
339
+ const task = 'psd';
340
+ let res = null;
341
+
342
+ const data = JSON.stringify({
343
+ task,
344
+ unconv_rec,
345
+ conv_rec,
346
+ sampleRate,
347
+ downsample,
348
+ });
349
+
350
+ await axios({
351
+ method: 'post',
352
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL,
353
+ url: `/task/${task}`,
354
+ headers: {
355
+ 'Content-Type': 'application/json',
356
+ },
357
+ data,
358
+ })
359
+ .then(response => {
360
+ res = response;
361
+ })
362
+ .catch(error => {
363
+ throw error;
364
+ });
365
+ return res.data[task];
366
+ };
367
+
368
+ getBackgroundNoisePSD = async ({background_rec, sampleRate}) => {
369
+ const task = 'background-psd';
370
+ let res = null;
371
+
372
+ const data = JSON.stringify({
373
+ task,
374
+ background_rec,
375
+ sampleRate,
376
+ });
377
+
378
+ await axios({
379
+ method: 'post',
380
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL,
381
+ url: `/task/${task}`,
382
+ headers: {
383
+ 'Content-Type': 'application/json',
384
+ },
385
+ data,
386
+ })
387
+ .then(response => {
388
+ res = response;
389
+ })
390
+ .catch(error => {
391
+ throw error;
392
+ });
393
+ return res.data[task];
394
+ };
395
+
396
+ getBackgroundNoisePSDWithRetry = async ({background_rec, sampleRate}) => {
397
+ let retryCount = 0;
398
+ let response = null;
399
+
400
+ while (retryCount < this.MAX_RETRY_COUNT) {
401
+ try {
402
+ response = await this.getBackgroundNoisePSD({background_rec, sampleRate});
403
+ // If the request is successful, break out of the loop
404
+ break;
405
+ } catch (error) {
406
+ console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
407
+ retryCount++;
408
+ await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
409
+ }
410
+ }
411
+
412
+ if (response) {
413
+ return response;
414
+ } else {
415
+ throw new Error(`Failed to get PSD after ${this.MAX_RETRY_COUNT} attempts.`);
416
+ }
417
+ };
418
+
419
+ getSubtractedPSD = async (rec, knownGains, knownFrequencies, sampleRate, downsample) => {
420
+ const task = 'subtracted-psd';
421
+ let res = null;
422
+
423
+ const data = JSON.stringify({
424
+ task,
425
+ rec,
426
+ knownGains,
427
+ knownFrequencies,
428
+ sampleRate,
429
+ downsample,
430
+ });
431
+
432
+ await axios({
433
+ method: 'post',
434
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL,
435
+ url: `/task/${task}`,
436
+ headers: {
437
+ 'Content-Type': 'application/json',
438
+ },
439
+ data,
440
+ })
441
+ .then(response => {
442
+ res = response;
443
+ })
444
+ .catch(error => {
445
+ throw error;
446
+ });
447
+ return res.data[task];
448
+ };
449
+
450
+ getSubtractedPSDWithRetry = async (rec, knownGains, knownFrequencies, sampleRate, downsample) => {
451
+ let retryCount = 0;
452
+ let response = null;
453
+
454
+ while (retryCount < this.MAX_RETRY_COUNT) {
455
+ try {
456
+ response = await this.getSubtractedPSD(
457
+ rec,
458
+ knownGains,
459
+ knownFrequencies,
460
+ sampleRate,
461
+ downsample
462
+ );
463
+ // If the request is successful, break out of the loop
464
+ break;
465
+ } catch (error) {
466
+ console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
467
+ retryCount++;
468
+ await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
469
+ }
470
+ }
471
+
472
+ if (response) {
473
+ return response;
474
+ } else {
475
+ throw new Error(`Failed to get PSD after ${this.MAX_RETRY_COUNT} attempts.`);
476
+ }
477
+ };
478
+
479
+ getPSDWithRetry = async ({unconv_rec, conv_rec, sampleRate, downsample}) => {
480
+ let retryCount = 0;
481
+ let response = null;
482
+
483
+ while (retryCount < this.MAX_RETRY_COUNT) {
484
+ try {
485
+ response = await this.getPSD({unconv_rec, conv_rec, sampleRate, downsample});
486
+ // If the request is successful, break out of the loop
487
+ break;
488
+ } catch (error) {
489
+ console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
490
+ retryCount++;
491
+ await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
492
+ }
493
+ }
494
+
495
+ if (response) {
496
+ return response;
497
+ } else {
498
+ throw new Error(`Failed to get PSD after ${this.MAX_RETRY_COUNT} attempts.`);
499
+ }
500
+ };
501
+
502
+ getComponentInverseImpulseResponse = async ({
503
+ payload,
504
+ mls,
505
+ lowHz,
506
+ highHz,
507
+ componentIRGains,
508
+ iirLength,
509
+ componentIRFreqs,
510
+ sampleRate,
511
+ mlsAmplitude,
512
+ irLength,
513
+ calibrateSoundSmoothOctaves,
514
+ calibrateSoundSmoothMinBandwidthHz,
515
+ calibrateSoundBurstFilteredExtraDb,
516
+ calibrateSoundIIRPhase,
517
+ downsample,
518
+ }) => {
519
+ const task = 'component-inverse-impulse-response';
520
+ let res = null;
521
+
522
+ const data = JSON.stringify({
523
+ task,
524
+ payload,
525
+ mls,
526
+ lowHz,
527
+ highHz,
528
+ iirLength,
529
+ componentIRGains,
530
+ componentIRFreqs,
531
+ sampleRate,
532
+ mlsAmplitude,
533
+ irLength,
534
+ calibrateSoundSmoothOctaves,
535
+ calibrateSoundSmoothMinBandwidthHz,
536
+ calibrateSoundBurstFilteredExtraDb,
537
+ calibrateSoundIIRPhase,
538
+ downsample,
539
+ });
540
+
541
+ await axios({
542
+ method: 'post',
543
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL,
544
+ url: `/task/${task}`,
545
+ headers: {
546
+ 'Content-Type': 'application/json',
547
+ },
548
+ data,
549
+ })
550
+ .then(response => {
551
+ res = response;
552
+ })
553
+ .catch(error => {
554
+ throw error;
555
+ });
556
+
557
+ return res.data[task];
558
+ };
559
+ getSystemInverseImpulseResponse = async ({
560
+ payload,
561
+ mls,
562
+ lowHz,
563
+ highHz,
564
+ iirLength,
565
+ sampleRate,
566
+ mlsAmplitude,
567
+ calibrateSoundBurstFilteredExtraDb,
568
+ calibrateSoundIIRPhase,
569
+ downsample,
570
+ }) => {
571
+ const task = 'system-inverse-impulse-response';
572
+ let res = null;
573
+
574
+ const data = JSON.stringify({
575
+ task,
576
+ payload,
577
+ mls,
578
+ lowHz,
579
+ iirLength,
580
+ highHz,
581
+ sampleRate,
582
+ mlsAmplitude,
583
+ calibrateSoundBurstFilteredExtraDb,
584
+ calibrateSoundIIRPhase,
585
+ downsample,
586
+ });
587
+
588
+ await axios({
589
+ method: 'post',
590
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL,
591
+ url: `/task/${task}`,
592
+ headers: {
593
+ 'Content-Type': 'application/json',
594
+ },
595
+ data,
596
+ })
597
+ .then(response => {
598
+ res = response;
599
+ })
600
+ .catch(error => {
601
+ throw error;
602
+ });
603
+
604
+ return res.data[task];
605
+ };
606
+
607
+ getMLSPSD = async ({mls, sampleRate, downsample}) => {
608
+ const task = 'mls-psd';
609
+ let res = null;
610
+
611
+ const data = JSON.stringify({
612
+ task,
613
+ mls,
614
+ sampleRate,
615
+ downsample,
616
+ });
617
+
618
+ await axios({
619
+ method: 'post',
620
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL,
621
+ url: `/task/${task}`,
622
+ headers: {
623
+ 'Content-Type': 'application/json',
624
+ },
625
+ data,
626
+ })
627
+ .then(response => {
628
+ res = response;
629
+ })
630
+ .catch(error => {
631
+ throw error;
632
+ });
633
+
634
+ return res.data[task];
635
+ };
636
+
637
+ getMLSPSDWithRetry = async ({mls, sampleRate, downsample}) => {
638
+ let retryCount = 0;
639
+ let response = null;
640
+
641
+ while (retryCount < this.MAX_RETRY_COUNT) {
642
+ try {
643
+ response = await this.getMLSPSD({mls, sampleRate, downsample});
644
+ // If the request is successful, break out of the loop
645
+ break;
646
+ } catch (error) {
647
+ console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
648
+ retryCount++;
649
+ await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
650
+ }
651
+ }
652
+
653
+ if (response) {
654
+ return response;
655
+ } else {
656
+ throw new Error(
657
+ `Failed to get inverse impulse response after ${this.MAX_RETRY_COUNT} attempts.`
658
+ );
659
+ }
660
+ };
661
+
662
+ getComponentInverseImpulseResponseWithRetry = async ({
663
+ payload,
664
+ mls,
665
+ lowHz,
666
+ highHz,
667
+ componentIRGains,
668
+ iirLength,
669
+ componentIRFreqs,
670
+ sampleRate,
671
+ mlsAmplitude,
672
+ irLength,
673
+ calibrateSoundSmoothOctaves,
674
+ calibrateSoundSmoothMinBandwidthHz,
675
+ calibrateSoundBurstFilteredExtraDb,
676
+ calibrateSoundIIRPhase,
677
+ downsample,
678
+ }) => {
679
+ let retryCount = 0;
680
+ let response = null;
681
+
682
+ while (retryCount < this.MAX_RETRY_COUNT) {
683
+ try {
684
+ response = await this.getComponentInverseImpulseResponse({
685
+ payload,
686
+ mls,
687
+ lowHz,
688
+ highHz,
689
+ componentIRGains,
690
+ iirLength,
691
+ componentIRFreqs,
692
+ sampleRate,
693
+ mlsAmplitude,
694
+ irLength,
695
+ calibrateSoundSmoothOctaves,
696
+ calibrateSoundSmoothMinBandwidthHz,
697
+ calibrateSoundBurstFilteredExtraDb,
698
+ calibrateSoundIIRPhase,
699
+ downsample,
700
+ });
701
+ // If the request is successful, break out of the loop
702
+ break;
703
+ } catch (error) {
704
+ console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
705
+ retryCount++;
706
+ await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
707
+ }
708
+ }
709
+
710
+ if (response) {
711
+ return response;
712
+ } else {
713
+ throw new Error(
714
+ `Failed to get inverse impulse response after ${this.MAX_RETRY_COUNT} attempts.`
715
+ );
716
+ }
717
+ };
718
+
719
+ getSystemInverseImpulseResponseWithRetry = async ({
720
+ payload,
721
+ mls,
722
+ lowHz,
723
+ highHz,
724
+ iirLength,
725
+ sampleRate,
726
+ mlsAmplitude,
727
+ calibrateSoundBurstFilteredExtraDb,
728
+ calibrateSoundIIRPhase,
729
+ downsample,
730
+ }) => {
731
+ let retryCount = 0;
732
+ let response = null;
733
+
734
+ while (retryCount < this.MAX_RETRY_COUNT) {
735
+ try {
736
+ response = await this.getSystemInverseImpulseResponse({
737
+ payload,
738
+ mls,
739
+ lowHz,
740
+ highHz,
741
+ iirLength,
742
+ sampleRate,
743
+ mlsAmplitude,
744
+ calibrateSoundBurstFilteredExtraDb,
745
+ calibrateSoundIIRPhase,
746
+ downsample,
747
+ });
748
+ // If the request is successful, break out of the loop
749
+ break;
750
+ } catch (error) {
751
+ console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
752
+ retryCount++;
753
+ await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
754
+ }
755
+ }
756
+
757
+ if (response) {
758
+ return response;
759
+ } else {
760
+ throw new Error(
761
+ `Failed to get inverse impulse response after ${this.MAX_RETRY_COUNT} attempts.`
762
+ );
763
+ }
764
+ };
765
+
766
+ getVolumeCalibration = async ({payload, sampleRate, lCalib}) => {
767
+ const task = 'volume';
768
+ let res = null;
769
+
770
+ console.log({payload});
771
+
772
+ const data = JSON.stringify({
773
+ task,
774
+ payload,
775
+ 'sample-rate': sampleRate,
776
+ lCalib,
777
+ });
778
+
779
+ const response = await axios({
780
+ method: 'post',
781
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL,
782
+ url: `/task/${task}`,
783
+ headers: {
784
+ 'Content-Type': 'application/json',
785
+ },
786
+ data,
787
+ })
788
+ .then(response => {
789
+ // if response.data is a string, parse it
790
+ if (typeof response.data === 'string') {
791
+ // response.data = response.data.replaceAll('Infinity', 99999999);
792
+ // response.data = JSON.parse(response.data);
793
+ //if there is Infinity in the string, throw an error
794
+ if (response.data.includes('Infinity')) {
795
+ throw new Error(
796
+ 'Server returned Infinity. Please make sure the microphone is recording correctly'
797
+ );
798
+ }
799
+ response.data = JSON.parse(response.data);
800
+ }
801
+ return response.data[task];
802
+ })
803
+ .catch(error => {
804
+ alert(
805
+ 'Invalid data. Please make sure the microphone is recording correctly and press on "Re-Record"'
806
+ );
807
+ throw error;
808
+ });
809
+
810
+ console.log(response);
811
+ return response;
812
+ };
813
+
814
+ getVolumeCalibrationParameters = async ({
815
+ inDBValues,
816
+ outDBSPLValues,
817
+ lCalib,
818
+ componentGainDBSPL,
819
+ }) => {
820
+ const task = 'volume-parameters';
821
+ let res = null;
822
+
823
+ const data = JSON.stringify({
824
+ task,
825
+ inDBValues,
826
+ outDBSPLValues,
827
+ lCalib,
828
+ componentGainDBSPL,
829
+ });
830
+
831
+ await axios({
832
+ method: 'post',
833
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL, //server
834
+ url: `/task/${task}`,
835
+ headers: {
836
+ 'Content-Type': 'application/json',
837
+ },
838
+ data,
839
+ })
840
+ .then(response => {
841
+ res = response;
842
+ console.log('data', data, 'res', res.data[task]);
843
+ })
844
+ .catch(error => {
845
+ throw error;
846
+ });
847
+
848
+ // console.log(res.data[task]);
849
+ //below is an example of res.data[task]
850
+ //{
851
+ // R: 16.56981076554259,
852
+ // RMSError: 1.9289162528535229
853
+ // T: -47.79799120884434,
854
+ // W: 61.0485247483732,
855
+ // backgroundDBSPL: 43.88233142069752,
856
+ // gainDBSPL: -128.24742161208985
857
+ //}
858
+ return res.data[task];
859
+ };
860
+
861
+ irConvolution = async ({input_signal, microphone_ir, loudspeaker_ir, duration, sample_rate}) => {
862
+ const task = 'ir-convolution';
863
+ let res = null;
864
+
865
+ const data = JSON.stringify({
866
+ input_signal,
867
+ microphone_ir,
868
+ loudspeaker_ir,
869
+ duration,
870
+ sample_rate,
871
+ });
872
+
873
+ await axios({
874
+ method: 'post',
875
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL, //server
876
+ url: `/task/${task}`,
877
+ headers: {
878
+ 'Content-Type': 'application/json',
879
+ },
880
+ data,
881
+ })
882
+ .then(response => {
883
+ res = response;
884
+ })
885
+ .catch(error => {
886
+ throw error;
887
+ });
888
+ console.log('res in irConvolution: ', res);
889
+ return res.data[task];
890
+ };
891
+
892
+ allHzPowerCheck = async ({
893
+ payload,
894
+ sampleRate,
895
+ binDesiredSec,
896
+ burstSec,
897
+ repeats,
898
+ warmUp,
899
+ downsample,
900
+ }) => {
901
+ const task = 'all-hz-check';
902
+ let res = null;
903
+
904
+ console.log({payload, sampleRate, binDesiredSec, burstSec, repeats, warmUp});
905
+
906
+ const data = JSON.stringify({
907
+ payload,
908
+ sampleRate,
909
+ binDesiredSec,
910
+ burstSec,
911
+ repeats,
912
+ warmUp,
913
+ downsample,
914
+ });
915
+
916
+ await axios({
917
+ method: 'post',
918
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL, //server
919
+ url: `/task/${task}`,
920
+ headers: {
921
+ 'Content-Type': 'application/json',
922
+ },
923
+ data,
924
+ })
925
+ .then(response => {
926
+ res = response;
927
+ if (typeof res.data === 'string') {
928
+ //if there is NaN in the string, replace it with 0
929
+ res.data = res.data.replace('NaN', '0');
930
+ res.data = JSON.parse(res.data);
931
+ }
932
+ console.log('res', res);
933
+ })
934
+ .catch(error => {
935
+ throw error;
936
+ });
937
+ return res.data[task];
938
+ };
939
+
940
+ volumePowerCheck = async ({payload, sampleRate, preSec, Sec, binDesiredSec}) => {
941
+ const task = 'volume-check';
942
+ let res = null;
943
+
944
+ const data = JSON.stringify({
945
+ payload,
946
+ sampleRate,
947
+ preSec,
948
+ Sec,
949
+ binDesiredSec,
950
+ });
951
+
952
+ await axios({
953
+ method: 'post',
954
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL, //server
955
+ url: `/task/${task}`,
956
+ headers: {
957
+ 'Content-Type': 'application/json',
958
+ },
959
+ data,
960
+ })
961
+ .then(response => {
962
+ res = response;
963
+ console.log(res.data[task]);
964
+ })
965
+ .catch(error => {
966
+ throw error;
967
+ });
968
+ return res.data[task];
969
+ };
970
+
971
+ volumePowerCheckWithRetry = async ({payload, sampleRate, preSec, Sec, binDesiredSec}) => {
972
+ let retryCount = 0;
973
+ let response = null;
974
+
975
+ while (retryCount < this.MAX_RETRY_COUNT) {
976
+ try {
977
+ response = await this.volumePowerCheck({
978
+ payload,
979
+ sampleRate,
980
+ preSec,
981
+ Sec,
982
+ binDesiredSec,
983
+ });
984
+ // If the request is successful, break out of the loop
985
+ break;
986
+ } catch (error) {
987
+ console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
988
+ retryCount++;
989
+ await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
990
+ }
991
+ }
992
+
993
+ if (response) {
994
+ return response;
995
+ } else {
996
+ throw new Error(`Failed to get volume power check after ${this.MAX_RETRY_COUNT} attempts.`);
997
+ }
998
+ };
999
+ }
1000
+
1001
+ export default PythonServerAPI;