speaker-calibration 2.2.21 → 2.2.23

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 CHANGED
@@ -818,7 +818,7 @@ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var axio
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 _audioRecorder__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./audioRecorder */ \"./src/tasks/audioRecorder.js\");\n/* harmony import */ var _server_PythonServerAPI__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../server/PythonServerAPI */ \"./src/server/PythonServerAPI.js\");\n/* harmony import */ var _utils__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../utils */ \"./src/utils.js\");\n/* eslint-disable no-await-in-loop */\r\n\r\n\r\n\r\n\r\n/**\r\n * .\r\n * .\r\n * .\r\n * Provides methods for calibrating the user's speakers\r\n *\r\n * @extends AudioRecorder\r\n */\r\nclass AudioCalibrator extends _audioRecorder__WEBPACK_IMPORTED_MODULE_0__[\"default\"] {\r\n /**\r\n *\r\n * @param numCaptures\r\n * @param numMLSPerCapture\r\n * @example\r\n */\r\n constructor(numCaptures = 1, numMLSPerCapture = 1) {\r\n super();\r\n this.numCaptures = numCaptures;\r\n this.numMLSPerCapture = numMLSPerCapture;\r\n this.pyServerAPI = new _server_PythonServerAPI__WEBPACK_IMPORTED_MODULE_1__[\"default\"]();\r\n }\r\n\r\n /** @private */\r\n #isCalibrating = false;\r\n\r\n /** @private */\r\n sourceAudioContext;\r\n\r\n /** @private */\r\n sourceAudioContextConvolved;\r\n\r\n /** @protected */\r\n numCalibratingRounds = 1;\r\n\r\n /** @protected */\r\n numSuccessfulCaptured = 0;\r\n\r\n /** @private */\r\n sourceSamplingRate;\r\n\r\n /** @protected */\r\n calibrationNodes = [];\r\n\r\n /** @protected */\r\n calibrationNodesConvolved = [];\r\n\r\n /** @protected */\r\n localAudio;\r\n\r\n /**\r\n * Called when a call is received.\r\n * Creates a local audio DOM element and attaches it to the page.\r\n *\r\n * @param targetElement\r\n * @example\r\n */\r\n createLocalAudio = targetElement => {\r\n this.localAudio = document.createElement('audio');\r\n this.localAudio.setAttribute('id', 'localAudio');\r\n targetElement.appendChild(this.localAudio);\r\n };\r\n\r\n /**\r\n *\r\n * @param {MediaStream} stream\r\n * @param {Function} playCalibrationAudio - (async) function that plays the calibration audio\r\n * @param {*} beforePlay - (async) function that is called before playing the audio\r\n * @param {*} beforeRecord - (async) function that is called before recording\r\n * @param {*} duringRecord - (async) function that is called while recording\r\n * @param {*} afterRecord - (async) function that is called after recording\r\n * @example\r\n */\r\n calibrationSteps = async (\r\n stream,\r\n playCalibrationAudio,\r\n beforePlay = async () => {},\r\n beforeRecord = async () => {},\r\n loopCondition = () => false,\r\n duringRecord = async () => {},\r\n afterRecord = async () => {},\r\n mode\r\n ) => {\r\n this.numSuccessfulCaptured = 0;\r\n\r\n // do something before playing such as using the MLS to fill the buffers\r\n console.warn('beforePlay');\r\n await beforePlay();\r\n\r\n // play calibration audio\r\n console.warn('playCalibrationAudio');\r\n playCalibrationAudio();\r\n\r\n // do something before recording such as awaiting a certain amount of time\r\n console.warn('beforeRecord');\r\n await beforeRecord();\r\n\r\n // calibration loop\r\n while (loopCondition()) {\r\n // start recording\r\n console.warn('startRecording');\r\n await this.startRecording(stream);\r\n\r\n // do something during the recording such as sleep n amount of time\r\n console.warn('duringRecord');\r\n await duringRecord();\r\n\r\n // when done, stop recording\r\n console.warn('stopRecording');\r\n await this.stopRecording(mode);\r\n\r\n // do something after recording such as start processing values\r\n console.warn('afterRecord');\r\n await afterRecord();\r\n\r\n // eslint-disable-next-line no-await-in-loop\r\n await (0,_utils__WEBPACK_IMPORTED_MODULE_2__.sleep)(1);\r\n }\r\n };\r\n\r\n /**\r\n *\r\n * @param {MediaStream} stream\r\n * @param {Function} playCalibrationAudio - (async) function that plays the calibration audio\r\n * @param {*} beforeRecord - (async) function that is called before recording\r\n * @param {*} afterRecord - (async) function that is called after recording\r\n * @param {Number} gainValue - the gain value to set the gain node to\r\n */\r\n volumeCalibrationSteps = async (\r\n stream,\r\n playCalibrationAudio,\r\n beforeRecord = () => {},\r\n afterRecord = () => {},\r\n gainValue,\r\n lCalib = 104.92978421490648\r\n ) => {\r\n this.numCalibratingRoundsCompleted = 0;\r\n\r\n // calibration loop\r\n while (!this.#isCalibrating && this.numCalibratingRoundsCompleted < this.numCalibratingRounds) {\r\n // before recording\r\n await beforeRecord(gainValue);\r\n\r\n // start recording\r\n await this.startRecording(stream);\r\n\r\n // play calibration audio\r\n console.log(`Calibration Round ${this.numCalibratingRoundsCompleted}`);\r\n await playCalibrationAudio();\r\n\r\n // when done, stop recording\r\n console.log('Calibration Round Complete');\r\n await this.stopRecording();\r\n\r\n // after recording\r\n await afterRecord(lCalib);\r\n\r\n this.calibrationNodes = [];\r\n\r\n // eslint-disable-next-line no-await-in-loop\r\n await (0,_utils__WEBPACK_IMPORTED_MODULE_2__.sleep)(2);\r\n this.numCalibratingRoundsCompleted += 1;\r\n }\r\n };\r\n\r\n /**\r\n * Getter for the isCalibrating property.\r\n *\r\n * @public\r\n * @returns - True if the audio is being calibrated, false otherwise.\r\n * @example\r\n */\r\n getCalibrationStatus = () => this.#isCalibrating;\r\n\r\n /** .\r\n * .\r\n * .\r\n * Set the sampling rate to the value received from the listener\r\n *\r\n * @param {*} sinkSamplingRate\r\n * @param samplingRate\r\n * @example\r\n */\r\n setSamplingRates = samplingRate => {\r\n this.sinkSamplingRate = samplingRate;\r\n this.sourceSamplingRate = samplingRate;\r\n\r\n // this.emit('update', {message: `sampling at ${samplingRate}Hz...`});\r\n };\r\n\r\n sampleRatesSet = () => this.sourceSamplingRate && this.sinkSamplingRate;\r\n\r\n addCalibrationNode = node => {\r\n this.calibrationNodes.push(node);\r\n };\r\n\r\n addCalibrationNodeConvolved = node => {\r\n \r\n this.calibrationNodesConvolved.push(node);\r\n }\r\n\r\n makeNewSourceAudioContext = () => {\r\n const options = {\r\n sampleRate: this.sourceSamplingRate,\r\n };\r\n\r\n this.sourceAudioContext = new (window.AudioContext ||\r\n window.webkitAudioContext ||\r\n window.audioContext)(options);\r\n\r\n return this.sourceAudioContext;\r\n };\r\n\r\n makeNewSourceAudioContextConvolved = () => {\r\n const options = {\r\n sampleRate: this.sourceSamplingRate,\r\n };\r\n\r\n this.sourceAudioContextConvolved = new (window.AudioContext ||\r\n window.webkitAudioContext ||\r\n window.audioContext)(options);\r\n\r\n return this.sourceAudioContextConvolved;\r\n };\r\n\r\n\r\n\r\n /** .\r\n * .\r\n * .\r\n * Download the result of the calibration roudns\r\n *\r\n * @example\r\n */\r\n downloadData = () => {\r\n const recordings = this.getAllRecordedSignals();\r\n const i = recordings.length - 1;\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_2__.saveToCSV)(recordings[i], `recordedMLSignal_${i}_unconvolved.csv`);\r\n };\r\n downloadSingleUnfilteredRecording = () => {\r\n const recordings = this.getAllRecordedSignals();\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_2__.saveToCSV)(recordings[0], `recordedMLSignal_unconvolved.csv`);\r\n }\r\n downloadSingleFilteredRecording = () => {\r\n const recordings = this.getAllFilteredRecordedSignals();\r\n console.log(\"Single filtered recording should be of length: \" + recordings[0].length);\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_2__.saveToCSV)(recordings[0], `recordedMLSignal_convolved.csv`);\r\n }\r\n downloadUnfilteredRecordings = () => {\r\n const recordings = this.getAllRecordedSignals();\r\n console.log(\"unfilterd download?\");\r\n for (let i = 0; i < recordings.length; i++){\r\n console.log(i);\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_2__.saveToCSV)(recordings[i], `recordedMLSignal_${i}_unconvolved.csv`);\r\n }\r\n };\r\n downloadFilteredRecordings = () => {\r\n const recordings = this.getAllFilteredRecordedSignals();\r\n for (let i = 0; i < recordings.length; i++){\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_2__.saveToCSV)(recordings[i], `recordedMLSignal_${i}_convolved.csv`);\r\n }\r\n };\r\n}\r\n\r\n/* harmony default export */ __webpack_exports__[\"default\"] = (AudioCalibrator);\r\n\n\n//# sourceURL=webpack://speakerCalibrator/./src/tasks/audioCalibrator.js?");
821
+ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _audioRecorder__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./audioRecorder */ \"./src/tasks/audioRecorder.js\");\n/* harmony import */ var _server_PythonServerAPI__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../server/PythonServerAPI */ \"./src/server/PythonServerAPI.js\");\n/* harmony import */ var _utils__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../utils */ \"./src/utils.js\");\n/* eslint-disable no-await-in-loop */\r\n\r\n\r\n\r\n\r\n/**\r\n * .\r\n * .\r\n * .\r\n * Provides methods for calibrating the user's speakers\r\n *\r\n * @extends AudioRecorder\r\n */\r\nclass AudioCalibrator extends _audioRecorder__WEBPACK_IMPORTED_MODULE_0__[\"default\"] {\r\n /**\r\n *\r\n * @param numCaptures\r\n * @param numMLSPerCapture\r\n * @example\r\n */\r\n constructor(numCaptures = 1, numMLSPerCapture = 1) {\r\n super();\r\n this.numCaptures = numCaptures;\r\n this.numMLSPerCapture = numMLSPerCapture;\r\n this.pyServerAPI = new _server_PythonServerAPI__WEBPACK_IMPORTED_MODULE_1__[\"default\"]();\r\n }\r\n\r\n /** @private */\r\n #isCalibrating = false;\r\n\r\n /** @private */\r\n sourceAudioContext;\r\n\r\n /** @private */\r\n sourceAudioContextConvolved;\r\n\r\n /** @protected */\r\n numCalibratingRounds = 1;\r\n\r\n /** @protected */\r\n numSuccessfulCaptured = 0;\r\n\r\n /** @private */\r\n sourceSamplingRate;\r\n\r\n /** @protected */\r\n calibrationNodes = [];\r\n\r\n /** @protected */\r\n calibrationNodesConvolved = [];\r\n\r\n /** @protected */\r\n localAudio;\r\n\r\n /**\r\n * Called when a call is received.\r\n * Creates a local audio DOM element and attaches it to the page.\r\n *\r\n * @param targetElement\r\n * @example\r\n */\r\n createLocalAudio = targetElement => {\r\n this.localAudio = document.createElement('audio');\r\n this.localAudio.setAttribute('id', 'localAudio');\r\n targetElement.appendChild(this.localAudio);\r\n };\r\n\r\n /**\r\n *\r\n * @param {MediaStream} stream\r\n * @param {Function} playCalibrationAudio - (async) function that plays the calibration audio\r\n * @param {*} beforePlay - (async) function that is called before playing the audio\r\n * @param {*} beforeRecord - (async) function that is called before recording\r\n * @param {*} duringRecord - (async) function that is called while recording\r\n * @param {*} afterRecord - (async) function that is called after recording\r\n * @example\r\n */\r\n calibrationSteps = async (\r\n stream,\r\n playCalibrationAudio,\r\n beforePlay = async () => {},\r\n beforeRecord = async () => {},\r\n loopCondition = () => false,\r\n duringRecord = async () => {},\r\n afterRecord = async () => {},\r\n mode,\r\n checkRec\r\n ) => {\r\n this.numSuccessfulCaptured = 0;\r\n\r\n // do something before playing such as using the MLS to fill the buffers\r\n console.warn('beforePlay');\r\n await beforePlay();\r\n\r\n // play calibration audio\r\n console.warn('playCalibrationAudio');\r\n playCalibrationAudio();\r\n\r\n // do something before recording such as awaiting a certain amount of time\r\n console.warn('beforeRecord');\r\n await beforeRecord();\r\n\r\n // calibration loop\r\n while (loopCondition()) {\r\n // start recording\r\n console.warn('startRecording');\r\n await this.startRecording(stream);\r\n\r\n // do something during the recording such as sleep n amount of time\r\n console.warn('duringRecord');\r\n await duringRecord();\r\n\r\n // when done, stop recording\r\n console.warn('stopRecording');\r\n await this.stopRecording(mode,checkRec);\r\n\r\n // do something after recording such as start processing values\r\n console.warn('afterRecord');\r\n await afterRecord();\r\n\r\n // eslint-disable-next-line no-await-in-loop\r\n await (0,_utils__WEBPACK_IMPORTED_MODULE_2__.sleep)(1);\r\n }\r\n };\r\n\r\n /**\r\n *\r\n * @param {MediaStream} stream\r\n * @param {Function} playCalibrationAudio - (async) function that plays the calibration audio\r\n * @param {*} beforeRecord - (async) function that is called before recording\r\n * @param {*} afterRecord - (async) function that is called after recording\r\n * @param {Number} gainValue - the gain value to set the gain node to\r\n */\r\n volumeCalibrationSteps = async (\r\n stream,\r\n playCalibrationAudio,\r\n beforeRecord = () => {},\r\n afterRecord = () => {},\r\n gainValue,\r\n lCalib = 104.92978421490648,\r\n checkRec\r\n ) => {\r\n this.numCalibratingRoundsCompleted = 0;\r\n\r\n // calibration loop\r\n while (!this.#isCalibrating && this.numCalibratingRoundsCompleted < this.numCalibratingRounds) {\r\n // before recording\r\n await beforeRecord(gainValue);\r\n\r\n // start recording\r\n await this.startRecording(stream);\r\n\r\n // play calibration audio\r\n console.log(`Calibration Round ${this.numCalibratingRoundsCompleted}`);\r\n await playCalibrationAudio();\r\n\r\n // when done, stop recording\r\n console.log('Calibration Round Complete');\r\n await this.stopRecording('volume',checkRec);\r\n\r\n // after recording\r\n await afterRecord(lCalib);\r\n\r\n this.calibrationNodes = [];\r\n\r\n // eslint-disable-next-line no-await-in-loop\r\n await (0,_utils__WEBPACK_IMPORTED_MODULE_2__.sleep)(2);\r\n this.numCalibratingRoundsCompleted += 1;\r\n }\r\n };\r\n\r\n /**\r\n * Getter for the isCalibrating property.\r\n *\r\n * @public\r\n * @returns - True if the audio is being calibrated, false otherwise.\r\n * @example\r\n */\r\n getCalibrationStatus = () => this.#isCalibrating;\r\n\r\n /** .\r\n * .\r\n * .\r\n * Set the sampling rate to the value received from the listener\r\n *\r\n * @param {*} sinkSamplingRate\r\n * @param samplingRate\r\n * @example\r\n */\r\n setSamplingRates = samplingRate => {\r\n this.sinkSamplingRate = samplingRate;\r\n this.sourceSamplingRate = samplingRate;\r\n\r\n // this.emit('update', {message: `sampling at ${samplingRate}Hz...`});\r\n };\r\n\r\n sampleRatesSet = () => this.sourceSamplingRate && this.sinkSamplingRate;\r\n\r\n addCalibrationNode = node => {\r\n this.calibrationNodes.push(node);\r\n };\r\n\r\n addCalibrationNodeConvolved = node => {\r\n \r\n this.calibrationNodesConvolved.push(node);\r\n }\r\n\r\n makeNewSourceAudioContext = () => {\r\n const options = {\r\n sampleRate: this.sourceSamplingRate,\r\n };\r\n\r\n this.sourceAudioContext = new (window.AudioContext ||\r\n window.webkitAudioContext ||\r\n window.audioContext)(options);\r\n\r\n return this.sourceAudioContext;\r\n };\r\n\r\n makeNewSourceAudioContextConvolved = () => {\r\n const options = {\r\n sampleRate: this.sourceSamplingRate,\r\n };\r\n\r\n this.sourceAudioContextConvolved = new (window.AudioContext ||\r\n window.webkitAudioContext ||\r\n window.audioContext)(options);\r\n\r\n return this.sourceAudioContextConvolved;\r\n };\r\n\r\n\r\n\r\n /** .\r\n * .\r\n * .\r\n * Download the result of the calibration roudns\r\n *\r\n * @example\r\n */\r\n downloadData = () => {\r\n const recordings = this.getAllRecordedSignals();\r\n const i = recordings.length - 1;\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_2__.saveToCSV)(recordings[i], `recordedMLSignal_${i}_unconvolved.csv`);\r\n };\r\n downloadSingleUnfilteredRecording = () => {\r\n const recordings = this.getAllRecordedSignals();\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_2__.saveToCSV)(recordings[recordings.length-1], `recordedMLSignal_unconvolved.csv`);\r\n }\r\n downloadSingleFilteredRecording = () => {\r\n const recordings = this.getAllFilteredRecordedSignals();\r\n console.log(\"Single filtered recording should be of length: \" + recordings[0].length);\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_2__.saveToCSV)(recordings[0], `recordedMLSignal_convolved.csv`);\r\n }\r\n downloadUnfilteredRecordings = () => {\r\n const recordings = this.getAllRecordedSignals();\r\n console.log(\"unfilterd download?\");\r\n for (let i = 0; i < recordings.length; i++){\r\n console.log(i);\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_2__.saveToCSV)(recordings[i], `recordedMLSignal_${i}_unconvolved.csv`);\r\n }\r\n };\r\n downloadFilteredRecordings = () => {\r\n const recordings = this.getAllFilteredRecordedSignals();\r\n for (let i = 0; i < recordings.length; i++){\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_2__.saveToCSV)(recordings[i], `recordedMLSignal_${i}_convolved.csv`);\r\n }\r\n };\r\n}\r\n\r\n/* harmony default export */ __webpack_exports__[\"default\"] = (AudioCalibrator);\r\n\n\n//# sourceURL=webpack://speakerCalibrator/./src/tasks/audioCalibrator.js?");
822
822
 
