speaker-calibration 2.0.0 → 2.1.0

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 (126) hide show
  1. package/.eslintignore +72 -0
  2. package/.eslintrc.json +40 -0
  3. package/.gitignore +78 -0
  4. package/.prettierignore +70 -0
  5. package/.prettierrc +15 -0
  6. package/LICENSE +20 -20
  7. package/README.md +133 -133
  8. package/__mocks__/fileMock.js +1 -0
  9. package/__mocks__/styleMock.js +1 -0
  10. package/babel.config.js +3 -0
  11. package/coverage/clover.xml +71 -0
  12. package/coverage/coverage-final.json +224 -0
  13. package/coverage/lcov-report/PythonServerInterface.js.html +265 -0
  14. package/coverage/lcov-report/base.css +354 -0
  15. package/coverage/lcov-report/block-navigation.js +82 -0
  16. package/coverage/lcov-report/favicon.png +0 -0
  17. package/coverage/lcov-report/index.html +123 -0
  18. package/coverage/lcov-report/prettify.css +101 -0
  19. package/coverage/lcov-report/prettify.js +937 -0
  20. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  21. package/coverage/lcov-report/sorter.js +189 -0
  22. package/coverage/lcov-report/src/index.html +121 -0
  23. package/coverage/lcov-report/src/server/PythonServerInterface.js.html +268 -0
  24. package/coverage/lcov-report/src/server/index.html +123 -0
  25. package/coverage/lcov-report/src/tasks/audioCalibrator.js.html +499 -0
  26. package/coverage/lcov-report/src/tasks/audioRecorder.js.html +412 -0
  27. package/coverage/lcov-report/src/tasks/index.html +143 -0
  28. package/coverage/lcov-report/src/tasks/volume/index.html +123 -0
  29. package/coverage/lcov-report/src/tasks/volume/volume.js.html +409 -0
  30. package/coverage/lcov-report/src/utils.js.html +172 -0
  31. package/coverage/lcov.info +91 -0
  32. package/dist/example/Queen-Bohemian_Rhapsody.wav +0 -0
  33. package/dist/example/Queen-Bohemian_Rhapsody_g_filtered.wav +0 -0
  34. package/dist/example/index.html +47 -0
  35. package/dist/example/listener.html +89 -0
  36. package/dist/example/server.js +49 -0
  37. package/dist/example/speaker.html +126 -0
  38. package/dist/example/speakerUI.js +217 -0
  39. package/dist/example/styles.css +40 -0
  40. package/dist/main.js +1 -1
  41. package/dist/mlsGen.js +6814 -6814
  42. package/dist/mlsGen.wasm +0 -0
  43. package/doc/AudioCalibrator.html +417 -0
  44. package/doc/AudioPeer.html +251 -0
  45. package/doc/AudioRecorder.html +195 -0
  46. package/doc/ImpulseResponse.html +215 -0
  47. package/doc/Listener.html +308 -0
  48. package/doc/MlsGenInterface.html +226 -0
  49. package/doc/MyEventEmitter.html +274 -0
  50. package/doc/PythonServerAPI.html +109 -0
  51. package/doc/Speaker-Calibration-UML-Diagram.png +0 -0
  52. package/doc/Speaker.html +276 -0
  53. package/doc/Takes%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +128 -0
  54. package/doc/Takes%20the%20url%20of%20the%20current%20site%0Aand%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +138 -0
  55. package/doc/Takes%20the%20url%20of%20the%20current%20site%20and%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +137 -0
  56. package/doc/Volume.html +88 -0
  57. package/doc/audioCalibrator.js.html +179 -0
  58. package/doc/audioPeer.js.html +175 -0
  59. package/doc/audioRecorder.js.html +163 -0
  60. package/doc/creates%20a%20new%20AudioRecorder%20instance.%20%0ASets%20up%20the%20audio%20context%20and%20file%20reader..html +114 -0
  61. package/doc/fonts/OpenSans-Bold-webfont.eot +0 -0
  62. package/doc/fonts/OpenSans-Bold-webfont.svg +1830 -0
  63. package/doc/fonts/OpenSans-Bold-webfont.woff +0 -0
  64. package/doc/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
  65. package/doc/fonts/OpenSans-BoldItalic-webfont.svg +1830 -0
  66. package/doc/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
  67. package/doc/fonts/OpenSans-Italic-webfont.eot +0 -0
  68. package/doc/fonts/OpenSans-Italic-webfont.svg +1830 -0
  69. package/doc/fonts/OpenSans-Italic-webfont.woff +0 -0
  70. package/doc/fonts/OpenSans-Light-webfont.eot +0 -0
  71. package/doc/fonts/OpenSans-Light-webfont.svg +1831 -0
  72. package/doc/fonts/OpenSans-Light-webfont.woff +0 -0
  73. package/doc/fonts/OpenSans-LightItalic-webfont.eot +0 -0
  74. package/doc/fonts/OpenSans-LightItalic-webfont.svg +1835 -0
  75. package/doc/fonts/OpenSans-LightItalic-webfont.woff +0 -0
  76. package/doc/fonts/OpenSans-Regular-webfont.eot +0 -0
  77. package/doc/fonts/OpenSans-Regular-webfont.svg +1831 -0
  78. package/doc/fonts/OpenSans-Regular-webfont.woff +0 -0
  79. package/doc/global.html +308 -0
  80. package/doc/index.html +58 -0
  81. package/doc/listener.js.html +170 -0
  82. package/doc/mlsGen_mlsGenInterface.js.html +117 -0
  83. package/doc/myEventEmitter.js.html +124 -0
  84. package/doc/peer-connection_audioPeer.js.html +188 -0
  85. package/doc/peer-connection_listener.js.html +311 -0
  86. package/doc/peer-connection_speaker.js.html +381 -0
  87. package/doc/sc-activity-diagram.png +0 -0
  88. package/doc/scripts/linenumber.js +25 -0
  89. package/doc/scripts/prettify/Apache-License-2.0.txt +202 -0
  90. package/doc/scripts/prettify/lang-css.js +24 -0
  91. package/doc/scripts/prettify/prettify.js +640 -0
  92. package/doc/server_PythonServerAPI.js.html +160 -0
  93. package/doc/speaker.js.html +248 -0
  94. package/doc/styles/jsdoc-default.css +371 -0
  95. package/doc/styles/prettify-jsdoc.css +111 -0
  96. package/doc/styles/prettify-tomorrow.css +163 -0
  97. package/doc/tasks_audioCalibrator.js.html +207 -0
  98. package/doc/tasks_audioRecorder.js.html +190 -0
  99. package/doc/tasks_impulse-response_impulseResponse.js.html +442 -0
  100. package/doc/tasks_impulse-response_mlsGen_mlsGenInterface.js.html +175 -0
  101. package/doc/tasks_volume_volume.js.html +185 -0
  102. package/doc/utils.js.html +105 -0
  103. package/jest.config.js +173 -0
  104. package/netlify.toml +27 -0
  105. package/package.json +67 -66
  106. package/src/index.html +21 -0
  107. package/src/main.js +21 -0
  108. package/src/myEventEmitter.js +83 -0
  109. package/src/peer-connection/audioPeer.js +151 -0
  110. package/src/peer-connection/listener.js +251 -0
  111. package/src/peer-connection/peerErrors.js +25 -0
  112. package/src/peer-connection/speaker.js +346 -0
  113. package/src/server/PythonServerAPI.js +117 -0
  114. package/src/tasks/audioCalibrator.js +218 -0
  115. package/src/tasks/audioRecorder.js +148 -0
  116. package/src/tasks/impulse-response/impulseResponse.js +436 -0
  117. package/src/tasks/impulse-response/mlsGen/mlsGen.cpp +99 -0
  118. package/src/tasks/impulse-response/mlsGen/mlsGen.hpp +304 -0
  119. package/src/tasks/impulse-response/mlsGen/mlsGenInterface.js +131 -0
  120. package/src/tasks/impulse-response/mlsGen/mlsGenTest.cpp +181 -0
  121. package/src/tasks/volume/volume.cpp +3 -0
  122. package/src/tasks/volume/volume.hpp +23 -0
  123. package/src/tasks/volume/volume.js +157 -0
  124. package/src/utils.js +55 -0
  125. package/webpack.config.js +37 -0
  126. package/README +0 -3
