speaker-calibration 2.2.97 → 2.2.98

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/.eslintignore +71 -71
  2. package/.eslintrc.json +40 -40
  3. package/.gitignore +81 -0
  4. package/.prettierignore +69 -69
  5. package/.prettierrc +14 -14
  6. package/LICENSE +20 -20
  7. package/README.md +133 -133
  8. package/__mocks__/fileMock.js +1 -1
  9. package/__mocks__/styleMock.js +1 -1
  10. package/babel.config.js +3 -3
  11. package/coverage/clover.xml +71 -71
  12. package/coverage/coverage-final.json +224 -224
  13. package/coverage/lcov-report/PythonServerInterface.js.html +265 -265
  14. package/coverage/lcov-report/base.css +354 -354
  15. package/coverage/lcov-report/block-navigation.js +82 -82
  16. package/coverage/lcov-report/index.html +123 -123
  17. package/coverage/lcov-report/prettify.css +101 -101
  18. package/coverage/lcov-report/prettify.js +937 -937
  19. package/coverage/lcov-report/sorter.js +189 -189
  20. package/coverage/lcov-report/src/index.html +121 -121
  21. package/coverage/lcov-report/src/server/PythonServerInterface.js.html +268 -268
  22. package/coverage/lcov-report/src/server/index.html +123 -123
  23. package/coverage/lcov-report/src/tasks/audioCalibrator.js.html +499 -499
  24. package/coverage/lcov-report/src/tasks/audioRecorder.js.html +412 -412
  25. package/coverage/lcov-report/src/tasks/index.html +143 -143
  26. package/coverage/lcov-report/src/tasks/volume/index.html +123 -123
  27. package/coverage/lcov-report/src/tasks/volume/volume.js.html +409 -409
  28. package/coverage/lcov-report/src/utils.js.html +172 -172
  29. package/coverage/lcov.info +91 -91
  30. package/dist/example/fetch-languages-sheets.js +77 -77
  31. package/dist/example/i18n.js +35846 -35846
  32. package/dist/example/index.html +47 -47
  33. package/dist/example/listener.html +62 -62
  34. package/dist/example/listener.js +95 -95
  35. package/dist/example/server.js +51 -51
  36. package/dist/example/speaker.html +145 -145
  37. package/dist/example/speakerUI.js +273 -273
  38. package/dist/example/styles.css +92 -92
  39. package/dist/main.js +17 -17
  40. package/dist/mlsGen.js +6814 -6814
  41. package/dist/mlsGen.wasm +0 -0
  42. package/dist/package-lock.json +1018 -1018
  43. package/dist/package.json +18 -18
  44. package/doc/AudioCalibrator.html +417 -417
  45. package/doc/AudioPeer.html +251 -251
  46. package/doc/AudioRecorder.html +195 -195
  47. package/doc/ImpulseResponse.html +215 -215
  48. package/doc/Listener.html +308 -308
  49. package/doc/MlsGenInterface.html +226 -226
  50. package/doc/MyEventEmitter.html +274 -274
  51. package/doc/PythonServerAPI.html +109 -109
  52. package/doc/Speaker.html +276 -276
  53. package/doc/Takes%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +128 -128
  54. package/doc/Takes%20the%20url%20of%20the%20current%20site%0Aand%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +138 -138
  55. package/doc/Takes%20the%20url%20of%20the%20current%20site%20and%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +137 -137
  56. package/doc/Volume.html +88 -88
  57. package/doc/audioCalibrator.js.html +179 -179
  58. package/doc/audioPeer.js.html +175 -175
  59. package/doc/audioRecorder.js.html +163 -163
  60. package/doc/creates%20a%20new%20AudioRecorder%20instance.%20%0ASets%20up%20the%20audio%20context%20and%20file%20reader..html +114 -114
  61. package/doc/fonts/OpenSans-Bold-webfont.svg +1829 -1829
  62. package/doc/fonts/OpenSans-BoldItalic-webfont.svg +1829 -1829
  63. package/doc/fonts/OpenSans-Italic-webfont.svg +1829 -1829
  64. package/doc/fonts/OpenSans-Light-webfont.svg +1830 -1830
  65. package/doc/fonts/OpenSans-LightItalic-webfont.svg +1834 -1834
  66. package/doc/fonts/OpenSans-Regular-webfont.svg +1830 -1830
  67. package/doc/global.html +308 -308
  68. package/doc/index.html +58 -58
  69. package/doc/listener.js.html +170 -170
  70. package/doc/mlsGen_mlsGenInterface.js.html +117 -117
  71. package/doc/myEventEmitter.js.html +124 -124
  72. package/doc/peer-connection_audioPeer.js.html +188 -188
  73. package/doc/peer-connection_listener.js.html +311 -311
  74. package/doc/peer-connection_speaker.js.html +381 -381
  75. package/doc/scripts/linenumber.js +25 -25
  76. package/doc/scripts/prettify/Apache-License-2.0.txt +202 -202
  77. package/doc/scripts/prettify/lang-css.js +24 -24
  78. package/doc/scripts/prettify/prettify.js +640 -640
  79. package/doc/server_PythonServerAPI.js.html +160 -160
  80. package/doc/speaker.js.html +248 -248
  81. package/doc/styles/jsdoc-default.css +371 -371
  82. package/doc/styles/prettify-jsdoc.css +111 -111
  83. package/doc/styles/prettify-tomorrow.css +163 -163
  84. package/doc/tasks_audioCalibrator.js.html +207 -207
  85. package/doc/tasks_audioRecorder.js.html +190 -190
  86. package/doc/tasks_impulse-response_impulseResponse.js.html +442 -442
  87. package/doc/tasks_impulse-response_mlsGen_mlsGenInterface.js.html +175 -175
  88. package/doc/tasks_volume_volume.js.html +185 -185
  89. package/doc/utils.js.html +105 -105
  90. package/jest.config.js +173 -173
  91. package/netlify.toml +26 -26
  92. package/package.json +69 -69
  93. package/src/config/firebase.js +26 -26
  94. package/src/index.html +21 -21
  95. package/src/main.js +23 -23
  96. package/src/myEventEmitter.js +83 -83
  97. package/src/peer-connection/audioPeer.js +151 -151
  98. package/src/peer-connection/listener.js +318 -318
  99. package/src/peer-connection/peerErrors.js +25 -25
  100. package/src/peer-connection/speaker.js +474 -474
  101. package/src/server/PythonServerAPI.js +673 -636
  102. package/src/tasks/audioCalibrator.js +308 -308
  103. package/src/tasks/audioRecorder.js +301 -301
  104. package/src/tasks/combination/combination.js +2280 -2262
  105. package/src/tasks/combination/mlsGen/mlsGen.cpp +98 -98
  106. package/src/tasks/combination/mlsGen/mlsGen.hpp +303 -303
  107. package/src/tasks/combination/mlsGen/mlsGenInterface.js +131 -131
  108. package/src/tasks/combination/mlsGen/mlsGenTest.cpp +180 -180
  109. package/src/tasks/impulse-response/impulseResponse.js +610 -610
  110. package/src/tasks/impulse-response/mlsGen/mlsGen.cpp +98 -98
  111. package/src/tasks/impulse-response/mlsGen/mlsGen.hpp +303 -303
  112. package/src/tasks/impulse-response/mlsGen/mlsGenInterface.js +131 -131
  113. package/src/tasks/impulse-response/mlsGen/mlsGenTest.cpp +180 -180
  114. package/src/tasks/volume/volume.cpp +2 -2
  115. package/src/tasks/volume/volume.hpp +22 -22
  116. package/src/tasks/volume/volume.js +279 -279
  117. package/src/utils.js +88 -88
  118. package/webpack.config.js +37 -37
  119. package/makefile +0 -74