823
823
  /***/ }),
824
824
 
@@ -829,7 +829,7 @@ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _aud
829
829
  /***/ (function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
830
830
 
831
831
  "use strict";
832
- eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _myEventEmitter__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../myEventEmitter */ \"./src/myEventEmitter.js\");\n\r\n\r\n/**\r\n * @class provides a simple interface for recording audio from a microphone\r\n * using the Media Recorder API.\r\n */\r\nclass AudioRecorder extends _myEventEmitter__WEBPACK_IMPORTED_MODULE_0__[\"default\"] {\r\n /** @private */\r\n #mediaRecorder;\r\n\r\n /** @private */\r\n #recordedChunks = [];\r\n\r\n /** @private */\r\n #audioBlob;\r\n\r\n /** @private */\r\n #audioContext;\r\n\r\n /** @private */\r\n #recordedSignals = [];\r\n\r\n /** @private */\r\n #filteredRecordings = [];\r\n\r\n /** @private */\r\n sinkSamplingRate;\r\n\r\n /**\r\n * Decode the audio data from the recorded audio blob.\r\n *\r\n * @private\r\n * @example\r\n */\r\n #saveRecording = async () => {\r\n const arrayBuffer = await this.#audioBlob.arrayBuffer();\r\n const audioBuffer = await this.#audioContext.decodeAudioData(arrayBuffer);\r\n const data = audioBuffer.getChannelData(0);\r\n\r\n console.log(`Decoded audio buffer with ${data.length} samples`);\r\n console.log(`Unfiltered recording should be of length: ${data.length}`);\r\n this.#recordedSignals.push(Array.from(data));\r\n };\r\n\r\n #saveFilteredRecording = async () => {\r\n const arrayBuffer = await this.#audioBlob.arrayBuffer();\r\n const audioBuffer = await this.#audioContext.decodeAudioData(arrayBuffer);\r\n const data = audioBuffer.getChannelData(0);\r\n\r\n console.log(`Decoded audio buffer with ${data.length} samples`);\r\n console.log(`Filtered recording should be of length: ${data.length}`);\r\n this.#filteredRecordings.push(Array.from(data));\r\n };\r\n\r\n /**\r\n * Event listener triggered when data is available in the media recorder.\r\n *\r\n * @private\r\n * @param e - The event object.\r\n * @example\r\n */\r\n #onRecorderDataAvailable = e => {\r\n if (e.data && e.data.size > 0) this.#recordedChunks.push(e.data);\r\n };\r\n\r\n /**\r\n * Method to create a media recorder object and set up event listeners.\r\n *\r\n * @private\r\n * @param stream - The stream of audio from the Listener.\r\n * @example\r\n */\r\n #setMediaRecorder = stream => {\r\n // Create a new MediaRecorder object\r\n this.#mediaRecorder = new MediaRecorder(stream);\r\n\r\n // Add event listeners\r\n this.#mediaRecorder.ondataavailable = e => this.#onRecorderDataAvailable(e);\r\n };\r\n\r\n #setAudioContext = () => {\r\n this.#audioContext = new (window.AudioContext ||\r\n window.webkitAudioContext ||\r\n window.audioContext)({\r\n sampleRate: this.sinkSamplingRate,\r\n //sampleRate: 96000\r\n });\r\n };\r\n\r\n /**\r\n * Public method to start the recording process.\r\n *\r\n * @param stream - The stream of audio from the Listener.\r\n * @example\r\n */\r\n startRecording = async stream => {\r\n // Create a fresh audio context\r\n this.#setAudioContext();\r\n // Set up media recorder if needed\r\n if (!this.#mediaRecorder) this.#setMediaRecorder(stream);\r\n // clear recorded chunks\r\n this.#recordedChunks = [];\r\n // start recording\r\n this.#mediaRecorder.start();\r\n };\r\n\r\n /**\r\n * Method to stop the recording process.\r\n *\r\n * @public\r\n * @example\r\n */\r\n stopRecording = async (mode) => {\r\n // Stop the media recorder, and wait for the data to be available\r\n await new Promise(resolve => {\r\n this.#mediaRecorder.onstop = () => {\r\n // when the stop event is triggered, resolve the promise\r\n this.#audioBlob = new Blob(this.#recordedChunks, {\r\n type: 'audio/wav; codecs=opus',\r\n });\r\n resolve(this.#audioBlob);\r\n };\r\n // call stop\r\n this.#mediaRecorder.stop();\r\n });\r\n // Now that we have data, save it\r\n if (mode === 'filtered'){\r\n await this.#saveFilteredRecording();\r\n }else{\r\n await this.#saveRecording();\r\n }\r\n };\r\n\r\n /** .\r\n * .\r\n * .\r\n * Public method to get the last recorded audio signal\r\n *\r\n * @returns\r\n * @example\r\n */\r\n getLastRecordedSignal = () => this.#recordedSignals[this.#recordedSignals.length - 1];\r\n\r\n /** .\r\n * .\r\n * .\r\n * Public method to get all the recorded audio signals\r\n *\r\n * @returns\r\n * @example\r\n */\r\n getAllRecordedSignals = () => this.#recordedSignals;\r\n\r\n /** .\r\n * .\r\n * .\r\n * Public method to get all the recorded audio signals\r\n *\r\n * @returns\r\n * @example\r\n */\r\n getAllFilteredRecordedSignals = () => this.#filteredRecordings;\r\n\r\n /** .\r\n * .\r\n * .\r\n * Public method to set the sampling rate used by the capture device\r\n *\r\n * @param {Number} sinkSamplingRate - The sampling rate of the capture device\r\n * @example\r\n */\r\n setSinkSamplingRate = sinkSamplingRate => {\r\n this.sinkSamplingRate = sinkSamplingRate;\r\n };\r\n}\r\n\r\n/* harmony default export */ __webpack_exports__[\"default\"] = (AudioRecorder);\r\n\n\n//# sourceURL=webpack://speakerCalibrator/./src/tasks/audioRecorder.js?");
832
+ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _myEventEmitter__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../myEventEmitter */ \"./src/myEventEmitter.js\");\n\r\n\r\n/**\r\n * @class provides a simple interface for recording audio from a microphone\r\n * using the Media Recorder API.\r\n */\r\nclass AudioRecorder extends _myEventEmitter__WEBPACK_IMPORTED_MODULE_0__[\"default\"] {\r\n /** @private */\r\n #mediaRecorder;\r\n\r\n /** @private */\r\n #recordedChunks = [];\r\n\r\n /** @private */\r\n #audioBlob;\r\n\r\n /** @private */\r\n #audioContext;\r\n\r\n /** @private */\r\n #recordedSignals = [];\r\n\r\n /** @private */\r\n #filteredRecordings = [];\r\n\r\n /** @private */\r\n sinkSamplingRate;\r\n\r\n /**\r\n * Decode the audio data from the recorded audio blob.\r\n *\r\n * @private\r\n * @example\r\n */\r\n #saveRecording = async (checkRec) => {\r\n const arrayBuffer = await this.#audioBlob.arrayBuffer();\r\n const audioBuffer = await this.#audioContext.decodeAudioData(arrayBuffer);\r\n const data = audioBuffer.getChannelData(0);\r\n const dataArray = Array.from(data);\r\n\r\n console.log(`Decoded audio buffer with ${data.length} samples`);\r\n console.log(`Unfiltered recording should be of length: ${data.length}`);\r\n if (checkRec == 'loudest'){\r\n const uniqueSet = new Set(dataArray);\r\n const numberOfUniqueValues = uniqueSet.size;\r\n const squaredValues = dataArray.map(value => value * value);\r\n const sum_of_squares = squaredValues.reduce((total, value) => total + value, 0);\r\n const squared_mean = sum_of_squares / dataArray.length;\r\n const dbLevel = 20 * Math.log10(Math.sqrt(squared_mean));\r\n console.log(\"Loudest 1000-Hz recording: \" + dbLevel + \" with \" + numberOfUniqueValues + \" unique values.\")\r\n }else if (checkRec == 'allhz'){\r\n const uniqueSet = new Set(dataArray);\r\n const numberOfUniqueValues = uniqueSet.size;\r\n const squaredValues = dataArray.map(value => value * value);\r\n const sum_of_squares = squaredValues.reduce((total, value) => total + value, 0);\r\n const squared_mean = sum_of_squares / dataArray.length;\r\n const dbLevel = 20 * Math.log10(Math.sqrt(squared_mean));\r\n console.log(\"All Hz Recording: \" + dbLevel + \" with \" + numberOfUniqueValues + \" unique values.\")\r\n }\r\n this.#recordedSignals.push(dataArray);\r\n };\r\n\r\n #saveFilteredRecording = async () => {\r\n const arrayBuffer = await this.#audioBlob.arrayBuffer();\r\n const audioBuffer = await this.#audioContext.decodeAudioData(arrayBuffer);\r\n const data = audioBuffer.getChannelData(0);\r\n\r\n console.log(`Decoded audio buffer with ${data.length} samples`);\r\n console.log(`Filtered recording should be of length: ${data.length}`);\r\n this.#filteredRecordings.push(Array.from(data));\r\n };\r\n\r\n /**\r\n * Event listener triggered when data is available in the media recorder.\r\n *\r\n * @private\r\n * @param e - The event object.\r\n * @example\r\n */\r\n #onRecorderDataAvailable = e => {\r\n if (e.data && e.data.size > 0) this.#recordedChunks.push(e.data);\r\n };\r\n\r\n /**\r\n * Method to create a media recorder object and set up event listeners.\r\n *\r\n * @private\r\n * @param stream - The stream of audio from the Listener.\r\n * @example\r\n */\r\n #setMediaRecorder = stream => {\r\n // Create a new MediaRecorder object\r\n this.#mediaRecorder = new MediaRecorder(stream);\r\n\r\n // Add event listeners\r\n this.#mediaRecorder.ondataavailable = e => this.#onRecorderDataAvailable(e);\r\n };\r\n\r\n #setAudioContext = () => {\r\n this.#audioContext = new (window.AudioContext ||\r\n window.webkitAudioContext ||\r\n window.audioContext)({\r\n sampleRate: this.sinkSamplingRate,\r\n //sampleRate: 96000\r\n });\r\n };\r\n\r\n /**\r\n * Public method to start the recording process.\r\n *\r\n * @param stream - The stream of audio from the Listener.\r\n * @example\r\n */\r\n startRecording = async stream => {\r\n // Create a fresh audio context\r\n this.#setAudioContext();\r\n // Set up media recorder if needed\r\n if (!this.#mediaRecorder) this.#setMediaRecorder(stream);\r\n // clear recorded chunks\r\n this.#recordedChunks = [];\r\n // start recording\r\n this.#mediaRecorder.start();\r\n };\r\n\r\n /**\r\n * Method to stop the recording process.\r\n *\r\n * @public\r\n * @example\r\n */\r\n stopRecording = async (mode,checkRec) => {\r\n // Stop the media recorder, and wait for the data to be available\r\n await new Promise(resolve => {\r\n this.#mediaRecorder.onstop = () => {\r\n // when the stop event is triggered, resolve the promise\r\n this.#audioBlob = new Blob(this.#recordedChunks, {\r\n type: 'audio/wav; codecs=opus',\r\n });\r\n resolve(this.#audioBlob);\r\n };\r\n // call stop\r\n this.#mediaRecorder.stop();\r\n });\r\n // Now that we have data, save it\r\n if (mode === 'filtered'){\r\n await this.#saveFilteredRecording();\r\n }else{\r\n await this.#saveRecording(checkRec);\r\n }\r\n };\r\n\r\n /** .\r\n * .\r\n * .\r\n * Public method to get the last recorded audio signal\r\n *\r\n * @returns\r\n * @example\r\n */\r\n getLastRecordedSignal = () => this.#recordedSignals[this.#recordedSignals.length - 1];\r\n\r\n /** .\r\n * .\r\n * .\r\n * Public method to get all the recorded audio signals\r\n *\r\n * @returns\r\n * @example\r\n */\r\n getAllRecordedSignals = () => this.#recordedSignals;\r\n\r\n /** .\r\n * .\r\n * .\r\n * Public method to get all the recorded audio signals\r\n *\r\n * @returns\r\n * @example\r\n */\r\n getAllFilteredRecordedSignals = () => this.#filteredRecordings;\r\n\r\n /** .\r\n * .\r\n * .\r\n * Public method to set the sampling rate used by the capture device\r\n *\r\n * @param {Number} sinkSamplingRate - The sampling rate of the capture device\r\n * @example\r\n */\r\n setSinkSamplingRate = sinkSamplingRate => {\r\n this.sinkSamplingRate = sinkSamplingRate;\r\n };\r\n}\r\n\r\n/* harmony default export */ __webpack_exports__[\"default\"] = (AudioRecorder);\r\n\n\n//# sourceURL=webpack://speakerCalibrator/./src/tasks/audioRecorder.js?");
833
833
 
