speaker-calibration 2.2.242 → 2.2.244

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.
@@ -1,359 +1,413 @@
1
- /* eslint-disable no-await-in-loop */
2
- import AudioRecorder from './audioRecorder';
3
- import PythonServerAPI from '../server/PythonServerAPI';
4
- import {sleep, saveToCSV} from '../utils';
5
-
6
- /**
7
- * .
8
- * .
9
- * .
10
- * Provides methods for calibrating the user's speakers
11
- *
12
- * @extends AudioRecorder
13
- */
14
- class AudioCalibrator extends AudioRecorder {
15
- /**
16
- *
17
- * @param numCaptures
18
- * @param numMLSPerCapture
19
- * @example
20
- */
21
- constructor(numCaptures = 1, numMLSPerCapture = 1) {
22
- super();
23
- this.numCaptures = numCaptures;
24
- this.numMLSPerCapture = numMLSPerCapture;
25
- this.pyServerAPI = new PythonServerAPI();
26
- this.currentTime = 0;
27
- }
28
-
29
- /** @private */
30
- isCalibrating = false;
31
-
32
- /** @private */
33
- sourceAudioContext;
34
-
35
- /** @private */
36
- sourceAudioContextConvolved;
37
-
38
- /** @protected */
39
- numCalibratingRounds = 1;
40
-
41
- /** @protected */
42
- numSuccessfulCaptured = 0;
43
-
44
- /** @private */
45
- sourceSamplingRate;
46
-
47
- /** @protected */
48
- calibrationNodes = [];
49
-
50
- /** @protected */
51
- calibrationNodesConvolved = [];
52
-
53
- /** @protected */
54
- localAudio;
55
-
56
- /** @private */
57
- startTime;
58
-
59
- numCalibratingRoundsCompleted = 0;
60
- /**
61
- * Called when a call is received.
62
- * Creates a local audio DOM element and attaches it to the page.
63
- *
64
- * @param targetElement
65
- * @example
66
- */
67
- createLocalAudio = targetElement => {
68
- this.localAudio = document.createElement('audio');
69
- this.localAudio.setAttribute('id', 'localAudio');
70
- targetElement.appendChild(this.localAudio);
71
- };
72
-
73
- addTimeStamp = taskName => {
74
- const currentTime = new Date().getTime(); // Current time in ms
75
- const elapsedTime = (currentTime - this.startTime) / 1000; // Convert to seconds
76
- const stepDuration = elapsedTime - this.currentTime;
77
-
78
- this.currentTime = elapsedTime; // Update for next step
79
-
80
- // Format numbers to 1 decimal place without padding
81
- const elapsedStr = elapsedTime.toFixed(1);
82
- const stepStr = stepDuration.toFixed(1);
83
-
84
- // Push timestamp string (without padding)
85
- this.timeStamp.push(`${elapsedStr} s. ∆ ${stepStr} s. ${taskName}`);
86
- };
87
-
88
- recordBackground = async (
89
- stream,
90
- loopCondition = () => false,
91
- duringRecord = async () => {},
92
- afterRecord = async () => {},
93
- mode,
94
- checkRec
95
- ) => {
96
- console.warn('before recording background noise');
97
- // calibration loop
98
- while (loopCondition()) {
99
- // start recording
100
- console.warn('startRecording');
101
- await this.startRecording(stream);
102
-
103
- // do something during the recording such as sleep n amount of time
104
- console.warn('duringRecord');
105
- await duringRecord();
106
-
107
- // when done, stop recording
108
- console.warn('stopRecording');
109
- await this.stopRecording(mode, checkRec);
110
-
111
- // do something after recording such as start processing values
112
- console.warn('afterRecord');
113
- await afterRecord();
114
-
115
- // eslint-disable-next-line no-await-in-loop
116
- await sleep(1);
117
- }
118
- };
119
-
120
- /**
121
- *
122
- * @param {MediaStream} stream
123
- * @param {Function} playCalibrationAudio - (async) function that plays the calibration audio
124
- * @param {*} beforePlay - (async) function that is called before playing the audio
125
- * @param {*} beforeRecord - (async) function that is called before recording
126
- * @param {*} duringRecord - (async) function that is called while recording
127
- * @param {*} afterRecord - (async) function that is called after recording
128
- * @example
129
- */
130
- calibrationSteps = async (
131
- stream,
132
- playCalibrationAudio,
133
- beforePlay = async () => {},
134
- beforeRecord = async () => {},
135
- loopCondition = () => false,
136
- duringRecord = async () => {},
137
- afterRecord = async () => {},
138
- mode,
139
- checkRec
140
- ) => {
141
- // if it finished 2 attempts, it move to next iteration so reset numSuccessfulCaptured
142
- if (this.numSuccessfulCaptured >= 2) {
143
- this.numSuccessfulCaptured = 0;
144
- }
145
-
146
- // do something before playing such as using the MLS to fill the buffers
147
- console.warn('beforePlay');
148
- await beforePlay();
149
-
150
- // play calibration audio
151
- console.warn('playCalibrationAudio');
152
- playCalibrationAudio();
153
-
154
- // do something before recording such as awaiting a certain amount of time
155
- console.warn('beforeRecord');
156
- await beforeRecord();
157
- const totalSec =
158
- this._calibrateSoundBurstPreSec +
159
- (this.numMLSPerCapture - this.num_mls_to_skip) * this._calibrateSoundBurstSec +
160
- this._calibrateSoundBurstPostSec;
161
-
162
- // calibration loop
163
- while (loopCondition()) {
164
- if (this.isCalibrating) break;
165
- // start recording
166
- console.warn('startRecording');
167
- await this.startRecording(stream);
168
-
169
- if (this.isCalibrating) break;
170
- // do something during the recording such as sleep n amount of time
171
- console.warn('duringRecord');
172
- await duringRecord();
173
-
174
- if (this.isCalibrating) break;
175
- // when done, stop recording
176
- console.warn('stopRecording');
177
- await this.stopRecording(mode, checkRec);
178
-
179
- if (this.isCalibrating) break;
180
- // do something after recording such as start processing values
181
- console.warn('afterRecord');
182
- await afterRecord();
183
-
184
- // eslint-disable-next-line no-await-in-loop
185
- await sleep(1);
186
- }
187
- };
188
-
189
- /**
190
- *
191
- * @param {MediaStream} stream
192
- * @param {Function} playCalibrationAudio - (async) function that plays the calibration audio
193
- * @param {*} beforeRecord - (async) function that is called before recording
194
- * @param {*} afterRecord - (async) function that is called after recording
195
- * @param {Number} gainValue - the gain value to set the gain node to
196
- */
197
- volumeCalibrationSteps = async (
198
- stream,
199
- playCalibrationAudio,
200
- beforeRecord = () => {},
201
- afterRecord = () => {},
202
- gainValue,
203
- lCalib = 104.92978421490648,
204
- checkRec,
205
- checkSD,
206
- maxSD,
207
- maxRetry
208
- ) => {
209
- this.numCalibratingRoundsCompleted = 0;
210
- console.log('maxSD in VolumeCaibrationSteps: ', maxSD, '0' >= maxSD);
211
- // calibration loop
212
- while (!this.isCalibrating && this.numCalibratingRoundsCompleted < maxRetry) {
213
- if (this.isCalibrating) break;
214
- // before recording
215
- await beforeRecord(gainValue);
216
- if (this.isCalibrating) break;
217
- // start recording
218
- await this.startRecording(stream);
219
- if (this.isCalibrating) break;
220
- // play calibration audio
221
- console.log(`Calibration Round ${this.numCalibratingRoundsCompleted}`);
222
- await playCalibrationAudio();
223
- if (this.isCalibrating) break;
224
- // when done, stop recording
225
- console.log('Calibration Round Complete');
226
- await this.stopRecording('volume', checkRec);
227
- if (this.isCalibrating) break;
228
- // after recording
229
- await afterRecord(lCalib);
230
- const sd = await checkSD();
231
- let sdMessage;
232
- if (sd <= maxSD) {
233
- console.log(`SD =${sd}, less than calibrateSound1000HzMaxSD_dB=${maxSD}`);
234
- this.numCalibratingRoundsCompleted += maxRetry;
235
- sdMessage = `. SD = ${sd} dB`;
236
- } else {
237
- // if exist the maxSD do it one more time and only one more time
238
- console.log(`SD =${sd}, greater than calibrateSound1000HzMaxSD_dB=${maxSD}`);
239
- this.numCalibratingRoundsCompleted += 1;
240
- sdMessage = `. SD = ${sd} > ${this.calibrateSound1000HzMaxSD_dB} dB.`;
241
- }
242
- this.addTimeStamp(
243
- `${this.calibrateSound1000HzPreSec.toFixed(1)}` +
244
- `+${this.calibrateSound1000HzSec.toFixed(1)}` +
245
- `+${this.calibrateSound1000HzPostSec.toFixed(1)} s. ` +
246
- `1000 Hz at ${this.inDB} dB${sdMessage}`
247
- );
248
- this.calibrationNodes = [];
249
-
250
- // eslint-disable-next-line no-await-in-loop
251
- await sleep(2);
252
- }
253
- };
254
-
255
- /**
256
- * Getter for the isCalibrating property.
257
- *
258
- * @public
259
- * @returns - True if the audio is being calibrated, false otherwise.
260
- * @example
261
- */
262
- getCalibrationStatus = () => this.isCalibrating;
263
-
264
- /** .
265
- * .
266
- * .
267
- * Set the sampling rate to the value received from the listener
268
- *
269
- * @param {*} sinkSamplingRate
270
- * @param samplingRate
271
- * @example
272
- */
273
- setSamplingRates = samplingRate => {
274
- this.sinkSamplingRate = samplingRate;
275
- this.sourceSamplingRate = samplingRate;
276
-
277
- // this.emit('update', {message: `sampling at ${samplingRate}Hz...`});
278
- };
279
-
280
- setSampleSize = sampleSize => {
281
- this.sampleSize = sampleSize;
282
- };
283
-
284
- setFlags = flags => {
285
- this.flags = flags;
286
- };
287
-
288
- sampleRatesSet = () => this.sourceSamplingRate && this.sinkSamplingRate;
289
-
290
- addCalibrationNode = node => {
291
- this.calibrationNodes.push(node);
292
- };
293
-
294
- addCalibrationNodeConvolved = node => {
295
- this.calibrationNodesConvolved.push(node);
296
- };
297
-
298
- makeNewSourceAudioContext = () => {
299
- const options = {
300
- sampleRate: this.sourceSamplingRate,
301
- };
302
-
303
- this.sourceAudioContext = new (window.AudioContext ||
304
- window.webkitAudioContext ||
305
- window.audioContext)(options);
306
-
307
- return this.sourceAudioContext;
308
- };
309
-
310
- makeNewSourceAudioContextConvolved = () => {
311
- const options = {
312
- sampleRate: this.sourceSamplingRate,
313
- };
314
-
315
- this.sourceAudioContextConvolved = new (window.AudioContext ||
316
- window.webkitAudioContext ||
317
- window.audioContext)(options);
318
-
319
- return this.sourceAudioContextConvolved;
320
- };
321
-
322
- /** .
323
- * .
324
- * .
325
- * Download the result of the calibration roudns
326
- *
327
- * @example
328
- */
329
- downloadData = () => {
330
- const recordings = this.getAllRecordedSignals();
331
- const i = recordings.length - 1;
332
- saveToCSV(recordings[i], `recordedMLSignal_${i}_unconvolved.csv`);
333
- };
334
- downloadSingleUnfilteredRecording = () => {
335
- const recordings = this.getAllUnfilteredRecordedSignals();
336
- saveToCSV(recordings[recordings.length - 1], `recordedMLSignal_unconvolved.csv`);
337
- };
338
- downloadSingleFilteredRecording = () => {
339
- const recordings = this.getAllFilteredRecordedSignals();
340
- console.log('Single filtered recording should be of length: ' + recordings[0].length);
341
- saveToCSV(recordings[0], `recordedMLSignal_convolved.csv`);
342
- };
343
- downloadUnfilteredRecordings = () => {
344
- const recordings = this.getAllRecordedSignals();
345
- console.log('unfilterd download?');
346
- for (let i = 0; i < recordings.length; i++) {
347
- console.log(i);
348
- saveToCSV(recordings[i], `recordedMLSignal_${i}_unconvolved.csv`);
349
- }
350
- };
351
- downloadFilteredRecordings = () => {
352
- const recordings = this.getAllFilteredRecordedSignals();
353
- for (let i = 0; i < recordings.length; i++) {
354
- saveToCSV(recordings[i], `recordedMLSignal_${i}_convolved.csv`);
355
- }
356
- };
357
- }
358
-
359
- export default AudioCalibrator;
1
+ /* eslint-disable no-await-in-loop */
2
+ import AudioRecorder from './audioRecorder';
3
+ import PythonServerAPI from '../server/PythonServerAPI';
4
+ import {sleep, saveToCSV} from '../utils';
5
+
6
+ /**
7
+ * .
8
+ * .
9
+ * .
10
+ * Provides methods for calibrating the user's speakers
11
+ *
12
+ * @extends AudioRecorder
13
+ */
14
+ class AudioCalibrator extends AudioRecorder {
15
+ /**
16
+ *
17
+ * @param numCaptures
18
+ * @param numMLSPerCapture
19
+ * @example
20
+ */
21
+ constructor(numCaptures = 1, numMLSPerCapture = 1) {
22
+ super();
23
+ this.numCaptures = numCaptures;
24
+ this.numMLSPerCapture = numMLSPerCapture;
25
+ this.pyServerAPI = new PythonServerAPI();
26
+ this.currentTime = 0;
27
+ }
28
+
29
+ /** @private */
30
+ isCalibrating = false;
31
+
32
+ /** @private */
33
+ sourceAudioContext;
34
+
35
+ /** @private */
36
+ sourceAudioContextConvolved;
37
+
38
+ /** @protected */
39
+ numCalibratingRounds = 1;
40
+
41
+ /** @protected */
42
+ numSuccessfulCaptured = 0;
43
+
44
+ /** @private */
45
+ sourceSamplingRate;
46
+
47
+ /** @protected */
48
+ calibrationNodes = [];
49
+
50
+ /** @protected */
51
+ calibrationNodesConvolved = [];
52
+
53
+ /** @protected */
54
+ localAudio;
55
+
56
+ /** @private */
57
+ startTime;
58
+
59
+ numCalibratingRoundsCompleted = 0;
60
+ /**
61
+ * Called when a call is received.
62
+ * Creates a local audio DOM element and attaches it to the page.
63
+ *
64
+ * @param targetElement
65
+ * @example
66
+ */
67
+ createLocalAudio = targetElement => {
68
+ this.localAudio = document.createElement('audio');
69
+ this.localAudio.setAttribute('id', 'localAudio');
70
+ targetElement.appendChild(this.localAudio);
71
+ };
72
+
73
+ addTimeStamp = taskName => {
74
+ const currentTime = new Date().getTime(); // Current time in ms
75
+ const elapsedTime = (currentTime - this.startTime) / 1000; // Convert to seconds
76
+ const stepDuration = elapsedTime - this.currentTime;
77
+
78
+ this.currentTime = elapsedTime; // Update for next step
79
+
80
+ // Format numbers to 1 decimal place without padding
81
+ const elapsedStr = elapsedTime.toFixed(1);
82
+ const stepStr = stepDuration.toFixed(1);
83
+
84
+ // Push timestamp string (without padding)
85
+ this.timeStamp.push(`${elapsedStr} s. ∆ ${stepStr} s. ${taskName}`);
86
+ };
87
+
88
+ recordBackground = async (
89
+ stream,
90
+ loopCondition = () => false,
91
+ duringRecord = async () => {},
92
+ afterRecord = async () => {},
93
+ mode,
94
+ checkRec
95
+ ) => {
96
+ console.warn('before recording background noise');
97
+ // calibration loop
98
+ while (loopCondition()) {
99
+ // start recording
100
+ console.warn('startRecording');
101
+ await this.startRecording(stream);
102
+
103
+ // do something during the recording such as sleep n amount of time
104
+ console.warn('duringRecord');
105
+ await duringRecord();
106
+
107
+ // when done, stop recording
108
+ console.warn('stopRecording');
109
+ await this.stopRecording(mode, checkRec);
110
+
111
+ // do something after recording such as start processing values
112
+ console.warn('afterRecord');
113
+ await afterRecord();
114
+
115
+ // eslint-disable-next-line no-await-in-loop
116
+ await sleep(1);
117
+ }
118
+ };
119
+
120
+ /**
121
+ *
122
+ * @param {MediaStream} stream
123
+ * @param {Function} playCalibrationAudio - (async) function that plays the calibration audio
124
+ * @param {*} beforePlay - (async) function that is called before playing the audio
125
+ * @param {*} beforeRecord - (async) function that is called before recording
126
+ * @param {*} duringRecord - (async) function that is called while recording
127
+ * @param {*} afterRecord - (async) function that is called after recording
128
+ * @example
129
+ */
130
+ calibrationSteps = async (
131
+ stream,
132
+ playCalibrationAudio,
133
+ beforePlay = async () => {},
134
+ beforeRecord = async () => {},
135
+ loopCondition = () => false,
136
+ duringRecord = async () => {},
137
+ afterRecord = async () => {},
138
+ mode,
139
+ checkRec
140
+ ) => {
141
+ // if it finished 2 attempts, it move to next iteration so reset numSuccessfulCaptured
142
+ if (this.numSuccessfulCaptured >= 2) {
143
+ this.numSuccessfulCaptured = 0;
144
+ }
145
+
146
+ // do something before playing such as using the MLS to fill the buffers
147
+ console.warn('beforePlay');
148
+ await beforePlay();
149
+
150
+ // play calibration audio
151
+ console.warn('playCalibrationAudio');
152
+ playCalibrationAudio();
153
+
154
+ // do something before recording such as awaiting a certain amount of time
155
+ console.warn('beforeRecord');
156
+ await beforeRecord();
157
+ const totalSec =
158
+ this._calibrateSoundBurstPreSec +
159
+ (this.numMLSPerCapture - this.num_mls_to_skip) * this._calibrateSoundBurstSec +
160
+ this._calibrateSoundBurstPostSec;
161
+
162
+ // calibration loop
163
+ while (loopCondition()) {
164
+ if (this.isCalibrating) break;
165
+ // start recording
166
+ console.warn('startRecording');
167
+ await this.startRecording(stream);
168
+
169
+ if (this.isCalibrating) break;
170
+ // do something during the recording such as sleep n amount of time
171
+ console.warn('duringRecord');
172
+ await duringRecord();
173
+
174
+ if (this.isCalibrating) break;
175
+ // when done, stop recording
176
+ console.warn('stopRecording');
177
+ await this.stopRecording(mode, checkRec);
178
+
179
+ if (this.isCalibrating) break;
180
+ // do something after recording such as start processing values
181
+ console.warn('afterRecord');
182
+ await afterRecord();
183
+
184
+ // eslint-disable-next-line no-await-in-loop
185
+ await sleep(1);
186
+ }
187
+ };
188
+
189
+ /**
190
+ *
191
+ * @param {MediaStream} stream
192
+ * @param {Function} playCalibrationAudio - (async) function that plays the calibration audio
193
+ * @param {*} beforeRecord - (async) function that is called before recording
194
+ * @param {*} afterRecord - (async) function that is called after recording
195
+ * @param {Number} gainValue - the gain value to set the gain node to
196
+ */
197
+ volumeCalibrationSteps = async (
198
+ stream,
199
+ playCalibrationAudio,
200
+ beforeRecord = () => {},
201
+ afterRecord = () => {},
202
+ gainValue,
203
+ lCalib = 104.92978421490648,
204
+ checkRec,
205
+ checkSD,
206
+ maxSD,
207
+ maxRetry
208
+ ) => {
209
+ this.numCalibratingRoundsCompleted = 0;
210
+ console.log('maxSD in VolumeCaibrationSteps: ', maxSD, '0' >= maxSD);
211
+ // calibration loop
212
+ while (!this.isCalibrating && this.numCalibratingRoundsCompleted < maxRetry) {
213
+ if (this.isCalibrating) break;
214
+ // before recording
215
+ await beforeRecord(gainValue);
216
+ if (this.isCalibrating) break;
217
+ // start recording
218
+ await this.startRecording(stream);
219
+ if (this.isCalibrating) break;
220
+ // play calibration audio
221
+ console.log(`Calibration Round ${this.numCalibratingRoundsCompleted}`);
222
+ await playCalibrationAudio();
223
+ if (this.isCalibrating) break;
224
+ // when done, stop recording
225
+ console.log('Calibration Round Complete');
226
+ await this.stopRecording('volume', checkRec);
227
+ if (this.isCalibrating) break;
228
+ // after recording
229
+ await afterRecord(lCalib);
230
+ const sd = await checkSD();
231
+ let sdMessage;
232
+ if (sd <= maxSD) {
233
+ console.log(`SD =${sd}, less than calibrateSound1000HzMaxSD_dB=${maxSD}`);
234
+ this.numCalibratingRoundsCompleted += maxRetry;
235
+ sdMessage = `. SD = ${sd} dB`;
236
+ } else {
237
+ // if exist the maxSD do it one more time and only one more time
238
+ console.log(`SD =${sd}, greater than calibrateSound1000HzMaxSD_dB=${maxSD}`);
239
+ this.numCalibratingRoundsCompleted += 1;
240
+ sdMessage = `. SD = ${sd} > ${this.calibrateSound1000HzMaxSD_dB} dB.`;
241
+ }
242
+ this.addTimeStamp(
243
+ `${this.calibrateSound1000HzPreSec.toFixed(1)}` +
244
+ `+${this.calibrateSound1000HzSec.toFixed(1)}` +
245
+ `+${this.calibrateSound1000HzPostSec.toFixed(1)} s. ` +
246
+ `1000 Hz at ${this.inDB} dB${sdMessage}`
247
+ );
248
+ this.calibrationNodes = [];
249
+
250
+ // eslint-disable-next-line no-await-in-loop
251
+ await sleep(2);
252
+ }
253
+ };
254
+
255
+ /**
256
+ * Simulates recording by convolving input signal with loudspeaker and microphone impulse responses
257
+ *
258
+ * @param {Array<number>} inputSignal - The input signal to be convolved
259
+ * @param {Array<number>} loudspeakerImpulseResponse - The loudspeaker impulse response
260
+ * @param {Array<number>} microphoneImpulseResponse - The microphone impulse response
261
+ * @param {Function} afterRecord - Callback function to execute after recording simulation
262
+ * @param {number} lCalib - Calibration parameter
263
+ * @param {string} checkRec - Recording check type
264
+ */
265
+ simulatedVolumeCalibrationSteps = async (
266
+ inputSignal,
267
+ loudspeakerImpulseResponse,
268
+ microphoneImpulseResponse,
269
+ afterRecord = () => {},
270
+ lCalib,
271
+ checkRec
272
+ ) => {
273
+ // Convolve with loudspeaker and microphone impulse responses
274
+ const convolvedSignalWithMicrophone = await this.pyServerAPI
275
+ .irConvolution({
276
+ input_signal: inputSignal,
277
+ loudspeaker_ir: loudspeakerImpulseResponse,
278
+ microphone_ir: microphoneImpulseResponse,
279
+ })
280
+ .then(res => {
281
+ console.log('res in simulatedVolumeCalibrationSteps: ', res);
282
+ return res['output_signal'];
283
+ });
284
+
285
+ // Log details about the simulated recording
286
+ const uniqueSet = new Set(convolvedSignalWithMicrophone);
287
+ const numberOfUniqueValues = uniqueSet.size;
288
+ const squaredValues = convolvedSignalWithMicrophone.map(value => value * value);
289
+ const sum_of_squares = squaredValues.reduce((total, value) => total + value, 0);
290
+ const squared_mean = sum_of_squares / convolvedSignalWithMicrophone.length;
291
+ const dbLevel = 20 * Math.log10(Math.sqrt(squared_mean));
292
+ const roundedDbLevel = Math.round(dbLevel * 10) / 10;
293
+
294
+ console.log(
295
+ '[SIMULATION] 1000-Hz recording: ' +
296
+ roundedDbLevel +
297
+ ' dB with ' +
298
+ numberOfUniqueValues +
299
+ ' unique values.'
300
+ );
301
+
302
+ // Save the simulated recording as if it were captured from a microphone
303
+ this.saveVolumeRecording(convolvedSignalWithMicrophone);
304
+
305
+ // Process the simulated recording
306
+ await afterRecord(lCalib);
307
+ };
308
+
309
+ /**
310
+ * Getter for the isCalibrating property.
311
+ *
312
+ * @public
313
+ * @returns - True if the audio is being calibrated, false otherwise.
314
+ * @example
315
+ */
316
+ getCalibrationStatus = () => this.isCalibrating;
317
+
318
+ /** .
319
+ * .
320
+ * .
321
+ * Set the sampling rate to the value received from the listener
322
+ *
323
+ * @param {*} sinkSamplingRate
324
+ * @param samplingRate
325
+ * @example
326
+ */
327
+ setSamplingRates = samplingRate => {
328
+ this.sinkSamplingRate = samplingRate;
329
+ this.sourceSamplingRate = samplingRate;
330
+
331
+ // this.emit('update', {message: `sampling at ${samplingRate}Hz...`});
332
+ };
333
+
334
+ setSampleSize = sampleSize => {
335
+ this.sampleSize = sampleSize;
336
+ };
337
+
338
+ setFlags = flags => {
339
+ this.flags = flags;
340
+ };
341
+
342
+ sampleRatesSet = () => this.sourceSamplingRate && this.sinkSamplingRate;
343
+
344
+ addCalibrationNode = node => {
345
+ this.calibrationNodes.push(node);
346
+ };
347
+
348
+ addCalibrationNodeConvolved = node => {
349
+ this.calibrationNodesConvolved.push(node);
350
+ };
351
+
352
+ makeNewSourceAudioContext = () => {
353
+ const options = {
354
+ sampleRate: this.sourceSamplingRate,
355
+ };
356
+
357
+ this.sourceAudioContext = new (window.AudioContext ||
358
+ window.webkitAudioContext ||
359
+ window.audioContext)(options);
360
+
361
+ return this.sourceAudioContext;
362
+ };
363
+
364
+ makeNewSourceAudioContextConvolved = () => {
365
+ const options = {
366
+ sampleRate: this.sourceSamplingRate,
367
+ };
368
+
369
+ this.sourceAudioContextConvolved = new (window.AudioContext ||
370
+ window.webkitAudioContext ||
371
+ window.audioContext)(options);
372
+
373
+ return this.sourceAudioContextConvolved;
374
+ };
375
+
376
+ /** .
377
+ * .
378
+ * .
379
+ * Download the result of the calibration roudns
380
+ *
381
+ * @example
382
+ */
383
+ downloadData = () => {
384
+ const recordings = this.getAllRecordedSignals();
385
+ const i = recordings.length - 1;
386
+ saveToCSV(recordings[i], `recordedMLSignal_${i}_unconvolved.csv`);
387
+ };
388
+ downloadSingleUnfilteredRecording = () => {
389
+ const recordings = this.getAllUnfilteredRecordedSignals();
390
+ saveToCSV(recordings[recordings.length - 1], `recordedMLSignal_unconvolved.csv`);
391
+ };
392
+ downloadSingleFilteredRecording = () => {
393
+ const recordings = this.getAllFilteredRecordedSignals();
394
+ console.log('Single filtered recording should be of length: ' + recordings[0].length);
395
+ saveToCSV(recordings[0], `recordedMLSignal_convolved.csv`);
396
+ };
397
+ downloadUnfilteredRecordings = () => {
398
+ const recordings = this.getAllRecordedSignals();
399
+ console.log('unfilterd download?');
400
+ for (let i = 0; i < recordings.length; i++) {
401
+ console.log(i);
402
+ saveToCSV(recordings[i], `recordedMLSignal_${i}_unconvolved.csv`);
403
+ }
404
+ };
405
+ downloadFilteredRecordings = () => {
406
+ const recordings = this.getAllFilteredRecordedSignals();
407
+ for (let i = 0; i < recordings.length; i++) {
408
+ saveToCSV(recordings[i], `recordedMLSignal_${i}_convolved.csv`);
409
+ }
410
+ };
411
+ }
412
+
413
+ export default AudioCalibrator;