speaker-calibration 2.1.1 → 2.1.3
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/dist/main.js +1 -1
- package/package.json +1 -1
- package/src/tasks/volume/volume.js +50 -6
package/dist/main.js
CHANGED
|
@@ -818,7 +818,7 @@ eval("__webpack_require__.r(__webpack_exports__);\n/* eslint-disable prefer-dest
|
|
|
818
818
|
/***/ (function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
|
|
819
819
|
|
|
820
820
|
"use strict";
|
|
821
|
-
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _audioCalibrator__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../audioCalibrator */ \"./src/tasks/audioCalibrator.js\");\n/* harmony import */ var axios__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! axios */ \"./node_modules/axios/index.js\");\n/* harmony import */ var axios__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(axios__WEBPACK_IMPORTED_MODULE_1__);\n/* harmony import */ var _utils__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../utils */ \"./src/utils.js\");\n\r\n\r\n\r\n\r\n/**\r\n *\r\n */\r\nclass Volume extends _audioCalibrator__WEBPACK_IMPORTED_MODULE_0__[\"default\"] {\r\n /**\r\n *\r\n * @param root0\r\n * @param root0.download\r\n * @param root0.numCalibrationRounds\r\n * @param root0.numCalibrationNodes\r\n * @example\r\n */\r\n\r\n /** @private */\r\n #CALIBRATION_TONE_FREQUENCY = 1000; // Hz\r\n\r\n /** @private */\r\n #CALIBRATION_TONE_TYPE = 'sine';\r\n\r\n /** @private */\r\n #CALIBRATION_TONE_DURATION = 5; // seconds\r\n\r\n /** @private */\r\n soundGainDBSPL = null;\r\n outDBSPL = null;\r\n\r\n handleIncomingData = data => {\r\n console.log('Received data: ', data);\r\n if (data.type === 'soundGainDBSPL') {\r\n this.soundGainDBSPL = data.value;\r\n } else {\r\n throw new Error(`Unknown data type: ${data.type}`);\r\n }\r\n };\r\n\r\n #getTruncatedSignal = (left = 3.5, right = 4.5) => {\r\n const start = Math.floor(left * this.sourceSamplingRate);\r\n const end = Math.floor(right * this.sourceSamplingRate);\r\n const result = Array.from(this.getLastRecordedSignal().slice(start, end));\r\n\r\n /**\r\n * function to check that capture was properly made\r\n * @param {*} list\r\n */\r\n const checkResult = list => {\r\n const setItem = new Set(list);\r\n if (setItem.size === 1 && setItem.has(0)) {\r\n console.warn(\r\n 'The last capture failed, all recorded signal is zero',\r\n this.getAllRecordedSignals()\r\n );\r\n }\r\n if (setItem.size === 0) {\r\n console.warn('The last capture failed, no recorded signal');\r\n }\r\n };\r\n checkResult(result);\r\n return result;\r\n };\r\n\r\n /** \r\n * \r\n * \r\n Construct a calibration Node with the calibration parameters and given gain value\r\n * @param {*} gainValue\r\n * */\r\n #createCalibrationToneWithGainValue = gainValue => {\r\n const audioContext = this.makeNewSourceAudioContext();\r\n const oscilator = audioContext.createOscillator();\r\n const gainNode = audioContext.createGain();\r\n\r\n oscilator.frequency.value = this.#CALIBRATION_TONE_FREQUENCY;\r\n oscilator.type = this.#CALIBRATION_TONE_TYPE;\r\n gainNode.gain.value = gainValue;\r\n\r\n oscilator.connect(gainNode);\r\n gainNode.connect(audioContext.destination);\r\n\r\n this.addCalibrationNode(oscilator);\r\n };\r\n\r\n /**\r\n * Construct a Calibration Node with the calibration parameters.\r\n *\r\n * @private\r\n * @example\r\n */\r\n #createCalibrationNode = () => {\r\n const audioContext = this.makeNewSourceAudioContext();\r\n const oscilator = audioContext.createOscillator();\r\n const gainNode = audioContext.createGain();\r\n\r\n oscilator.frequency.value = this.#CALIBRATION_TONE_FREQUENCY;\r\n oscilator.type = this.#CALIBRATION_TONE_TYPE;\r\n gainNode.gain.value = 0.04;\r\n\r\n oscilator.connect(gainNode);\r\n gainNode.connect(audioContext.destination);\r\n\r\n this.addCalibrationNode(oscilator);\r\n };\r\n\r\n #playCalibrationAudio = async () => {\r\n const totalDuration = this.#CALIBRATION_TONE_DURATION * 1.2;\r\n\r\n this.calibrationNodes[0].start(0);\r\n this.calibrationNodes[0].stop(totalDuration);\r\n console.log(`Playing a buffer of ${this.#CALIBRATION_TONE_DURATION} seconds of audio`);\r\n console.log(`Waiting a total of ${totalDuration} seconds`);\r\n await (0,_utils__WEBPACK_IMPORTED_MODULE_2__.sleep)(totalDuration);\r\n };\r\n\r\n #sendToServerForProcessing = () => {\r\n console.log('Sending data to server');\r\n this.pyServerAPI\r\n .getVolumeCalibration({\r\n sampleRate: this.sourceSamplingRate,\r\n payload: this.#getTruncatedSignal(),\r\n })\r\n .then(res => {\r\n if (this.outDBSPL === null) {\r\n this.outDBSPL = res;\r\n }\r\n })\r\n .catch(err => {\r\n console.warn(err);\r\n });\r\n };\r\n\r\n startCalibration = async (stream, gainValues) => {\r\n const trialIterations = gainValues.length;\r\n const soundGainDBSPLValues = [];\r\n const inDBValues = [];\r\n let inDB = 0;\r\n const outDBSPLValues = [];\r\n\r\n // run the calibration at different gain values provided by the user\r\n for (let i = 0; i < trialIterations; i++) {\r\n //convert gain to DB and add to inDB\r\n inDB = Math.log10(gainValues[i]) * 20;\r\n inDBValues.push(inDB);\r\n\r\n do {\r\n // eslint-disable-next-line no-await-in-loop\r\n await this.volumeCalibrationSteps(\r\n stream,\r\n this.#playCalibrationAudio,\r\n this.#createCalibrationToneWithGainValue,\r\n this.#sendToServerForProcessing,\r\n gainValues[i]\r\n );\r\n } while (this.outDBSPL === null);\r\n outDBSPLValues.push(this.outDBSPL);\r\n this.outDBSPL = null;\r\n }\r\n\r\n // get the volume calibration parameters from the server\r\n const parameters = await this.pyServerAPI\r\n .getVolumeCalibrationParameters({inDBValues: inDBValues, outDBSPLValues:
|
|
821
|
+
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _audioCalibrator__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../audioCalibrator */ \"./src/tasks/audioCalibrator.js\");\n/* harmony import */ var axios__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! axios */ \"./node_modules/axios/index.js\");\n/* harmony import */ var axios__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(axios__WEBPACK_IMPORTED_MODULE_1__);\n/* harmony import */ var _utils__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../utils */ \"./src/utils.js\");\n\r\n\r\n\r\n\r\n/**\r\n *\r\n */\r\nclass Volume extends _audioCalibrator__WEBPACK_IMPORTED_MODULE_0__[\"default\"] {\r\n /**\r\n *\r\n * @param root0\r\n * @param root0.download\r\n * @param root0.numCalibrationRounds\r\n * @param root0.numCalibrationNodes\r\n * @example\r\n */\r\n\r\n /** @private */\r\n #CALIBRATION_TONE_FREQUENCY = 1000; // Hz\r\n\r\n /** @private */\r\n #CALIBRATION_TONE_TYPE = 'sine';\r\n\r\n /** @private */\r\n #CALIBRATION_TONE_DURATION = 5; // seconds\r\n\r\n /** @private */\r\n soundGainDBSPL = null;\r\n outDBSPL = null;\r\n power = null;\r\n power1000 = null;\r\n RMS = null;\r\n THD = null;\r\n outDBSPL1000 = null;\r\n soundGainDBSPL1000 = null;\r\n\r\n handleIncomingData = data => {\r\n console.log('Received data: ', data);\r\n if (data.type === 'soundGainDBSPL') {\r\n this.soundGainDBSPL = data.value;\r\n } else {\r\n throw new Error(`Unknown data type: ${data.type}`);\r\n }\r\n };\r\n\r\n #getTruncatedSignal = (left = 3.5, right = 4.5) => {\r\n const start = Math.floor(left * this.sourceSamplingRate);\r\n const end = Math.floor(right * this.sourceSamplingRate);\r\n const result = Array.from(this.getLastRecordedSignal().slice(start, end));\r\n\r\n /**\r\n * function to check that capture was properly made\r\n * @param {*} list\r\n */\r\n const checkResult = list => {\r\n const setItem = new Set(list);\r\n if (setItem.size === 1 && setItem.has(0)) {\r\n console.warn(\r\n 'The last capture failed, all recorded signal is zero',\r\n this.getAllRecordedSignals()\r\n );\r\n }\r\n if (setItem.size === 0) {\r\n console.warn('The last capture failed, no recorded signal');\r\n }\r\n };\r\n checkResult(result);\r\n return result;\r\n };\r\n\r\n /** \r\n * \r\n * \r\n Construct a calibration Node with the calibration parameters and given gain value\r\n * @param {*} gainValue\r\n * */\r\n #createCalibrationToneWithGainValue = gainValue => {\r\n const audioContext = this.makeNewSourceAudioContext();\r\n const oscilator = audioContext.createOscillator();\r\n const gainNode = audioContext.createGain();\r\n\r\n oscilator.frequency.value = this.#CALIBRATION_TONE_FREQUENCY;\r\n oscilator.type = this.#CALIBRATION_TONE_TYPE;\r\n gainNode.gain.value = gainValue;\r\n\r\n oscilator.connect(gainNode);\r\n gainNode.connect(audioContext.destination);\r\n\r\n this.addCalibrationNode(oscilator);\r\n };\r\n\r\n /**\r\n * Construct a Calibration Node with the calibration parameters.\r\n *\r\n * @private\r\n * @example\r\n */\r\n #createCalibrationNode = () => {\r\n const audioContext = this.makeNewSourceAudioContext();\r\n const oscilator = audioContext.createOscillator();\r\n const gainNode = audioContext.createGain();\r\n\r\n oscilator.frequency.value = this.#CALIBRATION_TONE_FREQUENCY;\r\n oscilator.type = this.#CALIBRATION_TONE_TYPE;\r\n gainNode.gain.value = 0.04;\r\n\r\n oscilator.connect(gainNode);\r\n gainNode.connect(audioContext.destination);\r\n\r\n this.addCalibrationNode(oscilator);\r\n };\r\n\r\n #playCalibrationAudio = async () => {\r\n const totalDuration = this.#CALIBRATION_TONE_DURATION * 1.2;\r\n\r\n this.calibrationNodes[0].start(0);\r\n this.calibrationNodes[0].stop(totalDuration);\r\n console.log(`Playing a buffer of ${this.#CALIBRATION_TONE_DURATION} seconds of audio`);\r\n console.log(`Waiting a total of ${totalDuration} seconds`);\r\n await (0,_utils__WEBPACK_IMPORTED_MODULE_2__.sleep)(totalDuration);\r\n };\r\n\r\n #sendToServerForProcessing = () => {\r\n console.log('Sending data to server');\r\n this.pyServerAPI\r\n .getVolumeCalibration({\r\n sampleRate: this.sourceSamplingRate,\r\n payload: this.#getTruncatedSignal(),\r\n })\r\n .then(res => {\r\n if (this.outDBSPL === null) {\r\n this.outDBSPL = res['outDbSPL'];\r\n this.soundGainDBSPL = res['soundGainDbSPL'];\r\n this.outDBSPL1000 = res['outDbSPL1000'];\r\n this.power = res['power']\r\n this.power1000 = res['power1000']\r\n this.THD = res['thd']\r\n this.RMS = res['rms']\r\n this.soundGainDBSPL1000 = res['soundGainDbSPL1000'];\r\n }\r\n })\r\n .catch(err => {\r\n console.warn(err);\r\n });\r\n };\r\n\r\n startCalibration = async (stream, gainValues) => {\r\n const trialIterations = gainValues.length;\r\n const soundGainDBSPLValues = [];\r\n const soundGainDBSPL1000Values = [];\r\n const power1000Values = [];\r\n const powerValues = [];\r\n const rmsValues = [];\r\n const thdValues = [];\r\n const inDBValues = [];\r\n let inDB = 0;\r\n const outDBSPLValues = [];\r\n const outDBSPL1000Values = [];\r\n\r\n // run the calibration at different gain values provided by the user\r\n for (let i = 0; i < trialIterations; i++) {\r\n //convert gain to DB and add to inDB\r\n inDB = Math.log10(gainValues[i]) * 20;\r\n inDBValues.push(inDB);\r\n\r\n do {\r\n // eslint-disable-next-line no-await-in-loop\r\n await this.volumeCalibrationSteps(\r\n stream,\r\n this.#playCalibrationAudio,\r\n this.#createCalibrationToneWithGainValue,\r\n this.#sendToServerForProcessing,\r\n gainValues[i]\r\n );\r\n } while (this.outDBSPL === null);\r\n outDBSPL1000Values.push(this.outDBSPL1000);\r\n power1000Values.push(this.power1000);\r\n powerValues.push(this.power);\r\n rmsValues.push(this.RMS);\r\n thdValues.push(this.THD);\r\n outDBSPLValues.push(this.outDBSPL);\r\n soundGainDBSPLValues.push(this.soundGainDBSPL);\r\n soundGainDBSPL1000Values.push(this.soundGainDBSPL1000);\r\n\r\n this.outDBSPL = null;\r\n this.soundGainDBSPL = null;\r\n this.power1000 = null;\r\n this.power = null;\r\n this.outDBSPL1000 = null;\r\n this.RMS = null;\r\n this.THD = null;\r\n this.soundGainDBSPL1000=null;\r\n }\r\n\r\n // get the volume calibration parameters from the server\r\n const parameters = await this.pyServerAPI\r\n .getVolumeCalibrationParameters({inDBValues: inDBValues, outDBSPLValues: outDBSPL1000Values})\r\n .then(res => {\r\n return res;\r\n });\r\n const result = {\r\n parameters: parameters,\r\n inDBValues: inDBValues,\r\n outDBSPLValues: outDBSPLValues,\r\n soundGainDBSPLValues: soundGainDBSPLValues,\r\n powerValues: powerValues,\r\n power1000Values: power1000Values,\r\n outDBSPL1000Values: outDBSPL1000Values,\r\n rmsValues: rmsValues,\r\n thdValues: thdValues,\r\n soundGainDBSPL1000Values: soundGainDBSPL1000Values\r\n };\r\n\r\n return result;\r\n };\r\n}\r\n\r\n/* harmony default export */ __webpack_exports__[\"default\"] = (Volume);\r\n\n\n//# sourceURL=webpack://speakerCalibrator/./src/tasks/volume/volume.js?");
|
|
822
822
|
|
|
823
823
|
/***/ }),
|
|
824
824
|
|
package/package.json
CHANGED
|
@@ -27,6 +27,12 @@ class Volume extends AudioCalibrator {
|
|
|
27
27
|
/** @private */
|
|
28
28
|
soundGainDBSPL = null;
|
|
29
29
|
outDBSPL = null;
|
|
30
|
+
power = null;
|
|
31
|
+
power1000 = null;
|
|
32
|
+
RMS = null;
|
|
33
|
+
THD = null;
|
|
34
|
+
outDBSPL1000 = null;
|
|
35
|
+
soundGainDBSPL1000 = null;
|
|
30
36
|
|
|
31
37
|
handleIncomingData = data => {
|
|
32
38
|
console.log('Received data: ', data);
|
|
@@ -123,7 +129,14 @@ class Volume extends AudioCalibrator {
|
|
|
123
129
|
})
|
|
124
130
|
.then(res => {
|
|
125
131
|
if (this.outDBSPL === null) {
|
|
126
|
-
this.outDBSPL = res;
|
|
132
|
+
this.outDBSPL = res['outDbSPL'];
|
|
133
|
+
this.soundGainDBSPL = res['soundGainDbSPL'];
|
|
134
|
+
this.outDBSPL1000 = res['outDbSPL1000'];
|
|
135
|
+
this.power = res['power']
|
|
136
|
+
this.power1000 = res['power1000']
|
|
137
|
+
this.THD = res['thd']
|
|
138
|
+
this.RMS = res['rms']
|
|
139
|
+
this.soundGainDBSPL1000 = res['soundGainDbSPL1000'];
|
|
127
140
|
}
|
|
128
141
|
})
|
|
129
142
|
.catch(err => {
|
|
@@ -134,9 +147,15 @@ class Volume extends AudioCalibrator {
|
|
|
134
147
|
startCalibration = async (stream, gainValues) => {
|
|
135
148
|
const trialIterations = gainValues.length;
|
|
136
149
|
const soundGainDBSPLValues = [];
|
|
150
|
+
const soundGainDBSPL1000Values = [];
|
|
151
|
+
const power1000Values = [];
|
|
152
|
+
const powerValues = [];
|
|
153
|
+
const rmsValues = [];
|
|
154
|
+
const thdValues = [];
|
|
137
155
|
const inDBValues = [];
|
|
138
156
|
let inDB = 0;
|
|
139
157
|
const outDBSPLValues = [];
|
|
158
|
+
const outDBSPL1000Values = [];
|
|
140
159
|
|
|
141
160
|
// run the calibration at different gain values provided by the user
|
|
142
161
|
for (let i = 0; i < trialIterations; i++) {
|
|
@@ -154,20 +173,45 @@ class Volume extends AudioCalibrator {
|
|
|
154
173
|
gainValues[i]
|
|
155
174
|
);
|
|
156
175
|
} while (this.outDBSPL === null);
|
|
176
|
+
outDBSPL1000Values.push(this.outDBSPL1000);
|
|
177
|
+
power1000Values.push(this.power1000);
|
|
178
|
+
powerValues.push(this.power);
|
|
179
|
+
rmsValues.push(this.RMS);
|
|
180
|
+
thdValues.push(this.THD);
|
|
157
181
|
outDBSPLValues.push(this.outDBSPL);
|
|
182
|
+
soundGainDBSPLValues.push(this.soundGainDBSPL);
|
|
183
|
+
soundGainDBSPL1000Values.push(this.soundGainDBSPL1000);
|
|
184
|
+
|
|
158
185
|
this.outDBSPL = null;
|
|
186
|
+
this.soundGainDBSPL = null;
|
|
187
|
+
this.power1000 = null;
|
|
188
|
+
this.power = null;
|
|
189
|
+
this.outDBSPL1000 = null;
|
|
190
|
+
this.RMS = null;
|
|
191
|
+
this.THD = null;
|
|
192
|
+
this.soundGainDBSPL1000=null;
|
|
159
193
|
}
|
|
160
194
|
|
|
161
195
|
// get the volume calibration parameters from the server
|
|
162
196
|
const parameters = await this.pyServerAPI
|
|
163
|
-
.getVolumeCalibrationParameters({inDBValues: inDBValues, outDBSPLValues:
|
|
197
|
+
.getVolumeCalibrationParameters({inDBValues: inDBValues, outDBSPLValues: outDBSPL1000Values})
|
|
164
198
|
.then(res => {
|
|
165
|
-
// console.log(res);
|
|
166
199
|
return res;
|
|
167
200
|
});
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
201
|
+
const result = {
|
|
202
|
+
parameters: parameters,
|
|
203
|
+
inDBValues: inDBValues,
|
|
204
|
+
outDBSPLValues: outDBSPLValues,
|
|
205
|
+
soundGainDBSPLValues: soundGainDBSPLValues,
|
|
206
|
+
powerValues: powerValues,
|
|
207
|
+
power1000Values: power1000Values,
|
|
208
|
+
outDBSPL1000Values: outDBSPL1000Values,
|
|
209
|
+
rmsValues: rmsValues,
|
|
210
|
+
thdValues: thdValues,
|
|
211
|
+
soundGainDBSPL1000Values: soundGainDBSPL1000Values
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
return result;
|
|
171
215
|
};
|
|
172
216
|
}
|
|
173
217
|
|