834
834
  /***/ }),
835
835
 
@@ -840,7 +840,7 @@ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _myE
840
840
  /***/ (function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
841
841
 
842
842
  "use strict";
843
- 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 _utils__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../utils */ \"./src/utils.js\");\n/* harmony import */ var _config_firebase__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../config/firebase */ \"./src/config/firebase.js\");\n/* harmony import */ var firebase_database__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! firebase/database */ \"./node_modules/firebase/database/dist/esm/index.esm.js\");\n\r\n\r\n\r\n\r\n\r\n\r\n/**\r\n *\r\n */\r\nclass Combination extends _audioCalibrator__WEBPACK_IMPORTED_MODULE_0__[\"default\"] {\r\n /**\r\n * Default constructor. Creates an instance with any number of paramters passed or the default parameters defined here.\r\n *\r\n * @param {Object<boolean, number, number, number>} calibratorParams - paramter object\r\n * @param {boolean} [calibratorParams.download = false] - boolean flag to download captures\r\n * @param {number} [calibratorParams.mlsOrder = 18] - order of the MLS to be generated\r\n * @param {number} [calibratorParams.numCaptures = 5] - number of captures to perform\r\n * @param {number} [calibratorParams.numMLSPerCapture = 4] - number of bursts of MLS per capture\r\n */\r\n constructor({\r\n download = false,\r\n mlsOrder = 18,\r\n numCaptures = 3,\r\n numMLSPerCapture = 4,\r\n lowHz = 20,\r\n highHz = 10000,\r\n }) {\r\n super(numCaptures, numMLSPerCapture);\r\n this.#mlsOrder = parseInt(mlsOrder, 10);\r\n this.#P = 2 ** mlsOrder - 1;\r\n this.#download = download;\r\n this.#mls = [];\r\n this.#lowHz = lowHz;\r\n this.#highHz = highHz;\r\n }\r\n\r\n /** @private */\r\n stepNum = 0;\r\n\r\n /** @private */\r\n totalSteps = 25;\r\n\r\n /** @private */\r\n #download;\r\n\r\n /** @private */\r\n #mlsGenInterface;\r\n\r\n /** @private */\r\n #mlsBufferView;\r\n\r\n /** @private */\r\n componentInvertedImpulseResponse = null;\r\n\r\n /** @private */\r\n systemInvertedImpulseResponse = null;\r\n\r\n //averaged and subtracted ir returned from calibration used to calculated iir\r\n /** @private */\r\n ir = null;\r\n\r\n /** @private */\r\n impulseResponses = [];\r\n\r\n /** @private */\r\n #mlsOrder;\r\n\r\n /** @private */\r\n #lowHz;\r\n\r\n /** @private */\r\n #highHz;\r\n\r\n /** @private */\r\n #mls;\r\n\r\n /** @private */\r\n #P;\r\n\r\n /** @private */\r\n #audioContext;\r\n\r\n /** @private */\r\n TAPER_SECS = 5;\r\n\r\n /** @private */\r\n offsetGainNode;\r\n\r\n /** @private */\r\n componentConvolution;\r\n\r\n /** @private */\r\n systemConvolution;\r\n\r\n ////////////////////////volume\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 CALIBRATION_TONE_DURATION = 5; // seconds\r\n\r\n /** @private */\r\n outDBSPL = null;\r\n THD = null;\r\n outDBSPL1000 = null;\r\n\r\n /** @private */\r\n TAPER_SECS = 0.01; // seconds\r\n\r\n /** @private */\r\n status_denominator = 8;\r\n\r\n /** @private */\r\n status_numerator = 0;\r\n\r\n /** @private */\r\n percent_complete = 0;\r\n\r\n /** @private */\r\n status = ``;\r\n\r\n /**@private */\r\n status_literal = `<div style=\"display: flex; justify-content: center;\"><div style=\"width: 200px; height: 20px; border: 2px solid #000; border-radius: 10px;\"><div style=\"width: ${this.percent_complete}%; height: 100%; background-color: #00aaff; border-radius: 8px;\"></div></div></div>`;\r\n\r\n /**@private */\r\n componentIR = null;\r\n\r\n /**@private */\r\n oldComponentIR = null;\r\n\r\n /**@private */\r\n systemIR = null;\r\n\r\n /**@private */\r\n _calibrateSoundCheck = '';\r\n\r\n deviceType = null;\r\n\r\n deviceName = null;\r\n\r\n deviceInfo = null;\r\n\r\n desired_time_per_mls = 0;\r\n\r\n num_mls_to_skip = 0;\r\n\r\n desired_sampling_rate = 0;\r\n\r\n #currentConvolution = [];\r\n\r\n mode = 'unfiltered';\r\n\r\n sourceNode;\r\n\r\n autocorrelations = [];\r\n\r\n iirLength = 0;\r\n\r\n /**generate string template that gets reevaluated as variable increases */\r\n generateTemplate = () => {\r\n if (this.percent_complete > 100) {\r\n this.percent_complete = 100;\r\n }\r\n const template = `<div style=\"display: flex; justify-content: center;\"><div style=\"width: 800px; height: 20px; border: 2px solid #000; border-radius: 10px;\"><div style=\"width: ${this.percent_complete}%; height: 100%; background-color: #00aaff; border-radius: 8px;\"></div></div></div>`;\r\n return template;\r\n };\r\n\r\n /** increment numerator and percent for status bar */\r\n incrementStatusBar = () => {\r\n this.status_numerator += 1;\r\n this.percent_complete = (this.status_numerator / this.status_denominator) * 100;\r\n };\r\n\r\n setDeviceType = deviceType => {\r\n this.deviceType = deviceType;\r\n };\r\n\r\n setDeviceName = deviceName => {\r\n this.deviceName = deviceName;\r\n };\r\n\r\n setDeviceInfo = deviceInfo => {\r\n this.deviceInfo = deviceInfo;\r\n };\r\n\r\n /** .\r\n * .\r\n * .\r\n * Sends all the computed impulse responses to the backend server for processing\r\n *\r\n * @returns sets the resulting inverted impulse response to the class property\r\n * @example\r\n */\r\n sendSystemImpulseResponsesToServerForProcessing = async () => {\r\n const computedIRs = await Promise.all(this.impulseResponses);\r\n const filteredComputedIRs = computedIRs.filter(element => {\r\n return element != undefined;\r\n });\r\n const mls = this.#mls;\r\n const lowHz = this.#lowHz;\r\n const highHz = this.#highHz;\r\n const iirLength = this.iirLength;\r\n const num_periods = this.numMLSPerCapture + this.num_mls_to_skip;\r\n this.stepNum += 1;\r\n console.log('send impulse responses to server: ' + this.stepNum);\r\n this.status =\r\n `All Hz Calibration: computing the IIR...`.toString() + this.generateTemplate().toString();\r\n this.emit('update', {message: this.status});\r\n return this.pyServerAPI\r\n .getSystemInverseImpulseResponseWithRetry({\r\n payload: filteredComputedIRs.slice(0, this.numCaptures),\r\n mls,\r\n lowHz,\r\n highHz,\r\n iirLength,\r\n num_periods,\r\n sampleRate: this.sourceSamplingRate || 96000,\r\n })\r\n .then(res => {\r\n console.log(res);\r\n this.stepNum += 1;\r\n console.log('got impulse response ' + this.stepNum);\r\n this.incrementStatusBar();\r\n this.status =\r\n `All Hz Calibration: done computing the IIR...`.toString() +\r\n this.generateTemplate().toString();\r\n this.emit('update', {message: this.status});\r\n this.systemInvertedImpulseResponse = res['iir'];\r\n this.systemIR = res['ir'];\r\n this.systemConvolution = res['convolution'];\r\n })\r\n .catch(err => {\r\n console.error(err);\r\n });\r\n };\r\n\r\n /** .\r\n * .\r\n * .\r\n * Sends all the computed impulse responses to the backend server for processing\r\n *\r\n * @returns sets the resulting inverted impulse response to the class property\r\n * @example\r\n */\r\n sendComponentImpulseResponsesToServerForProcessing = async () => {\r\n const computedIRs = await Promise.all(this.impulseResponses);\r\n const filteredComputedIRs = computedIRs.filter(element => {\r\n return element != undefined;\r\n });\r\n const componentIRGains = this.componentIR['Gain'];\r\n const componentIRFreqs = this.componentIR['Freq'];\r\n const mls = this.#mls;\r\n const lowHz = this.#lowHz;\r\n const iirLength = this.iirLength;\r\n const num_periods = this.numMLSPerCapture + this.num_mls_to_skip;\r\n const highHz = this.#highHz;\r\n this.stepNum += 1;\r\n console.log('send impulse responses to server: ' + this.stepNum);\r\n this.status =\r\n `All Hz Calibration: computing the IIR...`.toString() + this.generateTemplate().toString();\r\n this.emit('update', {message: this.status});\r\n return this.pyServerAPI\r\n .getComponentInverseImpulseResponseWithRetry({\r\n payload: filteredComputedIRs.slice(0, this.numCaptures),\r\n mls,\r\n lowHz,\r\n highHz,\r\n iirLength,\r\n componentIRGains,\r\n componentIRFreqs,\r\n num_periods,\r\n sampleRate: this.sourceSamplingRate || 96000,\r\n })\r\n .then(res => {\r\n console.log(res);\r\n this.stepNum += 1;\r\n console.log('got impulse response ' + this.stepNum);\r\n this.incrementStatusBar();\r\n this.status =\r\n `All Hz Calibration: done computing the IIR...`.toString() +\r\n this.generateTemplate().toString();\r\n this.emit('update', {message: this.status});\r\n this.componentInvertedImpulseResponse = res['iir'];\r\n this.componentIR['Gain'] = res['ir'];\r\n this.componentIR['Freq'] = res['frequencies'];\r\n this.componentConvolution = res['convolution'];\r\n })\r\n .catch(err => {\r\n // this.emit('InvertedImpulseResponse', {res: false});\r\n console.error(err);\r\n });\r\n };\r\n\r\n /** .\r\n * .\r\n * .\r\n * Sends the recorded signal, or a given csv string of a signal, to the back end server for processing\r\n *\r\n * @param {<array>String} signalCsv - Optional csv string of a previously recorded signal, if given, this signal will be processed\r\n * @example\r\n */\r\n sendRecordingToServerForProcessing = signalCsv => {\r\n const allSignals = this.getAllRecordedSignals();\r\n const numSignals = allSignals.length;\r\n const mls = this.#mls;\r\n const payload =\r\n signalCsv && signalCsv.length > 0 ? (0,_utils__WEBPACK_IMPORTED_MODULE_1__.csvToArray)(signalCsv) : allSignals[numSignals - 1];\r\n console.log('sending rec');\r\n this.stepNum += 1;\r\n console.log('send rec ' + this.stepNum);\r\n this.status =\r\n `All Hz Calibration Step: computing the IR of the last recording...`.toString() +\r\n this.generateTemplate().toString();\r\n this.emit('update', {message: this.status});\r\n this.impulseResponses.push(\r\n this.pyServerAPI\r\n .getImpulseResponse({\r\n sampleRate: this.sourceSamplingRate || 96000,\r\n payload,\r\n mls,\r\n P: this.#P,\r\n numPeriods: this.numMLSPerCapture,\r\n })\r\n .then(res => {\r\n if (this.numSuccessfulCaptured < this.numCaptures) {\r\n this.numSuccessfulCaptured += 1;\r\n console.log('num succ capt: ' + this.numSuccessfulCaptured);\r\n this.stepNum += 1;\r\n console.log('got impulse response ' + this.stepNum);\r\n this.incrementStatusBar();\r\n this.status =\r\n `All Hz Calibration: ${this.numSuccessfulCaptured}/${this.numCaptures} IRs computed...`.toString() +\r\n this.generateTemplate().toString();\r\n this.emit('update', {\r\n message: this.status,\r\n });\r\n this.autocorrelations.push(res['autocorrelation']);\r\n return res['ir'];\r\n }\r\n })\r\n .catch(err => {\r\n console.error(err);\r\n })\r\n );\r\n };\r\n\r\n /**\r\n * Passed to the calibration steps function, awaits the desired amount of seconds to capture the desired number\r\n * of MLS periods defined in the constructor.\r\n *\r\n * @example\r\n */\r\n #awaitDesiredMLSLength = async () => {\r\n // seconds per MLS = P / SR\r\n // await N * P / SR\r\n this.stepNum += 1;\r\n console.log('await desired length ' + this.stepNum);\r\n this.status =\r\n `All Hz Calibration: sampling the calibration signal...`.toString() +\r\n `\\niteration ${this.stepNum}` +\r\n this.generateTemplate();\r\n this.emit('update', {\r\n message: this.status,\r\n });\r\n let time_to_wait = 0;\r\n if (this.mode === 'unfiltered') {\r\n time_to_wait = (this.#mls.length / this.sourceSamplingRate) * this.numMLSPerCapture;\r\n time_to_wait = time_to_wait * 1.1;\r\n } else if (this.mode === 'filtered') {\r\n time_to_wait =\r\n (this.#currentConvolution.length / this.sourceSamplingRate) *\r\n (this.numMLSPerCapture / (this.num_mls_to_skip + this.numMLSPerCapture));\r\n } else {\r\n throw new Error('Mode broke in awaitDesiredMLSLength');\r\n }\r\n\r\n await (0,_utils__WEBPACK_IMPORTED_MODULE_1__.sleep)(time_to_wait);\r\n };\r\n\r\n /** .\r\n * .\r\n * .\r\n * Passed to the calibration steps function, awaits the onset of the signal to ensure a steady state\r\n *\r\n * @example\r\n */\r\n #awaitSignalOnset = async () => {\r\n this.stepNum += 1;\r\n console.log('await signal onset ' + this.stepNum);\r\n this.status =\r\n `All Hz Calibration: waiting for the signal to stabilize...`.toString() +\r\n this.generateTemplate();\r\n this.emit('update', {\r\n message: this.status,\r\n });\r\n let number_of_bursts_to_skip = this.num_mls_to_skip;\r\n let time_to_sleep = 0;\r\n if (this.mode === 'unfiltered') {\r\n time_to_sleep = (this.#mls.length / this.sourceSamplingRate) * number_of_bursts_to_skip;\r\n } else if (this.mode === 'filtered') {\r\n console.log(this.#currentConvolution.length);\r\n time_to_sleep =\r\n (this.#currentConvolution.length / this.sourceSamplingRate) *\r\n (number_of_bursts_to_skip / (number_of_bursts_to_skip + this.numMLSPerCapture));\r\n } else {\r\n throw new Error('Mode broke in awaitSignalOnset');\r\n }\r\n await (0,_utils__WEBPACK_IMPORTED_MODULE_1__.sleep)(time_to_sleep);\r\n };\r\n\r\n /**\r\n * Called immediately after a recording is captured. Used to process the resulting signal\r\n * whether by sending the result to a server or by computing a result locally.\r\n *\r\n * @example\r\n */\r\n #afterMLSRecord = () => {\r\n console.log('after record');\r\n this.sendRecordingToServerForProcessing();\r\n };\r\n\r\n #afterMLSwIIRRecord = () => {\r\n if (this.numSuccessfulCaptured < 1) {\r\n this.numSuccessfulCaptured += 1;\r\n this.stepNum += 1;\r\n this.incrementStatusBar();\r\n console.log('after mls w iir record for some reason add numSucc capt ' + this.stepNum);\r\n this.status =\r\n `All Hz Calibration: ${this.numSuccessfulCaptured} recording of convolved MLS captured`.toString() +\r\n this.generateTemplate().toString();\r\n this.emit('update', {\r\n message: this.status,\r\n });\r\n }\r\n };\r\n\r\n /** .\r\n * .\r\n * .\r\n * Created an S Curver Buffer to taper the signal onset\r\n *\r\n * @param {*} length\r\n * @param {*} phase\r\n * @returns\r\n * @example\r\n */\r\n static createSCurveBuffer = (length, phase) => {\r\n const curve = new Float32Array(length);\r\n let i;\r\n for (i = 0; i < length; i += 1) {\r\n // scale the curve to be between 0-1\r\n curve[i] = Math.sin((Math.PI * i) / length - phase) / 2 + 0.5;\r\n }\r\n return curve;\r\n };\r\n\r\n static createInverseSCurveBuffer = (length, phase) => {\r\n const curve = new Float32Array(length);\r\n let i;\r\n let j = length - 1;\r\n for (i = 0; i < length; i += 1) {\r\n // scale the curve to be between 0-1\r\n curve[i] = Math.sin((Math.PI * j) / length - phase) / 2 + 0.5;\r\n j -= 1;\r\n }\r\n return curve;\r\n };\r\n\r\n /**\r\n * Construct a Calibration Node with the calibration parameters.\r\n *\r\n * @param dataBuffer\r\n * @private\r\n * @example\r\n */\r\n #createCalibrationNodeFromBuffer = dataBuffer => {\r\n console.log('databuffer');\r\n console.log(dataBuffer);\r\n if (!this.sourceAudioContext) {\r\n this.makeNewSourceAudioContext();\r\n }\r\n\r\n const buffer = this.sourceAudioContext.createBuffer(\r\n 1, // number of channels\r\n dataBuffer.length,\r\n this.sourceAudioContext.sampleRate // sample rate\r\n );\r\n\r\n const data = buffer.getChannelData(0); // get data\r\n // fill the buffer with our data\r\n try {\r\n for (let i = 0; i < dataBuffer.length; i += 1) {\r\n data[i] = dataBuffer[i] * 0.1;\r\n }\r\n } catch (error) {\r\n console.error(error);\r\n }\r\n\r\n this.sourceNode = this.sourceAudioContext.createBufferSource();\r\n\r\n this.sourceNode.buffer = buffer;\r\n if (this.mode === 'filtered') {\r\n this.sourceNode.loop = false;\r\n } else {\r\n this.sourceNode.loop = true;\r\n }\r\n\r\n this.sourceNode.connect(this.sourceAudioContext.destination);\r\n\r\n this.addCalibrationNode(this.sourceNode);\r\n };\r\n\r\n /**\r\n * Given a data buffer, creates the required calibration node\r\n *\r\n * @param {*} dataBufferArray\r\n * @example\r\n */\r\n #setCalibrationNodesFromBuffer = (dataBufferArray = [this.#mlsBufferView]) => {\r\n if (dataBufferArray.length === 1) {\r\n this.#createCalibrationNodeFromBuffer(dataBufferArray[0]);\r\n } else {\r\n throw new Error('The length of the data buffer array must be 1');\r\n }\r\n };\r\n\r\n /**\r\n * Creates an audio context and plays it for a few seconds.\r\n *\r\n * @private\r\n * @returns - Resolves when the audio is done playing.\r\n * @example\r\n */\r\n #playCalibrationAudio = () => {\r\n this.calibrationNodes[0].start(0);\r\n this.status = ``;\r\n if (this.mode === 'unfiltered') {\r\n this.#mls = this.calibrationNodes[0].buffer.getChannelData(0);\r\n console.log('play calibration audio ' + this.stepNum);\r\n this.status =\r\n `All Hz Calibration: playing the calibration tone...`.toString() +\r\n this.generateTemplate().toString();\r\n } else if (this.mode === 'filtered') {\r\n console.log('play convolved audio ' + this.stepNum);\r\n this.status =\r\n `All Hz Calibration: playing the convolved calibration tone...`.toString() +\r\n this.generateTemplate().toString();\r\n } else {\r\n throw new Error('Mode is incorrect');\r\n }\r\n this.emit('update', {message: this.status});\r\n this.stepNum += 1;\r\n console.log('sink sampling rate');\r\n console.log(this.sinkSamplingRate);\r\n console.log('source sampling rate');\r\n console.log(this.sourceSamplingRate);\r\n };\r\n\r\n /** .\r\n * .\r\n * .\r\n * Stops the audio with tapered offset\r\n *\r\n * @example\r\n */\r\n #stopCalibrationAudio = () => {\r\n this.calibrationNodes[0].stop(0);\r\n this.calibrationNodes = [];\r\n this.sourceNode.disconnect();\r\n this.stepNum += 1;\r\n console.log('stop calibration audio ' + this.stepNum);\r\n this.status =\r\n `All Hz Calibration: stopping the calibration tone...`.toString() +\r\n this.generateTemplate().toString();\r\n this.emit('update', {message: this.status});\r\n };\r\n\r\n playMLSwithIIR = async (stream, iir) => {\r\n this.mode = 'filtered';\r\n console.log('play mls with iir');\r\n this.invertedImpulseResponse = iir;\r\n\r\n await this.calibrationSteps(\r\n stream,\r\n this.#playCalibrationAudio, // play audio func (required)\r\n this.#createCalibrationNodeFromBuffer(this.#currentConvolution), // before play func\r\n this.#awaitSignalOnset, // before record\r\n () => this.numSuccessfulCaptured < 1,\r\n this.#awaitDesiredMLSLength, // during record\r\n this.#afterMLSwIIRRecord, // after record\r\n this.mode\r\n );\r\n };\r\n\r\n /**\r\n * Public method to start the calibration process. Objects intialized from webassembly allocate new memory\r\n * and must be manually freed. This function is responsible for intializing the MlsGenInterface,\r\n * and wrapping the calibration steps with a garbage collection safe gaurd.\r\n *\r\n * @public\r\n * @param stream - The stream of audio from the Listener.\r\n * @example\r\n */\r\n startCalibrationImpulseResponse = async stream => {\r\n let desired_time = this.desired_time_per_mls;\r\n\r\n console.log('MLS sequence should be of length: ' + this.sourceSamplingRate * desired_time);\r\n\r\n length = this.sourceSamplingRate * desired_time;\r\n //get mls here\r\n await this.pyServerAPI\r\n .getMLSWithRetry(length)\r\n .then(res => {\r\n console.log(res);\r\n this.#mlsBufferView = res['mls'];\r\n })\r\n .catch(err => {\r\n // this.emit('InvertedImpulseResponse', {res: false});\r\n console.error(err);\r\n });\r\n await this.calibrationSteps(\r\n stream,\r\n this.#playCalibrationAudio, // play audio func (required)\r\n this.#createCalibrationNodeFromBuffer(this.#mlsBufferView), // before play func\r\n this.#awaitSignalOnset, // before record\r\n () => this.numSuccessfulCaptured < this.numCaptures, // loop while true\r\n this.#awaitDesiredMLSLength, // during record\r\n this.#afterMLSRecord, // after record\r\n this.mode\r\n ),\r\n this.#stopCalibrationAudio();\r\n\r\n // at this stage we've captured all the required signals,\r\n // and have received IRs for each one\r\n // so let's send all the IRs to the server to be converted to a single IIR\r\n await this.sendSystemImpulseResponsesToServerForProcessing();\r\n await this.sendComponentImpulseResponsesToServerForProcessing();\r\n\r\n this.numSuccessfulCaptured = 0;\r\n\r\n let iir_ir_and_plots;\r\n if (this._calibrateSoundCheck != 'none') {\r\n if (this._calibrateSoundCheck != 'system') {\r\n this.#currentConvolution = this.componentConvolution;\r\n } else {\r\n this.#currentConvolution = this.systemConvolution;\r\n }\r\n await this.playMLSwithIIR(stream, this.invertedImpulseResponse);\r\n this.#stopCalibrationAudio();\r\n this.sourceAudioContext.close();\r\n let conv_recs = this.getAllFilteredRecordedSignals();\r\n let recs = this.getAllRecordedSignals();\r\n let unconv_rec = recs[0];\r\n let conv_rec = conv_recs[0];\r\n if (this._calibrateSoundCheck != 'system') {\r\n let knownGain = this.oldComponentIR.Gain;\r\n let knownFreq = this.oldComponentIR.Freq;\r\n let sampleRate = this.sourceSamplingRate || 96000;\r\n let unconv_results = await this.pyServerAPI\r\n .getSubtractedPSDWithRetry(unconv_rec, knownGain, knownFreq, sampleRate)\r\n .then(res => {\r\n this.incrementStatusBar();\r\n this.status =\r\n `All Hz Calibration: done computing the PSD graphs...`.toString() +\r\n this.generateTemplate().toString();\r\n this.emit('update', {message: this.status});\r\n return res;\r\n })\r\n .catch(err => {\r\n console.error(err);\r\n });\r\n\r\n let conv_results = await this.pyServerAPI\r\n .getSubtractedPSDWithRetry(conv_rec, knownGain, knownFreq, sampleRate)\r\n .then(res => {\r\n this.incrementStatusBar();\r\n this.status =\r\n `All Hz Calibration: done computing the PSD graphs...`.toString() +\r\n this.generateTemplate().toString();\r\n this.emit('update', {message: this.status});\r\n return res;\r\n })\r\n .catch(err => {\r\n console.error(err);\r\n });\r\n iir_ir_and_plots = {\r\n systemIIR: this.systemInvertedImpulseResponse,\r\n componentIIR: this.componentInvertedImpulseResponse,\r\n x_unconv: unconv_results['x'],\r\n y_unconv: unconv_results['y'],\r\n x_conv: conv_results['x'],\r\n y_conv: conv_results['y'],\r\n componentIR: this.componentIR,\r\n systemIR: this.systemIR,\r\n };\r\n } else {\r\n let results = await this.pyServerAPI\r\n .getPSDWithRetry({\r\n unconv_rec,\r\n conv_rec,\r\n sampleRate: this.sourceSamplingRate || 96000,\r\n })\r\n .then(res => {\r\n this.incrementStatusBar();\r\n this.status =\r\n `All Hz Calibration: done computing the PSD graphs...`.toString() +\r\n this.generateTemplate().toString();\r\n this.emit('update', {message: this.status});\r\n return res;\r\n })\r\n .catch(err => {\r\n console.error(err);\r\n });\r\n iir_ir_and_plots = {\r\n systemIIR: this.systemInvertedImpulseResponse,\r\n componentIIR: this.componentInvertedImpulseResponse,\r\n x_unconv: results['x_unconv'],\r\n y_unconv: results['y_unconv'],\r\n x_conv: results['x_conv'],\r\n y_conv: results['y_conv'],\r\n componentIR: this.componentIR,\r\n systemIR: this.systemIR,\r\n };\r\n }\r\n\r\n if (this.#download) {\r\n this.downloadSingleUnfilteredRecording();\r\n this.downloadSingleFilteredRecording();\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(this.#mls, 'MLS.csv');\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(this.componentConvolution, 'python_component_convolution_mls_iir.csv');\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(this.systemConvolution, 'python_system_convolution_mls_iir.csv');\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(this.componentInvertedImpulseResponse, 'componentIIR.csv');\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(this.systemInvertedImpulseResponse, 'systemIIR.csv');\r\n for (let i = 0; i < this.autocorrelations.length; i++) {\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(this.autocorrelations[i], `autocorrelation_${i}`);\r\n }\r\n const computedIRagain = await Promise.all(this.impulseResponses).then(res => {\r\n for (let i = 0; i < res.length; i++) {\r\n if (res[i] != undefined) {\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(res[i], `IR_${i}`);\r\n }\r\n }\r\n });\r\n }\r\n } else {\r\n iir_ir_and_plots = {\r\n systemIIR: this.systemInvertedImpulseResponse,\r\n componentIIR: this.componentInvertedImpulseResponse,\r\n x_unconv: [],\r\n y_unconv: [],\r\n x_conv: [],\r\n y_conv: [],\r\n componentIR: this.componentIR,\r\n systemIR: this.systemIR,\r\n };\r\n if (this.#download) {\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(this.#mls, 'MLS.csv');\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(this.componentConvolution, 'python_component_convolution_mls_iir.csv');\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(this.systemConvolution, 'python_system_convolution_mls_iir.csv');\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(this.componentInvertedImpulseResponse, 'componentIIR.csv');\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(this.systemInvertedImpulseResponse, 'systemIIR.csv');\r\n for (let i = 0; i < this.autocorrelations.length; i++) {\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(this.autocorrelations[i], `autocorrelation_${i}`);\r\n }\r\n const computedIRagain = await Promise.all(this.impulseResponses).then(res => {\r\n for (let i = 0; i < res.length; i++) {\r\n if (res[i] != undefined) {\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(res[i], `IR_${i}`);\r\n }\r\n }\r\n });\r\n }\r\n }\r\n\r\n this.percent_complete = 100;\r\n\r\n this.status = `All Hz Calibration: Finished`.toString() + this.generateTemplate().toString();\r\n this.emit('update', {message: this.status});\r\n\r\n //here after calibration we have the component calibration (either loudspeaker or microphone) in the same form as the componentIR\r\n //that was used to calibrate\r\n\r\n return iir_ir_and_plots;\r\n };\r\n\r\n //////////////////////volume\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 createSCurveBuffer = (onSetBool = true) => {\r\n const curve = new Float32Array(this.TAPER_SECS * this.sourceSamplingRate + 1);\r\n const frequency = 1 / (4 * this.TAPER_SECS);\r\n let j = 0;\r\n for (let i = 0; i < this.TAPER_SECS * this.sourceSamplingRate + 1; i += 1) {\r\n const phase = 2 * Math.PI * frequency * j;\r\n const onsetTaper = Math.pow(Math.sin(phase), 2);\r\n const offsetTaper = Math.pow(Math.cos(phase), 2);\r\n curve[i] = onSetBool ? onsetTaper : offsetTaper;\r\n j += 1 / this.sourceSamplingRate;\r\n }\r\n return curve;\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 const taperGainNode = audioContext.createGain();\r\n const offsetGainNode = audioContext.createGain();\r\n const totalDuration = this.CALIBRATION_TONE_DURATION * 1.2;\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(taperGainNode);\r\n const onsetCurve = this.createSCurveBuffer();\r\n taperGainNode.gain.setValueCurveAtTime(onsetCurve, 0, this.TAPER_SECS);\r\n taperGainNode.connect(offsetGainNode);\r\n const offsetCurve = this.createSCurveBuffer(false);\r\n offsetGainNode.gain.setValueCurveAtTime(\r\n offsetCurve,\r\n totalDuration - this.TAPER_SECS,\r\n this.TAPER_SECS\r\n );\r\n offsetGainNode.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 #playCalibrationAudioVolume = 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_1__.sleep)(totalDuration);\r\n };\r\n\r\n #sendToServerForProcessing = lCalib => {\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 lCalib: lCalib,\r\n })\r\n .then(res => {\r\n if (this.outDBSPL === null) {\r\n this.incrementStatusBar();\r\n this.outDBSPL = res['outDbSPL'];\r\n this.outDBSPL1000 = res['outDbSPL1000'];\r\n this.THD = res['thd'];\r\n }\r\n })\r\n .catch(err => {\r\n console.warn(err);\r\n });\r\n };\r\n\r\n startCalibrationVolume = async (stream, gainValues, lCalib, componentGainDBSPL) => {\r\n const trialIterations = gainValues.length;\r\n this.status_denominator += trialIterations;\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 // do one calibration that will be discarded\r\n const soundLevelToDiscard = -60;\r\n const gainToDiscard = Math.pow(10, soundLevelToDiscard / 20);\r\n this.status =\r\n `1000 Hz Calibration: Sound Level ${soundLevelToDiscard} dB`.toString() +\r\n this.generateTemplate().toString();\r\n //this.emit('update', {message: `1000 Hz Calibration: Sound Level ${soundLevelToDiscard} dB`});\r\n this.emit('update', {message: this.status});\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.#playCalibrationAudioVolume,\r\n this.#createCalibrationToneWithGainValue,\r\n this.#sendToServerForProcessing,\r\n gainToDiscard,\r\n lCalib //todo make this a class parameter\r\n );\r\n } while (this.outDBSPL === null);\r\n //reset the values\r\n //this.incrementStatusBar();\r\n\r\n this.outDBSPL = null;\r\n this.outDBSPL = null;\r\n this.outDBSPL1000 = null;\r\n this.THD = null;\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 // precision to 1 decimal place\r\n inDB = Math.round(inDB * 10) / 10;\r\n inDBValues.push(inDB);\r\n console.log('next update');\r\n this.status =\r\n `1000 Hz Calibration: Sound Level ${inDB} dB`.toString() +\r\n this.generateTemplate().toString();\r\n this.emit('update', {message: this.status});\r\n do {\r\n // eslint-disable-next-line no-await-in-loop\r\n await this.volumeCalibrationSteps(\r\n stream,\r\n this.#playCalibrationAudioVolume,\r\n this.#createCalibrationToneWithGainValue,\r\n this.#sendToServerForProcessing,\r\n gainValues[i],\r\n lCalib //todo make this a class parameter\r\n );\r\n } while (this.outDBSPL === null);\r\n outDBSPL1000Values.push(this.outDBSPL1000);\r\n thdValues.push(this.THD);\r\n outDBSPLValues.push(this.outDBSPL);\r\n\r\n this.outDBSPL = null;\r\n this.outDBSPL1000 = null;\r\n this.THD = 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({\r\n inDBValues: inDBValues,\r\n outDBSPLValues: outDBSPL1000Values,\r\n lCalib: lCalib,\r\n componentGainDBSPL,\r\n })\r\n .then(res => {\r\n this.incrementStatusBar();\r\n return res;\r\n });\r\n const result = {\r\n parameters: parameters,\r\n inDBValues: inDBValues,\r\n outDBSPLValues: outDBSPLValues,\r\n outDBSPL1000Values: outDBSPL1000Values,\r\n thdValues: thdValues,\r\n };\r\n\r\n return result;\r\n };\r\n\r\n // function to write frq and gain to firebase database given speakerID\r\n writeFrqGain = async (speakerID, frq, gain, OEM) => {\r\n // freq and gain are too large to take samples 1 in every 100 samples\r\n\r\n const sampledFrq = [];\r\n const sampledGain = [];\r\n for (let i = 0; i < frq.length; i += 100) {\r\n sampledFrq.push(frq[i]);\r\n sampledGain.push(gain[i]);\r\n }\r\n\r\n const data = {Freq: sampledFrq, Gain: sampledGain};\r\n\r\n await (0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.set)((0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.ref)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"], `Microphone2/${OEM}/${speakerID}/linear`), data);\r\n };\r\n\r\n // Function to Read frq and gain from firebase database given speakerID\r\n // returns an array of frq and gain if speakerID exists, returns null otherwise\r\n\r\n readFrqGain = async (speakerID, OEM) => {\r\n const dbRef = (0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.ref)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"]);\r\n const snapshot = await (0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.get)((0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.child)(dbRef, `Microphone2/${OEM}/${speakerID}/linear`));\r\n if (snapshot.exists()) {\r\n return snapshot.val();\r\n }\r\n return null;\r\n };\r\n\r\n readGainat1000Hz = async (speakerID, OEM) => {\r\n const dbRef = (0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.ref)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"]);\r\n const snapshot = await (0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.get)((0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.child)(dbRef, `Microphone2/${OEM}/${speakerID}/Gain1000`));\r\n if (snapshot.exists()) {\r\n return snapshot.val();\r\n }\r\n return null;\r\n };\r\n\r\n writeGainat1000Hz = async (speakerID, gain, OEM) => {\r\n const data = {Gain: gain};\r\n await (0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.set)((0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.ref)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"], `Microphone2/${OEM}/${speakerID}/Gain1000`), gain);\r\n };\r\n\r\n writeIsSmartPhone = async (speakerID, isSmartPhone, OEM) => {\r\n const data = {isSmartPhone: isSmartPhone};\r\n await (0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.set)((0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.ref)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"], `Microphone2/${OEM}/${speakerID}/isSmartPhone`), isSmartPhone);\r\n };\r\n\r\n doesMicrophoneExist = async (speakerID, OEM) => {\r\n const dbRef = (0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.ref)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"]);\r\n const snapshot = await (0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.get)((0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.child)(dbRef, `Microphone2/${OEM}/${speakerID}`));\r\n if (snapshot.exists()) {\r\n return true;\r\n }\r\n return false;\r\n };\r\n\r\n convertToDB = gain => {\r\n return Math.log10(gain) * 20;\r\n };\r\n\r\n // Function to perform linear interpolation between two points\r\n interpolate(x, x0, y0, x1, y1) {\r\n return y0 + ((x - x0) * (y1 - y0)) / (x1 - x0);\r\n }\r\n\r\n findGainatFrequency = (frequencies, gains, targetFrequency) => {\r\n // Find the index of the first frequency in the array greater than the target frequency\r\n let index = 0;\r\n while (index < frequencies.length && frequencies[index] < targetFrequency) {\r\n index++;\r\n }\r\n\r\n // Handle cases when the target frequency is outside the range of the given data\r\n if (index === 0) {\r\n return gains[0];\r\n } else if (index === frequencies.length) {\r\n return gains[gains.length - 1];\r\n } else {\r\n // Interpolate the gain based on the surrounding frequencies\r\n const x0 = frequencies[index - 1];\r\n const y0 = gains[index - 1];\r\n const x1 = frequencies[index];\r\n const y1 = gains[index];\r\n return this.interpolate(targetFrequency, x0, y0, x1, y1);\r\n }\r\n };\r\n\r\n // Example of how to use the writeFrqGain and readFrqGain functions\r\n // writeFrqGain('speaker1', [1, 2, 3], [4, 5, 6]);\r\n // Speaker1 is the speakerID you want to write to in the database\r\n // readFrqGain('MiniDSPUMIK_1').then(data => console.log(data));\r\n // MiniDSPUMIK_1 is the speakerID with some Data in the database\r\n //adding gainDBSPL\r\n startCalibration = async (\r\n stream,\r\n gainValues,\r\n lCalib = 104.92978421490648,\r\n componentIR = null,\r\n microphoneName = 'MiniDSP-UMIK1-711-4754-vertical',\r\n _calibrateSoundCheck = 'goal', //GOAL PASSed in by default\r\n isSmartPhone = false,\r\n _calibrateSoundBurstRepeats = 4,\r\n _calibrateSoundBurstSec = 1,\r\n _calibrateSoundBurstsWarmup = 1,\r\n _calibrateSoundHz = 48000,\r\n _calibrateSoundIIRSec = 0.2,\r\n calibrateSound1000HzSec = 5,\r\n micManufacturer = '',\r\n micSerialNumber = '',\r\n micModelNumber = '',\r\n micModelName = ''\r\n ) => {\r\n this.CALIBRATION_TONE_DURATION = calibrateSound1000HzSec;\r\n this.iirLength = Math.floor(_calibrateSoundIIRSec * this.sourceSamplingRate);\r\n console.log('device info:', this.deviceInfo);\r\n this.numMLSPerCapture = _calibrateSoundBurstRepeats;\r\n this.desired_time_per_mls = _calibrateSoundBurstSec;\r\n this.num_mls_to_skip = _calibrateSoundBurstsWarmup;\r\n this.desired_sampling_rate = _calibrateSoundHz;\r\n\r\n //feed calibration goal here\r\n this._calibrateSoundCheck = _calibrateSoundCheck;\r\n //check if a componentIR was given to the system, if it isn't check for the microphone. using dummy data here bc we need to\r\n //check the db based on the microphone currently connected\r\n\r\n //new lCalib found at top of calibration files *1000hz, make sure to correct\r\n //based on zeroing of 1000hz, search for \"*1000Hz\"\r\n const ID = isSmartPhone ? micModelNumber : micSerialNumber;\r\n const OEM = isSmartPhone ? this.deviceInfo.OEM : micManufacturer;\r\n if (componentIR == null) {\r\n //mode 'ir'\r\n //global variable this.componentIR must be set\r\n this.componentIR = await this.readFrqGain(ID, OEM).then(data => {\r\n return data;\r\n });\r\n\r\n lCalib = await this.readGainat1000Hz(ID, OEM);\r\n // this.componentGainDBSPL = this.convertToDB(lCalib);\r\n this.componentGainDBSPL = lCalib;\r\n //TODO: if this call to database is unknown, cannot perform experiment => return false\r\n if (this.componentIR == null) {\r\n this.status =\r\n `Microphone (${OEM},${ID}) is not found in the database. Please add it to the database.`.toString();\r\n this.emit('update', {message: this.status});\r\n return false;\r\n }\r\n } else {\r\n this.componentIR = componentIR;\r\n lCalib = this.findGainatFrequency(this.componentIR.Freq, this.componentIR.Gain, 1000);\r\n // this.componentGainDBSPL = this.convertToDB(lCalib);\r\n this.componentGainDBSPL = lCalib;\r\n await this.writeGainat1000Hz(ID, lCalib, OEM);\r\n await this.writeIsSmartPhone(ID, isSmartPhone, OEM);\r\n }\r\n\r\n this.oldComponentIR = this.componentIR;\r\n\r\n let volumeResults = await this.startCalibrationVolume(\r\n stream,\r\n gainValues,\r\n lCalib,\r\n this.componentGainDBSPL\r\n );\r\n\r\n let impulseResponseResults = await this.startCalibrationImpulseResponse(stream);\r\n\r\n if (componentIR != null) {\r\n //insert Freq and Gain from this.componentIR into db\r\n await this.writeFrqGain(\r\n ID,\r\n impulseResponseResults.componentIR.Freq,\r\n impulseResponseResults.componentIR.Gain,\r\n OEM\r\n );\r\n }\r\n\r\n const total_results = {...volumeResults, ...impulseResponseResults};\r\n\r\n total_results['micInfo'] = {\r\n micManufacturer: micManufacturer,\r\n micSerialNumber: micSerialNumber,\r\n micModelNumber: micModelNumber,\r\n micModelName: micModelName,\r\n ID: ID,\r\n OEM: OEM,\r\n };\r\n console.log('total results');\r\n console.log(total_results);\r\n return total_results;\r\n };\r\n}\r\n\r\n/* harmony default export */ __webpack_exports__[\"default\"] = (Combination);\r\n\n\n//# sourceURL=webpack://speakerCalibrator/./src/tasks/combination/combination.js?");
843
+ 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 _utils__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../utils */ \"./src/utils.js\");\n/* harmony import */ var _config_firebase__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../config/firebase */ \"./src/config/firebase.js\");\n/* harmony import */ var firebase_database__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! firebase/database */ \"./node_modules/firebase/database/dist/esm/index.esm.js\");\n\r\n\r\n\r\n\r\n\r\n\r\n/**\r\n *\r\n */\r\nclass Combination extends _audioCalibrator__WEBPACK_IMPORTED_MODULE_0__[\"default\"] {\r\n /**\r\n * Default constructor. Creates an instance with any number of paramters passed or the default parameters defined here.\r\n *\r\n * @param {Object<boolean, number, number, number>} calibratorParams - paramter object\r\n * @param {boolean} [calibratorParams.download = false] - boolean flag to download captures\r\n * @param {number} [calibratorParams.mlsOrder = 18] - order of the MLS to be generated\r\n * @param {number} [calibratorParams.numCaptures = 5] - number of captures to perform\r\n * @param {number} [calibratorParams.numMLSPerCapture = 4] - number of bursts of MLS per capture\r\n */\r\n constructor({\r\n download = false,\r\n mlsOrder = 18,\r\n numCaptures = 3,\r\n numMLSPerCapture = 4,\r\n lowHz = 20,\r\n highHz = 10000,\r\n }) {\r\n super(numCaptures, numMLSPerCapture);\r\n this.#mlsOrder = parseInt(mlsOrder, 10);\r\n this.#P = 2 ** mlsOrder - 1;\r\n this.#download = download;\r\n this.#mls = [];\r\n this.#lowHz = lowHz;\r\n this.#highHz = highHz;\r\n }\r\n\r\n /** @private */\r\n stepNum = 0;\r\n\r\n /** @private */\r\n totalSteps = 25;\r\n\r\n /** @private */\r\n #download;\r\n\r\n /** @private */\r\n #mlsGenInterface;\r\n\r\n /** @private */\r\n #mlsBufferView;\r\n\r\n /** @private */\r\n componentInvertedImpulseResponse = null;\r\n\r\n /** @private */\r\n systemInvertedImpulseResponse = null;\r\n\r\n //averaged and subtracted ir returned from calibration used to calculated iir\r\n /** @private */\r\n ir = null;\r\n\r\n /** @private */\r\n impulseResponses = [];\r\n\r\n /** @private */\r\n #mlsOrder;\r\n\r\n /** @private */\r\n #lowHz;\r\n\r\n /** @private */\r\n #highHz;\r\n\r\n /** @private */\r\n #mls;\r\n\r\n /** @private */\r\n #P;\r\n\r\n /** @private */\r\n #audioContext;\r\n\r\n /** @private */\r\n TAPER_SECS = 5;\r\n\r\n /** @private */\r\n offsetGainNode;\r\n\r\n /** @private */\r\n componentConvolution;\r\n\r\n /** @private */\r\n systemConvolution;\r\n\r\n ////////////////////////volume\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 CALIBRATION_TONE_DURATION = 5; // seconds\r\n\r\n /** @private */\r\n outDBSPL = null;\r\n THD = null;\r\n outDBSPL1000 = null;\r\n\r\n /** @private */\r\n TAPER_SECS = 0.01; // seconds\r\n\r\n /** @private */\r\n status_denominator = 8;\r\n\r\n /** @private */\r\n status_numerator = 0;\r\n\r\n /** @private */\r\n percent_complete = 0;\r\n\r\n /** @private */\r\n status = ``;\r\n\r\n /**@private */\r\n status_literal = `<div style=\"display: flex; justify-content: center;\"><div style=\"width: 200px; height: 20px; border: 2px solid #000; border-radius: 10px;\"><div style=\"width: ${this.percent_complete}%; height: 100%; background-color: #00aaff; border-radius: 8px;\"></div></div></div>`;\r\n\r\n /**@private */\r\n componentIR = null;\r\n\r\n /**@private */\r\n oldComponentIR = null;\r\n\r\n /**@private */\r\n systemIR = null;\r\n\r\n /**@private */\r\n _calibrateSoundCheck = '';\r\n\r\n deviceType = null;\r\n\r\n deviceName = null;\r\n\r\n deviceInfo = null;\r\n\r\n desired_time_per_mls = 0;\r\n\r\n num_mls_to_skip = 0;\r\n\r\n desired_sampling_rate = 0;\r\n\r\n #currentConvolution = [];\r\n\r\n mode = 'unfiltered';\r\n\r\n sourceNode;\r\n\r\n autocorrelations = [];\r\n\r\n iirLength = 0;\r\n\r\n /**generate string template that gets reevaluated as variable increases */\r\n generateTemplate = () => {\r\n if (this.percent_complete > 100) {\r\n this.percent_complete = 100;\r\n }\r\n const template = `<div style=\"display: flex; justify-content: center;\"><div style=\"width: 800px; height: 20px; border: 2px solid #000; border-radius: 10px;\"><div style=\"width: ${this.percent_complete}%; height: 100%; background-color: #00aaff; border-radius: 8px;\"></div></div></div>`;\r\n return template;\r\n };\r\n\r\n /** increment numerator and percent for status bar */\r\n incrementStatusBar = () => {\r\n this.status_numerator += 1;\r\n this.percent_complete = (this.status_numerator / this.status_denominator) * 100;\r\n };\r\n\r\n setDeviceType = deviceType => {\r\n this.deviceType = deviceType;\r\n };\r\n\r\n setDeviceName = deviceName => {\r\n this.deviceName = deviceName;\r\n };\r\n\r\n setDeviceInfo = deviceInfo => {\r\n this.deviceInfo = deviceInfo;\r\n };\r\n\r\n /** .\r\n * .\r\n * .\r\n * Sends all the computed impulse responses to the backend server for processing\r\n *\r\n * @returns sets the resulting inverted impulse response to the class property\r\n * @example\r\n */\r\n sendSystemImpulseResponsesToServerForProcessing = async () => {\r\n const computedIRs = await Promise.all(this.impulseResponses);\r\n const filteredComputedIRs = computedIRs.filter(element => {\r\n return element != undefined;\r\n });\r\n const mls = this.#mls;\r\n const lowHz = this.#lowHz;\r\n const highHz = this.#highHz;\r\n const iirLength = this.iirLength;\r\n const num_periods = this.numMLSPerCapture + this.num_mls_to_skip;\r\n this.stepNum += 1;\r\n console.log('send impulse responses to server: ' + this.stepNum);\r\n this.status =\r\n `All Hz Calibration: computing the IIR...`.toString() + this.generateTemplate().toString();\r\n this.emit('update', {message: this.status});\r\n return this.pyServerAPI\r\n .getSystemInverseImpulseResponseWithRetry({\r\n payload: filteredComputedIRs.slice(0, this.numCaptures),\r\n mls,\r\n lowHz,\r\n highHz,\r\n iirLength,\r\n num_periods,\r\n sampleRate: this.sourceSamplingRate || 96000,\r\n })\r\n .then(res => {\r\n console.log(res);\r\n this.stepNum += 1;\r\n console.log('got impulse response ' + this.stepNum);\r\n this.incrementStatusBar();\r\n this.status =\r\n `All Hz Calibration: done computing the IIR...`.toString() +\r\n this.generateTemplate().toString();\r\n this.emit('update', {message: this.status});\r\n this.systemInvertedImpulseResponse = res['iir'];\r\n this.systemIR = res['ir'];\r\n this.systemConvolution = res['convolution'];\r\n })\r\n .catch(err => {\r\n console.error(err);\r\n });\r\n };\r\n\r\n /** .\r\n * .\r\n * .\r\n * Sends all the computed impulse responses to the backend server for processing\r\n *\r\n * @returns sets the resulting inverted impulse response to the class property\r\n * @example\r\n */\r\n sendComponentImpulseResponsesToServerForProcessing = async () => {\r\n const computedIRs = await Promise.all(this.impulseResponses);\r\n const filteredComputedIRs = computedIRs.filter(element => {\r\n return element != undefined;\r\n });\r\n const componentIRGains = this.componentIR['Gain'];\r\n const componentIRFreqs = this.componentIR['Freq'];\r\n const mls = this.#mls;\r\n const lowHz = this.#lowHz;\r\n const iirLength = this.iirLength;\r\n const num_periods = this.numMLSPerCapture + this.num_mls_to_skip;\r\n const highHz = this.#highHz;\r\n this.stepNum += 1;\r\n console.log('send impulse responses to server: ' + this.stepNum);\r\n this.status =\r\n `All Hz Calibration: computing the IIR...`.toString() + this.generateTemplate().toString();\r\n this.emit('update', {message: this.status});\r\n return this.pyServerAPI\r\n .getComponentInverseImpulseResponseWithRetry({\r\n payload: filteredComputedIRs.slice(0, this.numCaptures),\r\n mls,\r\n lowHz,\r\n highHz,\r\n iirLength,\r\n componentIRGains,\r\n componentIRFreqs,\r\n num_periods,\r\n sampleRate: this.sourceSamplingRate || 96000,\r\n })\r\n .then(res => {\r\n console.log(res);\r\n this.stepNum += 1;\r\n console.log('got impulse response ' + this.stepNum);\r\n this.incrementStatusBar();\r\n this.status =\r\n `All Hz Calibration: done computing the IIR...`.toString() +\r\n this.generateTemplate().toString();\r\n this.emit('update', {message: this.status});\r\n this.componentInvertedImpulseResponse = res['iir'];\r\n this.componentIR['Gain'] = res['ir'];\r\n this.componentIR['Freq'] = res['frequencies'];\r\n this.componentConvolution = res['convolution'];\r\n })\r\n .catch(err => {\r\n // this.emit('InvertedImpulseResponse', {res: false});\r\n console.error(err);\r\n });\r\n };\r\n\r\n /** .\r\n * .\r\n * .\r\n * Sends the recorded signal, or a given csv string of a signal, to the back end server for processing\r\n *\r\n * @param {<array>String} signalCsv - Optional csv string of a previously recorded signal, if given, this signal will be processed\r\n * @example\r\n */\r\n sendRecordingToServerForProcessing = signalCsv => {\r\n const allSignals = this.getAllRecordedSignals();\r\n const numSignals = allSignals.length;\r\n const mls = this.#mls;\r\n const payload =\r\n signalCsv && signalCsv.length > 0 ? (0,_utils__WEBPACK_IMPORTED_MODULE_1__.csvToArray)(signalCsv) : allSignals[numSignals - 1];\r\n console.log('sending rec');\r\n this.stepNum += 1;\r\n console.log('send rec ' + this.stepNum);\r\n this.status =\r\n `All Hz Calibration Step: computing the IR of the last recording...`.toString() +\r\n this.generateTemplate().toString();\r\n this.emit('update', {message: this.status});\r\n this.impulseResponses.push(\r\n this.pyServerAPI\r\n .getImpulseResponse({\r\n sampleRate: this.sourceSamplingRate || 96000,\r\n payload,\r\n mls,\r\n P: this.#P,\r\n numPeriods: this.numMLSPerCapture,\r\n })\r\n .then(res => {\r\n if (this.numSuccessfulCaptured < this.numCaptures) {\r\n this.numSuccessfulCaptured += 1;\r\n console.log('num succ capt: ' + this.numSuccessfulCaptured);\r\n this.stepNum += 1;\r\n console.log('got impulse response ' + this.stepNum);\r\n this.incrementStatusBar();\r\n this.status =\r\n `All Hz Calibration: ${this.numSuccessfulCaptured}/${this.numCaptures} IRs computed...`.toString() +\r\n this.generateTemplate().toString();\r\n this.emit('update', {\r\n message: this.status,\r\n });\r\n this.autocorrelations.push(res['autocorrelation']);\r\n return res['ir'];\r\n }\r\n })\r\n .catch(err => {\r\n console.error(err);\r\n })\r\n );\r\n };\r\n\r\n /**\r\n * Passed to the calibration steps function, awaits the desired amount of seconds to capture the desired number\r\n * of MLS periods defined in the constructor.\r\n *\r\n * @example\r\n */\r\n #awaitDesiredMLSLength = async () => {\r\n // seconds per MLS = P / SR\r\n // await N * P / SR\r\n this.stepNum += 1;\r\n console.log('await desired length ' + this.stepNum);\r\n this.status =\r\n `All Hz Calibration: sampling the calibration signal...`.toString() +\r\n `\\niteration ${this.stepNum}` +\r\n this.generateTemplate();\r\n this.emit('update', {\r\n message: this.status,\r\n });\r\n let time_to_wait = 0;\r\n if (this.mode === 'unfiltered') {\r\n time_to_wait = (this.#mls.length / this.sourceSamplingRate) * this.numMLSPerCapture;\r\n time_to_wait = time_to_wait * 1.1;\r\n } else if (this.mode === 'filtered') {\r\n time_to_wait =\r\n (this.#currentConvolution.length / this.sourceSamplingRate) *\r\n (this.numMLSPerCapture / (this.num_mls_to_skip + this.numMLSPerCapture));\r\n } else {\r\n throw new Error('Mode broke in awaitDesiredMLSLength');\r\n }\r\n\r\n await (0,_utils__WEBPACK_IMPORTED_MODULE_1__.sleep)(time_to_wait);\r\n };\r\n\r\n /** .\r\n * .\r\n * .\r\n * Passed to the calibration steps function, awaits the onset of the signal to ensure a steady state\r\n *\r\n * @example\r\n */\r\n #awaitSignalOnset = async () => {\r\n this.stepNum += 1;\r\n console.log('await signal onset ' + this.stepNum);\r\n this.status =\r\n `All Hz Calibration: waiting for the signal to stabilize...`.toString() +\r\n this.generateTemplate();\r\n this.emit('update', {\r\n message: this.status,\r\n });\r\n let number_of_bursts_to_skip = this.num_mls_to_skip;\r\n let time_to_sleep = 0;\r\n if (this.mode === 'unfiltered') {\r\n time_to_sleep = (this.#mls.length / this.sourceSamplingRate) * number_of_bursts_to_skip;\r\n } else if (this.mode === 'filtered') {\r\n console.log(this.#currentConvolution.length);\r\n time_to_sleep =\r\n (this.#currentConvolution.length / this.sourceSamplingRate) *\r\n (number_of_bursts_to_skip / (number_of_bursts_to_skip + this.numMLSPerCapture));\r\n } else {\r\n throw new Error('Mode broke in awaitSignalOnset');\r\n }\r\n await (0,_utils__WEBPACK_IMPORTED_MODULE_1__.sleep)(time_to_sleep);\r\n };\r\n\r\n /**\r\n * Called immediately after a recording is captured. Used to process the resulting signal\r\n * whether by sending the result to a server or by computing a result locally.\r\n *\r\n * @example\r\n */\r\n #afterMLSRecord = () => {\r\n console.log('after record');\r\n this.sendRecordingToServerForProcessing();\r\n };\r\n\r\n #afterMLSwIIRRecord = () => {\r\n if (this.numSuccessfulCaptured < 1) {\r\n this.numSuccessfulCaptured += 1;\r\n this.stepNum += 1;\r\n this.incrementStatusBar();\r\n console.log('after mls w iir record for some reason add numSucc capt ' + this.stepNum);\r\n this.status =\r\n `All Hz Calibration: ${this.numSuccessfulCaptured} recording of convolved MLS captured`.toString() +\r\n this.generateTemplate().toString();\r\n this.emit('update', {\r\n message: this.status,\r\n });\r\n }\r\n };\r\n\r\n /** .\r\n * .\r\n * .\r\n * Created an S Curver Buffer to taper the signal onset\r\n *\r\n * @param {*} length\r\n * @param {*} phase\r\n * @returns\r\n * @example\r\n */\r\n static createSCurveBuffer = (length, phase) => {\r\n const curve = new Float32Array(length);\r\n let i;\r\n for (i = 0; i < length; i += 1) {\r\n // scale the curve to be between 0-1\r\n curve[i] = Math.sin((Math.PI * i) / length - phase) / 2 + 0.5;\r\n }\r\n return curve;\r\n };\r\n\r\n static createInverseSCurveBuffer = (length, phase) => {\r\n const curve = new Float32Array(length);\r\n let i;\r\n let j = length - 1;\r\n for (i = 0; i < length; i += 1) {\r\n // scale the curve to be between 0-1\r\n curve[i] = Math.sin((Math.PI * j) / length - phase) / 2 + 0.5;\r\n j -= 1;\r\n }\r\n return curve;\r\n };\r\n\r\n /**\r\n * Construct a Calibration Node with the calibration parameters.\r\n *\r\n * @param dataBuffer\r\n * @private\r\n * @example\r\n */\r\n #createCalibrationNodeFromBuffer = dataBuffer => {\r\n console.log('databuffer');\r\n console.log(dataBuffer);\r\n if (!this.sourceAudioContext) {\r\n this.makeNewSourceAudioContext();\r\n }\r\n\r\n const buffer = this.sourceAudioContext.createBuffer(\r\n 1, // number of channels\r\n dataBuffer.length,\r\n this.sourceAudioContext.sampleRate // sample rate\r\n );\r\n\r\n const data = buffer.getChannelData(0); // get data\r\n // fill the buffer with our data\r\n try {\r\n for (let i = 0; i < dataBuffer.length; i += 1) {\r\n data[i] = dataBuffer[i] * 0.1;\r\n }\r\n } catch (error) {\r\n console.error(error);\r\n }\r\n\r\n this.sourceNode = this.sourceAudioContext.createBufferSource();\r\n\r\n this.sourceNode.buffer = buffer;\r\n if (this.mode === 'filtered') {\r\n this.sourceNode.loop = false;\r\n } else {\r\n this.sourceNode.loop = true;\r\n }\r\n\r\n this.sourceNode.connect(this.sourceAudioContext.destination);\r\n\r\n this.addCalibrationNode(this.sourceNode);\r\n };\r\n\r\n /**\r\n * Given a data buffer, creates the required calibration node\r\n *\r\n * @param {*} dataBufferArray\r\n * @example\r\n */\r\n #setCalibrationNodesFromBuffer = (dataBufferArray = [this.#mlsBufferView]) => {\r\n if (dataBufferArray.length === 1) {\r\n this.#createCalibrationNodeFromBuffer(dataBufferArray[0]);\r\n } else {\r\n throw new Error('The length of the data buffer array must be 1');\r\n }\r\n };\r\n\r\n /**\r\n * Creates an audio context and plays it for a few seconds.\r\n *\r\n * @private\r\n * @returns - Resolves when the audio is done playing.\r\n * @example\r\n */\r\n #playCalibrationAudio = () => {\r\n this.calibrationNodes[0].start(0);\r\n this.status = ``;\r\n if (this.mode === 'unfiltered') {\r\n this.#mls = this.calibrationNodes[0].buffer.getChannelData(0);\r\n console.log('play calibration audio ' + this.stepNum);\r\n this.status =\r\n `All Hz Calibration: playing the calibration tone...`.toString() +\r\n this.generateTemplate().toString();\r\n } else if (this.mode === 'filtered') {\r\n console.log('play convolved audio ' + this.stepNum);\r\n this.status =\r\n `All Hz Calibration: playing the convolved calibration tone...`.toString() +\r\n this.generateTemplate().toString();\r\n } else {\r\n throw new Error('Mode is incorrect');\r\n }\r\n this.emit('update', {message: this.status});\r\n this.stepNum += 1;\r\n console.log('sink sampling rate');\r\n console.log(this.sinkSamplingRate);\r\n console.log('source sampling rate');\r\n console.log(this.sourceSamplingRate);\r\n };\r\n\r\n /** .\r\n * .\r\n * .\r\n * Stops the audio with tapered offset\r\n *\r\n * @example\r\n */\r\n #stopCalibrationAudio = () => {\r\n this.calibrationNodes[0].stop(0);\r\n this.calibrationNodes = [];\r\n this.sourceNode.disconnect();\r\n this.stepNum += 1;\r\n console.log('stop calibration audio ' + this.stepNum);\r\n this.status =\r\n `All Hz Calibration: stopping the calibration tone...`.toString() +\r\n this.generateTemplate().toString();\r\n this.emit('update', {message: this.status});\r\n };\r\n\r\n playMLSwithIIR = async (stream, iir) => {\r\n let checkRec = false;\r\n this.mode = 'filtered';\r\n console.log('play mls with iir');\r\n this.invertedImpulseResponse = iir;\r\n\r\n await this.calibrationSteps(\r\n stream,\r\n this.#playCalibrationAudio, // play audio func (required)\r\n this.#createCalibrationNodeFromBuffer(this.#currentConvolution), // before play func\r\n this.#awaitSignalOnset, // before record\r\n () => this.numSuccessfulCaptured < 1,\r\n this.#awaitDesiredMLSLength, // during record\r\n this.#afterMLSwIIRRecord, // after record\r\n this.mode,\r\n checkRec\r\n );\r\n };\r\n\r\n /**\r\n * Public method to start the calibration process. Objects intialized from webassembly allocate new memory\r\n * and must be manually freed. This function is responsible for intializing the MlsGenInterface,\r\n * and wrapping the calibration steps with a garbage collection safe gaurd.\r\n *\r\n * @public\r\n * @param stream - The stream of audio from the Listener.\r\n * @example\r\n */\r\n startCalibrationImpulseResponse = async stream => {\r\n let desired_time = this.desired_time_per_mls;\r\n let checkRec = 'allhz';\r\n\r\n console.log('MLS sequence should be of length: ' + this.sourceSamplingRate * desired_time);\r\n\r\n length = this.sourceSamplingRate * desired_time;\r\n //get mls here\r\n await this.pyServerAPI\r\n .getMLSWithRetry(length)\r\n .then(res => {\r\n console.log(res);\r\n this.#mlsBufferView = res['mls'];\r\n })\r\n .catch(err => {\r\n // this.emit('InvertedImpulseResponse', {res: false});\r\n console.error(err);\r\n });\r\n await this.calibrationSteps(\r\n stream,\r\n this.#playCalibrationAudio, // play audio func (required)\r\n this.#createCalibrationNodeFromBuffer(this.#mlsBufferView), // before play func\r\n this.#awaitSignalOnset, // before record\r\n () => this.numSuccessfulCaptured < this.numCaptures, // loop while true\r\n this.#awaitDesiredMLSLength, // during record\r\n this.#afterMLSRecord, // after record\r\n this.mode,\r\n checkRec\r\n ),\r\n this.#stopCalibrationAudio();\r\n checkRec = false;\r\n\r\n // at this stage we've captured all the required signals,\r\n // and have received IRs for each one\r\n // so let's send all the IRs to the server to be converted to a single IIR\r\n await this.sendSystemImpulseResponsesToServerForProcessing();\r\n await this.sendComponentImpulseResponsesToServerForProcessing();\r\n\r\n this.numSuccessfulCaptured = 0;\r\n\r\n let iir_ir_and_plots;\r\n if (this._calibrateSoundCheck != 'none') {\r\n if (this._calibrateSoundCheck != 'system') {\r\n this.#currentConvolution = this.componentConvolution;\r\n } else {\r\n this.#currentConvolution = this.systemConvolution;\r\n }\r\n await this.playMLSwithIIR(stream, this.invertedImpulseResponse);\r\n this.#stopCalibrationAudio();\r\n this.sourceAudioContext.close();\r\n let conv_recs = this.getAllFilteredRecordedSignals();\r\n let recs = this.getAllRecordedSignals();\r\n let unconv_rec = recs[0];\r\n let conv_rec = conv_recs[0];\r\n if (this._calibrateSoundCheck != 'system') {\r\n let knownGain = this.oldComponentIR.Gain;\r\n let knownFreq = this.oldComponentIR.Freq;\r\n let sampleRate = this.sourceSamplingRate || 96000;\r\n let unconv_results = await this.pyServerAPI\r\n .getSubtractedPSDWithRetry(unconv_rec, knownGain, knownFreq, sampleRate)\r\n .then(res => {\r\n this.incrementStatusBar();\r\n this.status =\r\n `All Hz Calibration: done computing the PSD graphs...`.toString() +\r\n this.generateTemplate().toString();\r\n this.emit('update', {message: this.status});\r\n return res;\r\n })\r\n .catch(err => {\r\n console.error(err);\r\n });\r\n\r\n let conv_results = await this.pyServerAPI\r\n .getSubtractedPSDWithRetry(conv_rec, knownGain, knownFreq, sampleRate)\r\n .then(res => {\r\n this.incrementStatusBar();\r\n this.status =\r\n `All Hz Calibration: done computing the PSD graphs...`.toString() +\r\n this.generateTemplate().toString();\r\n this.emit('update', {message: this.status});\r\n return res;\r\n })\r\n .catch(err => {\r\n console.error(err);\r\n });\r\n iir_ir_and_plots = {\r\n systemIIR: this.systemInvertedImpulseResponse,\r\n componentIIR: this.componentInvertedImpulseResponse,\r\n x_unconv: unconv_results['x'],\r\n y_unconv: unconv_results['y'],\r\n x_conv: conv_results['x'],\r\n y_conv: conv_results['y'],\r\n componentIR: this.componentIR,\r\n systemIR: this.systemIR,\r\n };\r\n } else {\r\n let results = await this.pyServerAPI\r\n .getPSDWithRetry({\r\n unconv_rec,\r\n conv_rec,\r\n sampleRate: this.sourceSamplingRate || 96000,\r\n })\r\n .then(res => {\r\n this.incrementStatusBar();\r\n this.status =\r\n `All Hz Calibration: done computing the PSD graphs...`.toString() +\r\n this.generateTemplate().toString();\r\n this.emit('update', {message: this.status});\r\n return res;\r\n })\r\n .catch(err => {\r\n console.error(err);\r\n });\r\n iir_ir_and_plots = {\r\n systemIIR: this.systemInvertedImpulseResponse,\r\n componentIIR: this.componentInvertedImpulseResponse,\r\n x_unconv: results['x_unconv'],\r\n y_unconv: results['y_unconv'],\r\n x_conv: results['x_conv'],\r\n y_conv: results['y_conv'],\r\n componentIR: this.componentIR,\r\n systemIR: this.systemIR,\r\n };\r\n }\r\n\r\n if (this.#download) {\r\n this.downloadSingleUnfilteredRecording();\r\n this.downloadSingleFilteredRecording();\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(this.#mls, 'MLS.csv');\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(this.componentConvolution, 'python_component_convolution_mls_iir.csv');\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(this.systemConvolution, 'python_system_convolution_mls_iir.csv');\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(this.componentInvertedImpulseResponse, 'componentIIR.csv');\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(this.systemInvertedImpulseResponse, 'systemIIR.csv');\r\n for (let i = 0; i < this.autocorrelations.length; i++) {\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(this.autocorrelations[i], `autocorrelation_${i}`);\r\n }\r\n const computedIRagain = await Promise.all(this.impulseResponses).then(res => {\r\n for (let i = 0; i < res.length; i++) {\r\n if (res[i] != undefined) {\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(res[i], `IR_${i}`);\r\n }\r\n }\r\n });\r\n }\r\n } else {\r\n iir_ir_and_plots = {\r\n systemIIR: this.systemInvertedImpulseResponse,\r\n componentIIR: this.componentInvertedImpulseResponse,\r\n x_unconv: [],\r\n y_unconv: [],\r\n x_conv: [],\r\n y_conv: [],\r\n componentIR: this.componentIR,\r\n systemIR: this.systemIR,\r\n };\r\n if (this.#download) {\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(this.#mls, 'MLS.csv');\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(this.componentConvolution, 'python_component_convolution_mls_iir.csv');\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(this.systemConvolution, 'python_system_convolution_mls_iir.csv');\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(this.componentInvertedImpulseResponse, 'componentIIR.csv');\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(this.systemInvertedImpulseResponse, 'systemIIR.csv');\r\n for (let i = 0; i < this.autocorrelations.length; i++) {\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(this.autocorrelations[i], `autocorrelation_${i}`);\r\n }\r\n const computedIRagain = await Promise.all(this.impulseResponses).then(res => {\r\n for (let i = 0; i < res.length; i++) {\r\n if (res[i] != undefined) {\r\n (0,_utils__WEBPACK_IMPORTED_MODULE_1__.saveToCSV)(res[i], `IR_${i}`);\r\n }\r\n }\r\n });\r\n }\r\n }\r\n\r\n this.percent_complete = 100;\r\n\r\n this.status = `All Hz Calibration: Finished`.toString() + this.generateTemplate().toString();\r\n this.emit('update', {message: this.status});\r\n\r\n //here after calibration we have the component calibration (either loudspeaker or microphone) in the same form as the componentIR\r\n //that was used to calibrate\r\n\r\n return iir_ir_and_plots;\r\n };\r\n\r\n //////////////////////volume\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 createSCurveBuffer = (onSetBool = true) => {\r\n const curve = new Float32Array(this.TAPER_SECS * this.sourceSamplingRate + 1);\r\n const frequency = 1 / (4 * this.TAPER_SECS);\r\n let j = 0;\r\n for (let i = 0; i < this.TAPER_SECS * this.sourceSamplingRate + 1; i += 1) {\r\n const phase = 2 * Math.PI * frequency * j;\r\n const onsetTaper = Math.pow(Math.sin(phase), 2);\r\n const offsetTaper = Math.pow(Math.cos(phase), 2);\r\n curve[i] = onSetBool ? onsetTaper : offsetTaper;\r\n j += 1 / this.sourceSamplingRate;\r\n }\r\n return curve;\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 const taperGainNode = audioContext.createGain();\r\n const offsetGainNode = audioContext.createGain();\r\n const totalDuration = this.CALIBRATION_TONE_DURATION * 1.2;\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(taperGainNode);\r\n const onsetCurve = this.createSCurveBuffer();\r\n taperGainNode.gain.setValueCurveAtTime(onsetCurve, 0, this.TAPER_SECS);\r\n taperGainNode.connect(offsetGainNode);\r\n const offsetCurve = this.createSCurveBuffer(false);\r\n offsetGainNode.gain.setValueCurveAtTime(\r\n offsetCurve,\r\n totalDuration - this.TAPER_SECS,\r\n this.TAPER_SECS\r\n );\r\n offsetGainNode.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 #playCalibrationAudioVolume = 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_1__.sleep)(totalDuration);\r\n };\r\n\r\n #sendToServerForProcessing = lCalib => {\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 lCalib: lCalib,\r\n })\r\n .then(res => {\r\n if (this.outDBSPL === null) {\r\n this.incrementStatusBar();\r\n this.outDBSPL = res['outDbSPL'];\r\n this.outDBSPL1000 = res['outDbSPL1000'];\r\n this.THD = res['thd'];\r\n }\r\n })\r\n .catch(err => {\r\n console.warn(err);\r\n });\r\n };\r\n\r\n startCalibrationVolume = async (stream, gainValues, lCalib, componentGainDBSPL) => {\r\n const trialIterations = gainValues.length;\r\n this.status_denominator += trialIterations;\r\n const thdValues = [];\r\n const inDBValues = [];\r\n let inDB = 0;\r\n const outDBSPLValues = [];\r\n const outDBSPL1000Values = [];\r\n let checkRec = false;\r\n\r\n // do one calibration that will be discarded\r\n const soundLevelToDiscard = -60;\r\n const gainToDiscard = Math.pow(10, soundLevelToDiscard / 20);\r\n this.status =\r\n `1000 Hz Calibration: Sound Level ${soundLevelToDiscard} dB`.toString() +\r\n this.generateTemplate().toString();\r\n //this.emit('update', {message: `1000 Hz Calibration: Sound Level ${soundLevelToDiscard} dB`});\r\n this.emit('update', {message: this.status});\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.#playCalibrationAudioVolume,\r\n this.#createCalibrationToneWithGainValue,\r\n this.#sendToServerForProcessing,\r\n gainToDiscard,\r\n lCalib, //todo make this a class parameter\r\n checkRec\r\n );\r\n } while (this.outDBSPL === null);\r\n //reset the values\r\n //this.incrementStatusBar();\r\n\r\n this.outDBSPL = null;\r\n this.outDBSPL = null;\r\n this.outDBSPL1000 = null;\r\n this.THD = null;\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 if (i == trialIterations - 1) {\r\n checkRec = 'loudest';\r\n }\r\n inDB = Math.log10(gainValues[i]) * 20;\r\n // precision to 1 decimal place\r\n inDB = Math.round(inDB * 10) / 10;\r\n inDBValues.push(inDB);\r\n console.log('next update');\r\n this.status =\r\n `1000 Hz Calibration: Sound Level ${inDB} dB`.toString() +\r\n this.generateTemplate().toString();\r\n this.emit('update', {message: this.status});\r\n do {\r\n // eslint-disable-next-line no-await-in-loop\r\n await this.volumeCalibrationSteps(\r\n stream,\r\n this.#playCalibrationAudioVolume,\r\n this.#createCalibrationToneWithGainValue,\r\n this.#sendToServerForProcessing,\r\n gainValues[i],\r\n lCalib, //todo make this a class parameter\r\n checkRec\r\n );\r\n } while (this.outDBSPL === null);\r\n outDBSPL1000Values.push(this.outDBSPL1000);\r\n thdValues.push(this.THD);\r\n outDBSPLValues.push(this.outDBSPL);\r\n\r\n this.outDBSPL = null;\r\n this.outDBSPL1000 = null;\r\n this.THD = 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({\r\n inDBValues: inDBValues,\r\n outDBSPLValues: outDBSPL1000Values,\r\n lCalib: lCalib,\r\n componentGainDBSPL,\r\n })\r\n .then(res => {\r\n this.incrementStatusBar();\r\n return res;\r\n });\r\n const result = {\r\n parameters: parameters,\r\n inDBValues: inDBValues,\r\n outDBSPLValues: outDBSPLValues,\r\n outDBSPL1000Values: outDBSPL1000Values,\r\n thdValues: thdValues,\r\n };\r\n\r\n return result;\r\n };\r\n\r\n // function to write frq and gain to firebase database given speakerID\r\n writeFrqGain = async (speakerID, frq, gain, OEM) => {\r\n // freq and gain are too large to take samples 1 in every 100 samples\r\n\r\n const sampledFrq = [];\r\n const sampledGain = [];\r\n for (let i = 0; i < frq.length; i += 100) {\r\n sampledFrq.push(frq[i]);\r\n sampledGain.push(gain[i]);\r\n }\r\n\r\n const data = {Freq: sampledFrq, Gain: sampledGain};\r\n\r\n await (0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.set)((0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.ref)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"], `Microphone2/${OEM}/${speakerID}/linear`), data);\r\n };\r\n\r\n // Function to Read frq and gain from firebase database given speakerID\r\n // returns an array of frq and gain if speakerID exists, returns null otherwise\r\n\r\n readFrqGain = async (speakerID, OEM) => {\r\n const dbRef = (0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.ref)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"]);\r\n const snapshot = await (0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.get)((0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.child)(dbRef, `Microphone2/${OEM}/${speakerID}/linear`));\r\n if (snapshot.exists()) {\r\n return snapshot.val();\r\n }\r\n return null;\r\n };\r\n\r\n readGainat1000Hz = async (speakerID, OEM) => {\r\n const dbRef = (0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.ref)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"]);\r\n const snapshot = await (0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.get)((0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.child)(dbRef, `Microphone2/${OEM}/${speakerID}/Gain1000`));\r\n if (snapshot.exists()) {\r\n return snapshot.val();\r\n }\r\n return null;\r\n };\r\n\r\n writeGainat1000Hz = async (speakerID, gain, OEM) => {\r\n const data = {Gain: gain};\r\n await (0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.set)((0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.ref)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"], `Microphone2/${OEM}/${speakerID}/Gain1000`), gain);\r\n };\r\n\r\n writeIsSmartPhone = async (speakerID, isSmartPhone, OEM) => {\r\n const data = {isSmartPhone: isSmartPhone};\r\n await (0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.set)((0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.ref)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"], `Microphone2/${OEM}/${speakerID}/isSmartPhone`), isSmartPhone);\r\n };\r\n\r\n doesMicrophoneExist = async (speakerID, OEM) => {\r\n const dbRef = (0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.ref)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"]);\r\n const snapshot = await (0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.get)((0,firebase_database__WEBPACK_IMPORTED_MODULE_3__.child)(dbRef, `Microphone2/${OEM}/${speakerID}`));\r\n if (snapshot.exists()) {\r\n return true;\r\n }\r\n return false;\r\n };\r\n\r\n convertToDB = gain => {\r\n return Math.log10(gain) * 20;\r\n };\r\n\r\n // Function to perform linear interpolation between two points\r\n interpolate(x, x0, y0, x1, y1) {\r\n return y0 + ((x - x0) * (y1 - y0)) / (x1 - x0);\r\n }\r\n\r\n findGainatFrequency = (frequencies, gains, targetFrequency) => {\r\n // Find the index of the first frequency in the array greater than the target frequency\r\n let index = 0;\r\n while (index < frequencies.length && frequencies[index] < targetFrequency) {\r\n index++;\r\n }\r\n\r\n // Handle cases when the target frequency is outside the range of the given data\r\n if (index === 0) {\r\n return gains[0];\r\n } else if (index === frequencies.length) {\r\n return gains[gains.length - 1];\r\n } else {\r\n // Interpolate the gain based on the surrounding frequencies\r\n const x0 = frequencies[index - 1];\r\n const y0 = gains[index - 1];\r\n const x1 = frequencies[index];\r\n const y1 = gains[index];\r\n return this.interpolate(targetFrequency, x0, y0, x1, y1);\r\n }\r\n };\r\n\r\n // Example of how to use the writeFrqGain and readFrqGain functions\r\n // writeFrqGain('speaker1', [1, 2, 3], [4, 5, 6]);\r\n // Speaker1 is the speakerID you want to write to in the database\r\n // readFrqGain('MiniDSPUMIK_1').then(data => console.log(data));\r\n // MiniDSPUMIK_1 is the speakerID with some Data in the database\r\n //adding gainDBSPL\r\n startCalibration = async (\r\n stream,\r\n gainValues,\r\n lCalib = 104.92978421490648,\r\n componentIR = null,\r\n microphoneName = 'MiniDSP-UMIK1-711-4754-vertical',\r\n _calibrateSoundCheck = 'goal', //GOAL PASSed in by default\r\n isSmartPhone = false,\r\n _calibrateSoundBurstRepeats = 4,\r\n _calibrateSoundBurstSec = 1,\r\n _calibrateSoundBurstsWarmup = 1,\r\n _calibrateSoundHz = 48000,\r\n _calibrateSoundIIRSec = 0.2,\r\n calibrateSound1000HzSec = 5,\r\n micManufacturer = '',\r\n micSerialNumber = '',\r\n micModelNumber = '',\r\n micModelName = ''\r\n ) => {\r\n this.CALIBRATION_TONE_DURATION = calibrateSound1000HzSec;\r\n this.iirLength = Math.floor(_calibrateSoundIIRSec * this.sourceSamplingRate);\r\n console.log('device info:', this.deviceInfo);\r\n this.numMLSPerCapture = _calibrateSoundBurstRepeats;\r\n this.desired_time_per_mls = _calibrateSoundBurstSec;\r\n this.num_mls_to_skip = _calibrateSoundBurstsWarmup;\r\n this.desired_sampling_rate = _calibrateSoundHz;\r\n\r\n //feed calibration goal here\r\n this._calibrateSoundCheck = _calibrateSoundCheck;\r\n //check if a componentIR was given to the system, if it isn't check for the microphone. using dummy data here bc we need to\r\n //check the db based on the microphone currently connected\r\n\r\n //new lCalib found at top of calibration files *1000hz, make sure to correct\r\n //based on zeroing of 1000hz, search for \"*1000Hz\"\r\n const ID = isSmartPhone ? micModelNumber : micSerialNumber;\r\n const OEM = isSmartPhone ? this.deviceInfo.OEM : micManufacturer;\r\n if (componentIR == null) {\r\n //mode 'ir'\r\n //global variable this.componentIR must be set\r\n this.componentIR = await this.readFrqGain(ID, OEM).then(data => {\r\n return data;\r\n });\r\n\r\n lCalib = await this.readGainat1000Hz(ID, OEM);\r\n // this.componentGainDBSPL = this.convertToDB(lCalib);\r\n this.componentGainDBSPL = lCalib;\r\n //TODO: if this call to database is unknown, cannot perform experiment => return false\r\n if (this.componentIR == null) {\r\n this.status =\r\n `Microphone (${OEM},${ID}) is not found in the database. Please add it to the database.`.toString();\r\n this.emit('update', {message: this.status});\r\n return false;\r\n }\r\n } else {\r\n this.componentIR = componentIR;\r\n lCalib = this.findGainatFrequency(this.componentIR.Freq, this.componentIR.Gain, 1000);\r\n // this.componentGainDBSPL = this.convertToDB(lCalib);\r\n this.componentGainDBSPL = lCalib;\r\n await this.writeGainat1000Hz(ID, lCalib, OEM);\r\n await this.writeIsSmartPhone(ID, isSmartPhone, OEM);\r\n }\r\n\r\n this.oldComponentIR = this.componentIR;\r\n\r\n let volumeResults = await this.startCalibrationVolume(\r\n stream,\r\n gainValues,\r\n lCalib,\r\n this.componentGainDBSPL\r\n );\r\n\r\n let impulseResponseResults = await this.startCalibrationImpulseResponse(stream);\r\n\r\n if (componentIR != null) {\r\n //insert Freq and Gain from this.componentIR into db\r\n await this.writeFrqGain(\r\n ID,\r\n impulseResponseResults.componentIR.Freq,\r\n impulseResponseResults.componentIR.Gain,\r\n OEM\r\n );\r\n }\r\n\r\n const total_results = {...volumeResults, ...impulseResponseResults};\r\n\r\n total_results['micInfo'] = {\r\n micManufacturer: micManufacturer,\r\n micSerialNumber: micSerialNumber,\r\n micModelNumber: micModelNumber,\r\n micModelName: micModelName,\r\n ID: ID,\r\n OEM: OEM,\r\n micInfo: this.deviceInfo,\r\n };\r\n console.log('total results');\r\n console.log(total_results);\r\n return total_results;\r\n };\r\n}\r\n\r\n/* harmony default export */ __webpack_exports__[\"default\"] = (Combination);\r\n\n\n//# sourceURL=webpack://speakerCalibrator/./src/tasks/combination/combination.js?");
844
844
 
