speaker-calibration 2.2.201 → 2.2.203

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 (141) 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/favicon.png +0 -0
  17. package/coverage/lcov-report/index.html +123 -123
  18. package/coverage/lcov-report/prettify.css +101 -101
  19. package/coverage/lcov-report/prettify.js +937 -937
  20. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  21. package/coverage/lcov-report/sorter.js +189 -189
  22. package/coverage/lcov-report/src/index.html +121 -121
  23. package/coverage/lcov-report/src/server/PythonServerInterface.js.html +268 -268
  24. package/coverage/lcov-report/src/server/index.html +123 -123
  25. package/coverage/lcov-report/src/tasks/audioCalibrator.js.html +499 -499
  26. package/coverage/lcov-report/src/tasks/audioRecorder.js.html +412 -412
  27. package/coverage/lcov-report/src/tasks/index.html +143 -143
  28. package/coverage/lcov-report/src/tasks/volume/index.html +123 -123
  29. package/coverage/lcov-report/src/tasks/volume/volume.js.html +409 -409
  30. package/coverage/lcov-report/src/utils.js.html +172 -172
  31. package/coverage/lcov.info +91 -91
  32. package/dist/Procfile +0 -0
  33. package/dist/example/NoSleep.min.js +1 -1
  34. package/dist/example/credentials.json.gpg +0 -0
  35. package/dist/example/fetch-languages-sheets.js +77 -77
  36. package/dist/example/i18n.js +27366 -27366
  37. package/dist/example/index.html +47 -47
  38. package/dist/example/listener.html +79 -79
  39. package/dist/example/listener.js +149 -149
  40. package/dist/example/server.js +51 -51
  41. package/dist/example/speaker.html +145 -145
  42. package/dist/example/speakerUI.js +273 -273
  43. package/dist/example/styles.css +99 -99
  44. package/dist/main.js +18 -18
  45. package/dist/main.js.LICENSE.txt +0 -0
  46. package/dist/mlsGen.js +6814 -6814
  47. package/dist/mlsGen.wasm +0 -0
  48. package/dist/package-lock.json +1018 -1018
  49. package/dist/package.json +18 -18
  50. package/doc/AudioCalibrator.html +417 -417
  51. package/doc/AudioPeer.html +251 -251
  52. package/doc/AudioRecorder.html +195 -195
  53. package/doc/ImpulseResponse.html +215 -215
  54. package/doc/Listener.html +308 -308
  55. package/doc/MlsGenInterface.html +226 -226
  56. package/doc/MyEventEmitter.html +274 -274
  57. package/doc/PythonServerAPI.html +109 -109
  58. package/doc/Speaker-Calibration-UML-Diagram.png +0 -0
  59. package/doc/Speaker.html +276 -276
  60. package/doc/Takes%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +128 -128
  61. package/doc/Takes%20the%20url%20of%20the%20current%20site%0Aand%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +138 -138
  62. package/doc/Takes%20the%20url%20of%20the%20current%20site%20and%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +137 -137
  63. package/doc/Volume.html +88 -88
  64. package/doc/audioCalibrator.js.html +179 -179
  65. package/doc/audioPeer.js.html +175 -175
  66. package/doc/audioRecorder.js.html +163 -163
  67. package/doc/creates%20a%20new%20AudioRecorder%20instance.%20%0ASets%20up%20the%20audio%20context%20and%20file%20reader..html +114 -114
  68. package/doc/fonts/OpenSans-Bold-webfont.eot +0 -0
  69. package/doc/fonts/OpenSans-Bold-webfont.svg +1829 -1829
  70. package/doc/fonts/OpenSans-Bold-webfont.woff +0 -0
  71. package/doc/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
  72. package/doc/fonts/OpenSans-BoldItalic-webfont.svg +1829 -1829
  73. package/doc/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
  74. package/doc/fonts/OpenSans-Italic-webfont.eot +0 -0
  75. package/doc/fonts/OpenSans-Italic-webfont.svg +1829 -1829
  76. package/doc/fonts/OpenSans-Italic-webfont.woff +0 -0
  77. package/doc/fonts/OpenSans-Light-webfont.eot +0 -0
  78. package/doc/fonts/OpenSans-Light-webfont.svg +1830 -1830
  79. package/doc/fonts/OpenSans-Light-webfont.woff +0 -0
  80. package/doc/fonts/OpenSans-LightItalic-webfont.eot +0 -0
  81. package/doc/fonts/OpenSans-LightItalic-webfont.svg +1834 -1834
  82. package/doc/fonts/OpenSans-LightItalic-webfont.woff +0 -0
  83. package/doc/fonts/OpenSans-Regular-webfont.eot +0 -0
  84. package/doc/fonts/OpenSans-Regular-webfont.svg +1830 -1830
  85. package/doc/fonts/OpenSans-Regular-webfont.woff +0 -0
  86. package/doc/global.html +308 -308
  87. package/doc/index.html +58 -58
  88. package/doc/listener.js.html +170 -170
  89. package/doc/mlsGen_mlsGenInterface.js.html +117 -117
  90. package/doc/myEventEmitter.js.html +124 -124
  91. package/doc/peer-connection_audioPeer.js.html +188 -188
  92. package/doc/peer-connection_listener.js.html +311 -311
  93. package/doc/peer-connection_speaker.js.html +381 -381
  94. package/doc/sc-activity-diagram.png +0 -0
  95. package/doc/scripts/linenumber.js +25 -25
  96. package/doc/scripts/prettify/Apache-License-2.0.txt +202 -202
  97. package/doc/scripts/prettify/lang-css.js +24 -24
  98. package/doc/scripts/prettify/prettify.js +640 -640
  99. package/doc/server_PythonServerAPI.js.html +160 -160
  100. package/doc/speaker.js.html +248 -248
  101. package/doc/styles/jsdoc-default.css +371 -371
  102. package/doc/styles/prettify-jsdoc.css +111 -111
  103. package/doc/styles/prettify-tomorrow.css +163 -163
  104. package/doc/tasks_audioCalibrator.js.html +207 -207
  105. package/doc/tasks_audioRecorder.js.html +190 -190
  106. package/doc/tasks_impulse-response_impulseResponse.js.html +442 -442
  107. package/doc/tasks_impulse-response_mlsGen_mlsGenInterface.js.html +175 -175
  108. package/doc/tasks_volume_volume.js.html +185 -185
  109. package/doc/utils.js.html +105 -105
  110. package/jest.config.js +173 -173
  111. package/netlify.toml +26 -26
  112. package/package.json +73 -73
  113. package/src/config/firebase.js +26 -26
  114. package/src/index.html +21 -21
  115. package/src/main.js +23 -23
  116. package/src/myEventEmitter.js +83 -83
  117. package/src/peer-connection/audioPeer.js +178 -178
  118. package/src/peer-connection/listener.js +340 -340
  119. package/src/peer-connection/peerErrors.js +25 -25
  120. package/src/peer-connection/speaker.js +731 -731
  121. package/src/powerCheck.js +98 -98
  122. package/src/server/PythonServerAPI.js +869 -869
  123. package/src/tasks/audioCalibrator.js +336 -335
  124. package/src/tasks/audioRecorder.js +315 -315
  125. package/src/tasks/combination/combination.js +2996 -2968
  126. package/src/tasks/combination/mlsGen/mlsGen.cpp +98 -98
  127. package/src/tasks/combination/mlsGen/mlsGen.hpp +303 -303
  128. package/src/tasks/combination/mlsGen/mlsGenInterface.js +131 -131
  129. package/src/tasks/combination/mlsGen/mlsGenTest.cpp +180 -180
  130. package/src/tasks/impulse-response/impulseResponse.js +610 -610
  131. package/src/tasks/impulse-response/mlsGen/mlsGen.cpp +98 -98
  132. package/src/tasks/impulse-response/mlsGen/mlsGen.hpp +303 -303
  133. package/src/tasks/impulse-response/mlsGen/mlsGenInterface.js +131 -131
  134. package/src/tasks/impulse-response/mlsGen/mlsGenTest.cpp +180 -180
  135. package/src/tasks/volume/volume.cpp +2 -2
  136. package/src/tasks/volume/volume.hpp +22 -22
  137. package/src/tasks/volume/volume.js +279 -279
  138. package/src/utils.js +205 -205
  139. package/webpack.config.js +37 -37
  140. package/.github/workflows/update-phrases.yml +0 -37
  141. package/makefile +0 -74