@@ -0,0 +1,304 @@
1
+ #ifndef SPEAKER_CALIBRATION_SRC_TASKS_IMPULSE_RESPOMSE_MLSGEN_MLSGEN_HPP_
2
+ #define SPEAKER_CALIBRATION_SRC_TASKS_IMPULSE_RESPOMSE_MLSGEN_MLSGEN_HPP_
3
+
4
+ // setup emscripten for vscode intelli sense:
5
+ // https://gist.github.com/wayou/59f3a8e4fbab050fbb32e94dd9582660'
6
+ #ifdef __EMSCRIPTEN__
7
+ #include <emscripten/bind.h>
8
+ #include <emscripten/val.h>
9
+ #endif
10
+
11
+ #include "kiss_fft.h"
12
+
13
+ /**
14
+ * @brief Exposes methods for generating an MLS signal, and calculating the
15
+ * impulse response of a recording. This class is compiled to webassembly using
16
+ * emscripten, embind, and val.
17
+ *
18
+ */
19
+ class MLSGen {
20
+ private:
21
+ // MLS parameters
22
+ long N; // mls factor
23
+ long P; // len of mls
24
+ long C; // len of recorded signals
25
+
26
+ // Async Clock Adjustment Parameters
27
+ long srcSR;
28
+ long sinkSR;
29
+
30
+ // MLS data
31
+ bool *mls;
32
+ long *tagL;
33
+ long *tagS;
34
+ float *generatedSignal; // MLS signal at +- 1
35
+
36
+ // IR data
37
+ float *recordedSignal; // isolated mls signal
38
+ float *recordedSignals; // full capture
39
+ float *perm; // permutation of recorded signals
40
+ float *resp; // impulse response of recorded signals
41
+
42
+
43
+ // Internals
44
+ void GenerateSignal();
45
+ void generateMls();
46
+ void fastHadamard();
47
+ void permuteSignal();
48
+ void permuteResponse();
49
+ void generateTagL();
50
+ void generateTagS();
51
+ void estimateDiff();
52
+ void computeCorrelation();
53
+ void computeFilter();
54
+
55
+ public:
56
+ /**
57
+ * @brief Construct a new MLSGen object with the given factor and
58
+ * sampling frequencies.
59
+ *
60
+ * @param N - number of bits
61
+ * @param srcSR - source sampling frequency
62
+ * @param sinkSR - sink sampling frequency
63
+ */
64
+ MLSGen(long N, long srcSR, long sinkSR);
65
+
66
+ #ifndef __EMSCRIPTEN__
67
+ /**
68
+ * @brief Destruct the MLSGen object.
69
+ * @return void
70
+ */
71
+ ~MLSGen();
72
+ #endif
73
+
74
+ #ifdef __EMSCRIPTEN__
75
+ /**
76
+ * @brief Destroy the MLSGen object
77
+ *
78
+ */
79
+ void Destruct();
80
+
81
+ /**
82
+ * @brief Generates an MLS signal according to the parameters set in the
83
+ * constructor. Returns a memory view of the MLS signal.
84
+ *
85
+ * @return emscripten::val
86
+ */
87
+ emscripten::val getMLS();
88
+
89
+ /**
90
+ * @brief Get the Recorded Signals Memory View object. This memory view can
91
+ * then be set in the javascript code.
92
+ *
93
+ * @return emscripten::val
94
+ */
95
+ emscripten::val setRecordedSignalsMemoryView(long sizeRecordedSignals);
96
+
97
+ emscripten::val getRecordedSignalsMemoryView();
98
+
99
+ /**
100
+ * @brief Get the Impulse Response. Returns a memory view of the impulse
101
+ * response.
102
+ *
103
+ * @return emscripten::val
104
+ */
105
+ emscripten::val getImpulseResponse();
106
+ #endif
107
+ };
108
+
109
+ void MLSGen::GenerateSignal() {
110
+ long i;
111
+ for (i = 0; i < P; i++) // Simulate a system with h = {2, 0.4, 0.2, -0.1,
112
+ // -0.8}, just an example
113
+ {
114
+ recordedSignal[i] = 2.0 * generatedSignal[(P + i - 0) % P] +
115
+ 0.4 * generatedSignal[(P + i - 1) % P] +
116
+ 0.2 * generatedSignal[(P + i - 2) % P] -
117
+ 0.1 * generatedSignal[(P + i - 3) % P] -
118
+ 0.8 * generatedSignal[(P + i - 4) % P];
119
+ }
120
+ }
121
+
122
+ void MLSGen::generateMls() {
123
+ const long maxNoTaps = 18;
124
+ const bool tapsTab[16][18] = {
125
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
126
+ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
127
+ 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0,
128
+ 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
129
+ 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1,
130
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0,
131
+ 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
132
+ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0,
133
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
134
+ 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0,
135
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
136
+ 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
137
+ bool taps[maxNoTaps];
138
+ long i, j;
139
+ bool *delayLine = new bool[maxNoTaps];
140
+ long sum;
141
+ for (i = 0; i < N; i++) // copy the N’th taps table
142
+ {
143
+ taps[i] = tapsTab[maxNoTaps - N][i];
144
+ delayLine[i] = 1;
145
+ }
146
+ for (i = 0; i < P; i++) // Generate an MLS by summing the taps mod 2
147
+ {
148
+ sum = 0;
149
+ for (j = 0; j < N; j++) {
150
+ sum += taps[j] * delayLine[j];
151
+ }
152
+ sum &= 1; // mod 2
153
+ mls[i] = delayLine[N - 1];
154
+ for (j = N - 2; j >= 0; j--) {
155
+ delayLine[j + 1] = delayLine[j];
156
+ }
157
+ delayLine[0] = *(bool *)&sum;
158
+ }
159
+ delete[] delayLine;
160
+ }
161
+
162
+ void MLSGen::fastHadamard() {
163
+ long i, i1, j, k, k1, k2, P1;
164
+ double temp;
165
+ P1 = P + 1;
166
+ k1 = P1;
167
+ for (k = 0; k < N; k++) {
168
+ k2 = k1 >> 1;
169
+ for (j = 0; j < k2; j++) {
170
+ for (i = j; i < P1; i = i + k1) {
171
+ i1 = i + k2;
172
+ temp = perm[i] + perm[i1];
173
+ perm[i1] = perm[i] - perm[i1];
174
+ perm[i] = temp;
175
+ }
176
+ }
177
+ k1 = k1 >> 1;
178
+ }
179
+ }
180
+
181
+ void MLSGen::permuteSignal() {
182
+ long i;
183
+ double dc = 0;
184
+ for (i = 0; i < P; i++) dc += recordedSignal[i];
185
+ perm[0] = -dc;
186
+ for (i = 0; i < P; i++) // Just a permutation of the measured signal
187
+ perm[tagS[i]] = recordedSignal[i];
188
+ }
189
+
190
+ void MLSGen::permuteResponse() {
191
+ long i;
192
+ const double fact = 1 / double(P + 1);
193
+ for (i = 0; i < P; i++) // Just a permutation of the impulse response
194
+ {
195
+ resp[i] = perm[tagL[i]] * fact;
196
+ }
197
+ resp[P] = 0;
198
+ }
199
+
200
+ void MLSGen::generateTagL() {
201
+ long i, j;
202
+ long *colSum = new long[P];
203
+ long *index = new long[N];
204
+ for (i = 0; i < P; i++) // Run through all the columns in the autocorr matrix
205
+ {
206
+ colSum[i] = 0;
207
+ for (j = 0; j < N; j++) // Find colSum as the value of the first N elements
208
+ // regarded as a binary number
209
+ {
210
+ colSum[i] += mls[(P + i - j) % P] << (N - 1 - j);
211
+ }
212
+ for (j = 0; j < N; j++) // Figure out if colSum is a 2^j number and store
213
+ // the column as the j’th index
214
+ {
215
+ if (colSum[i] == (1 << j)) index[j] = i;
216
+ }
217
+ }
218
+ for (i = 0; i < P; i++) // For each row in the L matrix
219
+ {
220
+ tagL[i] = 0;
221
+ for (j = 0; j < N; j++) // Find the tagL as the value of the rows in the L
222
+ // matrix regarded as a binary number
223
+ {
224
+ tagL[i] += mls[(P + index[j] - i) % P] * (1 << j);
225
+ }
226
+ }
227
+ delete[] colSum;
228
+ delete[] index;
229
+ }
230
+
231
+ void MLSGen::generateTagS() {
232
+ long i, j;
233
+ for (i = 0; i < P; i++) // For each column in the S matrix
234
+ {
235
+ tagS[i] = 0;
236
+ for (j = 0; j < N; j++) // Find the tagS as the value of the columns in the
237
+ // S matrix regarded as a binary number
238
+ {
239
+ tagS[i] += mls[(P + i - j) % P] * (1 << (N - 1 - j));
240
+ }
241
+ }
242
+ }
243
+
244
+ // #compute_correlation = (recorded, generated, P) => {
245
+
246
+ // // cross correlate to find the best match
247
+ // size = len(v) * len(generated);
248
+ // const fftr2r_x = new fftw.r2r.fft1d(size);
249
+ // const xCorr = fftr2r_x.backward(
250
+ // fftr2r_x.forward(v - v_avg) * fftr2r_x.forward(g_reversed - g_avg)
251
+ // );
252
+ // fftr2r_x.dispose(); // manual garbage collection
253
+ // const lag = this.#argMax(xCorr) - Math.floor(v.length / 2);
254
+
255
+ // // auto correlate to find the sampling difference
256
+ // size = len(v) * len(v);
257
+ // const fftr2r_auto = new fftw.r2r.fft1d(size);
258
+ // const autoCorr_full = fftr2r_auto.backward(
259
+ // fftr2r_auto.forward(v) * fftr2r_auto.forward(v_reversed)
260
+ // );
261
+ // const autoCorr = autoCorr_full.slice(len(autoCorr_full) - len(v), len(autoCorr_full));
262
+ // const inflection = this.#npdiff(Math.sign(this.#npdiff(autoCorr)));
263
+ // const peaks = inflection.map((x, i) => (x < 0 ? 1 : 0));
264
+ // };
265
+
266
+ void MLSGen::computeCorrelation() {
267
+ // inverse FFT of size C
268
+ kiss_fft_cfg xcor_cfg = kiss_fft_alloc(C,1,0,0 );
269
+ kiss_fft_cpx *xcor_in = new kiss_fft_cpx[C];
270
+ kiss_fft_cpx *xcor_out = new kiss_fft_cpx[C];
271
+
272
+ // set up input and output arrays
273
+ for(int i = 0; i < C; i++) {
274
+ xcor_in[i].r = recordedSignals[i];
275
+ xcor_in[i].i = 0;
276
+ }
277
+
278
+ // compute iFFT
279
+ kiss_fft( xcor_cfg , xcor_in , xcor_out );
280
+ // free memory
281
+ kiss_fft_free(xcor_cfg);
282
+
283
+ // find max index
284
+ int max_index = 0;
285
+ double max_value = 0;
286
+ for(int i = 0; i < C; i++) {
287
+ if(xcor_out[i].r > max_value) {
288
+ max_index = i;
289
+ max_value = xcor_out[i].r;
290
+ }
291
+ }
292
+
293
+ // use max index to compute lag
294
+ int lag = max_index - C/2;
295
+
296
+
297
+ }
298
+
299
+ void MLSGen::computeFilter() {
300
+
301
+ }
302
+
303
+
304
+ #endif // SPEAKER_CALIBRATION_SRC_TASKS_IMPULSE_RESPOMSE_MLSGEN_MLSGEN_HPP_
@@ -0,0 +1,131 @@
1
+ /* eslint-disable prefer-destructuring */
2
+ /* eslint-disable dot-notation */
3
+ // eslint-disable-next-line import/extensions
4
+ const createMLSGenModule = require('../../../../dist/mlsGen.js');
5
+
6
+ /**
7
+ * MLSGenInterface provides a class for interfacing with the MLSGen WASM module.
8
+ */
9
+ class MlsGenInterface {
10
+ /** @private */
11
+ #mlsOrder;
12
+
13
+ /** @private */
14
+ #WASMInstance; // the WASM module instance
15
+
16
+ /** @private */
17
+ #MLSGenInstance; // the MLSGen object instance
18
+
19
+ /**
20
+ * Creates an instance of MlsGenInterface.
21
+ * Makes a call to the WASM glue code to load the WASM module.
22
+ *
23
+ * @param WASMInstance
24
+ * @param mlsOrder
25
+ * @param sourceSamplingRate
26
+ * @param sinkSamplingRate
27
+ * @example
28
+ */
29
+ constructor(WASMInstance, mlsOrder, sourceSamplingRate, sinkSamplingRate) {
30
+ this.#mlsOrder = mlsOrder;
31
+ this.#WASMInstance = WASMInstance;
32
+
33
+ console.warn('initializing MLSGen, need to manually garbage collect');
34
+ this.#MLSGenInstance = new this.#WASMInstance['MLSGen'](
35
+ mlsOrder,
36
+ sourceSamplingRate,
37
+ sinkSamplingRate
38
+ );
39
+ }
40
+
41
+ /**
42
+ * Factory function that provide an asynchronous function that fetches the WASM module
43
+ * and returns a promise that resolves when the module is loaded.
44
+ *
45
+ * @param mlsOrder
46
+ * @param sourceSamplingRate - The sampling rate of the source audio.
47
+ * @param sinkSamplingRate - The sampling rate of the sink audio.
48
+ * @returns MlsGenInterface.
49
+ * @example
50
+ */
51
+ static factory = async (mlsOrder, sourceSamplingRate, sinkSamplingRate) => {
52
+ if (sourceSamplingRate === undefined || sinkSamplingRate === undefined) {
53
+ throw new Error('sourceSamplingRate and sinkSamplingRate must be defined');
54
+ }
55
+ return new MlsGenInterface(
56
+ await createMLSGenModule().then(instance => instance),
57
+ mlsOrder,
58
+ sourceSamplingRate,
59
+ sinkSamplingRate
60
+ );
61
+ };
62
+
63
+ /**
64
+ * A Higher-Order function that takes an async callback function that access the MLSGen object,
65
+ * providing safe garbage collection.
66
+ *
67
+ * @param func
68
+ * @param args
69
+ * @param funcsWithParams
70
+ * @example
71
+ */
72
+ withGarbageCollection = async funcsWithParams => {
73
+ try {
74
+ for (let i = 0; i < funcsWithParams.length; i += 1) {
75
+ const funcWithParams = funcsWithParams[i];
76
+ // eslint-disable-next-line no-await-in-loop
77
+ await funcWithParams();
78
+ }
79
+ } catch (error) {
80
+ console.error(error);
81
+ } finally {
82
+ // garbage collect
83
+ if (
84
+ this !== undefined &&
85
+ this !== null &&
86
+ this.#MLSGenInstance !== undefined &&
87
+ this.#MLSGenInstance !== null
88
+ ) {
89
+ this.#MLSGenInstance['Destruct'](); // Call the destructor
90
+ this.#MLSGenInstance['delete'](); // Delete the object
91
+ console.warn(`GARBAGE COLLECTION: deleted MLSGen`);
92
+ this.#WASMInstance['doLeakCheck'](); // Check for memory leaks
93
+ }
94
+ }
95
+ };
96
+
97
+ /**
98
+ * Calculate and return the Impulse Response of the recorded signal.
99
+ *
100
+ * @returns
101
+ * @example
102
+ */
103
+ getImpulseResponse = () => this.#MLSGenInstance['getImpulseResponse']();
104
+
105
+ /**
106
+ * Given a recorded MLS signal, this function sets the recordedSignal property of the MLSGen object.
107
+ *
108
+ * @param signals
109
+ * @example
110
+ */
111
+ setRecordedSignals = signals => {
112
+ // get memory view
113
+ const averagedSignals = this.average(signals);
114
+ const recordedSignalsMemoryView = this.#MLSGenInstance['setRecordedSignalsMemoryView'](
115
+ averagedSignals.byteLength
116
+ );
117
+ for (let i = 0; i < averagedSignals.length; i += 1) {
118
+ recordedSignalsMemoryView[i] = averagedSignals[i];
119
+ }
120
+ };
121
+
122
+ /**
123
+ * Calculate the Maximum Length Sequence (MLS) with period P = 2^N - 1
124
+ * using the MLSGen WASM module.
125
+ *
126
+ * @example
127
+ */
128
+ getMLS = () => this.#MLSGenInstance['getMLS']();
129
+ }
130
+
131
+ export default MlsGenInterface;
@@ -0,0 +1,181 @@
1
+ #include "stdio.h"
2
+
3
+ void GenerateSignal(bool *mls, double *signal, long P) {
4
+ long i;
5
+ double *input = new double[P];
6
+ for (i = 0; i < P; i++) // Change 0 to 1 and 1 to -1
7
+ {
8
+ input[i] = -2 * mls[i] + 1;
9
+ }
10
+ for (i = 0; i < P; i++) // Simulate a system with h = {2, 0.4, 0.2, -0.1,
11
+ // -0.8}, just an example
12
+ {
13
+ signal[i] = 2.0 * input[(P + i - 0) % P] + 0.4 * input[(P + i - 1) % P] +
14
+ 0.2 * input[(P + i - 2) % P] - 0.1 * input[(P + i - 3) % P] -
15
+ 0.8 * input[(P + i - 4) % P];
16
+ }
17
+ delete[] input;
18
+ }
19
+
20
+ void GenerateMls(bool *mls, long P, long N) {
21
+ const long maxNoTaps = 18;
22
+ const bool tapsTab[16][18] = {
23
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
24
+ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
25
+ 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0,
26
+ 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
27
+ 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1,
28
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0,
29
+ 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
30
+ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0,
31
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
32
+ 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0,
33
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
34
+ 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
35
+ bool taps[maxNoTaps];
36
+ long i, j;
37
+ bool *delayLine = new bool[maxNoTaps];
38
+ long sum;
39
+ for (i = 0; i < N; i++) // copy the N'th taps table
40
+ {
41
+ taps[i] = tapsTab[maxNoTaps - N][i];
42
+ delayLine[i] = 1;
43
+ }
44
+ for (i = 0; i < P; i++) // Generate an MLS by summing the taps mod 2
45
+ {
46
+ sum = 0;
47
+ for (j = 0; j < N; j++) {
48
+ sum += taps[j] * delayLine[j];
49
+ }
50
+ sum &= 1; // mod 2
51
+ mls[i] = delayLine[N - 1];
52
+ for (j = N - 2; j >= 0; j--) {
53
+ delayLine[j + 1] = delayLine[j];
54
+ }
55
+ delayLine[0] = *(bool *)&sum;
56
+ }
57
+ delete[] delayLine;
58
+ }
59
+
60
+ void FastHadamard(double *x, long P1, long N) {
61
+ long i, i1, j, k, k1, k2;
62
+ double temp;
63
+ k1 = P1;
64
+ for (k = 0; k < N; k++) {
65
+ k2 = k1 >> 1;
66
+ for (j = 0; j < k2; j++) {
67
+ for (i = j; i < P1; i = i + k1) {
68
+ i1 = i + k2;
69
+ temp = x[i] + x[i1];
70
+ x[i1] = x[i] - x[i1];
71
+ x[i] = temp;
72
+ }
73
+ }
74
+ k1 = k1 >> 1;
75
+ }
76
+ }
77
+
78
+ void PermuteSignal(double *sig, double *perm, long *tagS, long P) {
79
+ long i;
80
+ double dc = 0;
81
+ for (i = 0; i < P; i++) dc += sig[i];
82
+ perm[0] = -dc;
83
+ for (i = 0; i < P; i++) { // Just a permutation of the measured signal
84
+ perm[tagS[i]] = sig[i];
85
+ }
86
+ }
87
+ void PermuteResponse(double *perm, double *resp, long *tagL, long P) {
88
+ long i;
89
+ const double fact = 1 / double(P + 1);
90
+ for (i = 0; i < P; i++) // Just a permutation of the impulse response
91
+ {
92
+ resp[i] = perm[tagL[i]] * fact;
93
+ }
94
+ resp[P] = 0;
95
+ }
96
+
97
+ void GeneratetagL(bool *mls, long *tagL, long P, long N) {
98
+ long i, j;
99
+ long *colSum = new long[P];
100
+ long *index = new long[N];
101
+ for (i = 0; i < P; i++) // Run through all the columns in the autocorr matrix
102
+ {
103
+ colSum[i] = 0;
104
+ for (j = 0; j < N; j++) // Find colSum as the value of the first N elements
105
+ // regarded as a binary number
106
+ {
107
+ colSum[i] += mls[(P + i - j) % P] << (N - 1 - j);
108
+ }
109
+ for (j = 0; j < N; j++) // Figure out if colSum is a 2^j number and store
110
+ // the column as the j'th index
111
+ {
112
+ if (colSum[i] == (1 << j)) index[j] = i;
113
+ }
114
+ }
115
+ for (i = 0; i < P; i++) // For each row in the L matrix
116
+ {
117
+ tagL[i] = 0;
118
+ for (j = 0; j < N; j++) // Find the tagL as the value of the rows in the L
119
+ // matrix regarded as a binary number
120
+ {
121
+ tagL[i] += mls[(P + index[j] - i) % P] * (1 << j);
122
+ }
123
+ }
124
+ delete[] colSum;
125
+ delete[] index;
126
+ }
127
+
128
+ void GeneratetagS(bool *mls, long *tagS, long P, long N) {
129
+ long i, j;
130
+ for (i = 0; i < P; i++) // For each column in the S matrix
131
+ {
132
+ tagS[i] = 0;
133
+ for (j = 0; j < N; j++) // Find the tagS as the value of the columns in the
134
+ // S matrix regarded as a binary number
135
+ {
136
+ tagS[i] += mls[(P + i - j) % P] * (1 << (N - 1 - j));
137
+ }
138
+ }
139
+ }
140
+
141
+ int main() {
142
+ const long N = 18;
143
+ const long P = (1 << N) - 1;
144
+ long i;
145
+ bool *mls = new bool[P];
146
+ long *tagL = new long[P];
147
+ long *tagS = new long[P];
148
+ double *signal = new double[P];
149
+ double *perm = new double[P + 1];
150
+ double *resp = new double[P + 1];
151
+ GenerateMls(mls, P, N); // Generate the Maximum length sequence
152
+ GeneratetagL(mls, tagL, P, N); // Generate tagL for the L matrix
153
+ GeneratetagS(mls, tagS, P, N); // Generate tagS for the S matrix
154
+ GenerateSignal(mls, signal,
155
+ P); // Do a simulated measurement and get the signal
156
+ PermuteSignal(signal, perm, tagS, P); // Permute the signal according to tagS
157
+ FastHadamard(perm, P + 1, N); // Do a Hadamard transform in place
158
+ PermuteResponse(perm, resp, tagL,
159
+ P); // Permute the impulseresponse according to tagL
160
+
161
+ printf("The mls is:\n");
162
+ for (i = 0; i < 10; i++) printf("%d\n", mls[i]);
163
+ printf("The tagL is:\n");
164
+ for (i = 0; i < 10; i++) printf("%ld\n", tagL[i]);
165
+ printf("The tagS is:\n");
166
+ for (i = 0; i < 10; i++) printf("%ld\n", tagS[i]);
167
+ printf("The signal is:\n");
168
+ for (i = 0; i < 10; i++) printf("%f\n", signal[i]);
169
+ printf("The permuted signal is:\n");
170
+ for (i = 0; i < 10; i++) printf("%f\n", perm[i]);
171
+ printf("Impulse response:\n");
172
+ for (i = 0; i < 10; i++) printf("%10.5f\n", resp[i]);
173
+ delete[] mls;
174
+ delete[] tagL;
175
+ delete[] tagS;
176
+ delete[] signal;
177
+ delete[] perm;
178
+ delete[] resp;
179
+
180
+ return 0;
181
+ }
@@ -0,0 +1,3 @@
1
+ #include "volume.hpp"
2
+
3
+ #include <sanitizer/lsan_interface.h>
@@ -0,0 +1,23 @@
1
+ #ifndef SPEAKER_CALIBRATION_SRC_TASKS_VOLUMME_VOLUME_HPP_
2
+ #define SPEAKER_CALIBRATION_SRC_TASKS_VOLUMME_VOLUME_HPP_
3
+
4
+ // setup emscripten for vscode intelli sense:
5
+ // https://gist.github.com/wayou/59f3a8e4fbab050fbb32e94dd9582660'
6
+ #ifdef __EMSCRIPTEN__
7
+ #include <emscripten/bind.h>
8
+ #include <emscripten/val.h>
9
+ #endif
10
+
11
+ #include <math.h>
12
+
13
+ class Volume {
14
+ private:
15
+ const long REF_POWER = 2 * pow(10, -10);
16
+ const long REF_PRESSURE = 2 * pow(10, -5);
17
+ const long TARGET_RANGE[2] = { 3.5, 4.5 };
18
+ long sampleRate;
19
+ long *sineSignal;
20
+ long *recordedSineSignal;
21
+ }
22
+
23
+ #endif // SPEAKER_CALIBRATION_SRC_TASKS_VOLUMME_VOLUME_HPP_