@@ -1,636 +1,673 @@
1
- import axios from 'axios';
2
- /**
3
- *
4
- */
5
- class PythonServerAPI {
6
- static PYTHON_SERVER_URL = 'https://easyeyes-python-flask-server.herokuapp.com';
7
-
8
- static TEST_SERVER_URL = 'http://127.0.0.1:5000';
9
- // static PYTHON_SERVER_URL ='http://127.0.0.1:5000';
10
-
11
- /** @private */
12
- MAX_RETRY_COUNT = 3;
13
- /** @private */
14
- RETRY_DELAY_MS = 1000;
15
- /**
16
- * @param data- -
17
- * g = inverted impulse response, when convolved with the impulse
18
- * reponse, they cancel out.
19
- * @param data.payload
20
- * @param data.sampleRate
21
- * @param data.P
22
- * @param data-.payload
23
- * @param data-.sampleRate
24
- * @param data-.P
25
- * @returns
26
- * @example
27
- */
28
- getImpulseResponse = async ({
29
- mls,
30
- payload,
31
- sampleRate,
32
- P,
33
- numPeriods
34
- }) => {
35
- const task = 'impulse-response';
36
- let res = null;
37
-
38
- console.log({payload});
39
-
40
- const data = JSON.stringify({
41
- task,
42
- payload,
43
- 'sample-rate': sampleRate,
44
- mls,
45
- P,
46
- numPeriods,
47
- });
48
-
49
- await axios({
50
- method: 'post',
51
- baseURL: PythonServerAPI.PYTHON_SERVER_URL,
52
- url: `/task/${task}`,
53
- headers: {
54
- 'Content-Type': 'application/json',
55
- },
56
- data,
57
- })
58
- .then(response => {
59
- res = response;
60
- })
61
- .catch(error => {
62
- throw error;
63
- });
64
- return res.data[task];
65
- };
66
-
67
- getMLS = async ({length,calibrateSoundBurstDb}) => {
68
- const task = 'mls';
69
- let res = null;
70
-
71
- const data = JSON.stringify({
72
- task,
73
- length: length,
74
- calibrateSoundBurstDb: calibrateSoundBurstDb
75
- });
76
-
77
- await axios({
78
- method: 'post',
79
- baseURL: PythonServerAPI.PYTHON_SERVER_URL,
80
- url: `/task/${task}`,
81
- headers: {
82
- 'Content-Type': 'application/json',
83
- },
84
- data,
85
- })
86
- .then(response => {
87
- res = response;
88
- })
89
- .catch(error => {
90
- throw error;
91
- });
92
-
93
- return res.data[task];
94
- };
95
- getMLSWithRetry = async ({length,calibrateSoundBurstDb}) => {
96
- let retryCount = 0;
97
- let response = null;
98
-
99
- while (retryCount < this.MAX_RETRY_COUNT) {
100
- try {
101
- response = await this.getMLS({length,calibrateSoundBurstDb});
102
- // If the request is successful, break out of the loop
103
- break;
104
- } catch (error) {
105
- console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
106
- retryCount++;
107
- await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
108
- }
109
- }
110
-
111
- if (response) {
112
- return response;
113
- } else {
114
- throw new Error(`Failed to get MLS after ${this.MAX_RETRY_COUNT} attempts.`);
115
- }
116
- };
117
-
118
- getPSD = async ({unconv_rec, conv_rec, sampleRate}) => {
119
- const task = 'psd';
120
- let res = null;
121
-
122
- const data = JSON.stringify({
123
- task,
124
- unconv_rec,
125
- conv_rec,
126
- sampleRate,
127
- });
128
-
129
- await axios({
130
- method: 'post',
131
- baseURL: PythonServerAPI.PYTHON_SERVER_URL,
132
- url: `/task/${task}`,
133
- headers: {
134
- 'Content-Type': 'application/json',
135
- },
136
- data,
137
- })
138
- .then(response => {
139
- res = response;
140
- })
141
- .catch(error => {
142
- throw error;
143
- });
144
- return res.data[task];
145
- };
146
-
147
- getBackgroundNoisePSD = async ({background_rec, sampleRate}) => {
148
- const task = 'background-psd';
149
- let res = null;
150
-
151
- const data = JSON.stringify({
152
- task,
153
- background_rec,
154
- sampleRate,
155
- });
156
-
157
- await axios({
158
- method: 'post',
159
- baseURL: PythonServerAPI.PYTHON_SERVER_URL,
160
- url: `/task/${task}`,
161
- headers: {
162
- 'Content-Type': 'application/json',
163
- },
164
- data,
165
- })
166
- .then(response => {
167
- res = response;
168
- })
169
- .catch(error => {
170
- throw error;
171
- });
172
- return res.data[task];
173
- };
174
-
175
- getBackgroundNoisePSDWithRetry = async ({background_rec, sampleRate}) => {
176
- let retryCount = 0;
177
- let response = null;
178
-
179
- while (retryCount < this.MAX_RETRY_COUNT) {
180
- try {
181
- response = await this.getBackgroundNoisePSD({background_rec, sampleRate});
182
- // If the request is successful, break out of the loop
183
- break;
184
- } catch (error) {
185
- console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
186
- retryCount++;
187
- await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
188
- }
189
- }
190
-
191
- if (response) {
192
- return response;
193
- } else {
194
- throw new Error(`Failed to get PSD after ${this.MAX_RETRY_COUNT} attempts.`);
195
- }
196
- };
197
-
198
- getSubtractedPSD = async (rec, knownGains, knownFrequencies, sampleRate) => {
199
- const task = 'subtracted-psd';
200
- let res = null;
201
-
202
- const data = JSON.stringify({
203
- task,
204
- rec,
205
- knownGains,
206
- knownFrequencies,
207
- sampleRate,
208
- });
209
-
210
- await axios({
211
- method: 'post',
212
- baseURL: PythonServerAPI.PYTHON_SERVER_URL,
213
- url: `/task/${task}`,
214
- headers: {
215
- 'Content-Type': 'application/json',
216
- },
217
- data,
218
- })
219
- .then(response => {
220
- res = response;
221
- })
222
- .catch(error => {
223
- throw error;
224
- });
225
- return res.data[task];
226
- };
227
-
228
- getSubtractedPSDWithRetry = async (rec, knownGains, knownFrequencies, sampleRate) => {
229
- let retryCount = 0;
230
- let response = null;
231
-
232
- while (retryCount < this.MAX_RETRY_COUNT) {
233
- try {
234
- response = await this.getSubtractedPSD(rec, knownGains, knownFrequencies, sampleRate);
235
- // If the request is successful, break out of the loop
236
- break;
237
- } catch (error) {
238
- console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
239
- retryCount++;
240
- await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
241
- }
242
- }
243
-
244
- if (response) {
245
- return response;
246
- } else {
247
- throw new Error(`Failed to get PSD after ${this.MAX_RETRY_COUNT} attempts.`);
248
- }
249
- };
250
-
251
- getPSDWithRetry = async ({unconv_rec, conv_rec, sampleRate}) => {
252
- let retryCount = 0;
253
- let response = null;
254
-
255
- while (retryCount < this.MAX_RETRY_COUNT) {
256
- try {
257
- response = await this.getPSD({unconv_rec, conv_rec, sampleRate});
258
- // If the request is successful, break out of the loop
259
- break;
260
- } catch (error) {
261
- console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
262
- retryCount++;
263
- await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
264
- }
265
- }
266
-
267
- if (response) {
268
- return response;
269
- } else {
270
- throw new Error(`Failed to get PSD after ${this.MAX_RETRY_COUNT} attempts.`);
271
- }
272
- };
273
-
274
- getComponentInverseImpulseResponse = async ({
275
- payload,
276
- mls,
277
- lowHz,
278
- highHz,
279
- componentIRGains,
280
- iirLength,
281
- componentIRFreqs,
282
- num_periods,
283
- sampleRate,
284
- calibrateSoundBurstDb,
285
- irLength,
286
- calibrateSoundSmoothOctaves
287
- }) => {
288
- const task = 'component-inverse-impulse-response';
289
- let res = null;
290
-
291
- console.log({payload});
292
-
293
- const data = JSON.stringify({
294
- task,
295
- payload,
296
- mls,
297
- lowHz,
298
- highHz,
299
- iirLength,
300
- componentIRGains,
301
- componentIRFreqs,
302
- num_periods,
303
- sampleRate,
304
- calibrateSoundBurstDb,
305
- irLength,
306
- calibrateSoundSmoothOctaves
307
- });
308
-
309
- await axios({
310
- method: 'post',
311
- baseURL: PythonServerAPI.PYTHON_SERVER_URL,
312
- url: `/task/${task}`,
313
- headers: {
314
- 'Content-Type': 'application/json',
315
- },
316
- data,
317
- })
318
- .then(response => {
319
- res = response;
320
- })
321
- .catch(error => {
322
- throw error;
323
- });
324
-
325
- return res.data[task];
326
- };
327
- getSystemInverseImpulseResponse = async ({
328
- payload,
329
- mls,
330
- lowHz,
331
- highHz,
332
- iirLength,
333
- num_periods,
334
- sampleRate,
335
- calibrateSoundBurstDb
336
- }) => {
337
- const task = 'system-inverse-impulse-response';
338
- let res = null;
339
-
340
- console.log({payload});
341
-
342
- const data = JSON.stringify({
343
- task,
344
- payload,
345
- mls,
346
- lowHz,
347
- iirLength,
348
- highHz,
349
- num_periods,
350
- sampleRate,
351
- calibrateSoundBurstDb
352
- });
353
-
354
- await axios({
355
- method: 'post',
356
- baseURL: PythonServerAPI.PYTHON_SERVER_URL,
357
- url: `/task/${task}`,
358
- headers: {
359
- 'Content-Type': 'application/json',
360
- },
361
- data,
362
- })
363
- .then(response => {
364
- res = response;
365
- })
366
- .catch(error => {
367
- throw error;
368
- });
369
-
370
- return res.data[task];
371
- };
372
-
373
- getMLSPSD = async ({mls, sampleRate}) => {
374
- const task = 'mls-psd';
375
- let res = null;
376
-
377
- const data = JSON.stringify({
378
- task,
379
- mls,
380
- sampleRate,
381
- });
382
-
383
- await axios({
384
- method: 'post',
385
- baseURL: PythonServerAPI.PYTHON_SERVER_URL,
386
- url: `/task/${task}`,
387
- headers: {
388
- 'Content-Type': 'application/json',
389
- },
390
- data,
391
- })
392
- .then(response => {
393
- res = response;
394
- })
395
- .catch(error => {
396
- throw error;
397
- });
398
-
399
- return res.data[task];
400
- };
401
-
402
- getMLSPSDWithRetry = async ({mls, sampleRate}) => {
403
- let retryCount = 0;
404
- let response = null;
405
-
406
- while (retryCount < this.MAX_RETRY_COUNT) {
407
- try {
408
- response = await this.getMLSPSD({mls, sampleRate});
409
- // If the request is successful, break out of the loop
410
- break;
411
- } catch (error) {
412
- console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
413
- retryCount++;
414
- await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
415
- }
416
- }
417
-
418
- if (response) {
419
- return response;
420
- } else {
421
- throw new Error(
422
- `Failed to get inverse impulse response after ${this.MAX_RETRY_COUNT} attempts.`
423
- );
424
- }
425
- };
426
-
427
- getComponentInverseImpulseResponseWithRetry = async ({
428
- payload,
429
- mls,
430
- lowHz,
431
- highHz,
432
- componentIRGains,
433
- iirLength,
434
- componentIRFreqs,
435
- num_periods,
436
- sampleRate,
437
- calibrateSoundBurstDb,
438
- irLength,
439
- calibrateSoundSmoothOctaves
440
- }) => {
441
- let retryCount = 0;
442
- let response = null;
443
-
444
- while (retryCount < this.MAX_RETRY_COUNT) {
445
- try {
446
- response = await this.getComponentInverseImpulseResponse({
447
- payload,
448
- mls,
449
- lowHz,
450
- highHz,
451
- componentIRGains,
452
- iirLength,
453
- componentIRFreqs,
454
- num_periods,
455
- sampleRate,
456
- calibrateSoundBurstDb,
457
- irLength,
458
- calibrateSoundSmoothOctaves
459
- });
460
- // If the request is successful, break out of the loop
461
- break;
462
- } catch (error) {
463
- console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
464
- retryCount++;
465
- await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
466
- }
467
- }
468
-
469
- if (response) {
470
- return response;
471
- } else {
472
- throw new Error(
473
- `Failed to get inverse impulse response after ${this.MAX_RETRY_COUNT} attempts.`
474
- );
475
- }
476
- };
477
-
478
- getSystemInverseImpulseResponseWithRetry = async ({
479
- payload,
480
- mls,
481
- lowHz,
482
- highHz,
483
- iirLength,
484
- num_periods,
485
- sampleRate,
486
- calibrateSoundBurstDb
487
- }) => {
488
- let retryCount = 0;
489
- let response = null;
490
-
491
- while (retryCount < this.MAX_RETRY_COUNT) {
492
- try {
493
- response = await this.getSystemInverseImpulseResponse({
494
- payload,
495
- mls,
496
- lowHz,
497
- highHz,
498
- iirLength,
499
- num_periods,
500
- sampleRate,
501
- calibrateSoundBurstDb
502
- });
503
- // If the request is successful, break out of the loop
504
- break;
505
- } catch (error) {
506
- console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
507
- retryCount++;
508
- await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
509
- }
510
- }
511
-
512
- if (response) {
513
- return response;
514
- } else {
515
- throw new Error(
516
- `Failed to get inverse impulse response after ${this.MAX_RETRY_COUNT} attempts.`
517
- );
518
- }
519
- };
520
-
521
- getVolumeCalibration = async ({payload, sampleRate, lCalib}) => {
522
- const task = 'volume';
523
- let res = null;
524
-
525
- console.log({payload});
526
-
527
- const data = JSON.stringify({
528
- task,
529
- payload,
530
- 'sample-rate': sampleRate,
531
- lCalib,
532
- });
533
-
534
- await axios({
535
- method: 'post',
536
- baseURL: PythonServerAPI.PYTHON_SERVER_URL,
537
- url: `/task/${task}`,
538
- headers: {
539
- 'Content-Type': 'application/json',
540
- },
541
- data,
542
- })
543
- .then(response => {
544
- res = response;
545
- })
546
- .catch(error => {
547
- throw error;
548
- });
549
-
550
- return res.data[task];
551
- };
552
-
553
- getVolumeCalibrationParameters = async ({
554
- inDBValues,
555
- outDBSPLValues,
556
- lCalib,
557
- componentGainDBSPL,
558
- }) => {
559
- const task = 'volume-parameters';
560
- let res = null;
561
-
562
- const data = JSON.stringify({
563
- task,
564
- inDBValues,
565
- outDBSPLValues,
566
- lCalib,
567
- componentGainDBSPL,
568
- });
569
-
570
- await axios({
571
- method: 'post',
572
- baseURL: PythonServerAPI.PYTHON_SERVER_URL, //server
573
- url: `/task/${task}`,
574
- headers: {
575
- 'Content-Type': 'application/json',
576
- },
577
- data,
578
- })
579
- .then(response => {
580
- res = response;
581
- console.log(res.data[task]);
582
- })
583
- .catch(error => {
584
- throw error;
585
- });
586
-
587
- // console.log(res.data[task]);
588
- //below is an example of res.data[task]
589
- //{
590
- // R: 16.56981076554259,
591
- // RMSError: 1.9289162528535229
592
- // T: -47.79799120884434,
593
- // W: 61.0485247483732,
594
- // backgroundDBSPL: 43.88233142069752,
595
- // gainDBSPL: -128.24742161208985
596
- //}
597
- return res.data[task];
598
- };
599
-
600
- allHzVolumeCheck = async ({
601
- payload,
602
- sampleRate,
603
- binDesiredSec,
604
- burstSec
605
- }) => {
606
- const task = 'volume-check';
607
- let res = null;
608
-
609
- const data = JSON.stringify({
610
- payload,
611
- sampleRate,
612
- binDesiredSec,
613
- burstSec
614
- });
615
-
616
- await axios({
617
- method: 'post',
618
- baseURL: PythonServerAPI.PYTHON_SERVER_URL, //server
619
- url: `/task/${task}`,
620
- headers: {
621
- 'Content-Type': 'application/json',
622
- },
623
- data,
624
- })
625
- .then(response => {
626
- res = response;
627
- console.log(res.data[task]);
628
- })
629
- .catch(error => {
630
- throw error;
631
- });
632
- return res.data[task];
633
- };
634
- }
635
-
636
- export default PythonServerAPI;
1
+ import axios from 'axios';
2
+ /**
3
+ *
4
+ */
5
+ class PythonServerAPI {
6
+ static PYTHON_SERVER_URL = 'https://easyeyes-python-flask-server.herokuapp.com';
7
+
8
+ static TEST_SERVER_URL = 'http://127.0.0.1:5000';
9
+ // static PYTHON_SERVER_URL ='http://127.0.0.1:5000';
10
+
11
+ /** @private */
12
+ MAX_RETRY_COUNT = 3;
13
+ /** @private */
14
+ RETRY_DELAY_MS = 1000;
15
+ /**
16
+ * @param data- -
17
+ * g = inverted impulse response, when convolved with the impulse
18
+ * reponse, they cancel out.
19
+ * @param data.payload
20
+ * @param data.sampleRate
21
+ * @param data.P
22
+ * @param data-.payload
23
+ * @param data-.sampleRate
24
+ * @param data-.P
25
+ * @returns
26
+ * @example
27
+ */
28
+ getImpulseResponse = async ({
29
+ mls,
30
+ payload,
31
+ sampleRate,
32
+ P,
33
+ numPeriods
34
+ }) => {
35
+ const task = 'impulse-response';
36
+ let res = null;
37
+
38
+ console.log({payload});
39
+
40
+ const data = JSON.stringify({
41
+ task,
42
+ payload,
43
+ 'sample-rate': sampleRate,
44
+ mls,
45
+ P,
46
+ numPeriods,
47
+ });
48
+
49
+ await axios({
50
+ method: 'post',
51
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL,
52
+ url: `/task/${task}`,
53
+ headers: {
54
+ 'Content-Type': 'application/json',
55
+ },
56
+ data,
57
+ })
58
+ .then(response => {
59
+ res = response;
60
+ })
61
+ .catch(error => {
62
+ throw error;
63
+ });
64
+ return res.data[task];
65
+ };
66
+
67
+ getMLS = async ({length,calibrateSoundBurstDb}) => {
68
+ const task = 'mls';
69
+ let res = null;
70
+
71
+ const data = JSON.stringify({
72
+ task,
73
+ length: length,
74
+ calibrateSoundBurstDb: calibrateSoundBurstDb
75
+ });
76
+
77
+ await axios({
78
+ method: 'post',
79
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL,
80
+ url: `/task/${task}`,
81
+ headers: {
82
+ 'Content-Type': 'application/json',
83
+ },
84
+ data,
85
+ })
86
+ .then(response => {
87
+ res = response;
88
+ })
89
+ .catch(error => {
90
+ throw error;
91
+ });
92
+
93
+ return res.data[task];
94
+ };
95
+ getMLSWithRetry = async ({length,calibrateSoundBurstDb}) => {
96
+ let retryCount = 0;
97
+ let response = null;
98
+
99
+ while (retryCount < this.MAX_RETRY_COUNT) {
100
+ try {
101
+ response = await this.getMLS({length,calibrateSoundBurstDb});
102
+ // If the request is successful, break out of the loop
103
+ break;
104
+ } catch (error) {
105
+ console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
106
+ retryCount++;
107
+ await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
108
+ }
109
+ }
110
+
111
+ if (response) {
112
+ return response;
113
+ } else {
114
+ throw new Error(`Failed to get MLS after ${this.MAX_RETRY_COUNT} attempts.`);
115
+ }
116
+ };
117
+
118
+ getPSD = async ({unconv_rec, conv_rec, sampleRate}) => {
119
+ const task = 'psd';
120
+ let res = null;
121
+
122
+ const data = JSON.stringify({
123
+ task,
124
+ unconv_rec,
125
+ conv_rec,
126
+ sampleRate,
127
+ });
128
+
129
+ await axios({
130
+ method: 'post',
131
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL,
132
+ url: `/task/${task}`,
133
+ headers: {
134
+ 'Content-Type': 'application/json',
135
+ },
136
+ data,
137
+ })
138
+ .then(response => {
139
+ res = response;
140
+ })
141
+ .catch(error => {
142
+ throw error;
143
+ });
144
+ return res.data[task];
145
+ };
146
+
147
+ getBackgroundNoisePSD = async ({background_rec, sampleRate}) => {
148
+ const task = 'background-psd';
149
+ let res = null;
150
+
151
+ const data = JSON.stringify({
152
+ task,
153
+ background_rec,
154
+ sampleRate,
155
+ });
156
+
157
+ await axios({
158
+ method: 'post',
159
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL,
160
+ url: `/task/${task}`,
161
+ headers: {
162
+ 'Content-Type': 'application/json',
163
+ },
164
+ data,
165
+ })
166
+ .then(response => {
167
+ res = response;
168
+ })
169
+ .catch(error => {
170
+ throw error;
171
+ });
172
+ return res.data[task];
173
+ };
174
+
175
+ getBackgroundNoisePSDWithRetry = async ({background_rec, sampleRate}) => {
176
+ let retryCount = 0;
177
+ let response = null;
178
+
179
+ while (retryCount < this.MAX_RETRY_COUNT) {
180
+ try {
181
+ response = await this.getBackgroundNoisePSD({background_rec, sampleRate});
182
+ // If the request is successful, break out of the loop
183
+ break;
184
+ } catch (error) {
185
+ console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
186
+ retryCount++;
187
+ await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
188
+ }
189
+ }
190
+
191
+ if (response) {
192
+ return response;
193
+ } else {
194
+ throw new Error(`Failed to get PSD after ${this.MAX_RETRY_COUNT} attempts.`);
195
+ }
196
+ };
197
+
198
+ getSubtractedPSD = async (rec, knownGains, knownFrequencies, sampleRate) => {
199
+ const task = 'subtracted-psd';
200
+ let res = null;
201
+
202
+ const data = JSON.stringify({
203
+ task,
204
+ rec,
205
+ knownGains,
206
+ knownFrequencies,
207
+ sampleRate,
208
+ });
209
+
210
+ await axios({
211
+ method: 'post',
212
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL,
213
+ url: `/task/${task}`,
214
+ headers: {
215
+ 'Content-Type': 'application/json',
216
+ },
217
+ data,
218
+ })
219
+ .then(response => {
220
+ res = response;
221
+ })
222
+ .catch(error => {
223
+ throw error;
224
+ });
225
+ return res.data[task];
226
+ };
227
+
228
+ getSubtractedPSDWithRetry = async (rec, knownGains, knownFrequencies, sampleRate) => {
229
+ let retryCount = 0;
230
+ let response = null;
231
+
232
+ while (retryCount < this.MAX_RETRY_COUNT) {
233
+ try {
234
+ response = await this.getSubtractedPSD(rec, knownGains, knownFrequencies, sampleRate);
235
+ // If the request is successful, break out of the loop
236
+ break;
237
+ } catch (error) {
238
+ console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
239
+ retryCount++;
240
+ await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
241
+ }
242
+ }
243
+
244
+ if (response) {
245
+ return response;
246
+ } else {
247
+ throw new Error(`Failed to get PSD after ${this.MAX_RETRY_COUNT} attempts.`);
248
+ }
249
+ };
250
+
251
+ getPSDWithRetry = async ({unconv_rec, conv_rec, sampleRate}) => {
252
+ let retryCount = 0;
253
+ let response = null;
254
+
255
+ while (retryCount < this.MAX_RETRY_COUNT) {
256
+ try {
257
+ response = await this.getPSD({unconv_rec, conv_rec, sampleRate});
258
+ // If the request is successful, break out of the loop
259
+ break;
260
+ } catch (error) {
261
+ console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
262
+ retryCount++;
263
+ await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
264
+ }
265
+ }
266
+
267
+ if (response) {
268
+ return response;
269
+ } else {
270
+ throw new Error(`Failed to get PSD after ${this.MAX_RETRY_COUNT} attempts.`);
271
+ }
272
+ };
273
+
274
+ getComponentInverseImpulseResponse = async ({
275
+ payload,
276
+ mls,
277
+ lowHz,
278
+ highHz,
279
+ componentIRGains,
280
+ iirLength,
281
+ componentIRFreqs,
282
+ num_periods,
283
+ sampleRate,
284
+ calibrateSoundBurstDb,
285
+ irLength,
286
+ calibrateSoundSmoothOctaves
287
+ }) => {
288
+ const task = 'component-inverse-impulse-response';
289
+ let res = null;
290
+
291
+ console.log({payload});
292
+
293
+ const data = JSON.stringify({
294
+ task,
295
+ payload,
296
+ mls,
297
+ lowHz,
298
+ highHz,
299
+ iirLength,
300
+ componentIRGains,
301
+ componentIRFreqs,
302
+ num_periods,
303
+ sampleRate,
304
+ calibrateSoundBurstDb,
305
+ irLength,
306
+ calibrateSoundSmoothOctaves
307
+ });
308
+
309
+ await axios({
310
+ method: 'post',
311
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL,
312
+ url: `/task/${task}`,
313
+ headers: {
314
+ 'Content-Type': 'application/json',
315
+ },
316
+ data,
317
+ })
318
+ .then(response => {
319
+ res = response;
320
+ })
321
+ .catch(error => {
322
+ throw error;
323
+ });
324
+
325
+ return res.data[task];
326
+ };
327
+ getSystemInverseImpulseResponse = async ({
328
+ payload,
329
+ mls,
330
+ lowHz,
331
+ highHz,
332
+ iirLength,
333
+ num_periods,
334
+ sampleRate,
335
+ calibrateSoundBurstDb
336
+ }) => {
337
+ const task = 'system-inverse-impulse-response';
338
+ let res = null;
339
+
340
+ console.log({payload});
341
+
342
+ const data = JSON.stringify({
343
+ task,
344
+ payload,
345
+ mls,
346
+ lowHz,
347
+ iirLength,
348
+ highHz,
349
+ num_periods,
350
+ sampleRate,
351
+ calibrateSoundBurstDb
352
+ });
353
+
354
+ await axios({
355
+ method: 'post',
356
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL,
357
+ url: `/task/${task}`,
358
+ headers: {
359
+ 'Content-Type': 'application/json',
360
+ },
361
+ data,
362
+ })
363
+ .then(response => {
364
+ res = response;
365
+ })
366
+ .catch(error => {
367
+ throw error;
368
+ });
369
+
370
+ return res.data[task];
371
+ };
372
+
373
+ getMLSPSD = async ({mls, sampleRate}) => {
374
+ const task = 'mls-psd';
375
+ let res = null;
376
+
377
+ const data = JSON.stringify({
378
+ task,
379
+ mls,
380
+ sampleRate,
381
+ });
382
+
383
+ await axios({
384
+ method: 'post',
385
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL,
386
+ url: `/task/${task}`,
387
+ headers: {
388
+ 'Content-Type': 'application/json',
389
+ },
390
+ data,
391
+ })
392
+ .then(response => {
393
+ res = response;
394
+ })
395
+ .catch(error => {
396
+ throw error;
397
+ });
398
+
399
+ return res.data[task];
400
+ };
401
+
402
+ getMLSPSDWithRetry = async ({mls, sampleRate}) => {
403
+ let retryCount = 0;
404
+ let response = null;
405
+
406
+ while (retryCount < this.MAX_RETRY_COUNT) {
407
+ try {
408
+ response = await this.getMLSPSD({mls, sampleRate});
409
+ // If the request is successful, break out of the loop
410
+ break;
411
+ } catch (error) {
412
+ console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
413
+ retryCount++;
414
+ await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
415
+ }
416
+ }
417
+
418
+ if (response) {
419
+ return response;
420
+ } else {
421
+ throw new Error(
422
+ `Failed to get inverse impulse response after ${this.MAX_RETRY_COUNT} attempts.`
423
+ );
424
+ }
425
+ };
426
+
427
+ getComponentInverseImpulseResponseWithRetry = async ({
428
+ payload,
429
+ mls,
430
+ lowHz,
431
+ highHz,
432
+ componentIRGains,
433
+ iirLength,
434
+ componentIRFreqs,
435
+ num_periods,
436
+ sampleRate,
437
+ calibrateSoundBurstDb,
438
+ irLength,
439
+ calibrateSoundSmoothOctaves
440
+ }) => {
441
+ let retryCount = 0;
442
+ let response = null;
443
+
444
+ while (retryCount < this.MAX_RETRY_COUNT) {
445
+ try {
446
+ response = await this.getComponentInverseImpulseResponse({
447
+ payload,
448
+ mls,
449
+ lowHz,
450
+ highHz,
451
+ componentIRGains,
452
+ iirLength,
453
+ componentIRFreqs,
454
+ num_periods,
455
+ sampleRate,
456
+ calibrateSoundBurstDb,
457
+ irLength,
458
+ calibrateSoundSmoothOctaves
459
+ });
460
+ // If the request is successful, break out of the loop
461
+ break;
462
+ } catch (error) {
463
+ console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
464
+ retryCount++;
465
+ await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
466
+ }
467
+ }
468
+
469
+ if (response) {
470
+ return response;
471
+ } else {
472
+ throw new Error(
473
+ `Failed to get inverse impulse response after ${this.MAX_RETRY_COUNT} attempts.`
474
+ );
475
+ }
476
+ };
477
+
478
+ getSystemInverseImpulseResponseWithRetry = async ({
479
+ payload,
480
+ mls,
481
+ lowHz,
482
+ highHz,
483
+ iirLength,
484
+ num_periods,
485
+ sampleRate,
486
+ calibrateSoundBurstDb
487
+ }) => {
488
+ let retryCount = 0;
489
+ let response = null;
490
+
491
+ while (retryCount < this.MAX_RETRY_COUNT) {
492
+ try {
493
+ response = await this.getSystemInverseImpulseResponse({
494
+ payload,
495
+ mls,
496
+ lowHz,
497
+ highHz,
498
+ iirLength,
499
+ num_periods,
500
+ sampleRate,
501
+ calibrateSoundBurstDb
502
+ });
503
+ // If the request is successful, break out of the loop
504
+ break;
505
+ } catch (error) {
506
+ console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);
507
+ retryCount++;
508
+ await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));
509
+ }
510
+ }
511
+
512
+ if (response) {
513
+ return response;
514
+ } else {
515
+ throw new Error(
516
+ `Failed to get inverse impulse response after ${this.MAX_RETRY_COUNT} attempts.`
517
+ );
518
+ }
519
+ };
520
+
521
+ getVolumeCalibration = async ({payload, sampleRate, lCalib}) => {
522
+ const task = 'volume';
523
+ let res = null;
524
+
525
+ console.log({payload});
526
+
527
+ const data = JSON.stringify({
528
+ task,
529
+ payload,
530
+ 'sample-rate': sampleRate,
531
+ lCalib,
532
+ });
533
+
534
+ await axios({
535
+ method: 'post',
536
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL,
537
+ url: `/task/${task}`,
538
+ headers: {
539
+ 'Content-Type': 'application/json',
540
+ },
541
+ data,
542
+ })
543
+ .then(response => {
544
+ res = response;
545
+ })
546
+ .catch(error => {
547
+ throw error;
548
+ });
549
+
550
+ return res.data[task];
551
+ };
552
+
553
+ getVolumeCalibrationParameters = async ({
554
+ inDBValues,
555
+ outDBSPLValues,
556
+ lCalib,
557
+ componentGainDBSPL,
558
+ }) => {
559
+ const task = 'volume-parameters';
560
+ let res = null;
561
+
562
+ const data = JSON.stringify({
563
+ task,
564
+ inDBValues,
565
+ outDBSPLValues,
566
+ lCalib,
567
+ componentGainDBSPL,
568
+ });
569
+
570
+ await axios({
571
+ method: 'post',
572
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL, //server
573
+ url: `/task/${task}`,
574
+ headers: {
575
+ 'Content-Type': 'application/json',
576
+ },
577
+ data,
578
+ })
579
+ .then(response => {
580
+ res = response;
581
+ console.log(res.data[task]);
582
+ })
583
+ .catch(error => {
584
+ throw error;
585
+ });
586
+
587
+ // console.log(res.data[task]);
588
+ //below is an example of res.data[task]
589
+ //{
590
+ // R: 16.56981076554259,
591
+ // RMSError: 1.9289162528535229
592
+ // T: -47.79799120884434,
593
+ // W: 61.0485247483732,
594
+ // backgroundDBSPL: 43.88233142069752,
595
+ // gainDBSPL: -128.24742161208985
596
+ //}
597
+ return res.data[task];
598
+ };
599
+
600
+ allHzPowerCheck = async ({
601
+ payload,
602
+ sampleRate,
603
+ binDesiredSec,
604
+ burstSec
605
+ }) => {
606
+ const task = 'all-hz-check';
607
+ let res = null;
608
+
609
+ const data = JSON.stringify({
610
+ payload,
611
+ sampleRate,
612
+ binDesiredSec,
613
+ burstSec
614
+ });
615
+
616
+ await axios({
617
+ method: 'post',
618
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL, //server
619
+ url: `/task/${task}`,
620
+ headers: {
621
+ 'Content-Type': 'application/json',
622
+ },
623
+ data,
624
+ })
625
+ .then(response => {
626
+ res = response;
627
+ console.log(res.data[task]);
628
+ })
629
+ .catch(error => {
630
+ throw error;
631
+ });
632
+ return res.data[task];
633
+ };
634
+
635
+ volumePowerCheck = async ({
636
+ payload,
637
+ sampleRate,
638
+ preSec,
639
+ Sec,
640
+ binDesiredSec
641
+ }) => {
642
+ const task = 'volume-check';
643
+ let res = null;
644
+
645
+ const data = JSON.stringify({
646
+ payload,
647
+ sampleRate,
648
+ preSec,
649
+ Sec,
650
+ binDesiredSec
651
+ });
652
+
653
+ await axios({
654
+ method: 'post',
655
+ baseURL: PythonServerAPI.PYTHON_SERVER_URL, //server
656
+ url: `/task/${task}`,
657
+ headers: {
658
+ 'Content-Type': 'application/json',
659
+ },
660
+ data,
661
+ })
662
+ .then(response => {
663
+ res = response;
664
+ console.log(res.data[task]);
665
+ })
666
+ .catch(error => {
667
+ throw error;
668
+ });
669
+ return res.data[task];
670
+ };
671
+ }
672
+
673
+ export default PythonServerAPI;