speaker-calibration 2.2.188 → 2.2.190

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