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.
Files changed (121) 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/NoSleep.min.js +1 -1
  31. package/dist/example/fetch-languages-sheets.js +77 -77
  32. package/dist/example/i18n.js +29654 -30000
  33. package/dist/example/index.html +47 -47
  34. package/dist/example/listener.html +81 -81
  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 +152 -152
  39. package/dist/main.js +1 -1
  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 +78 -78
  93. package/src/config/firebase.js +26 -26
  94. package/src/index.html +21 -21
  95. package/src/listener-app/PhonePeer.js +474 -474
  96. package/src/listener-app/listener.js +377 -377
  97. package/src/main.js +22 -22
  98. package/src/myEventEmitter.js +83 -83
  99. package/src/peer-connection/audioPeer.js +100 -100
  100. package/src/peer-connection/listener.js +299 -299
  101. package/src/peer-connection/peerErrors.js +25 -25
  102. package/src/peer-connection/speaker.js +963 -963
  103. package/src/powerCheck.js +110 -110
  104. package/src/server/PythonServerAPI.js +959 -959
  105. package/src/tasks/combination/combination.js +44 -29
  106. package/src/tasks/combination/mlsGen/mlsGen.cpp +98 -98
  107. package/src/tasks/combination/mlsGen/mlsGen.hpp +303 -303
  108. package/src/tasks/combination/mlsGen/mlsGenInterface.js +131 -131
  109. package/src/tasks/combination/mlsGen/mlsGenTest.cpp +180 -180
  110. package/src/tasks/impulse-response/impulseResponse.js +610 -610
  111. package/src/tasks/impulse-response/mlsGen/mlsGen.cpp +98 -98
  112. package/src/tasks/impulse-response/mlsGen/mlsGen.hpp +303 -303
  113. package/src/tasks/impulse-response/mlsGen/mlsGenInterface.js +131 -131
  114. package/src/tasks/impulse-response/mlsGen/mlsGenTest.cpp +180 -180
  115. package/src/tasks/volume/volume.cpp +2 -2
  116. package/src/tasks/volume/volume.hpp +22 -22
  117. package/src/tasks/volume/volume.js +279 -279
  118. package/src/utils.js +205 -205
  119. package/webpack.config.js +65 -65
  120. package/.github/workflows/update-phrases.yml +0 -37
  121. package/makefile +0 -74
