speaker-calibration 2.2.247 → 2.2.249
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.
- package/.eslintignore +71 -71
- package/.eslintrc.json +40 -40
- package/.gitignore +81 -0
- package/.prettierignore +69 -69
- package/.prettierrc +14 -14
- package/LICENSE +20 -20
- package/README.md +133 -133
- package/__mocks__/fileMock.js +1 -1
- package/__mocks__/styleMock.js +1 -1
- package/babel.config.js +3 -3
- package/coverage/clover.xml +71 -71
- package/coverage/coverage-final.json +224 -224
- package/coverage/lcov-report/PythonServerInterface.js.html +265 -265
- package/coverage/lcov-report/base.css +354 -354
- package/coverage/lcov-report/block-navigation.js +82 -82
- package/coverage/lcov-report/index.html +123 -123
- package/coverage/lcov-report/prettify.css +101 -101
- package/coverage/lcov-report/prettify.js +937 -937
- package/coverage/lcov-report/sorter.js +189 -189
- package/coverage/lcov-report/src/index.html +121 -121
- package/coverage/lcov-report/src/server/PythonServerInterface.js.html +268 -268
- package/coverage/lcov-report/src/server/index.html +123 -123
- package/coverage/lcov-report/src/tasks/audioCalibrator.js.html +499 -499
- package/coverage/lcov-report/src/tasks/audioRecorder.js.html +412 -412
- package/coverage/lcov-report/src/tasks/index.html +143 -143
- package/coverage/lcov-report/src/tasks/volume/index.html +123 -123
- package/coverage/lcov-report/src/tasks/volume/volume.js.html +409 -409
- package/coverage/lcov-report/src/utils.js.html +172 -172
- package/coverage/lcov.info +91 -91
- package/dist/example/NoSleep.min.js +1 -1
- package/dist/example/fetch-languages-sheets.js +77 -77
- package/dist/example/i18n.js +29654 -30000
- package/dist/example/index.html +47 -47
- package/dist/example/listener.html +81 -81
- package/dist/example/server.js +51 -51
- package/dist/example/speaker.html +145 -145
- package/dist/example/speakerUI.js +273 -273
- package/dist/example/styles.css +152 -152
- package/dist/main.js +1 -1
- package/dist/mlsGen.js +6814 -6814
- package/dist/mlsGen.wasm +0 -0
- package/dist/package-lock.json +1018 -1018
- package/dist/package.json +18 -18
- package/doc/AudioCalibrator.html +417 -417
- package/doc/AudioPeer.html +251 -251
- package/doc/AudioRecorder.html +195 -195
- package/doc/ImpulseResponse.html +215 -215
- package/doc/Listener.html +308 -308
- package/doc/MlsGenInterface.html +226 -226
- package/doc/MyEventEmitter.html +274 -274
- package/doc/PythonServerAPI.html +109 -109
- package/doc/Speaker.html +276 -276
- package/doc/Takes%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +128 -128
- package/doc/Takes%20the%20url%20of%20the%20current%20site%0Aand%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +138 -138
- package/doc/Takes%20the%20url%20of%20the%20current%20site%20and%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +137 -137
- package/doc/Volume.html +88 -88
- package/doc/audioCalibrator.js.html +179 -179
- package/doc/audioPeer.js.html +175 -175
- package/doc/audioRecorder.js.html +163 -163
- package/doc/creates%20a%20new%20AudioRecorder%20instance.%20%0ASets%20up%20the%20audio%20context%20and%20file%20reader..html +114 -114
- package/doc/fonts/OpenSans-Bold-webfont.svg +1829 -1829
- package/doc/fonts/OpenSans-BoldItalic-webfont.svg +1829 -1829
- package/doc/fonts/OpenSans-Italic-webfont.svg +1829 -1829
- package/doc/fonts/OpenSans-Light-webfont.svg +1830 -1830
- package/doc/fonts/OpenSans-LightItalic-webfont.svg +1834 -1834
- package/doc/fonts/OpenSans-Regular-webfont.svg +1830 -1830
- package/doc/global.html +308 -308
- package/doc/index.html +58 -58
- package/doc/listener.js.html +170 -170
- package/doc/mlsGen_mlsGenInterface.js.html +117 -117
- package/doc/myEventEmitter.js.html +124 -124
- package/doc/peer-connection_audioPeer.js.html +188 -188
- package/doc/peer-connection_listener.js.html +311 -311
- package/doc/peer-connection_speaker.js.html +381 -381
- package/doc/scripts/linenumber.js +25 -25
- package/doc/scripts/prettify/Apache-License-2.0.txt +202 -202
- package/doc/scripts/prettify/lang-css.js +24 -24
- package/doc/scripts/prettify/prettify.js +640 -640
- package/doc/server_PythonServerAPI.js.html +160 -160
- package/doc/speaker.js.html +248 -248
- package/doc/styles/jsdoc-default.css +371 -371
- package/doc/styles/prettify-jsdoc.css +111 -111
- package/doc/styles/prettify-tomorrow.css +163 -163
- package/doc/tasks_audioCalibrator.js.html +207 -207
- package/doc/tasks_audioRecorder.js.html +190 -190
- package/doc/tasks_impulse-response_impulseResponse.js.html +442 -442
- package/doc/tasks_impulse-response_mlsGen_mlsGenInterface.js.html +175 -175
- package/doc/tasks_volume_volume.js.html +185 -185
- package/doc/utils.js.html +105 -105
- package/jest.config.js +173 -173
- package/netlify.toml +26 -26
- package/package.json +78 -78
- package/src/config/firebase.js +26 -26
- package/src/index.html +21 -21
- package/src/listener-app/PhonePeer.js +474 -474
- package/src/listener-app/listener.js +377 -377
- package/src/main.js +22 -22
- package/src/myEventEmitter.js +83 -83
- package/src/peer-connection/audioPeer.js +100 -100
- package/src/peer-connection/listener.js +299 -299
- package/src/peer-connection/peerErrors.js +25 -25
- package/src/peer-connection/speaker.js +963 -963
- package/src/powerCheck.js +110 -110
- package/src/server/PythonServerAPI.js +959 -959
- package/src/tasks/combination/combination.js +44 -29
- package/src/tasks/combination/mlsGen/mlsGen.cpp +98 -98
- package/src/tasks/combination/mlsGen/mlsGen.hpp +303 -303
- package/src/tasks/combination/mlsGen/mlsGenInterface.js +131 -131
- package/src/tasks/combination/mlsGen/mlsGenTest.cpp +180 -180
- package/src/tasks/impulse-response/impulseResponse.js +610 -610
- package/src/tasks/impulse-response/mlsGen/mlsGen.cpp +98 -98
- package/src/tasks/impulse-response/mlsGen/mlsGen.hpp +303 -303
- package/src/tasks/impulse-response/mlsGen/mlsGenInterface.js +131 -131
- package/src/tasks/impulse-response/mlsGen/mlsGenTest.cpp +180 -180
- package/src/tasks/volume/volume.cpp +2 -2
- package/src/tasks/volume/volume.hpp +22 -22
- package/src/tasks/volume/volume.js +279 -279
- package/src/utils.js +205 -205
- package/webpack.config.js +65 -65
- package/.github/workflows/update-phrases.yml +0 -37
- package/makefile +0 -74
|
@@ -1,304 +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 *)∑
|
|
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
|
-
|
|
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 *)∑
|
|
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
304
|
#endif // SPEAKER_CALIBRATION_SRC_TASKS_IMPULSE_RESPOMSE_MLSGEN_MLSGEN_HPP_
|