speaker-calibration 2.2.83 → 2.2.85

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