@@ -1,869 +1,869 @@
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': 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
- volumePowerCheckWithRetry = async ({
835
- payload,
836
- sampleRate,
837
- preSec,
838
- Sec,
839
- binDesiredSec
840
- }) => {
841
- let retryCount = 0;
842
- let response = null;
843
-
844
- while (retryCount < this.MAX_RETRY_COUNT) {
845
- try {
846
- response = await this.volumePowerCheck({
847
- payload,
848
- sampleRate,
849
- preSec,
850
- Sec,
851
- binDesiredSec});
852
- // If the request is successful, break out of the loop
853
- break;
854
- } catch (error) {
855
- console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
856
- retryCount++;
857
- await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
858
- }
859
- }
860
-
861
- if (response) {
862
- return response;
863
- } else {
864
- throw new Error(`Failed to get volume power check after ${this.MAX_RETRY_COUNT} attempts.`);
865
- }
866
- };
867
- }
868
-
869
- 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': 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
+ volumePowerCheckWithRetry = async ({
835
+ payload,
836
+ sampleRate,
837
+ preSec,
838
+ Sec,
839
+ binDesiredSec
840
+ }) => {
841
+ let retryCount = 0;
842
+ let response = null;
843
+
844
+ while (retryCount < this.MAX_RETRY_COUNT) {
845
+ try {
846
+ response = await this.volumePowerCheck({
847
+ payload,
848
+ sampleRate,
849
+ preSec,
850
+ Sec,
851
+ binDesiredSec});
852
+ // If the request is successful, break out of the loop
853
+ break;
854
+ } catch (error) {
855
+ console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
856
+ retryCount++;
857
+ await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
858
+ }
859
+ }
860
+
861
+ if (response) {
862
+ return response;
863
+ } else {
864
+ throw new Error(`Failed to get volume power check after ${this.MAX_RETRY_COUNT} attempts.`);
865
+ }
866
+ };
867
+ }
868
+
869
+ export default PythonServerAPI;