@@ -1,442 +1,442 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="utf-8" />
5
- <title>JSDoc: Source: tasks/impulse-response/impulseResponse.js</title>
6
-
7
- <script src="scripts/prettify/prettify.js"></script>
8
- <script src="scripts/prettify/lang-css.js"></script>
9
- <!--[if lt IE 9]>
10
- <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
11
- <![endif]-->
12
- <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css" />
13
- <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css" />
14
- </head>
15
-
16
- <body>
17
- <div id="main">
18
- <h1 class="page-title">Source: tasks/impulse-response/impulseResponse.js</h1>
19
-
20
- <section>
21
- <article>
22
- <pre
23
- class="prettyprint source linenums"
24
- ><code>import AudioCalibrator from '../audioCalibrator';
25
- import MlsGenInterface from './mlsGen/mlsGenInterface';
26
-
27
- import {sleep, csvToArray} from '../../utils';
28
-
29
- /**
30
- *
31
- */
32
- class ImpulseResponse extends AudioCalibrator {
33
- /**
34
- * Default constructor. Creates an instance with any number of paramters passed or the default parameters defined here.
35
- * @param {*} param0
36
- */
37
- constructor({download = false, mlsOrder = 18, numCaptures = 5, numMLSPerCapture = 4}) {
38
- super(numCaptures, numMLSPerCapture);
39
- this.#mlsOrder = parseInt(mlsOrder, 10);
40
- this.#P = 2 ** mlsOrder - 1;
41
- this.#download = download;
42
- }
43
-
44
- /** @private */
45
- #download;
46
-
47
- /** @private */
48
- #mlsGenInterface;
49
-
50
- /** @private */
51
- #mlsBufferView;
52
-
53
- /** @private */
54
- invertedImpulseResponse = null;
55
-
56
- /** @private */
57
- impulseResponses = [];
58
-
59
- /** @private */
60
- #mlsOrder;
61
-
62
- #P;
63
-
64
- /** @private */
65
- TAPER_SECS = 5;
66
-
67
- /** @private */
68
- offsetGainNode;
69
-
70
- /**
71
- * Sends all the computed impulse responses to the backend server for processing
72
- * @returns sets the resulting inverted impulse response to the class property
73
- */
74
- sendImpulseResponsesToServerForProcessing = async () => {
75
- const computedIRs = await Promise.all(this.impulseResponses);
76
- this.emit('update', {message: `computing the IIR...`});
77
- return this.pyServerAPI
78
- .getInverseImpulseResponse({
79
- payload: computedIRs,
80
- })
81
- .then(res => {
82
- this.emit('update', {message: `done computing the IIR...`});
83
- this.invertedImpulseResponse = res;
84
- })
85
- .catch(err => {
86
- // this.emit('InvertedImpulseResponse', {res: false});
87
- console.error(err);
88
- });
89
- };
90
-
91
- /**
92
- * Sends the recorded signal, or a given csv string of a signal, to the back end server for processing
93
- * @param {&lt;array>String} signalCsv - Optional csv string of a previously recorded signal, if given, this signal will be processed
94
- */
95
- sendRecordingToServerForProcessing = signalCsv => {
96
- const allSignals = this.getAllRecordedSignals();
97
- const numSignals = allSignals.length;
98
- const payload =
99
- signalCsv &amp;&amp; signalCsv.length > 0 ? csvToArray(signalCsv) : allSignals[numSignals - 1];
100
-
101
- this.emit('update', {message: `computing the IR of the last recording...`});
102
- this.impulseResponses.push(
103
- this.pyServerAPI
104
- .getImpulseResponse({
105
- sampleRate: this.sourceSamplingRate || 96000,
106
- payload,
107
- P: this.#P,
108
- })
109
- .then(res => {
110
- if (this.numSuccessfulCaptured &lt; this.numCaptures) {
111
- this.numSuccessfulCaptured += 1;
112
- this.emit('update', {
113
- message: `${this.numSuccessfulCaptured}/${this.numCaptures} IRs computed...`,
114
- });
115
- }
116
- return res;
117
- })
118
- .catch(err => {
119
- console.error(err);
120
- })
121
- );
122
- };
123
-
124
- /**
125
- * Passed to the calibration steps function, awaits the desired amount of seconds to capture the desired number
126
- * of MLS periods defined in the constructor
127
- */
128
- #awaitDesiredMLSLength = async () => {
129
- // seconds per MLS = P / SR
130
- // await N * P / SR
131
- this.emit('update', {
132
- message: `sampling the calibration signal...`,
133
- });
134
- await sleep((this.#P / this.sourceSamplingRate) * this.numMLSPerCapture);
135
- };
136
-
137
- /**
138
- * Passed to the calibration steps function, awaits the onset of the signal to ensure a steady state
139
- */
140
- #awaitSignalOnset = async () => {
141
- this.emit('update', {
142
- message: `waiting for the signal to stabalize...`,
143
- });
144
- await sleep(this.TAPER_SECS);
145
- };
146
-
147
- /**
148
- * Called immediately after a recording is captured. Used to process the resulting signal
149
- * whether by sending the result to a server or by computing a result locally
150
- */
151
- #afterMLSRecord = () => {
152
- if (this.#download) {
153
- this.downloadData();
154
- }
155
- this.#stopCalibrationAudio();
156
- this.sendRecordingToServerForProcessing();
157
- };
158
-
159
- #afterMLSwIIRRecord = () => {
160
- if (this.#download) {
161
- this.downloadData();
162
- }
163
- this.#stopCalibrationAudio();
164
- };
165
-
166
- /**
167
- * Created an S Curver Buffer to taper the signal onset
168
- * @param {*} length
169
- * @param {*} phase
170
- * @returns
171
- */
172
- static createSCurveBuffer = (length, phase) => {
173
- const curve = new Float32Array(length);
174
- let i;
175
- for (i = 0; i &lt; length; i += 1) {
176
- // scale the curve to be between 0-1
177
- curve[i] = Math.sin((Math.PI * i) / length - phase) / 2 + 0.5;
178
- }
179
- return curve;
180
- };
181
-
182
- static createInverseSCurveBuffer = (length, phase) => {
183
- const curve = new Float32Array(length);
184
- let i;
185
- let j = length - 1;
186
- for (i = 0; i &lt; length; i += 1) {
187
- // scale the curve to be between 0-1
188
- curve[i] = Math.sin((Math.PI * j) / length - phase) / 2 + 0.5;
189
- j -= 1;
190
- }
191
- return curve;
192
- };
193
-
194
- /**
195
- * Construct a Calibration Node with the calibration parameters.
196
- * @private
197
- */
198
- #createPureTonenNode = CALIBRATION_TONE_FREQUENCY => {
199
- const audioContext = this.makeNewSourceAudioContext();
200
- const oscilator = audioContext.createOscillator();
201
- const gainNode = audioContext.createGain();
202
-
203
- oscilator.frequency.value = CALIBRATION_TONE_FREQUENCY;
204
- oscilator.type = 'sine';
205
- gainNode.gain.value = 0.04;
206
-
207
- oscilator.connect(gainNode);
208
- gainNode.connect(audioContext.destination);
209
-
210
- this.addCalibrationNode(oscilator);
211
- };
212
-
213
- /**
214
- * Construct a Calibration Node with the calibration parameters.
215
- * @private
216
- */
217
- #createCalibrationNodeFromBuffer = dataBuffer => {
218
- const audioContext = this.makeNewSourceAudioContext();
219
- const buffer = audioContext.createBuffer(
220
- 1, // number of channels
221
- dataBuffer.length,
222
- audioContext.sampleRate // sample rate
223
- );
224
-
225
- const data = buffer.getChannelData(0); // get data
226
- // fill the buffer with our data
227
- try {
228
- for (let i = 0; i &lt; dataBuffer.length; i += 1) {
229
- data[i] = dataBuffer[i];
230
- }
231
- } catch (error) {
232
- console.error(error);
233
- }
234
-
235
- const source = audioContext.createBufferSource();
236
-
237
- source.buffer = buffer;
238
- source.loop = true;
239
- source.connect(audioContext.destination);
240
-
241
- this.addCalibrationNode(source);
242
- };
243
-
244
- /**
245
- * Given a data buffer, creates the required calibration node
246
- * @param {*} dataBufferArray
247
- */
248
- #setCalibrationNodesFromBuffer = (dataBufferArray = [this.#mlsBufferView]) => {
249
- if (dataBufferArray.length === 1) {
250
- this.#createCalibrationNodeFromBuffer(dataBufferArray[0]);
251
- } else {
252
- throw new Error('The length of the data buffer array must be 1');
253
- }
254
- };
255
-
256
- #createImpulseResponseFilterGraph = (calibrationSignal, iir) => {
257
- const audioCtx = this.makeNewSourceAudioContext();
258
-
259
- // -------------------------------------------------------- IIR
260
- const iirBuffer = audioCtx.createBuffer(
261
- 1,
262
- // TODO: quality check this
263
- iir.length - 1,
264
- audioCtx.sampleRate
265
- );
266
-
267
- // Fill the buffer with the inverted impulse response
268
- const iirChannelZeroBuffer = iirBuffer.getChannelData(0);
269
- for (let i = 0; i &lt; iirBuffer.length; i++) {
270
- // audio needs to be in [-1.0; 1.0]
271
- iirChannelZeroBuffer[i] = iir[i];
272
- }
273
-
274
- const convolverNode = audioCtx.createConvolver();
275
-
276
- convolverNode.normalize = false;
277
- convolverNode.channelCount = 1;
278
- convolverNode.buffer = iirBuffer;
279
-
280
- // ------------------------------------------------------ MLS
281
- const calibrationSignalBuffer = audioCtx.createBuffer(
282
- 1, // number of channels
283
- calibrationSignal.length,
284
- audioCtx.sampleRate // sample rate
285
- );
286
-
287
- const mlsChannelZeroBuffer = calibrationSignalBuffer.getChannelData(0); // get data
288
- // fill the buffer with our data
289
- try {
290
- for (let i = 0; i &lt; calibrationSignal.length; i += 1) {
291
- mlsChannelZeroBuffer[i] = calibrationSignal[i];
292
- }
293
- } catch (error) {
294
- console.error(error);
295
- }
296
-
297
- const sourceNode = audioCtx.createBufferSource();
298
-
299
- sourceNode.buffer = calibrationSignalBuffer;
300
- sourceNode.loop = true;
301
- sourceNode.connect(convolverNode);
302
-
303
- convolverNode.connect(audioCtx.destination);
304
-
305
- console.log({convolverNode, sourceNode});
306
-
307
- this.addCalibrationNode(sourceNode);
308
- };
309
-
310
- #createIIRwMLSGraph = () => {
311
- this.#createImpulseResponseFilterGraph(this.impulseResponses, [this.#mlsBufferView][0]);
312
- };
313
-
314
- /**
315
- * Creates an audio context and plays it for a few seconds.
316
- * @private
317
- * @returns {Promise} - Resolves when the audio is done playing.
318
- */
319
- #playCalibrationAudio = () => {
320
- this.calibrationNodes[0].start(0);
321
- this.emit('update', {message: 'playing the calibration tone...'});
322
- };
323
-
324
- /**
325
- * Stops the audio with tapered offset
326
- */
327
- #stopCalibrationAudio = () => {
328
- this.calibrationNodes[0].stop();
329
- this.emit('update', {message: 'stopping the calibration tone...'});
330
- };
331
-
332
- playMLSwithIIR = async (stream, iir) => {
333
- this.invertedImpulseResponse = iir;
334
- // initialize the MLSGenInterface object with it's factory method
335
- await MlsGenInterface.factory(
336
- this.#mlsOrder,
337
- this.sinkSamplingRate,
338
- this.sourceSamplingRate
339
- ).then(mlsGenInterface => {
340
- this.#mlsGenInterface = mlsGenInterface;
341
- this.#mlsBufferView = this.#mlsGenInterface.getMLS();
342
- });
343
-
344
- // after intializating, start the calibration steps with garbage collection
345
- await this.#mlsGenInterface.withGarbageCollection([
346
- [
347
- this.calibrationSteps,
348
- [
349
- stream,
350
- this.#playCalibrationAudio, // play audio func (required)
351
- this.#createImpulseResponseFilterGraph, // before play func
352
- null, // before record
353
- this.#awaitDesiredMLSLength, // during record
354
- this.#afterMLSwIIRRecord, // after record
355
- ],
356
- ],
357
- ]);
358
- };
359
-
360
- /**
361
- * Public method to start the calibration process. Objects intialized from webassembly allocate new memory
362
- * and must be manually freed. This function is responsible for intializing the MlsGenInterface,
363
- * and wrapping the calibration steps with a garbage collection safe gaurd.
364
- * @public
365
- * @param {MediaStream} stream - The stream of audio from the Listener.
366
- */
367
- startCalibration = async stream => {
368
- // initialize the MLSGenInterface object with it's factory method
369
- await MlsGenInterface.factory(
370
- this.#mlsOrder,
371
- this.sinkSamplingRate,
372
- this.sourceSamplingRate
373
- ).then(mlsGenInterface => {
374
- this.#mlsGenInterface = mlsGenInterface;
375
- this.#mlsBufferView = this.#mlsGenInterface.getMLS();
376
- });
377
-
378
- // after intializating, start the calibration steps with garbage collection
379
- await this.#mlsGenInterface.withGarbageCollection([
380
- [
381
- this.calibrationSteps,
382
- [
383
- stream,
384
- this.#playCalibrationAudio, // play audio func (required)
385
- this.#setCalibrationNodesFromBuffer, // before play func
386
- null, // before record
387
- this.#awaitDesiredMLSLength, // during record
388
- this.#afterMLSRecord, // after record
389
- ],
390
- ],
391
- ]);
392
-
393
- // await the server response
394
- await this.sendImpulseResponsesToServerForProcessing();
395
- await this.playMLSwithIIR(stream, this.invertedImpulseResponse);
396
-
397
- return this.invertedImpulseResponse;
398
- };
399
- }
400
-
401
- export default ImpulseResponse;
402
- </code></pre>
403
- </article>
404
- </section>
405
- </div>
406
-
407
- <nav>
408
- <h2><a href="index.html">Home</a></h2>
409
- <h3>Classes</h3>
410
- <ul>
411
- <li><a href="AudioCalibrator.html">AudioCalibrator</a></li>
412
- <li><a href="AudioPeer.html">AudioPeer</a></li>
413
- <li><a href="AudioRecorder.html">AudioRecorder</a></li>
414
- <li><a href="ImpulseResponse.html">ImpulseResponse</a></li>
415
- <li><a href="Listener.html">Listener</a></li>
416
- <li><a href="MlsGenInterface.html">MlsGenInterface</a></li>
417
- <li><a href="MyEventEmitter.html">MyEventEmitter</a></li>
418
- <li><a href="PythonServerAPI.html">PythonServerAPI</a></li>
419
- <li><a href="Speaker.html">Speaker</a></li>
420
- <li><a href="Volume.html">Volume</a></li>
421
- </ul>
422
- <h3>Global</h3>
423
- <ul>
424
- <li><a href="global.html#csvToArray">csvToArray</a></li>
425
- <li><a href="global.html#saveToCSV">saveToCSV</a></li>
426
- <li><a href="global.html#sleep">sleep</a></li>
427
- </ul>
428
- </nav>
429
-
430
- <br class="clear" />
431
-
432
- <footer>
433
- Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.10</a> on Fri
434
- Jul 29 2022 15:09:48 GMT-0400 (Eastern Daylight Time)
435
- </footer>
436
-
437
- <script>
438
- prettyPrint();
439
- </script>
440
- <script src="scripts/linenumber.js"></script>
441
- </body>
442
- </html>
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <title>JSDoc: Source: tasks/impulse-response/impulseResponse.js</title>
6
+
7
+ <script src="scripts/prettify/prettify.js"></script>
8
+ <script src="scripts/prettify/lang-css.js"></script>
9
+ <!--[if lt IE 9]>
10
+ <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
11
+ <![endif]-->
12
+ <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css" />
13
+ <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css" />
14
+ </head>
15
+
16
+ <body>
17
+ <div id="main">
18
+ <h1 class="page-title">Source: tasks/impulse-response/impulseResponse.js</h1>
19
+
20
+ <section>
21
+ <article>
22
+ <pre
23
+ class="prettyprint source linenums"
24
+ ><code>import AudioCalibrator from '../audioCalibrator';
25
+ import MlsGenInterface from './mlsGen/mlsGenInterface';
26
+
27
+ import {sleep, csvToArray} from '../../utils';
28
+
29
+ /**
30
+ *
31
+ */
32
+ class ImpulseResponse extends AudioCalibrator {
33
+ /**
34
+ * Default constructor. Creates an instance with any number of paramters passed or the default parameters defined here.
35
+ * @param {*} param0
36
+ */
37
+ constructor({download = false, mlsOrder = 18, numCaptures = 5, numMLSPerCapture = 4}) {
38
+ super(numCaptures, numMLSPerCapture);
39
+ this.#mlsOrder = parseInt(mlsOrder, 10);
40
+ this.#P = 2 ** mlsOrder - 1;
41
+ this.#download = download;
42
+ }
43
+
44
+ /** @private */
45
+ #download;
46
+
47
+ /** @private */
48
+ #mlsGenInterface;
49
+
50
+ /** @private */
51
+ #mlsBufferView;
52
+
53
+ /** @private */
54
+ invertedImpulseResponse = null;
55
+
56
+ /** @private */
57
+ impulseResponses = [];
58
+
59
+ /** @private */
60
+ #mlsOrder;
61
+
62
+ #P;
63
+
64
+ /** @private */
65
+ TAPER_SECS = 5;
66
+
67
+ /** @private */
68
+ offsetGainNode;
69
+
70
+ /**
71
+ * Sends all the computed impulse responses to the backend server for processing
72
+ * @returns sets the resulting inverted impulse response to the class property
73
+ */
74
+ sendImpulseResponsesToServerForProcessing = async () => {
75
+ const computedIRs = await Promise.all(this.impulseResponses);
76
+ this.emit('update', {message: `computing the IIR...`});
77
+ return this.pyServerAPI
78
+ .getInverseImpulseResponse({
79
+ payload: computedIRs,
80
+ })
81
+ .then(res => {
82
+ this.emit('update', {message: `done computing the IIR...`});
83
+ this.invertedImpulseResponse = res;
84
+ })
85
+ .catch(err => {
86
+ // this.emit('InvertedImpulseResponse', {res: false});
87
+ console.error(err);
88
+ });
89
+ };
90
+
91
+ /**
92
+ * Sends the recorded signal, or a given csv string of a signal, to the back end server for processing
93
+ * @param {&lt;array>String} signalCsv - Optional csv string of a previously recorded signal, if given, this signal will be processed
94
+ */
95
+ sendRecordingToServerForProcessing = signalCsv => {
96
+ const allSignals = this.getAllRecordedSignals();
97
+ const numSignals = allSignals.length;
98
+ const payload =
99
+ signalCsv &amp;&amp; signalCsv.length > 0 ? csvToArray(signalCsv) : allSignals[numSignals - 1];
100
+
101
+ this.emit('update', {message: `computing the IR of the last recording...`});
102
+ this.impulseResponses.push(
103
+ this.pyServerAPI
104
+ .getImpulseResponse({
105
+ sampleRate: this.sourceSamplingRate || 96000,
106
+ payload,
107
+ P: this.#P,
108
+ })
109
+ .then(res => {
110
+ if (this.numSuccessfulCaptured &lt; this.numCaptures) {
111
+ this.numSuccessfulCaptured += 1;
112
+ this.emit('update', {
113
+ message: `${this.numSuccessfulCaptured}/${this.numCaptures} IRs computed...`,
114
+ });
115
+ }
116
+ return res;
117
+ })
118
+ .catch(err => {
119
+ console.error(err);
120
+ })
121
+ );
122
+ };
123
+
124
+ /**
125
+ * Passed to the calibration steps function, awaits the desired amount of seconds to capture the desired number
126
+ * of MLS periods defined in the constructor
127
+ */
128
+ #awaitDesiredMLSLength = async () => {
129
+ // seconds per MLS = P / SR
130
+ // await N * P / SR
131
+ this.emit('update', {
132
+ message: `sampling the calibration signal...`,
133
+ });
134
+ await sleep((this.#P / this.sourceSamplingRate) * this.numMLSPerCapture);
135
+ };
136
+
137
+ /**
138
+ * Passed to the calibration steps function, awaits the onset of the signal to ensure a steady state
139
+ */
140
+ #awaitSignalOnset = async () => {
141
+ this.emit('update', {
142
+ message: `waiting for the signal to stabalize...`,
143
+ });
144
+ await sleep(this.TAPER_SECS);
145
+ };
146
+
147
+ /**
148
+ * Called immediately after a recording is captured. Used to process the resulting signal
149
+ * whether by sending the result to a server or by computing a result locally
150
+ */
151
+ #afterMLSRecord = () => {
152
+ if (this.#download) {
153
+ this.downloadData();
154
+ }
155
+ this.#stopCalibrationAudio();
156
+ this.sendRecordingToServerForProcessing();
157
+ };
158
+
159
+ #afterMLSwIIRRecord = () => {
160
+ if (this.#download) {
161
+ this.downloadData();
162
+ }
163
+ this.#stopCalibrationAudio();
164
+ };
165
+
166
+ /**
167
+ * Created an S Curver Buffer to taper the signal onset
168
+ * @param {*} length
169
+ * @param {*} phase
170
+ * @returns
171
+ */
172
+ static createSCurveBuffer = (length, phase) => {
173
+ const curve = new Float32Array(length);
174
+ let i;
175
+ for (i = 0; i &lt; length; i += 1) {
176
+ // scale the curve to be between 0-1
177
+ curve[i] = Math.sin((Math.PI * i) / length - phase) / 2 + 0.5;
178
+ }
179
+ return curve;
180
+ };
181
+
182
+ static createInverseSCurveBuffer = (length, phase) => {
183
+ const curve = new Float32Array(length);
184
+ let i;
185
+ let j = length - 1;
186
+ for (i = 0; i &lt; length; i += 1) {
187
+ // scale the curve to be between 0-1
188
+ curve[i] = Math.sin((Math.PI * j) / length - phase) / 2 + 0.5;
189
+ j -= 1;
190
+ }
191
+ return curve;
192
+ };
193
+
194
+ /**
195
+ * Construct a Calibration Node with the calibration parameters.
196
+ * @private
197
+ */
198
+ #createPureTonenNode = CALIBRATION_TONE_FREQUENCY => {
199
+ const audioContext = this.makeNewSourceAudioContext();
200
+ const oscilator = audioContext.createOscillator();
201
+ const gainNode = audioContext.createGain();
202
+
203
+ oscilator.frequency.value = CALIBRATION_TONE_FREQUENCY;
204
+ oscilator.type = 'sine';
205
+ gainNode.gain.value = 0.04;
206
+
207
+ oscilator.connect(gainNode);
208
+ gainNode.connect(audioContext.destination);
209
+
210
+ this.addCalibrationNode(oscilator);
211
+ };
212
+
213
+ /**
214
+ * Construct a Calibration Node with the calibration parameters.
215
+ * @private
216
+ */
217
+ #createCalibrationNodeFromBuffer = dataBuffer => {
218
+ const audioContext = this.makeNewSourceAudioContext();
219
+ const buffer = audioContext.createBuffer(
220
+ 1, // number of channels
221
+ dataBuffer.length,
222
+ audioContext.sampleRate // sample rate
223
+ );
224
+
225
+ const data = buffer.getChannelData(0); // get data
226
+ // fill the buffer with our data
227
+ try {
228
+ for (let i = 0; i &lt; dataBuffer.length; i += 1) {
229
+ data[i] = dataBuffer[i];
230
+ }
231
+ } catch (error) {
232
+ console.error(error);
233
+ }
234
+
235
+ const source = audioContext.createBufferSource();
236
+
237
+ source.buffer = buffer;
238
+ source.loop = true;
239
+ source.connect(audioContext.destination);
240
+
241
+ this.addCalibrationNode(source);
242
+ };
243
+
244
+ /**
245
+ * Given a data buffer, creates the required calibration node
246
+ * @param {*} dataBufferArray
247
+ */
248
+ #setCalibrationNodesFromBuffer = (dataBufferArray = [this.#mlsBufferView]) => {
249
+ if (dataBufferArray.length === 1) {
250
+ this.#createCalibrationNodeFromBuffer(dataBufferArray[0]);
251
+ } else {
252
+ throw new Error('The length of the data buffer array must be 1');
253
+ }
254
+ };
255
+
256
+ #createImpulseResponseFilterGraph = (calibrationSignal, iir) => {
257
+ const audioCtx = this.makeNewSourceAudioContext();
258
+
259
+ // -------------------------------------------------------- IIR
260
+ const iirBuffer = audioCtx.createBuffer(
261
+ 1,
262
+ // TODO: quality check this
263
+ iir.length - 1,
264
+ audioCtx.sampleRate
265
+ );
266
+
267
+ // Fill the buffer with the inverted impulse response
268
+ const iirChannelZeroBuffer = iirBuffer.getChannelData(0);
269
+ for (let i = 0; i &lt; iirBuffer.length; i++) {
270
+ // audio needs to be in [-1.0; 1.0]
271
+ iirChannelZeroBuffer[i] = iir[i];
272
+ }
273
+
274
+ const convolverNode = audioCtx.createConvolver();
275
+
276
+ convolverNode.normalize = false;
277
+ convolverNode.channelCount = 1;
278
+ convolverNode.buffer = iirBuffer;
279
+
280
+ // ------------------------------------------------------ MLS
281
+ const calibrationSignalBuffer = audioCtx.createBuffer(
282
+ 1, // number of channels
283
+ calibrationSignal.length,
284
+ audioCtx.sampleRate // sample rate
285
+ );
286
+
287
+ const mlsChannelZeroBuffer = calibrationSignalBuffer.getChannelData(0); // get data
288
+ // fill the buffer with our data
289
+ try {
290
+ for (let i = 0; i &lt; calibrationSignal.length; i += 1) {
291
+ mlsChannelZeroBuffer[i] = calibrationSignal[i];
292
+ }
293
+ } catch (error) {
294
+ console.error(error);
295
+ }
296
+
297
+ const sourceNode = audioCtx.createBufferSource();
298
+
299
+ sourceNode.buffer = calibrationSignalBuffer;
300
+ sourceNode.loop = true;
301
+ sourceNode.connect(convolverNode);
302
+
303
+ convolverNode.connect(audioCtx.destination);
304
+
305
+ console.log({convolverNode, sourceNode});
306
+
307
+ this.addCalibrationNode(sourceNode);
308
+ };
309
+
310
+ #createIIRwMLSGraph = () => {
311
+ this.#createImpulseResponseFilterGraph(this.impulseResponses, [this.#mlsBufferView][0]);
312
+ };
313
+
314
+ /**
315
+ * Creates an audio context and plays it for a few seconds.
316
+ * @private
317
+ * @returns {Promise} - Resolves when the audio is done playing.
318
+ */
319
+ #playCalibrationAudio = () => {
320
+ this.calibrationNodes[0].start(0);
321
+ this.emit('update', {message: 'playing the calibration tone...'});
322
+ };
323
+
324
+ /**
325
+ * Stops the audio with tapered offset
326
+ */
327
+ #stopCalibrationAudio = () => {
328
+ this.calibrationNodes[0].stop();
329
+ this.emit('update', {message: 'stopping the calibration tone...'});
330
+ };
331
+
332
+ playMLSwithIIR = async (stream, iir) => {
333
+ this.invertedImpulseResponse = iir;
334
+ // initialize the MLSGenInterface object with it's factory method
335
+ await MlsGenInterface.factory(
336
+ this.#mlsOrder,
337
+ this.sinkSamplingRate,
338
+ this.sourceSamplingRate
339
+ ).then(mlsGenInterface => {
340
+ this.#mlsGenInterface = mlsGenInterface;
341
+ this.#mlsBufferView = this.#mlsGenInterface.getMLS();
342
+ });
343
+
344
+ // after intializating, start the calibration steps with garbage collection
345
+ await this.#mlsGenInterface.withGarbageCollection([
346
+ [
347
+ this.calibrationSteps,
348
+ [
349
+ stream,
350
+ this.#playCalibrationAudio, // play audio func (required)
351
+ this.#createImpulseResponseFilterGraph, // before play func
352
+ null, // before record
353
+ this.#awaitDesiredMLSLength, // during record
354
+ this.#afterMLSwIIRRecord, // after record
355
+ ],
356
+ ],
357
+ ]);
358
+ };
359
+
360
+ /**
361
+ * Public method to start the calibration process. Objects intialized from webassembly allocate new memory
362
+ * and must be manually freed. This function is responsible for intializing the MlsGenInterface,
363
+ * and wrapping the calibration steps with a garbage collection safe gaurd.
364
+ * @public
365
+ * @param {MediaStream} stream - The stream of audio from the Listener.
366
+ */
367
+ startCalibration = async stream => {
368
+ // initialize the MLSGenInterface object with it's factory method
369
+ await MlsGenInterface.factory(
370
+ this.#mlsOrder,
371
+ this.sinkSamplingRate,
372
+ this.sourceSamplingRate
373
+ ).then(mlsGenInterface => {
374
+ this.#mlsGenInterface = mlsGenInterface;
375
+ this.#mlsBufferView = this.#mlsGenInterface.getMLS();
376
+ });
377
+
378
+ // after intializating, start the calibration steps with garbage collection
379
+ await this.#mlsGenInterface.withGarbageCollection([
380
+ [
381
+ this.calibrationSteps,
382
+ [
383
+ stream,
384
+ this.#playCalibrationAudio, // play audio func (required)
385
+ this.#setCalibrationNodesFromBuffer, // before play func
386
+ null, // before record
387
+ this.#awaitDesiredMLSLength, // during record
388
+ this.#afterMLSRecord, // after record
389
+ ],
390
+ ],
391
+ ]);
392
+
393
+ // await the server response
394
+ await this.sendImpulseResponsesToServerForProcessing();
395
+ await this.playMLSwithIIR(stream, this.invertedImpulseResponse);
396
+
397
+ return this.invertedImpulseResponse;
398
+ };
399
+ }
400
+
401
+ export default ImpulseResponse;
402
+ </code></pre>
403
+ </article>
404
+ </section>
405
+ </div>
406
+
407
+ <nav>
408
+ <h2><a href="index.html">Home</a></h2>
409
+ <h3>Classes</h3>
410
+ <ul>
411
+ <li><a href="AudioCalibrator.html">AudioCalibrator</a></li>
412
+ <li><a href="AudioPeer.html">AudioPeer</a></li>
413
+ <li><a href="AudioRecorder.html">AudioRecorder</a></li>
414
+ <li><a href="ImpulseResponse.html">ImpulseResponse</a></li>
415
+ <li><a href="Listener.html">Listener</a></li>
416
+ <li><a href="MlsGenInterface.html">MlsGenInterface</a></li>
417
+ <li><a href="MyEventEmitter.html">MyEventEmitter</a></li>
418
+ <li><a href="PythonServerAPI.html">PythonServerAPI</a></li>
419
+ <li><a href="Speaker.html">Speaker</a></li>
420
+ <li><a href="Volume.html">Volume</a></li>
421
+ </ul>
422
+ <h3>Global</h3>
423
+ <ul>
424
+ <li><a href="global.html#csvToArray">csvToArray</a></li>
425
+ <li><a href="global.html#saveToCSV">saveToCSV</a></li>
426
+ <li><a href="global.html#sleep">sleep</a></li>
427
+ </ul>
428
+ </nav>
429
+
430
+ <br class="clear" />
431
+
432
+ <footer>
433
+ Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.10</a> on Fri
434
+ Jul 29 2022 15:09:48 GMT-0400 (Eastern Daylight Time)
435
+ </footer>
436
+
437
+ <script>
438
+ prettyPrint();
439
+ </script>
440
+ <script src="scripts/linenumber.js"></script>
441
+ </body>
442
+ </html>