845
845
  /***/ }),
846
846
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "speaker-calibration",
3
- "version": "2.2.21",
3
+ "version": "2.2.23",
4
4
  "description": "Speaker calibration library for auditory testing",
5
5
  "main": "dist/main.js",
6
6
  "directories": {
@@ -83,7 +83,8 @@ class AudioCalibrator extends AudioRecorder {
83
83
  loopCondition = () => false,
84
84
  duringRecord = async () => {},
85
85
  afterRecord = async () => {},
86
- mode
86
+ mode,
87
+ checkRec
87
88
  ) => {
88
89
  this.numSuccessfulCaptured = 0;
89
90
 
@@ -111,7 +112,7 @@ class AudioCalibrator extends AudioRecorder {
111
112
 
112
113
  // when done, stop recording
113
114
  console.warn('stopRecording');
114
- await this.stopRecording(mode);
115
+ await this.stopRecording(mode,checkRec);
115
116
 
116
117
  // do something after recording such as start processing values
117
118
  console.warn('afterRecord');
@@ -136,7 +137,8 @@ class AudioCalibrator extends AudioRecorder {
136
137
  beforeRecord = () => {},
137
138
  afterRecord = () => {},
138
139
  gainValue,
139
- lCalib = 104.92978421490648
140
+ lCalib = 104.92978421490648,
141
+ checkRec
140
142
  ) => {
141
143
  this.numCalibratingRoundsCompleted = 0;
142
144
 
@@ -154,7 +156,7 @@ class AudioCalibrator extends AudioRecorder {
154
156
 
155
157
  // when done, stop recording
156
158
  console.log('Calibration Round Complete');
157
- await this.stopRecording();
159
+ await this.stopRecording('volume',checkRec);
158
160
 
159
161
  // after recording
160
162
  await afterRecord(lCalib);
@@ -243,7 +245,7 @@ class AudioCalibrator extends AudioRecorder {
243
245
  };
244
246
  downloadSingleUnfilteredRecording = () => {
245
247
  const recordings = this.getAllRecordedSignals();
246
- saveToCSV(recordings[0], `recordedMLSignal_unconvolved.csv`);
248
+ saveToCSV(recordings[recordings.length-1], `recordedMLSignal_unconvolved.csv`);
247
249
  }
248
250
  downloadSingleFilteredRecording = () => {
249
251
  const recordings = this.getAllFilteredRecordedSignals();
@@ -32,14 +32,32 @@ class AudioRecorder extends MyEventEmitter {
32
32
  * @private
33
33
  * @example
34
34
  */
35
- #saveRecording = async () => {
35
+ #saveRecording = async (checkRec) => {
36
36
  const arrayBuffer = await this.#audioBlob.arrayBuffer();
37
37
  const audioBuffer = await this.#audioContext.decodeAudioData(arrayBuffer);
38
38
  const data = audioBuffer.getChannelData(0);
39
+ const dataArray = Array.from(data);
39
40
 
40
41
  console.log(`Decoded audio buffer with ${data.length} samples`);
41
42
  console.log(`Unfiltered recording should be of length: ${data.length}`);
42
- this.#recordedSignals.push(Array.from(data));
43
+ if (checkRec == 'loudest'){
44
+ const uniqueSet = new Set(dataArray);
45
+ const numberOfUniqueValues = uniqueSet.size;
46
+ const squaredValues = dataArray.map(value => value * value);
47
+ const sum_of_squares = squaredValues.reduce((total, value) => total + value, 0);
48
+ const squared_mean = sum_of_squares / dataArray.length;
49
+ const dbLevel = 20 * Math.log10(Math.sqrt(squared_mean));
50
+ console.log("Loudest 1000-Hz recording: " + dbLevel + " with " + numberOfUniqueValues + " unique values.")
51
+ }else if (checkRec == 'allhz'){
52
+ const uniqueSet = new Set(dataArray);
53
+ const numberOfUniqueValues = uniqueSet.size;
54
+ const squaredValues = dataArray.map(value => value * value);
55
+ const sum_of_squares = squaredValues.reduce((total, value) => total + value, 0);
56
+ const squared_mean = sum_of_squares / dataArray.length;
57
+ const dbLevel = 20 * Math.log10(Math.sqrt(squared_mean));
58
+ console.log("All Hz Recording: " + dbLevel + " with " + numberOfUniqueValues + " unique values.")
59
+ }
60
+ this.#recordedSignals.push(dataArray);
43
61
  };
44
62
 
45
63
  #saveFilteredRecording = async () => {
@@ -110,7 +128,7 @@ class AudioRecorder extends MyEventEmitter {
110
128
  * @public
111
129
  * @example
112
130
  */
113
- stopRecording = async (mode) => {
131
+ stopRecording = async (mode,checkRec) => {
114
132
  // Stop the media recorder, and wait for the data to be available
115
133
  await new Promise(resolve => {
116
134
  this.#mediaRecorder.onstop = () => {
@@ -127,7 +145,7 @@ class AudioRecorder extends MyEventEmitter {
127
145
  if (mode === 'filtered'){
128
146
  await this.#saveFilteredRecording();
129
147
  }else{
130
- await this.#saveRecording();
148
+ await this.#saveRecording(checkRec);
131
149
  }
132
150
  };
133
151
 
@@ -578,6 +578,7 @@ class Combination extends AudioCalibrator {
578
578
  };
579
579
 
580
580
  playMLSwithIIR = async (stream, iir) => {
581
+ let checkRec = false;
581
582
  this.mode = 'filtered';
582
583
  console.log('play mls with iir');
583
584
  this.invertedImpulseResponse = iir;
@@ -590,7 +591,8 @@ class Combination extends AudioCalibrator {
590
591
  () => this.numSuccessfulCaptured < 1,
591
592
  this.#awaitDesiredMLSLength, // during record
592
593
  this.#afterMLSwIIRRecord, // after record
593
- this.mode
594
+ this.mode,
595
+ checkRec
594
596
  );
595
597
  };
596
598
 
@@ -605,6 +607,7 @@ class Combination extends AudioCalibrator {
605
607
  */
606
608
  startCalibrationImpulseResponse = async stream => {
607
609
  let desired_time = this.desired_time_per_mls;
610
+ let checkRec = 'allhz';
608
611
 
609
612
  console.log('MLS sequence should be of length: ' + this.sourceSamplingRate * desired_time);
610
613
 
@@ -628,9 +631,11 @@ class Combination extends AudioCalibrator {
628
631
  () => this.numSuccessfulCaptured < this.numCaptures, // loop while true
629
632
  this.#awaitDesiredMLSLength, // during record
630
633
  this.#afterMLSRecord, // after record
631
- this.mode
634
+ this.mode,
635
+ checkRec
632
636
  ),
633
637
  this.#stopCalibrationAudio();
638
+ checkRec = false;
634
639
 
635
640
  // at this stage we've captured all the required signals,
636
641
  // and have received IRs for each one
@@ -928,6 +933,7 @@ class Combination extends AudioCalibrator {
928
933
  let inDB = 0;
929
934
  const outDBSPLValues = [];
930
935
  const outDBSPL1000Values = [];
936
+ let checkRec = false;
931
937
 
932
938
  // do one calibration that will be discarded
933
939
  const soundLevelToDiscard = -60;
@@ -946,7 +952,8 @@ class Combination extends AudioCalibrator {
946
952
  this.#createCalibrationToneWithGainValue,
947
953
  this.#sendToServerForProcessing,
948
954
  gainToDiscard,
949
- lCalib //todo make this a class parameter
955
+ lCalib, //todo make this a class parameter
956
+ checkRec
950
957
  );
951
958
  } while (this.outDBSPL === null);
952
959
  //reset the values
@@ -960,6 +967,9 @@ class Combination extends AudioCalibrator {
960
967
  // run the calibration at different gain values provided by the user
961
968
  for (let i = 0; i < trialIterations; i++) {
962
969
  //convert gain to DB and add to inDB
970
+ if (i == trialIterations - 1) {
971
+ checkRec = 'loudest';
972
+ }
963
973
  inDB = Math.log10(gainValues[i]) * 20;
964
974
  // precision to 1 decimal place
965
975
  inDB = Math.round(inDB * 10) / 10;
@@ -977,7 +987,8 @@ class Combination extends AudioCalibrator {
977
987
  this.#createCalibrationToneWithGainValue,
978
988
  this.#sendToServerForProcessing,
979
989
  gainValues[i],
980
- lCalib //todo make this a class parameter
990
+ lCalib, //todo make this a class parameter
991
+ checkRec
981
992
  );
982
993
  } while (this.outDBSPL === null);
983
994
  outDBSPL1000Values.push(this.outDBSPL1000);
@@ -1197,6 +1208,7 @@ class Combination extends AudioCalibrator {
1197
1208
  micModelName: micModelName,
1198
1209
  ID: ID,
1199
1210
  OEM: OEM,
1211
+ micInfo: this.deviceInfo,
1200
1212
  };
1201
1213
  console.log('total results');
1202
1214
  console.log(total_results);