speaker-calibration 2.2.97 → 2.2.99
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/example/listener.js
CHANGED
|
@@ -8,7 +8,7 @@ const isSmartPhone = urlParams.get('isSmartPhone');
|
|
|
8
8
|
|
|
9
9
|
const listenerParameters = {
|
|
10
10
|
targetElementId: 'display',
|
|
11
|
-
|
|
11
|
+
microphoneFromAPI: '',
|
|
12
12
|
};
|
|
13
13
|
|
|
14
14
|
const container = document.getElementById('listenerContainer');
|
|
@@ -36,7 +36,7 @@ switch (isSmartPhone) {
|
|
|
36
36
|
container.style.display = 'block';
|
|
37
37
|
// event listener for id calibrationBeginButton
|
|
38
38
|
const calibrationBeginButton = document.getElementById('calibrationBeginButton');
|
|
39
|
-
calibrationBeginButton.addEventListener('click', () => {
|
|
39
|
+
calibrationBeginButton.addEventListener('click', async () => {
|
|
40
40
|
// remove the button
|
|
41
41
|
calibrationBeginButton.remove();
|
|
42
42
|
// remove turn message
|
|
@@ -56,6 +56,26 @@ switch (isSmartPhone) {
|
|
|
56
56
|
fontSize--;
|
|
57
57
|
recordingInProgressElement.style.fontSize = fontSize + 'px';
|
|
58
58
|
}
|
|
59
|
+
const webAudioDeviceNames = {microphone: ''};
|
|
60
|
+
const externalMicList = ['UMIK', 'Airpods', 'Bluetooth'];
|
|
61
|
+
try {
|
|
62
|
+
const stream = await navigator.mediaDevices.getUserMedia({audio: true});
|
|
63
|
+
if (stream) {
|
|
64
|
+
const devices = await navigator.mediaDevices.enumerateDevices();
|
|
65
|
+
const mics = devices.filter(device => device.kind === 'audioinput');
|
|
66
|
+
mics.forEach(mic => {
|
|
67
|
+
if (externalMicList.some(externalMic => mic.label.includes(externalMic))) {
|
|
68
|
+
webAudioDeviceNames.microphone = mic.label;
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
if (webAudioDeviceNames.microphone === '') {
|
|
72
|
+
webAudioDeviceNames.microphone = mics[0].label;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
} catch (err) {
|
|
76
|
+
console.log(err);
|
|
77
|
+
}
|
|
78
|
+
listenerParameters.microphoneFromAPI = webAudioDeviceNames.microphone;
|
|
59
79
|
window.listener = new speakerCalibrator.Listener(listenerParameters);
|
|
60
80
|
console.log(window.listener);
|
|
61
81
|
});
|
package/dist/main.js
CHANGED
|
@@ -785,7 +785,7 @@ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var peer
|
|
|
785
785
|
/***/ (function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
|
|
786
786
|
|
|
787
787
|
"use strict";
|
|
788
|
-
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _audioPeer__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./audioPeer */ \"./src/peer-connection/audioPeer.js\");\n/* harmony import */ var _peerErrors__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./peerErrors */ \"./src/peer-connection/peerErrors.js\");\n/* harmony import */ var axios__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! axios */ \"./node_modules/axios/index.js\");\n/* harmony import */ var axios__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(axios__WEBPACK_IMPORTED_MODULE_2__);\n\r\n\r\n\r\n\r\n/**\r\n * @class Handles the listener's side of the connection. Responsible for getting access to user's microphone,\r\n * and initiating a call to the Speaker.\r\n * @augments AudioPeer\r\n */\r\nclass Listener extends _audioPeer__WEBPACK_IMPORTED_MODULE_0__[\"default\"] {\r\n /**\r\n * Takes a target element where html elements will be appended.\r\n *\r\n * @param params - See type definition for initParameters.\r\n * @example\r\n */\r\n constructor(params) {\r\n super(params);\r\n // this.deviceInfoFromUser = params.deviceInfoFromUser\r\n // ? params.deviceInfoFromUser\r\n // : {modelNumber: '', modelName: ''};\r\n this.startTime = Date.now();\r\n this.receiverPeerId = null;\r\n\r\n const urlParameters = this.parseURLSearchParams();\r\n this.calibrateSoundHz =\r\n urlParameters.calibrateSoundHz !== null && urlParameters.calibrateSoundHz !== undefined\r\n ? urlParameters.calibrateSoundHz\r\n : 48000;\r\n this.calibrateSoundSamplingDesiredBits =\r\n urlParameters.calibrateSoundSamplingDesiredBits !== null &&\r\n urlParameters.calibrateSoundSamplingDesiredBits !== undefined\r\n ? urlParameters.calibrateSoundSamplingDesiredBits\r\n : 24;\r\n this.speakerPeerId = urlParameters.speakerPeerId;\r\n\r\n this.peer.on('open', this.onPeerOpen);\r\n this.peer.on('connection', this.onPeerConnection);\r\n this.peer.on('disconnected', this.onPeerDisconnected);\r\n this.peer.on('close', this.onPeerClose);\r\n this.peer.on('error', this.onPeerError);\r\n }\r\n\r\n onPeerOpen = id => {\r\n this.displayUpdate('Listener - onPeerOpen');\r\n // Workaround for peer.reconnect deleting previous id\r\n\r\n if (id === null) {\r\n this.displayUpdate('Received null id from peer open');\r\n this.peer.id = this.lastPeerId;\r\n } else {\r\n this.lastPeerId = this.peer.id;\r\n }\r\n\r\n this.join();\r\n };\r\n\r\n onPeerConnection = connection => {\r\n this.displayUpdate('Listener - onPeerConnection');\r\n // Disallow incoming connections\r\n connection.on('open', () => {\r\n connection.send('Sender does not accept incoming connections');\r\n setTimeout(() => {\r\n connection.close();\r\n }, 500);\r\n });\r\n };\r\n\r\n onConnData = data => {\r\n this.displayUpdate('Listener - onConnData');\r\n const hasSpeakerID = Object.prototype.hasOwnProperty.call(data, 'speakerPeerId');\r\n if (!hasSpeakerID) {\r\n this.displayUpdate('Error in parsing data received! Must set \"speakerPeerId\" property');\r\n throw new _peerErrors__WEBPACK_IMPORTED_MODULE_1__.MissingSpeakerIdError('Must set \"speakerPeerId\" property');\r\n } else {\r\n // this.conn.close();\r\n this.displayUpdate(this.speakerPeerId);\r\n this.speakerPeerId = data.speakerPeerId;\r\n const newParams = {\r\n speakerPeerId: this.speakerPeerId,\r\n };\r\n /*\r\n FUTURE does this limit usable environments?\r\n ie does this work if internet is lost after initial page load?\r\n */\r\n window.location.search = this.queryStringFromObject(newParams); // Redirect to correctly constructed keypad page\r\n }\r\n };\r\n\r\n join = async () => {\r\n this.displayUpdate('Listener - join');\r\n /**\r\n * Create the connection between the two Peers.\r\n *\r\n * Sets up callbacks that handle any events related to the\r\n * connection and data received on it.\r\n */\r\n // Close old connection\r\n if (this.conn) {\r\n this.displayUpdate('Closing old connection');\r\n this.conn.close();\r\n }\r\n\r\n // Create connection to destination peer specified by the query param\r\n this.displayUpdate(`Creating connection to: ${this.speakerPeerId}`);\r\n this.conn = this.peer.connect(this.speakerPeerId, {\r\n reliable: true,\r\n });\r\n\r\n this.displayUpdate('Created connection');\r\n this.conn.on('open', async () => {\r\n this.displayUpdate('Listener - conn open');\r\n await this.getDeviceInfo();\r\n // this.sendSamplingRate();\r\n await this.openAudioStream();\r\n });\r\n\r\n // Handle incoming data (messages only since this is the signal sender)\r\n this.conn.on('data', this.onConnData);\r\n this.conn.on('close', () => {\r\n console.log('Connection closed');\r\n });\r\n };\r\n\r\n getMobileOS = () => {\r\n const ua = navigator.userAgent;\r\n if (/android/i.test(ua)) {\r\n return 'Android';\r\n }\r\n if (\r\n /iPad|iPhone|iPod/.test(ua) ||\r\n ((navigator?.userAgentData?.platform || navigator?.platform) === 'MacIntel' &&\r\n navigator.maxTouchPoints > 1)\r\n ) {\r\n return 'iOS';\r\n }\r\n return 'Other';\r\n };\r\n\r\n sendSamplingRate = sampleRate => {\r\n this.displayUpdate('Listener - sendSamplingRate');\r\n this.conn.send({\r\n name: 'samplingRate',\r\n payload: sampleRate,\r\n });\r\n };\r\n\r\n sendSampleSize = sampleSize => {\r\n this.displayUpdate('Listener - sendSampleSize');\r\n this.conn.send({\r\n name: 'sampleSize',\r\n payload: sampleSize,\r\n });\r\n };\r\n\r\n getDeviceInfo = async () => {\r\n try {\r\n const deviceInfo = {};\r\n fod.complete(function (data) {\r\n deviceInfo['IsMobile'] = data.device['ismobile'];\r\n deviceInfo['HardwareName'] = data.device['hardwarename'];\r\n deviceInfo['HardwareFamily'] = data.device['hardwarefamily'];\r\n deviceInfo['HardwareModel'] = data.device['hardwaremodel'];\r\n deviceInfo['OEM'] = data.device['oem'];\r\n deviceInfo['HardwareModelVariants'] = data.device['hardwaremodelvariants'];\r\n deviceInfo['DeviceId'] = data.device['deviceid'];\r\n deviceInfo['PlatformName'] = data.device['platformname'];\r\n deviceInfo['PlatformVersion'] = data.device['platformversion'];\r\n deviceInfo['DeviceType'] = data.device['devicetype'];\r\n // deviceInfo['deviceInfoFromUser'] = this.deviceInfoFromUser;\r\n });\r\n // deviceInfo['deviceInfoFromUser'] = this.deviceInfoFromUser;\r\n this.conn.send({\r\n name: 'deviceInfo',\r\n payload: deviceInfo,\r\n });\r\n return deviceInfo;\r\n } catch (error) {\r\n console.error('Error fetching or executing script:', error.message);\r\n return null;\r\n }\r\n };\r\n\r\n applyHQTrackConstraints = async stream => {\r\n // Contraint the incoming audio to the sampling rate we want\r\n\r\n const track = stream.getAudioTracks()[0];\r\n const capabilities = track.getCapabilities();\r\n\r\n this.displayUpdate(\r\n `Listener Track Capabilities - ${JSON.stringify(capabilities, undefined, 2)}`\r\n );\r\n\r\n const constraints = track.getConstraints();\r\n\r\n if (capabilities.echoCancellation) {\r\n constraints.echoCancellation = false;\r\n }\r\n\r\n if (capabilities.sampleRate) {\r\n constraints.sampleRate = this.calibrateSoundHz;\r\n }\r\n\r\n if (capabilities.sampleSize) {\r\n constraints.sampleSize = this.calibrateSoundSamplingDesiredBits;\r\n }\r\n\r\n if (capabilities.channelCount) {\r\n constraints.channelCount = 1;\r\n }\r\n\r\n this.displayUpdate(`Listener Track Constraints - ${JSON.stringify(constraints, undefined, 2)}`);\r\n\r\n // await the promise\r\n try {\r\n await track.applyConstraints(constraints);\r\n } catch (err) {\r\n console.error(err);\r\n this.displayUpdate(`Error applying constraints to track: ${err}`);\r\n }\r\n\r\n const settings = track.getSettings();\r\n this.displayUpdate(`Listener Track Settings - ${JSON.stringify(settings, undefined, 2)}`);\r\n return settings;\r\n };\r\n\r\n getMediaDevicesAudioContraints = () => {\r\n const availableConstraints = navigator.mediaDevices.getSupportedConstraints();\r\n\r\n this.displayUpdate(\r\n `Listener MediaDevices Available Contraints - ${JSON.stringify(\r\n availableConstraints,\r\n undefined,\r\n 2\r\n )}`\r\n );\r\n\r\n const contraints = {\r\n // ...(availableConstraints.echoCancellation && availableConstraints.echoCancellation == true\r\n // ? {echoCancellation: {exact: false}}\r\n // : {}),\r\n ...(availableConstraints.sampleRate && availableConstraints.sampleRate == true\r\n ? {sampleRate: {ideal: this.calibrateSoundHz}}\r\n : {}),\r\n ...(availableConstraints.sampleSize && availableConstraints.sampleSize == true\r\n ? {sampleSize: {ideal: this.calibrateSoundSamplingDesiredBits}}\r\n : {}),\r\n ...(availableConstraints.channelCount && availableConstraints.channelCount == true\r\n ? {channelCount: {exact: 1}}\r\n : {}),\r\n echoCancellation: false,\r\n noiseSuppression: false,\r\n autoGainControl: false,\r\n };\r\n\r\n console.log(contraints);\r\n\r\n this.displayUpdate(\r\n `Listener MediaDevices Contraints - ${JSON.stringify(contraints, undefined, 2)}`\r\n );\r\n\r\n return contraints;\r\n };\r\n\r\n openAudioStream = async () => {\r\n this.displayUpdate('Listener - openAudioStream');\r\n const mobileOS = this.getMobileOS();\r\n if (false) {}\r\n \r\n const audioContext = new (window.AudioContext || window.webkitAudioContext)();\r\n navigator.mediaDevices\r\n .getUserMedia({\r\n audio: this.getMediaDevicesAudioContraints(),\r\n video: false,\r\n })\r\n .then(stream => {\r\n const sourceNode = audioContext.createMediaStreamSource(stream);\r\n const sampleRate = sourceNode.context.sampleRate;\r\n console.log(sampleRate);\r\n const audioBuffer = audioContext.createBuffer(1, 1, sampleRate);\r\n const sampleSizeInBits = audioBuffer.getChannelData(0).BYTES_PER_ELEMENT * 8;\r\n console.log(sampleSizeInBits);\r\n audioContext.close();\r\n this.applyHQTrackConstraints(stream)\r\n .then(settings => {\r\n this.sendSamplingRate(sampleRate);\r\n // let sampleSize = settings.sampleSize;\r\n // if (!sampleSize){\r\n // sampleSize = this.calibrateSoundSamplingDesiredBits;\r\n // }\r\n this.sendSampleSize(sampleSizeInBits);\r\n this.peer.call(this.speakerPeerId, stream); // one-way call\r\n this.displayUpdate('Listener - openAudioStream');\r\n })\r\n .catch(err => {\r\n console.log(err);\r\n this.displayUpdate(\r\n `Listener - Error in applyHQTrackConstraints - ${JSON.stringify(err, undefined, 2)}`\r\n );\r\n });\r\n })\r\n .catch(err => {\r\n console.error(err);\r\n this.displayUpdate(\r\n `Listener - Error in getUserMedia - ${JSON.stringify(err, undefined, 2)}`\r\n );\r\n });\r\n };\r\n}\r\n\r\n/* harmony default export */ __webpack_exports__[\"default\"] = (Listener);\r\n\n\n//# sourceURL=webpack://speakerCalibrator/./src/peer-connection/listener.js?");
|
|
788
|
+
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _audioPeer__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./audioPeer */ \"./src/peer-connection/audioPeer.js\");\n/* harmony import */ var _peerErrors__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./peerErrors */ \"./src/peer-connection/peerErrors.js\");\n/* harmony import */ var axios__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! axios */ \"./node_modules/axios/index.js\");\n/* harmony import */ var axios__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(axios__WEBPACK_IMPORTED_MODULE_2__);\n\r\n\r\n\r\n\r\n/**\r\n * @class Handles the listener's side of the connection. Responsible for getting access to user's microphone,\r\n * and initiating a call to the Speaker.\r\n * @augments AudioPeer\r\n */\r\nclass Listener extends _audioPeer__WEBPACK_IMPORTED_MODULE_0__[\"default\"] {\r\n /**\r\n * Takes a target element where html elements will be appended.\r\n *\r\n * @param params - See type definition for initParameters.\r\n * @example\r\n */\r\n constructor(params) {\r\n super(params);\r\n this.microphoneFromAPI = params.microphoneFromAPI ? params.microphoneFromAPI : '';\r\n // this.deviceInfoFromUser = params.deviceInfoFromUser\r\n // ? params.deviceInfoFromUser\r\n // : {modelNumber: '', modelName: ''};\r\n this.startTime = Date.now();\r\n this.receiverPeerId = null;\r\n\r\n const urlParameters = this.parseURLSearchParams();\r\n this.calibrateSoundHz =\r\n urlParameters.calibrateSoundHz !== null && urlParameters.calibrateSoundHz !== undefined\r\n ? urlParameters.calibrateSoundHz\r\n : 48000;\r\n this.calibrateSoundSamplingDesiredBits =\r\n urlParameters.calibrateSoundSamplingDesiredBits !== null &&\r\n urlParameters.calibrateSoundSamplingDesiredBits !== undefined\r\n ? urlParameters.calibrateSoundSamplingDesiredBits\r\n : 24;\r\n this.speakerPeerId = urlParameters.speakerPeerId;\r\n\r\n this.peer.on('open', this.onPeerOpen);\r\n this.peer.on('connection', this.onPeerConnection);\r\n this.peer.on('disconnected', this.onPeerDisconnected);\r\n this.peer.on('close', this.onPeerClose);\r\n this.peer.on('error', this.onPeerError);\r\n }\r\n\r\n onPeerOpen = id => {\r\n this.displayUpdate('Listener - onPeerOpen');\r\n // Workaround for peer.reconnect deleting previous id\r\n\r\n if (id === null) {\r\n this.displayUpdate('Received null id from peer open');\r\n this.peer.id = this.lastPeerId;\r\n } else {\r\n this.lastPeerId = this.peer.id;\r\n }\r\n\r\n this.join();\r\n };\r\n\r\n onPeerConnection = connection => {\r\n this.displayUpdate('Listener - onPeerConnection');\r\n // Disallow incoming connections\r\n connection.on('open', () => {\r\n connection.send('Sender does not accept incoming connections');\r\n setTimeout(() => {\r\n connection.close();\r\n }, 500);\r\n });\r\n };\r\n\r\n onConnData = data => {\r\n this.displayUpdate('Listener - onConnData');\r\n const hasSpeakerID = Object.prototype.hasOwnProperty.call(data, 'speakerPeerId');\r\n if (!hasSpeakerID) {\r\n this.displayUpdate('Error in parsing data received! Must set \"speakerPeerId\" property');\r\n throw new _peerErrors__WEBPACK_IMPORTED_MODULE_1__.MissingSpeakerIdError('Must set \"speakerPeerId\" property');\r\n } else {\r\n // this.conn.close();\r\n this.displayUpdate(this.speakerPeerId);\r\n this.speakerPeerId = data.speakerPeerId;\r\n const newParams = {\r\n speakerPeerId: this.speakerPeerId,\r\n };\r\n /*\r\n FUTURE does this limit usable environments?\r\n ie does this work if internet is lost after initial page load?\r\n */\r\n window.location.search = this.queryStringFromObject(newParams); // Redirect to correctly constructed keypad page\r\n }\r\n };\r\n\r\n join = async () => {\r\n this.displayUpdate('Listener - join');\r\n /**\r\n * Create the connection between the two Peers.\r\n *\r\n * Sets up callbacks that handle any events related to the\r\n * connection and data received on it.\r\n */\r\n // Close old connection\r\n if (this.conn) {\r\n this.displayUpdate('Closing old connection');\r\n this.conn.close();\r\n }\r\n\r\n // Create connection to destination peer specified by the query param\r\n this.displayUpdate(`Creating connection to: ${this.speakerPeerId}`);\r\n this.conn = this.peer.connect(this.speakerPeerId, {\r\n reliable: true,\r\n });\r\n\r\n this.displayUpdate('Created connection');\r\n this.conn.on('open', async () => {\r\n this.displayUpdate('Listener - conn open');\r\n await this.getDeviceInfo();\r\n // this.sendSamplingRate();\r\n await this.openAudioStream();\r\n });\r\n\r\n // Handle incoming data (messages only since this is the signal sender)\r\n this.conn.on('data', this.onConnData);\r\n this.conn.on('close', () => {\r\n console.log('Connection closed');\r\n });\r\n };\r\n\r\n getMobileOS = () => {\r\n const ua = navigator.userAgent;\r\n if (/android/i.test(ua)) {\r\n return 'Android';\r\n }\r\n if (\r\n /iPad|iPhone|iPod/.test(ua) ||\r\n ((navigator?.userAgentData?.platform || navigator?.platform) === 'MacIntel' &&\r\n navigator.maxTouchPoints > 1)\r\n ) {\r\n return 'iOS';\r\n }\r\n return 'Other';\r\n };\r\n\r\n sendSamplingRate = sampleRate => {\r\n this.displayUpdate('Listener - sendSamplingRate');\r\n this.conn.send({\r\n name: 'samplingRate',\r\n payload: sampleRate,\r\n });\r\n };\r\n\r\n sendSampleSize = sampleSize => {\r\n this.displayUpdate('Listener - sendSampleSize');\r\n this.conn.send({\r\n name: 'sampleSize',\r\n payload: sampleSize,\r\n });\r\n };\r\n\r\n getDeviceInfo = async () => {\r\n try {\r\n const deviceInfo = {};\r\n fod.complete(function (data) {\r\n deviceInfo['IsMobile'] = data.device['ismobile'];\r\n deviceInfo['HardwareName'] = data.device['hardwarename'];\r\n deviceInfo['HardwareFamily'] = data.device['hardwarefamily'];\r\n deviceInfo['HardwareModel'] = data.device['hardwaremodel'];\r\n deviceInfo['OEM'] = data.device['oem'];\r\n deviceInfo['HardwareModelVariants'] = data.device['hardwaremodelvariants'];\r\n deviceInfo['DeviceId'] = data.device['deviceid'];\r\n deviceInfo['PlatformName'] = data.device['platformname'];\r\n deviceInfo['PlatformVersion'] = data.device['platformversion'];\r\n deviceInfo['DeviceType'] = data.device['devicetype'];\r\n // deviceInfo['deviceInfoFromUser'] = this.deviceInfoFromUser;\r\n });\r\n // deviceInfo['deviceInfoFromUser'] = this.deviceInfoFromUser;\r\n deviceInfo['microphoneFromAPI'] = this.microphoneFromAPI;\r\n this.conn.send({\r\n name: 'deviceInfo',\r\n payload: deviceInfo,\r\n });\r\n return deviceInfo;\r\n } catch (error) {\r\n console.error('Error fetching or executing script:', error.message);\r\n return null;\r\n }\r\n };\r\n\r\n applyHQTrackConstraints = async stream => {\r\n // Contraint the incoming audio to the sampling rate we want\r\n\r\n const track = stream.getAudioTracks()[0];\r\n const capabilities = track.getCapabilities();\r\n\r\n this.displayUpdate(\r\n `Listener Track Capabilities - ${JSON.stringify(capabilities, undefined, 2)}`\r\n );\r\n\r\n const constraints = track.getConstraints();\r\n\r\n if (capabilities.echoCancellation) {\r\n constraints.echoCancellation = false;\r\n }\r\n\r\n if (capabilities.sampleRate) {\r\n constraints.sampleRate = this.calibrateSoundHz;\r\n }\r\n\r\n if (capabilities.sampleSize) {\r\n constraints.sampleSize = this.calibrateSoundSamplingDesiredBits;\r\n }\r\n\r\n if (capabilities.channelCount) {\r\n constraints.channelCount = 1;\r\n }\r\n\r\n this.displayUpdate(`Listener Track Constraints - ${JSON.stringify(constraints, undefined, 2)}`);\r\n\r\n // await the promise\r\n try {\r\n await track.applyConstraints(constraints);\r\n } catch (err) {\r\n console.error(err);\r\n this.displayUpdate(`Error applying constraints to track: ${err}`);\r\n }\r\n\r\n const settings = track.getSettings();\r\n this.displayUpdate(`Listener Track Settings - ${JSON.stringify(settings, undefined, 2)}`);\r\n return settings;\r\n };\r\n\r\n getMediaDevicesAudioContraints = () => {\r\n const availableConstraints = navigator.mediaDevices.getSupportedConstraints();\r\n\r\n this.displayUpdate(\r\n `Listener MediaDevices Available Contraints - ${JSON.stringify(\r\n availableConstraints,\r\n undefined,\r\n 2\r\n )}`\r\n );\r\n\r\n const contraints = {\r\n // ...(availableConstraints.echoCancellation && availableConstraints.echoCancellation == true\r\n // ? {echoCancellation: {exact: false}}\r\n // : {}),\r\n ...(availableConstraints.sampleRate && availableConstraints.sampleRate == true\r\n ? {sampleRate: {ideal: this.calibrateSoundHz}}\r\n : {}),\r\n ...(availableConstraints.sampleSize && availableConstraints.sampleSize == true\r\n ? {sampleSize: {ideal: this.calibrateSoundSamplingDesiredBits}}\r\n : {}),\r\n ...(availableConstraints.channelCount && availableConstraints.channelCount == true\r\n ? {channelCount: {exact: 1}}\r\n : {}),\r\n echoCancellation: false,\r\n noiseSuppression: false,\r\n autoGainControl: false,\r\n };\r\n\r\n console.log(contraints);\r\n\r\n this.displayUpdate(\r\n `Listener MediaDevices Contraints - ${JSON.stringify(contraints, undefined, 2)}`\r\n );\r\n\r\n return contraints;\r\n };\r\n\r\n openAudioStream = async () => {\r\n this.displayUpdate('Listener - openAudioStream');\r\n const mobileOS = this.getMobileOS();\r\n if (false) {}\r\n\r\n const audioContext = new (window.AudioContext || window.webkitAudioContext)();\r\n navigator.mediaDevices\r\n .getUserMedia({\r\n audio: this.getMediaDevicesAudioContraints(),\r\n video: false,\r\n })\r\n .then(stream => {\r\n const sourceNode = audioContext.createMediaStreamSource(stream);\r\n const sampleRate = sourceNode.context.sampleRate;\r\n console.log(sampleRate);\r\n const audioBuffer = audioContext.createBuffer(1, 1, sampleRate);\r\n const sampleSizeInBits = audioBuffer.getChannelData(0).BYTES_PER_ELEMENT * 8;\r\n console.log(sampleSizeInBits);\r\n audioContext.close();\r\n this.applyHQTrackConstraints(stream)\r\n .then(settings => {\r\n this.sendSamplingRate(sampleRate);\r\n // let sampleSize = settings.sampleSize;\r\n // if (!sampleSize){\r\n // sampleSize = this.calibrateSoundSamplingDesiredBits;\r\n // }\r\n this.sendSampleSize(sampleSizeInBits);\r\n this.peer.call(this.speakerPeerId, stream); // one-way call\r\n this.displayUpdate('Listener - openAudioStream');\r\n })\r\n .catch(err => {\r\n console.log(err);\r\n this.displayUpdate(\r\n `Listener - Error in applyHQTrackConstraints - ${JSON.stringify(err, undefined, 2)}`\r\n );\r\n });\r\n })\r\n .catch(err => {\r\n console.error(err);\r\n this.displayUpdate(\r\n `Listener - Error in getUserMedia - ${JSON.stringify(err, undefined, 2)}`\r\n );\r\n });\r\n };\r\n}\r\n\r\n/* harmony default export */ __webpack_exports__[\"default\"] = (Listener);\r\n\n\n//# sourceURL=webpack://speakerCalibrator/./src/peer-connection/listener.js?");
|
|
789
789
|
|
|
790
790
|
/***/ }),
|
|
791
791
|
|
|
@@ -818,7 +818,7 @@ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var qrco
|
|
|
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 axios__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! axios */ \"./node_modules/axios/index.js\");\n/* harmony import */ var axios__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(axios__WEBPACK_IMPORTED_MODULE_0__);\n\r\n/**\r\n *\r\n */\r\nclass PythonServerAPI {\r\n static PYTHON_SERVER_URL = 'https://easyeyes-python-flask-server.herokuapp.com';\r\n\r\n static TEST_SERVER_URL = 'http://127.0.0.1:5000';\r\n // static PYTHON_SERVER_URL ='http://127.0.0.1:5000';\r\n\r\n /** @private */\r\n MAX_RETRY_COUNT = 3;\r\n /** @private */\r\n RETRY_DELAY_MS = 1000;\r\n /**\r\n * @param data- -\r\n * g = inverted impulse response, when convolved with the impulse\r\n * reponse, they cancel out.\r\n * @param data.payload\r\n * @param data.sampleRate\r\n * @param data.P\r\n * @param data-.payload\r\n * @param data-.sampleRate\r\n * @param data-.P\r\n * @returns\r\n * @example\r\n */\r\n getImpulseResponse = async ({\r\n mls, \r\n payload, \r\n sampleRate, \r\n P, \r\n numPeriods\r\n }) => {\r\n const task = 'impulse-response';\r\n let res = null;\r\n\r\n console.log({payload});\r\n\r\n const data = JSON.stringify({\r\n task,\r\n payload,\r\n 'sample-rate': sampleRate,\r\n mls,\r\n P,\r\n numPeriods,\r\n });\r\n\r\n await axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n method: 'post',\r\n baseURL: PythonServerAPI.PYTHON_SERVER_URL,\r\n url: `/task/${task}`,\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n data,\r\n })\r\n .then(response => {\r\n res = response;\r\n })\r\n .catch(error => {\r\n throw error;\r\n });\r\n return res.data[task];\r\n };\r\n\r\n getMLS = async ({length,calibrateSoundBurstDb}) => {\r\n const task = 'mls';\r\n let res = null;\r\n\r\n const data = JSON.stringify({\r\n task,\r\n length: length,\r\n calibrateSoundBurstDb: calibrateSoundBurstDb\r\n });\r\n\r\n await axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n method: 'post',\r\n baseURL: PythonServerAPI.PYTHON_SERVER_URL,\r\n url: `/task/${task}`,\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n data,\r\n })\r\n .then(response => {\r\n res = response;\r\n })\r\n .catch(error => {\r\n throw error;\r\n });\r\n\r\n return res.data[task];\r\n };\r\n getMLSWithRetry = async ({length,calibrateSoundBurstDb}) => {\r\n let retryCount = 0;\r\n let response = null;\r\n\r\n while (retryCount < this.MAX_RETRY_COUNT) {\r\n try {\r\n response = await this.getMLS({length,calibrateSoundBurstDb});\r\n // If the request is successful, break out of the loop\r\n break;\r\n } catch (error) {\r\n console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);\r\n retryCount++;\r\n await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));\r\n }\r\n }\r\n\r\n if (response) {\r\n return response;\r\n } else {\r\n throw new Error(`Failed to get MLS after ${this.MAX_RETRY_COUNT} attempts.`);\r\n }\r\n };\r\n\r\n getPSD = async ({unconv_rec, conv_rec, sampleRate}) => {\r\n const task = 'psd';\r\n let res = null;\r\n\r\n const data = JSON.stringify({\r\n task,\r\n unconv_rec,\r\n conv_rec,\r\n sampleRate,\r\n });\r\n\r\n await axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n method: 'post',\r\n baseURL: PythonServerAPI.PYTHON_SERVER_URL,\r\n url: `/task/${task}`,\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n data,\r\n })\r\n .then(response => {\r\n res = response;\r\n })\r\n .catch(error => {\r\n throw error;\r\n });\r\n return res.data[task];\r\n };\r\n\r\n getBackgroundNoisePSD = async ({background_rec, sampleRate}) => {\r\n const task = 'background-psd';\r\n let res = null;\r\n\r\n const data = JSON.stringify({\r\n task,\r\n background_rec,\r\n sampleRate,\r\n });\r\n\r\n await axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n method: 'post',\r\n baseURL: PythonServerAPI.PYTHON_SERVER_URL,\r\n url: `/task/${task}`,\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n data,\r\n })\r\n .then(response => {\r\n res = response;\r\n })\r\n .catch(error => {\r\n throw error;\r\n });\r\n return res.data[task];\r\n };\r\n\r\n getBackgroundNoisePSDWithRetry = async ({background_rec, sampleRate}) => {\r\n let retryCount = 0;\r\n let response = null;\r\n\r\n while (retryCount < this.MAX_RETRY_COUNT) {\r\n try {\r\n response = await this.getBackgroundNoisePSD({background_rec, sampleRate});\r\n // If the request is successful, break out of the loop\r\n break;\r\n } catch (error) {\r\n console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);\r\n retryCount++;\r\n await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));\r\n }\r\n }\r\n\r\n if (response) {\r\n return response;\r\n } else {\r\n throw new Error(`Failed to get PSD after ${this.MAX_RETRY_COUNT} attempts.`);\r\n }\r\n };\r\n\r\n getSubtractedPSD = async (rec, knownGains, knownFrequencies, sampleRate) => {\r\n const task = 'subtracted-psd';\r\n let res = null;\r\n\r\n const data = JSON.stringify({\r\n task,\r\n rec,\r\n knownGains,\r\n knownFrequencies,\r\n sampleRate,\r\n });\r\n\r\n await axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n method: 'post',\r\n baseURL: PythonServerAPI.PYTHON_SERVER_URL,\r\n url: `/task/${task}`,\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n data,\r\n })\r\n .then(response => {\r\n res = response;\r\n })\r\n .catch(error => {\r\n throw error;\r\n });\r\n return res.data[task];\r\n };\r\n\r\n getSubtractedPSDWithRetry = async (rec, knownGains, knownFrequencies, sampleRate) => {\r\n let retryCount = 0;\r\n let response = null;\r\n\r\n while (retryCount < this.MAX_RETRY_COUNT) {\r\n try {\r\n response = await this.getSubtractedPSD(rec, knownGains, knownFrequencies, sampleRate);\r\n // If the request is successful, break out of the loop\r\n break;\r\n } catch (error) {\r\n console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);\r\n retryCount++;\r\n await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));\r\n }\r\n }\r\n\r\n if (response) {\r\n return response;\r\n } else {\r\n throw new Error(`Failed to get PSD after ${this.MAX_RETRY_COUNT} attempts.`);\r\n }\r\n };\r\n\r\n getPSDWithRetry = async ({unconv_rec, conv_rec, sampleRate}) => {\r\n let retryCount = 0;\r\n let response = null;\r\n\r\n while (retryCount < this.MAX_RETRY_COUNT) {\r\n try {\r\n response = await this.getPSD({unconv_rec, conv_rec, sampleRate});\r\n // If the request is successful, break out of the loop\r\n break;\r\n } catch (error) {\r\n console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);\r\n retryCount++;\r\n await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));\r\n }\r\n }\r\n\r\n if (response) {\r\n return response;\r\n } else {\r\n throw new Error(`Failed to get PSD after ${this.MAX_RETRY_COUNT} attempts.`);\r\n }\r\n };\r\n\r\n getComponentInverseImpulseResponse = async ({\r\n payload,\r\n mls,\r\n lowHz,\r\n highHz,\r\n componentIRGains,\r\n iirLength,\r\n componentIRFreqs,\r\n num_periods,\r\n sampleRate,\r\n calibrateSoundBurstDb,\r\n irLength,\r\n calibrateSoundSmoothOctaves\r\n }) => {\r\n const task = 'component-inverse-impulse-response';\r\n let res = null;\r\n\r\n console.log({payload});\r\n\r\n const data = JSON.stringify({\r\n task,\r\n payload,\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,\r\n calibrateSoundBurstDb,\r\n irLength,\r\n calibrateSoundSmoothOctaves\r\n });\r\n\r\n await axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n method: 'post',\r\n baseURL: PythonServerAPI.PYTHON_SERVER_URL,\r\n url: `/task/${task}`,\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n data,\r\n })\r\n .then(response => {\r\n res = response;\r\n })\r\n .catch(error => {\r\n throw error;\r\n });\r\n\r\n return res.data[task];\r\n };\r\n getSystemInverseImpulseResponse = async ({\r\n payload,\r\n mls,\r\n lowHz,\r\n highHz,\r\n iirLength,\r\n num_periods,\r\n sampleRate,\r\n calibrateSoundBurstDb\r\n }) => {\r\n const task = 'system-inverse-impulse-response';\r\n let res = null;\r\n\r\n console.log({payload});\r\n\r\n const data = JSON.stringify({\r\n task,\r\n payload,\r\n mls,\r\n lowHz,\r\n iirLength,\r\n highHz,\r\n num_periods,\r\n sampleRate,\r\n calibrateSoundBurstDb\r\n });\r\n\r\n await axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n method: 'post',\r\n baseURL: PythonServerAPI.PYTHON_SERVER_URL,\r\n url: `/task/${task}`,\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n data,\r\n })\r\n .then(response => {\r\n res = response;\r\n })\r\n .catch(error => {\r\n throw error;\r\n });\r\n\r\n return res.data[task];\r\n };\r\n\r\n getMLSPSD = async ({mls, sampleRate}) => {\r\n const task = 'mls-psd';\r\n let res = null;\r\n\r\n const data = JSON.stringify({\r\n task,\r\n mls,\r\n sampleRate,\r\n });\r\n\r\n await axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n method: 'post',\r\n baseURL: PythonServerAPI.PYTHON_SERVER_URL,\r\n url: `/task/${task}`,\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n data,\r\n })\r\n .then(response => {\r\n res = response;\r\n })\r\n .catch(error => {\r\n throw error;\r\n });\r\n\r\n return res.data[task];\r\n };\r\n\r\n getMLSPSDWithRetry = async ({mls, sampleRate}) => {\r\n let retryCount = 0;\r\n let response = null;\r\n\r\n while (retryCount < this.MAX_RETRY_COUNT) {\r\n try {\r\n response = await this.getMLSPSD({mls, sampleRate});\r\n // If the request is successful, break out of the loop\r\n break;\r\n } catch (error) {\r\n console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);\r\n retryCount++;\r\n await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));\r\n }\r\n }\r\n\r\n if (response) {\r\n return response;\r\n } else {\r\n throw new Error(\r\n `Failed to get inverse impulse response after ${this.MAX_RETRY_COUNT} attempts.`\r\n );\r\n }\r\n };\r\n\r\n getComponentInverseImpulseResponseWithRetry = async ({\r\n payload,\r\n mls,\r\n lowHz,\r\n highHz,\r\n componentIRGains,\r\n iirLength,\r\n componentIRFreqs,\r\n num_periods,\r\n sampleRate,\r\n calibrateSoundBurstDb,\r\n irLength,\r\n calibrateSoundSmoothOctaves\r\n }) => {\r\n let retryCount = 0;\r\n let response = null;\r\n\r\n while (retryCount < this.MAX_RETRY_COUNT) {\r\n try {\r\n response = await this.getComponentInverseImpulseResponse({\r\n payload,\r\n mls,\r\n lowHz,\r\n highHz,\r\n componentIRGains,\r\n iirLength,\r\n componentIRFreqs,\r\n num_periods,\r\n sampleRate,\r\n calibrateSoundBurstDb,\r\n irLength,\r\n calibrateSoundSmoothOctaves\r\n });\r\n // If the request is successful, break out of the loop\r\n break;\r\n } catch (error) {\r\n console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);\r\n retryCount++;\r\n await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));\r\n }\r\n }\r\n\r\n if (response) {\r\n return response;\r\n } else {\r\n throw new Error(\r\n `Failed to get inverse impulse response after ${this.MAX_RETRY_COUNT} attempts.`\r\n );\r\n }\r\n };\r\n\r\n getSystemInverseImpulseResponseWithRetry = async ({\r\n payload,\r\n mls,\r\n lowHz,\r\n highHz,\r\n iirLength,\r\n num_periods,\r\n sampleRate,\r\n calibrateSoundBurstDb\r\n }) => {\r\n let retryCount = 0;\r\n let response = null;\r\n\r\n while (retryCount < this.MAX_RETRY_COUNT) {\r\n try {\r\n response = await this.getSystemInverseImpulseResponse({\r\n payload,\r\n mls,\r\n lowHz,\r\n highHz,\r\n iirLength,\r\n num_periods,\r\n sampleRate,\r\n calibrateSoundBurstDb\r\n });\r\n // If the request is successful, break out of the loop\r\n break;\r\n } catch (error) {\r\n console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);\r\n retryCount++;\r\n await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));\r\n }\r\n }\r\n\r\n if (response) {\r\n return response;\r\n } else {\r\n throw new Error(\r\n `Failed to get inverse impulse response after ${this.MAX_RETRY_COUNT} attempts.`\r\n );\r\n }\r\n };\r\n\r\n getVolumeCalibration = async ({payload, sampleRate, lCalib}) => {\r\n const task = 'volume';\r\n let res = null;\r\n\r\n console.log({payload});\r\n\r\n const data = JSON.stringify({\r\n task,\r\n payload,\r\n 'sample-rate': sampleRate,\r\n lCalib,\r\n });\r\n\r\n await axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n method: 'post',\r\n baseURL: PythonServerAPI.PYTHON_SERVER_URL,\r\n url: `/task/${task}`,\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n data,\r\n })\r\n .then(response => {\r\n res = response;\r\n })\r\n .catch(error => {\r\n throw error;\r\n });\r\n\r\n return res.data[task];\r\n };\r\n\r\n getVolumeCalibrationParameters = async ({\r\n inDBValues,\r\n outDBSPLValues,\r\n lCalib,\r\n componentGainDBSPL,\r\n }) => {\r\n const task = 'volume-parameters';\r\n let res = null;\r\n\r\n const data = JSON.stringify({\r\n task,\r\n inDBValues,\r\n outDBSPLValues,\r\n lCalib,\r\n componentGainDBSPL,\r\n });\r\n\r\n await axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n method: 'post',\r\n baseURL: PythonServerAPI.PYTHON_SERVER_URL, //server\r\n url: `/task/${task}`,\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n data,\r\n })\r\n .then(response => {\r\n res = response;\r\n console.log(res.data[task]);\r\n })\r\n .catch(error => {\r\n throw error;\r\n });\r\n\r\n // console.log(res.data[task]);\r\n //below is an example of res.data[task]\r\n //{\r\n // R: 16.56981076554259,\r\n // RMSError: 1.9289162528535229\r\n // T: -47.79799120884434,\r\n // W: 61.0485247483732,\r\n // backgroundDBSPL: 43.88233142069752,\r\n // gainDBSPL: -128.24742161208985\r\n //}\r\n return res.data[task];\r\n };\r\n\r\n allHzVolumeCheck = async ({\r\n payload, \r\n sampleRate, \r\n binDesiredSec,\r\n burstSec\r\n }) => {\r\n const task = 'volume-check';\r\n let res = null;\r\n\r\n const data = JSON.stringify({\r\n payload, \r\n sampleRate, \r\n binDesiredSec,\r\n burstSec\r\n });\r\n\r\n await axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n method: 'post',\r\n baseURL: PythonServerAPI.PYTHON_SERVER_URL, //server\r\n url: `/task/${task}`,\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n data,\r\n })\r\n .then(response => {\r\n res = response;\r\n console.log(res.data[task]);\r\n })\r\n .catch(error => {\r\n throw error;\r\n });\r\n return res.data[task];\r\n };\r\n}\r\n\r\n/* harmony default export */ __webpack_exports__[\"default\"] = (PythonServerAPI);\r\n\n\n//# sourceURL=webpack://speakerCalibrator/./src/server/PythonServerAPI.js?");
|
|
821
|
+
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var axios__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! axios */ \"./node_modules/axios/index.js\");\n/* harmony import */ var axios__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(axios__WEBPACK_IMPORTED_MODULE_0__);\n\r\n/**\r\n *\r\n */\r\nclass PythonServerAPI {\r\n static PYTHON_SERVER_URL = 'https://easyeyes-python-flask-server.herokuapp.com';\r\n\r\n static TEST_SERVER_URL = 'http://127.0.0.1:5000';\r\n // static PYTHON_SERVER_URL ='http://127.0.0.1:5000';\r\n\r\n /** @private */\r\n MAX_RETRY_COUNT = 3;\r\n /** @private */\r\n RETRY_DELAY_MS = 1000;\r\n /**\r\n * @param data- -\r\n * g = inverted impulse response, when convolved with the impulse\r\n * reponse, they cancel out.\r\n * @param data.payload\r\n * @param data.sampleRate\r\n * @param data.P\r\n * @param data-.payload\r\n * @param data-.sampleRate\r\n * @param data-.P\r\n * @returns\r\n * @example\r\n */\r\n getImpulseResponse = async ({\r\n mls, \r\n payload, \r\n sampleRate, \r\n P, \r\n numPeriods\r\n }) => {\r\n const task = 'impulse-response';\r\n let res = null;\r\n\r\n console.log({payload});\r\n\r\n const data = JSON.stringify({\r\n task,\r\n payload,\r\n 'sample-rate': sampleRate,\r\n mls,\r\n P,\r\n numPeriods,\r\n });\r\n\r\n await axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n method: 'post',\r\n baseURL: PythonServerAPI.PYTHON_SERVER_URL,\r\n url: `/task/${task}`,\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n data,\r\n })\r\n .then(response => {\r\n res = response;\r\n })\r\n .catch(error => {\r\n throw error;\r\n });\r\n return res.data[task];\r\n };\r\n\r\n getMLS = async ({length,calibrateSoundBurstDb}) => {\r\n const task = 'mls';\r\n let res = null;\r\n\r\n const data = JSON.stringify({\r\n task,\r\n length: length,\r\n calibrateSoundBurstDb: calibrateSoundBurstDb\r\n });\r\n\r\n await axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n method: 'post',\r\n baseURL: PythonServerAPI.PYTHON_SERVER_URL,\r\n url: `/task/${task}`,\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n data,\r\n })\r\n .then(response => {\r\n res = response;\r\n })\r\n .catch(error => {\r\n throw error;\r\n });\r\n\r\n return res.data[task];\r\n };\r\n getMLSWithRetry = async ({length,calibrateSoundBurstDb}) => {\r\n let retryCount = 0;\r\n let response = null;\r\n\r\n while (retryCount < this.MAX_RETRY_COUNT) {\r\n try {\r\n response = await this.getMLS({length,calibrateSoundBurstDb});\r\n // If the request is successful, break out of the loop\r\n break;\r\n } catch (error) {\r\n console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);\r\n retryCount++;\r\n await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));\r\n }\r\n }\r\n\r\n if (response) {\r\n return response;\r\n } else {\r\n throw new Error(`Failed to get MLS after ${this.MAX_RETRY_COUNT} attempts.`);\r\n }\r\n };\r\n\r\n getPSD = async ({unconv_rec, conv_rec, sampleRate}) => {\r\n const task = 'psd';\r\n let res = null;\r\n\r\n const data = JSON.stringify({\r\n task,\r\n unconv_rec,\r\n conv_rec,\r\n sampleRate,\r\n });\r\n\r\n await axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n method: 'post',\r\n baseURL: PythonServerAPI.PYTHON_SERVER_URL,\r\n url: `/task/${task}`,\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n data,\r\n })\r\n .then(response => {\r\n res = response;\r\n })\r\n .catch(error => {\r\n throw error;\r\n });\r\n return res.data[task];\r\n };\r\n\r\n getBackgroundNoisePSD = async ({background_rec, sampleRate}) => {\r\n const task = 'background-psd';\r\n let res = null;\r\n\r\n const data = JSON.stringify({\r\n task,\r\n background_rec,\r\n sampleRate,\r\n });\r\n\r\n await axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n method: 'post',\r\n baseURL: PythonServerAPI.PYTHON_SERVER_URL,\r\n url: `/task/${task}`,\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n data,\r\n })\r\n .then(response => {\r\n res = response;\r\n })\r\n .catch(error => {\r\n throw error;\r\n });\r\n return res.data[task];\r\n };\r\n\r\n getBackgroundNoisePSDWithRetry = async ({background_rec, sampleRate}) => {\r\n let retryCount = 0;\r\n let response = null;\r\n\r\n while (retryCount < this.MAX_RETRY_COUNT) {\r\n try {\r\n response = await this.getBackgroundNoisePSD({background_rec, sampleRate});\r\n // If the request is successful, break out of the loop\r\n break;\r\n } catch (error) {\r\n console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);\r\n retryCount++;\r\n await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));\r\n }\r\n }\r\n\r\n if (response) {\r\n return response;\r\n } else {\r\n throw new Error(`Failed to get PSD after ${this.MAX_RETRY_COUNT} attempts.`);\r\n }\r\n };\r\n\r\n getSubtractedPSD = async (rec, knownGains, knownFrequencies, sampleRate) => {\r\n const task = 'subtracted-psd';\r\n let res = null;\r\n\r\n const data = JSON.stringify({\r\n task,\r\n rec,\r\n knownGains,\r\n knownFrequencies,\r\n sampleRate,\r\n });\r\n\r\n await axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n method: 'post',\r\n baseURL: PythonServerAPI.PYTHON_SERVER_URL,\r\n url: `/task/${task}`,\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n data,\r\n })\r\n .then(response => {\r\n res = response;\r\n })\r\n .catch(error => {\r\n throw error;\r\n });\r\n return res.data[task];\r\n };\r\n\r\n getSubtractedPSDWithRetry = async (rec, knownGains, knownFrequencies, sampleRate) => {\r\n let retryCount = 0;\r\n let response = null;\r\n\r\n while (retryCount < this.MAX_RETRY_COUNT) {\r\n try {\r\n response = await this.getSubtractedPSD(rec, knownGains, knownFrequencies, sampleRate);\r\n // If the request is successful, break out of the loop\r\n break;\r\n } catch (error) {\r\n console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);\r\n retryCount++;\r\n await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));\r\n }\r\n }\r\n\r\n if (response) {\r\n return response;\r\n } else {\r\n throw new Error(`Failed to get PSD after ${this.MAX_RETRY_COUNT} attempts.`);\r\n }\r\n };\r\n\r\n getPSDWithRetry = async ({unconv_rec, conv_rec, sampleRate}) => {\r\n let retryCount = 0;\r\n let response = null;\r\n\r\n while (retryCount < this.MAX_RETRY_COUNT) {\r\n try {\r\n response = await this.getPSD({unconv_rec, conv_rec, sampleRate});\r\n // If the request is successful, break out of the loop\r\n break;\r\n } catch (error) {\r\n console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);\r\n retryCount++;\r\n await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));\r\n }\r\n }\r\n\r\n if (response) {\r\n return response;\r\n } else {\r\n throw new Error(`Failed to get PSD after ${this.MAX_RETRY_COUNT} attempts.`);\r\n }\r\n };\r\n\r\n getComponentInverseImpulseResponse = async ({\r\n payload,\r\n mls,\r\n lowHz,\r\n highHz,\r\n componentIRGains,\r\n iirLength,\r\n componentIRFreqs,\r\n num_periods,\r\n sampleRate,\r\n calibrateSoundBurstDb,\r\n irLength,\r\n calibrateSoundSmoothOctaves\r\n }) => {\r\n const task = 'component-inverse-impulse-response';\r\n let res = null;\r\n\r\n console.log({payload});\r\n\r\n const data = JSON.stringify({\r\n task,\r\n payload,\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,\r\n calibrateSoundBurstDb,\r\n irLength,\r\n calibrateSoundSmoothOctaves\r\n });\r\n\r\n await axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n method: 'post',\r\n baseURL: PythonServerAPI.PYTHON_SERVER_URL,\r\n url: `/task/${task}`,\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n data,\r\n })\r\n .then(response => {\r\n res = response;\r\n })\r\n .catch(error => {\r\n throw error;\r\n });\r\n\r\n return res.data[task];\r\n };\r\n getSystemInverseImpulseResponse = async ({\r\n payload,\r\n mls,\r\n lowHz,\r\n highHz,\r\n iirLength,\r\n num_periods,\r\n sampleRate,\r\n calibrateSoundBurstDb\r\n }) => {\r\n const task = 'system-inverse-impulse-response';\r\n let res = null;\r\n\r\n console.log({payload});\r\n\r\n const data = JSON.stringify({\r\n task,\r\n payload,\r\n mls,\r\n lowHz,\r\n iirLength,\r\n highHz,\r\n num_periods,\r\n sampleRate,\r\n calibrateSoundBurstDb\r\n });\r\n\r\n await axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n method: 'post',\r\n baseURL: PythonServerAPI.PYTHON_SERVER_URL,\r\n url: `/task/${task}`,\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n data,\r\n })\r\n .then(response => {\r\n res = response;\r\n })\r\n .catch(error => {\r\n throw error;\r\n });\r\n\r\n return res.data[task];\r\n };\r\n\r\n getMLSPSD = async ({mls, sampleRate}) => {\r\n const task = 'mls-psd';\r\n let res = null;\r\n\r\n const data = JSON.stringify({\r\n task,\r\n mls,\r\n sampleRate,\r\n });\r\n\r\n await axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n method: 'post',\r\n baseURL: PythonServerAPI.PYTHON_SERVER_URL,\r\n url: `/task/${task}`,\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n data,\r\n })\r\n .then(response => {\r\n res = response;\r\n })\r\n .catch(error => {\r\n throw error;\r\n });\r\n\r\n return res.data[task];\r\n };\r\n\r\n getMLSPSDWithRetry = async ({mls, sampleRate}) => {\r\n let retryCount = 0;\r\n let response = null;\r\n\r\n while (retryCount < this.MAX_RETRY_COUNT) {\r\n try {\r\n response = await this.getMLSPSD({mls, sampleRate});\r\n // If the request is successful, break out of the loop\r\n break;\r\n } catch (error) {\r\n console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);\r\n retryCount++;\r\n await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));\r\n }\r\n }\r\n\r\n if (response) {\r\n return response;\r\n } else {\r\n throw new Error(\r\n `Failed to get inverse impulse response after ${this.MAX_RETRY_COUNT} attempts.`\r\n );\r\n }\r\n };\r\n\r\n getComponentInverseImpulseResponseWithRetry = async ({\r\n payload,\r\n mls,\r\n lowHz,\r\n highHz,\r\n componentIRGains,\r\n iirLength,\r\n componentIRFreqs,\r\n num_periods,\r\n sampleRate,\r\n calibrateSoundBurstDb,\r\n irLength,\r\n calibrateSoundSmoothOctaves\r\n }) => {\r\n let retryCount = 0;\r\n let response = null;\r\n\r\n while (retryCount < this.MAX_RETRY_COUNT) {\r\n try {\r\n response = await this.getComponentInverseImpulseResponse({\r\n payload,\r\n mls,\r\n lowHz,\r\n highHz,\r\n componentIRGains,\r\n iirLength,\r\n componentIRFreqs,\r\n num_periods,\r\n sampleRate,\r\n calibrateSoundBurstDb,\r\n irLength,\r\n calibrateSoundSmoothOctaves\r\n });\r\n // If the request is successful, break out of the loop\r\n break;\r\n } catch (error) {\r\n console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);\r\n retryCount++;\r\n await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));\r\n }\r\n }\r\n\r\n if (response) {\r\n return response;\r\n } else {\r\n throw new Error(\r\n `Failed to get inverse impulse response after ${this.MAX_RETRY_COUNT} attempts.`\r\n );\r\n }\r\n };\r\n\r\n getSystemInverseImpulseResponseWithRetry = async ({\r\n payload,\r\n mls,\r\n lowHz,\r\n highHz,\r\n iirLength,\r\n num_periods,\r\n sampleRate,\r\n calibrateSoundBurstDb\r\n }) => {\r\n let retryCount = 0;\r\n let response = null;\r\n\r\n while (retryCount < this.MAX_RETRY_COUNT) {\r\n try {\r\n response = await this.getSystemInverseImpulseResponse({\r\n payload,\r\n mls,\r\n lowHz,\r\n highHz,\r\n iirLength,\r\n num_periods,\r\n sampleRate,\r\n calibrateSoundBurstDb\r\n });\r\n // If the request is successful, break out of the loop\r\n break;\r\n } catch (error) {\r\n console.error(`Error occurred. Retrying... (${retryCount + 1}/${this.MAX_RETRY_COUNT})`);\r\n retryCount++;\r\n await new Promise(resolve => setTimeout(resolve, this.RETRY_DELAY_MS));\r\n }\r\n }\r\n\r\n if (response) {\r\n return response;\r\n } else {\r\n throw new Error(\r\n `Failed to get inverse impulse response after ${this.MAX_RETRY_COUNT} attempts.`\r\n );\r\n }\r\n };\r\n\r\n getVolumeCalibration = async ({payload, sampleRate, lCalib}) => {\r\n const task = 'volume';\r\n let res = null;\r\n\r\n console.log({payload});\r\n\r\n const data = JSON.stringify({\r\n task,\r\n payload,\r\n 'sample-rate': sampleRate,\r\n lCalib,\r\n });\r\n\r\n await axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n method: 'post',\r\n baseURL: PythonServerAPI.PYTHON_SERVER_URL,\r\n url: `/task/${task}`,\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n data,\r\n })\r\n .then(response => {\r\n res = response;\r\n })\r\n .catch(error => {\r\n throw error;\r\n });\r\n\r\n return res.data[task];\r\n };\r\n\r\n getVolumeCalibrationParameters = async ({\r\n inDBValues,\r\n outDBSPLValues,\r\n lCalib,\r\n componentGainDBSPL,\r\n }) => {\r\n const task = 'volume-parameters';\r\n let res = null;\r\n\r\n const data = JSON.stringify({\r\n task,\r\n inDBValues,\r\n outDBSPLValues,\r\n lCalib,\r\n componentGainDBSPL,\r\n });\r\n\r\n await axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n method: 'post',\r\n baseURL: PythonServerAPI.PYTHON_SERVER_URL, //server\r\n url: `/task/${task}`,\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n data,\r\n })\r\n .then(response => {\r\n res = response;\r\n console.log(res.data[task]);\r\n })\r\n .catch(error => {\r\n throw error;\r\n });\r\n\r\n // console.log(res.data[task]);\r\n //below is an example of res.data[task]\r\n //{\r\n // R: 16.56981076554259,\r\n // RMSError: 1.9289162528535229\r\n // T: -47.79799120884434,\r\n // W: 61.0485247483732,\r\n // backgroundDBSPL: 43.88233142069752,\r\n // gainDBSPL: -128.24742161208985\r\n //}\r\n return res.data[task];\r\n };\r\n\r\n allHzPowerCheck = async ({\r\n payload, \r\n sampleRate, \r\n binDesiredSec,\r\n burstSec\r\n }) => {\r\n const task = 'all-hz-check';\r\n let res = null;\r\n\r\n const data = JSON.stringify({\r\n payload, \r\n sampleRate, \r\n binDesiredSec,\r\n burstSec\r\n });\r\n\r\n await axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n method: 'post',\r\n baseURL: PythonServerAPI.PYTHON_SERVER_URL, //server\r\n url: `/task/${task}`,\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n data,\r\n })\r\n .then(response => {\r\n res = response;\r\n console.log(res.data[task]);\r\n })\r\n .catch(error => {\r\n throw error;\r\n });\r\n return res.data[task];\r\n };\r\n\r\n volumePowerCheck = async ({\r\n payload, \r\n sampleRate, \r\n preSec,\r\n Sec,\r\n binDesiredSec\r\n }) => {\r\n const task = 'volume-check';\r\n let res = null;\r\n\r\n const data = JSON.stringify({\r\n payload, \r\n sampleRate, \r\n preSec,\r\n Sec,\r\n binDesiredSec\r\n });\r\n\r\n await axios__WEBPACK_IMPORTED_MODULE_0___default()({\r\n method: 'post',\r\n baseURL: PythonServerAPI.PYTHON_SERVER_URL, //server\r\n url: `/task/${task}`,\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n },\r\n data,\r\n })\r\n .then(response => {\r\n res = response;\r\n console.log(res.data[task]);\r\n })\r\n .catch(error => {\r\n throw error;\r\n });\r\n return res.data[task];\r\n };\r\n}\r\n\r\n/* harmony default export */ __webpack_exports__[\"default\"] = (PythonServerAPI);\r\n\n\n//# sourceURL=webpack://speakerCalibrator/./src/server/PythonServerAPI.js?");
|
|
822
822
|
|
|
823
823
|
/***/ }),
|
|
824
824
|
|
|
@@ -851,7 +851,7 @@ eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _myE
|
|
|
851
851
|
/***/ (function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
|
|
852
852
|
|
|
853
853
|
"use strict";
|
|
854
|
-
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/* harmony import */ var firebase_firestore__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! firebase/firestore */ \"./node_modules/firebase/firestore/dist/esm/index.esm.js\");\n\r\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 = 2] - 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 = 2,\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 componentIROrigin = {\r\n Freq: [],\r\n Gain: [],\r\n };\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 calibrateSound1000HzPreSec = 3.5;\r\n calibrateSound1000HzSec = 1.0;\r\n calibrateSound1000HzPostSec = 0.5;\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 irLength = 0;\r\n\r\n componentInvertedImpulseResponseNoBandpass = [];\r\n\r\n componentIRInTimeDomain = [];\r\n\r\n systemInvertedImpulseResponseNoBandpass = [];\r\n\r\n _calibrateSoundBackgroundSecs;\r\n\r\n _calibrateSoundSmoothOctaves;\r\n\r\n background_noise = {};\r\n\r\n numSuccessfulBackgroundCaptured;\r\n\r\n _calibrateSoundBurstDb;\r\n\r\n webAudioDeviceNames = {loudspeaker: '', microphone: '', loudspeakerText: '', microphoneText: ''};\r\n\r\n recordingChecks = {\r\n unfiltered: [],\r\n system: [],\r\n component: [],\r\n };\r\n\r\n soundCheck = '';\r\n\r\n filteredMLSRange = {\r\n component: {\r\n Min: null,\r\n Max: null,\r\n },\r\n system: {\r\n Min: null,\r\n Max: null,\r\n },\r\n };\r\n\r\n /** @private */\r\n timeStamp = [];\r\n\r\n /** @private */\r\n startTime;\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 reportWebAudioNames = `<br>${this.webAudioDeviceNames.loudspeakerText} <br> ${this.webAudioDeviceNames.microphoneText}`;\r\n const reportParameters = `<br> Sampling: Loudspeaker ${this.sourceSamplingRate} Hz, Microphone ${this.sinkSamplingRate} Hz, ${this.sampleSize} bits`;\r\n const template = `<div style=\"display: flex; justify-content: center; margin-top:12px;\"><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 reportWebAudioNames + reportParameters + 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 this.addTimeStamp('Get system iir');\r\n const computedIRs = await Promise.all(this.impulseResponses);\r\n const filteredComputedIRs = computedIRs.filter(element => {\r\n return element != undefined;\r\n }); //log any errors that are found in this step\r\n const mls = this.#mls;\r\n const lowHz = this.#lowHz; //gain of 1 below cutoff, need gain of 0\r\n const highHz = this.#highHz; //check error for anything other than 10 kHz\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 calibrateSoundBurstDb: this._calibrateSoundBurstDb,\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 this.systemInvertedImpulseResponseNoBandpass = res['iirNoBandpass'];\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 this.addTimeStamp('Get component iir');\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 irLength = this.irLength;\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 calibrateSoundBurstDb: this._calibrateSoundBurstDb,\r\n irLength,\r\n calibrateSoundSmoothOctaves: this._calibrateSoundSmoothOctaves,\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.componentIROrigin['Freq'] = res['frequencies'];\r\n this.componentIROrigin['Gain'] = res['irOrigin'];\r\n this.componentConvolution = res['convolution'];\r\n this.componentInvertedImpulseResponseNoBandpass = res['iirNoBandpass'];\r\n this.componentIRInTimeDomain = res['irTime'];\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 sendBackgroundRecording = () => {\r\n const allSignals = this.getAllBackgroundRecordings();\r\n const numSignals = allSignals.length;\r\n const background_rec_whole = allSignals[numSignals - 1];\r\n const fraction = 0.5 / (this._calibrateSoundBackgroundSecs + 0.5);\r\n // Calculate the starting index for slicing the array\r\n const startIndex = Math.round(fraction * background_rec_whole.length);\r\n // Slice the array from the calculated start index to the end of the array\r\n const background_rec = background_rec_whole.slice(startIndex);\r\n console.log('Sending background recording to server for processing');\r\n this.addTimeStamp('Get background PSD');\r\n this.pyServerAPI\r\n .getBackgroundNoisePSDWithRetry({\r\n background_rec,\r\n sampleRate: this.sourceSamplingRate || 96000,\r\n })\r\n .then(res => {\r\n if (this.numSuccessfulBackgroundCaptured < 1) {\r\n this.numSuccessfulBackgroundCaptured += 1;\r\n //storing all background data in background_psd object\r\n this.background_noise['x_background'] = res['x_background'];\r\n this.background_noise['y_background'] = res['y_background'];\r\n this.background_noise['recording'] = background_rec;\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 * .\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 = async signalCsv => {\r\n const allSignals = this.getAllUnfilteredRecordedSignals();\r\n console.log(\r\n 'Obtaining last all hz unfiltered recording from #allHzUnfilteredRecordings to send to server for processing'\r\n );\r\n const numSignals = allSignals.length;\r\n const mls = this.#mlsBufferView;\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 await this.pyServerAPI\r\n .allHzVolumeCheck({\r\n payload,\r\n sampleRate: this.sourceSamplingRate || 96000,\r\n binDesiredSec: this._calibrateSoundPowerBinDesiredSec,\r\n burstSec: this.desired_time_per_mls,\r\n })\r\n .then(result => {\r\n this.recordingChecks['unfiltered'].push(result);\r\n if (result['sd'] < this._calibrateSoundPowerDbSDToleratedDb) {\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, //get rid of this\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 } else if (result['sd'] > this._calibrateSoundPowerDbSDToleratedDb) {\r\n this.clearLastUnfilteredRecordedSignals();\r\n console.log('unfiltered rec', this.getAllUnfilteredRecordedSignals.length);\r\n }\r\n })\r\n .catch(err => {\r\n console.error(err);\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 //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 //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 time_to_wait =\r\n (this.#currentConvolution.length / this.sourceSamplingRate) * this.numMLSPerCapture;\r\n time_to_wait = time_to_wait * 1.1;\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 * Passed to the background noise recording function, awaits the desired amount of seconds to capture the desired number\r\n * of seconds of background noise\r\n *\r\n * @example\r\n */\r\n #awaitBackgroundNoiseRecording = async () => {\r\n console.log(\r\n 'Waiting ' + this._calibrateSoundBackgroundSecs + ' second(s) to record background noise'\r\n );\r\n let time_to_wait = this._calibrateSoundBackgroundSecs + 0.5;\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 time_to_sleep =\r\n (this.#currentConvolution.length / this.sourceSamplingRate) * number_of_bursts_to_skip;\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 = async () => {\r\n await this.checkPowerVariation();\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('length databuffer');\r\n console.log(dataBuffer.length);\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\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];\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\r\n if (this.mode === 'filtered') {\r\n //used to not loop filtered\r\n this.sourceNode.loop = true;\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.addTimeStamp('Play unfiltered mls');\r\n this.calibrationNodes[0].start(0);\r\n this.status = ``;\r\n if (this.mode === 'unfiltered') {\r\n console.log('mls', this.#mls); // before multiplied by calibrateSoundBurstDb\r\n console.log('mls buffer view', this.#mlsBufferView); // after multiplied by calibrateSoundBurstDb\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 console.log('sample size');\r\n console.log(this.sampleSize);\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, convolution) => {\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(convolution), // 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 bothSoundCheck = async stream => {\r\n let iir_ir_and_plots;\r\n this.#currentConvolution = this.componentConvolution;\r\n this.filteredMLSRange.component.Min = (0,_utils__WEBPACK_IMPORTED_MODULE_1__.findMinValue)(this.#currentConvolution);\r\n this.filteredMLSRange.component.Max = (0,_utils__WEBPACK_IMPORTED_MODULE_1__.findMaxValue)(this.#currentConvolution);\r\n this.addTimeStamp('Play MLS with component IIR');\r\n this.soundCheck = 'component';\r\n await this.playMLSwithIIR(stream, this.#currentConvolution);\r\n this.#stopCalibrationAudio();\r\n let component_conv_recs = this.getAllFilteredRecordedSignals();\r\n let return_component_conv_rec = component_conv_recs[component_conv_recs.length - 1];\r\n this.clearAllFilteredRecordedSignals();\r\n // await this.checkPowerVariation(return_component_conv_rec);\r\n this.numSuccessfulCaptured = 0;\r\n this.#currentConvolution = this.systemConvolution;\r\n this.filteredMLSRange.system.Min = (0,_utils__WEBPACK_IMPORTED_MODULE_1__.findMinValue)(this.#currentConvolution);\r\n this.filteredMLSRange.system.Max = (0,_utils__WEBPACK_IMPORTED_MODULE_1__.findMaxValue)(this.#currentConvolution);\r\n this.soundCheck = 'system';\r\n this.addTimeStamp('Play MLS with system IIR');\r\n await this.playMLSwithIIR(stream, this.#currentConvolution);\r\n\r\n this.#stopCalibrationAudio();\r\n\r\n let system_conv_recs = this.getAllFilteredRecordedSignals();\r\n let return_system_conv_rec = system_conv_recs[system_conv_recs.length - 1];\r\n // await this.checkPowerVariation(return_system_conv_rec);\r\n\r\n this.clearAllFilteredRecordedSignals();\r\n\r\n this.sourceAudioContext.close();\r\n let recs = this.getAllUnfilteredRecordedSignals();\r\n let unconv_rec = recs[0];\r\n let return_unconv_rec = unconv_rec;\r\n let conv_rec = component_conv_recs[component_conv_recs.length - 1];\r\n\r\n //psd of component\r\n let knownGain = this.oldComponentIR.Gain;\r\n let knownFreq = this.oldComponentIR.Freq;\r\n let sampleRate = this.sourceSamplingRate || 96000;\r\n this.addTimeStamp('Get PSD of mls recording');\r\n let component_unconv_rec_psd = 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 this.addTimeStamp('Get PSD of filtered recording (component)');\r\n let component_conv_rec_psd = 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\r\n conv_rec = system_conv_recs[system_conv_recs.length - 1];\r\n //psd of system\r\n this.addTimeStamp('Get PSD of filtered recording (system) and unfiltered recording');\r\n let system_recs_psd = 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\r\n //iir w/ and without bandpass psd. done\r\n unconv_rec = this.componentInvertedImpulseResponseNoBandpass;\r\n conv_rec = this.componentInvertedImpulseResponse;\r\n this.addTimeStamp('Get PSD of component iir and component iir no band pass');\r\n let component_iir_psd = 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 unconv_rec = this.systemInvertedImpulseResponseNoBandpass;\r\n conv_rec = this.systemInvertedImpulseResponse;\r\n this.addTimeStamp('Get PSD of system iir and system iir no band pass');\r\n let system_iir_psd = 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\r\n this.addTimeStamp('Get PSD of mls sequence');\r\n let mls_psd = await this.pyServerAPI\r\n .getMLSPSDWithRetry({mls: this.#mlsBufferView, sampleRate: this.sourceSamplingRate || 96000})\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 this.addTimeStamp('Get PSD of filered mls (system)');\r\n let system_filtered_mls_psd = await this.pyServerAPI\r\n .getMLSPSDWithRetry({\r\n mls: this.systemConvolution,\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\r\n this.addTimeStamp('Get PSD of filered mls (component)');\r\n let component_filtered_mls_psd = await this.pyServerAPI\r\n .getMLSPSDWithRetry({\r\n mls: this.componentConvolution,\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\r\n let gainValue = this.getGainDBSPL();\r\n\r\n iir_ir_and_plots = {\r\n filtered_recording: {\r\n component: return_component_conv_rec,\r\n system: return_system_conv_rec,\r\n },\r\n unfiltered_recording: this.getAllUnfilteredRecordedSignals()[0],\r\n system: {\r\n iir: this.systemInvertedImpulseResponse,\r\n ir: this.systemIR,\r\n iir_psd: {\r\n y: system_iir_psd['y_conv'],\r\n x: system_iir_psd['x_conv'],\r\n y_no_bandpass: system_iir_psd['y_unconv'],\r\n x_no_bandpass: system_iir_psd['x_unconv'],\r\n },\r\n filtered_mls_psd: {\r\n x: system_filtered_mls_psd['x_mls'],\r\n y: system_filtered_mls_psd['y_mls'],\r\n },\r\n convolution: this.systemConvolution,\r\n psd: {\r\n unconv: {\r\n x: system_recs_psd['x_unconv'],\r\n y: system_recs_psd['y_unconv'],\r\n },\r\n conv: {\r\n x: system_recs_psd['x_conv'],\r\n y: system_recs_psd['y_conv'],\r\n },\r\n },\r\n },\r\n component: {\r\n iir: this.componentInvertedImpulseResponse,\r\n ir: this.componentIR,\r\n ir_origin: this.componentIROrigin,\r\n ir_in_time_domain: this.componentIRInTimeDomain,\r\n iir_psd: {\r\n y: component_iir_psd['y_conv'],\r\n x: component_iir_psd['x_conv'],\r\n y_no_bandpass: component_iir_psd['y_unconv'],\r\n x_no_bandpass: component_iir_psd['x_unconv'],\r\n },\r\n filtered_mls_psd: {\r\n x: component_filtered_mls_psd['x_mls'],\r\n y: component_filtered_mls_psd['y_mls'],\r\n },\r\n convolution: this.componentConvolution,\r\n psd: {\r\n unconv: {\r\n x: component_unconv_rec_psd['x'],\r\n y: component_unconv_rec_psd['y'],\r\n },\r\n conv: {\r\n x: component_conv_rec_psd['x'],\r\n y: component_conv_rec_psd['y'],\r\n },\r\n },\r\n gainDBSPL: gainValue,\r\n },\r\n mls: this.#mlsBufferView,\r\n mls_psd: {\r\n x: mls_psd['x_mls'],\r\n y: mls_psd['y_mls'],\r\n },\r\n autocorrelations: this.autocorrelations,\r\n impulseResponses: [],\r\n };\r\n\r\n return iir_ir_and_plots;\r\n };\r\n\r\n singleSoundCheck = async stream => {\r\n let iir_ir_and_plots;\r\n if (this._calibrateSoundCheck != 'system') {\r\n this.#currentConvolution = this.componentConvolution;\r\n this.filteredMLSRange.component.Min = (0,_utils__WEBPACK_IMPORTED_MODULE_1__.findMinValue)(this.#currentConvolution);\r\n this.filteredMLSRange.component.Max = (0,_utils__WEBPACK_IMPORTED_MODULE_1__.findMaxValue)(this.#currentConvolution);\r\n this.addTimeStamp('Play MLS with component IIR');\r\n this.soundCheck = 'component';\r\n await this.playMLSwithIIR(stream, this.#currentConvolution);\r\n this.#stopCalibrationAudio();\r\n } else {\r\n this.#currentConvolution = this.systemConvolution;\r\n this.filteredMLSRange.system.Min = (0,_utils__WEBPACK_IMPORTED_MODULE_1__.findMinValue)(this.#currentConvolution);\r\n this.filteredMLSRange.system.Max = (0,_utils__WEBPACK_IMPORTED_MODULE_1__.findMaxValue)(this.#currentConvolution);\r\n this.addTimeStamp('Play MLS with system IIR');\r\n this.soundCheck = 'systen';\r\n await this.playMLSwithIIR(stream, this.#currentConvolution);\r\n this.#stopCalibrationAudio();\r\n }\r\n let conv_recs = this.getAllFilteredRecordedSignals();\r\n let recs = this.getAllUnfilteredRecordedSignals();\r\n this.clearAllFilteredRecordedSignals();\r\n console.log('Obtaining unfiltered recording from #allHzUnfilteredRecordings to calculate PSD');\r\n console.log('Obtaining filtered recording from #allHzFilteredRecordings to calculate PSD');\r\n let unconv_rec = recs[0];\r\n let return_unconv_rec = unconv_rec;\r\n let conv_rec = conv_recs[conv_recs.length - 1];\r\n let return_conv_rec = conv_rec;\r\n this.sourceAudioContext.close();\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 this.addTimeStamp('Get PSD of mls recording');\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 this.addTimeStamp('Get PSD recording of filtered recording (component)');\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\r\n unconv_rec = this.componentInvertedImpulseResponseNoBandpass;\r\n conv_rec = this.componentInvertedImpulseResponse;\r\n this.addTimeStamp('Get PSD of component iir and component iir no bandpass');\r\n let component_iir_psd = 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 unconv_rec = this.systemInvertedImpulseResponseNoBandpass;\r\n conv_rec = this.systemInvertedImpulseResponse;\r\n this.addTimeStamp('Get PSD of system iir and system iir no bandpass');\r\n let system_iir_psd = 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\r\n this.addTimeStamp('Get PSD of mls sequence');\r\n let mls_psd = await this.pyServerAPI\r\n .getMLSPSDWithRetry({\r\n mls: this.#mlsBufferView,\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\r\n this.addTimeStamp('Get PSD of filtered mls (component)');\r\n let filtered_mls_psd = await this.pyServerAPI\r\n .getMLSPSDWithRetry({\r\n mls: this.componentConvolution,\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\r\n let gainValue = this.getGainDBSPL();\r\n iir_ir_and_plots = {\r\n unfiltered_recording: return_unconv_rec,\r\n filtered_recording: return_conv_rec,\r\n system: {\r\n iir: this.systemInvertedImpulseResponse,\r\n ir: this.systemIR,\r\n iir_psd: {\r\n y: system_iir_psd['y_conv'],\r\n x: system_iir_psd['y_conv'],\r\n y_no_bandpass: system_iir_psd['y_unconv'],\r\n x_no_bandpass: system_iir_psd['x_unconv'],\r\n },\r\n filtered_recording: [],\r\n filtered_mls_psd: {},\r\n convolution: this.systemConvolution,\r\n psd: {\r\n unconv: {\r\n x: [],\r\n y: [],\r\n },\r\n conv: {\r\n x: [],\r\n y: [],\r\n },\r\n },\r\n },\r\n component: {\r\n iir: this.componentInvertedImpulseResponse,\r\n ir: this.componentIR,\r\n ir_origin: this.componentIROrigin,\r\n ir_in_time_domain: this.componentIRInTimeDomain,\r\n iir_psd: {\r\n y: component_iir_psd['y_conv'],\r\n x: component_iir_psd['x_conv'],\r\n y_no_bandpass: component_iir_psd['y_unconv'],\r\n x_no_bandpass: component_iir_psd['x_unconv'],\r\n },\r\n filtered_mls_psd: {\r\n x: filtered_mls_psd['x_mls'],\r\n y: filtered_mls_psd['y_mls'],\r\n },\r\n convolution: this.componentConvolution,\r\n psd: {\r\n unconv: {\r\n x: unconv_results['x'],\r\n y: unconv_results['y'],\r\n },\r\n conv: {\r\n x: conv_results['x'],\r\n y: conv_results['y'],\r\n },\r\n },\r\n gainDBSPL: gainValue,\r\n },\r\n mls: this.#mlsBufferView,\r\n mls_psd: {\r\n x: mls_psd['x_mls'],\r\n y: mls_psd['y_mls'],\r\n },\r\n autocorrelations: this.autocorrelations,\r\n impulseResponses: [],\r\n };\r\n } else {\r\n this.addTimeStamp('Get PSD of filtered recording (system) and unfiltered recording');\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\r\n //iir w/ and without bandpass psd\r\n unconv_rec = this.componentInvertedImpulseResponseNoBandpass;\r\n conv_rec = this.componentInvertedImpulseResponse;\r\n this.addTimeStamp('Get PSD of component iir and component iir no band pass');\r\n let component_iir_psd = 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 unconv_rec = this.systemInvertedImpulseResponseNoBandpass;\r\n conv_rec = this.systemInvertedImpulseResponse;\r\n this.addTimeStamp('Get PSD of system iir and system iir no band pass');\r\n let system_iir_psd = 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\r\n this.addTimeStamp('Get PSD of mls sequence');\r\n let mls_psd = await this.pyServerAPI\r\n .getMLSPSDWithRetry({\r\n mls: this.#mlsBufferView,\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\r\n this.addTimeStamp('Get PSD of filtered mls (system)');\r\n let filtered_mls_psd = await this.pyServerAPI\r\n .getMLSPSDWithRetry({\r\n mls: this.systemConvolution,\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\r\n let gainValue = this.getGainDBSPL();\r\n iir_ir_and_plots = {\r\n unfiltered_recording: return_unconv_rec,\r\n filtered_recording: return_conv_rec,\r\n system: {\r\n iir: this.systemInvertedImpulseResponse,\r\n ir: this.systemIR,\r\n iir_psd: {\r\n y: system_iir_psd['y_conv'],\r\n x: system_iir_psd['y_conv'],\r\n y_no_bandpass: system_iir_psd['y_unconv'],\r\n x_no_bandpass: system_iir_psd['x_unconv'],\r\n },\r\n filtered_recording: [],\r\n filtered_mls_psd: {\r\n x: filtered_mls_psd['x_mls'],\r\n y: filtered_mls_psd['y_mls'],\r\n },\r\n convolution: this.systemConvolution,\r\n psd: {\r\n unconv: {\r\n x: results['x_unconv'],\r\n y: results['y_unconv'],\r\n },\r\n conv: {\r\n x: results['x_conv'],\r\n y: results['y_conv'],\r\n },\r\n },\r\n },\r\n component: {\r\n iir: this.componentInvertedImpulseResponse,\r\n ir: this.componentIR,\r\n ir_origin: this.componentIROrigin,\r\n ir_in_time_domain: this.componentIRInTimeDomain,\r\n iir_psd: {\r\n y: component_iir_psd['y_conv'],\r\n x: component_iir_psd['x_conv'],\r\n y_no_bandpass: component_iir_psd['y_unconv'],\r\n x_no_bandpass: component_iir_psd['x_unconv'],\r\n },\r\n filtered_mls_psd: {},\r\n convolution: this.componentConvolution,\r\n psd: {\r\n unconv: {\r\n x: [],\r\n y: [],\r\n },\r\n conv: {\r\n x: [],\r\n y: [],\r\n },\r\n },\r\n gainDBSPL: gainValue,\r\n },\r\n mls: this.#mlsBufferView,\r\n mls_psd: {\r\n x: mls_psd['x_mls'],\r\n y: mls_psd['y_mls'],\r\n },\r\n autocorrelations: this.autocorrelations,\r\n impulseResponses: [],\r\n };\r\n }\r\n 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 iir_ir_and_plots['impulseResponses'].push(res[i]);\r\n }\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\r\n return iir_ir_and_plots;\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 const calibrateSoundBurstDb = this._calibrateSoundBurstDb;\r\n this.addTimeStamp('Get MLS sequence');\r\n await this.pyServerAPI\r\n .getMLSWithRetry({length, calibrateSoundBurstDb})\r\n .then(res => {\r\n console.log(res);\r\n this.#mlsBufferView = res['mls'];\r\n this.#mls = res['unscaledMLS'];\r\n })\r\n .catch(err => {\r\n // this.emit('InvertedImpulseResponse', {res: false});\r\n console.error(err);\r\n });\r\n this.numSuccessfulBackgroundCaptured = 0;\r\n if (this._calibrateSoundBackgroundSecs > 0) {\r\n this.mode = 'background';\r\n this.status =\r\n `All Hz Calibration: sampling the background noise...`.toString() +\r\n this.generateTemplate().toString();\r\n this.emit('update', {message: this.status});\r\n await this.recordBackground(\r\n stream, //stream\r\n () => this.numSuccessfulBackgroundCaptured < 1, //loop condition\r\n this.#awaitBackgroundNoiseRecording, //sleep to record\r\n this.sendBackgroundRecording, //send to get PSD\r\n this.mode,\r\n checkRec\r\n );\r\n this.incrementStatusBar();\r\n }\r\n this.mode = 'unfiltered';\r\n this.numSuccessfulCaptured = 0;\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 //do single check\r\n if (this._calibrateSoundCheck == 'goal' || this._calibrateSoundCheck == 'system') {\r\n iir_ir_and_plots = await this.singleSoundCheck(stream);\r\n } else {\r\n //both\r\n iir_ir_and_plots = await this.bothSoundCheck(stream);\r\n }\r\n } else {\r\n let unconv_rec = this.componentInvertedImpulseResponseNoBandpass;\r\n let conv_rec = this.componentInvertedImpulseResponse;\r\n let component_iir_psd = 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 unconv_rec = this.systemInvertedImpulseResponseNoBandpass;\r\n conv_rec = this.systemInvertedImpulseResponse;\r\n let system_iir_psd = 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\r\n let gainValue = this.getGainDBSPL();\r\n iir_ir_and_plots = {\r\n unfiltered_recording: return_unconv_rec,\r\n filtered_recording: return_conv_rec,\r\n system: {\r\n iir: this.systemInvertedImpulseResponse,\r\n ir: this.systemIR,\r\n iir_psd: {\r\n y: system_iir_psd['y_conv'],\r\n x: system_iir_psd['y_conv'],\r\n y_no_bandpass: system_iir_psd['y_unconv'],\r\n x_no_bandpass: system_iir_psd['x_unconv'],\r\n },\r\n filtered_recording: [],\r\n convolution: this.systemConvolution,\r\n psd: {\r\n unconv: {\r\n x: [],\r\n y: [],\r\n },\r\n conv: {\r\n x: [],\r\n y: [],\r\n },\r\n },\r\n },\r\n component: {\r\n iir: this.componentInvertedImpulseResponse,\r\n ir: this.componentIR,\r\n ir_in_time_domain: this.componentIRInTimeDomain,\r\n iir_psd: {\r\n y: component_iir_psd['y_conv'],\r\n x: component_iir_psd['x_conv'],\r\n y_no_bandpass: component_iir_psd['y_unconv'],\r\n x_no_bandpass: component_iir_psd['x_unconv'],\r\n },\r\n convolution: this.componentConvolution,\r\n psd: {\r\n unconv: {\r\n x: [],\r\n y: [],\r\n },\r\n conv: {\r\n x: [],\r\n y: [],\r\n },\r\n },\r\n gainDBSPL: gainValue,\r\n },\r\n mls: this.#mlsBufferView,\r\n autocorrelations: this.autocorrelations,\r\n impulseResponses: [],\r\n };\r\n 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 iir_ir_and_plots['impulseResponses'].push(res[i]);\r\n }\r\n }\r\n });\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 // saveToJSON(iir_ir_and_plots);\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.getLastVolumeRecordedSignal().slice(start, end));\r\n console.log(\r\n 'Obtaining last 1000 hz recording from #allVolumeRecordings to send for processing'\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.getAllVolumeRecordedSignals()\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.addTimeStamp('Send volume data to server');\r\n let left = this.calibrateSound1000HzPreSec;\r\n let right = this.calibrateSound1000HzPreSec + this.calibrateSound1000HzSec;\r\n this.pyServerAPI\r\n .getVolumeCalibration({\r\n sampleRate: this.sourceSamplingRate,\r\n payload: this.#getTruncatedSignal(left, right),\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 this.startTime = new Date().getTime();\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 this.addTimeStamp('Get Volume Calibration Parameters');\r\n\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 writeFrqGainToFirestore = async (speakerID, frq, gain, OEM, documentID) => {\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 // update Microphone/OEM/speakerID/default/linear\r\n const docRef = (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.doc)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"], 'Microphone', OEM, speakerID, documentID);\r\n await (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.updateDoc)(docRef, {\r\n linear: data,\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 readFrqGainFromFirestore = async (speakerID, OEM, documentID) => {\r\n const docRef = (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.doc)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"], 'Microphone', OEM, speakerID, documentID);\r\n\r\n const docSnap = await (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.getDoc)(docRef);\r\n\r\n if (docSnap.exists()) {\r\n return docSnap.data().linear;\r\n } else {\r\n return null;\r\n }\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 readGainat1000HzFromFirestore = async (speakerID, OEM, documentID) => {\r\n const docRef = (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.doc)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"], 'Microphone', OEM, speakerID, documentID);\r\n const docSnap = await (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.getDoc)(docRef);\r\n\r\n if (docSnap.exists()) {\r\n return docSnap.data().Gain1000;\r\n } else {\r\n return null;\r\n }\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 writeGainat1000HzToFirestore = async (speakerID, gain, OEM, documentID) => {\r\n const docRef = (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.doc)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"], 'Microphone', OEM, speakerID, documentID);\r\n\r\n await (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.updateDoc)(docRef, {\r\n Gain1000: gain,\r\n });\r\n };\r\n\r\n writeGainat1000Hz = async (speakerID, gain, OEM) => {\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 writeIsSmartPhoneToFirestore = async (speakerID, isSmartPhone, OEM) => {\r\n // if Microphone/OEM/speakerID/default exists, leave it alone and create a new document at Microphone/OEM/speakerID and return the id of the new document\r\n const OEMdocRef = (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.doc)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"], 'Microphone', OEM);\r\n const OEMdocSnap = await (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.getDoc)(OEMdocRef);\r\n // if OEM does not exist, create it with dummy field\r\n if (!OEMdocSnap.exists()) {\r\n await (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.setDoc)(OEMdocRef, {dummy: 'dummy'});\r\n }\r\n // save the collectionIDs in the OEM document as a field. If the field already exists, add the new collectionID to the array\r\n await (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.updateDoc)(OEMdocRef, {\r\n collectionIDs: (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.arrayUnion)(speakerID),\r\n });\r\n const docRef = (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.doc)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"], 'Microphone', OEM, speakerID, 'default');\r\n const docSnap = await (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.getDoc)(docRef);\r\n\r\n if (docSnap.exists()) {\r\n // add new document\r\n const collectionRef = (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.collection)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"], 'Microphone', OEM, speakerID);\r\n // add the new document and return the id\r\n const docRef = await (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.addDoc)(collectionRef, {isSmartPhone: isSmartPhone});\r\n return docRef.id;\r\n } else {\r\n // create document at Microphone/OEM/speakerID/default\r\n await (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.setDoc)(docRef, {isSmartPhone: isSmartPhone});\r\n return 'default';\r\n }\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 writeMicrophoneInfoToFirestore = async (speakerID, micInfo, OEM, documentID) => {\r\n const docRef = (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.doc)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"], 'Microphone', OEM, speakerID, documentID);\r\n await (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.updateDoc)(docRef, {\r\n info: micInfo,\r\n });\r\n };\r\n\r\n doesMicrophoneExistInFirestore = async (speakerID, OEM, documentID) => {\r\n const docRef = (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.doc)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"], 'Microphone', OEM, speakerID, documentID);\r\n const docSnap = await (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.getDoc)(docRef);\r\n if (docSnap.exists()) {\r\n return true;\r\n }\r\n return false;\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 addMicrophoneInfo = async (speakerID, OEM, micInfo) => {\r\n // add to database if /info does not exist\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}/info`));\r\n if (!snapshot.exists()) {\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}/info`), micInfo);\r\n }\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 // add time stamp\r\n addTimeStamp = taskName => {\r\n let startTaskTime = (new Date().getTime() - this.startTime) / 1000;\r\n this.timeStamp.push(`SOUND ${Number(startTaskTime.toFixed(1))} s. ${taskName}`);\r\n };\r\n\r\n checkPowerVariation = async () => {\r\n const recordings = this.getAllFilteredRecordedSignals();\r\n const rec = recordings[recordings.length - 1];\r\n console.log(rec);\r\n await this.pyServerAPI\r\n .allHzVolumeCheck({\r\n payload: rec,\r\n sampleRate: this.sourceSamplingRate || 96000,\r\n binDesiredSec: this._calibrateSoundPowerBinDesiredSec,\r\n burstSec: this.desired_time_per_mls,\r\n })\r\n .then(result => {\r\n this.recordingChecks[this.soundCheck].push(result);\r\n if (result['sd'] > this._calibrateSoundPowerDbSDToleratedDb) {\r\n console.log('filtered recording sd too high');\r\n } else {\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 getGainDBSPL = () => {\r\n var freqIndex = this.componentIR.Freq.indexOf(1000);\r\n\r\n // If freqIndex is not -1 (meaning 1000 is found in the freq array)\r\n if (freqIndex !== -1) {\r\n // Get the corresponding gain value using the index\r\n var gainValue = this.componentIR.Gain[freqIndex];\r\n return gainValue;\r\n } else {\r\n console.log('Freq 1000 not found in the array.');\r\n return null;\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 _calibrateSoundBurstDb = 0.1,\r\n _calibrateSoundBurstRepeats = 3,\r\n _calibrateSoundBurstSec = 1,\r\n _calibrateSoundBurstsWarmup = 1,\r\n _calibrateSoundHz = 48000,\r\n _calibrateSoundIIRSec = 0.2,\r\n _calibrateSoundIRSec = 0.05,\r\n calibrateSound1000HzPreSec = 3.5,\r\n calibrateSound1000HzSec = 1.0,\r\n calibrateSound1000HzPostSec = 0.5,\r\n _calibrateSoundBackgroundSecs = 0,\r\n _calibrateSoundSmoothOctaves = 0.33,\r\n _calibrateSoundPowerBinDesiredSec = 0.2,\r\n _calibrateSoundPowerDbSDToleratedDb = 1,\r\n micManufacturer = '',\r\n micSerialNumber = '',\r\n micModelNumber = '',\r\n micModelName = '',\r\n calibrateMicrophonesBool,\r\n authorEmails,\r\n webAudioDeviceNames,\r\n userIDs\r\n ) => {\r\n this._calibrateSoundBurstDb = _calibrateSoundBurstDb;\r\n this.CALIBRATION_TONE_DURATION =\r\n calibrateSound1000HzPreSec + calibrateSound1000HzSec + calibrateSound1000HzPostSec;\r\n this.calibrateSound1000HzPreSec = calibrateSound1000HzPreSec;\r\n this.calibrateSound1000HzSec = calibrateSound1000HzSec;\r\n this.calibrateSound1000HzPostSec = calibrateSound1000HzPostSec;\r\n this.iirLength = Math.floor(_calibrateSoundIIRSec * this.sourceSamplingRate);\r\n this.irLength = Math.floor(_calibrateSoundIRSec * 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 this._calibrateSoundBackgroundSecs = _calibrateSoundBackgroundSecs;\r\n this._calibrateSoundSmoothOctaves = _calibrateSoundSmoothOctaves;\r\n this._calibrateSoundPowerBinDesiredSec = _calibrateSoundPowerBinDesiredSec;\r\n this._calibrateSoundPowerDbSDToleratedDb = _calibrateSoundPowerDbSDToleratedDb;\r\n this.webAudioDeviceNames = webAudioDeviceNames;\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\r\n ? micModelName === 'umik-1' || micModelName === 'umik-2'\r\n ? 'minidsp'\r\n : this.deviceInfo.OEM.toLowerCase().split(' ').join('')\r\n : micManufacturer;\r\n // const ID = \"711-4754\";\r\n // const OEM = \"minidsp\";\r\n const micInfo = {\r\n micModelName: isSmartPhone ? micModelName : microphoneName,\r\n OEM: isSmartPhone\r\n ? micModelName === 'umik-1' || micModelName === 'umik-2'\r\n ? 'miniDSP'\r\n : this.deviceInfo.OEM\r\n : micManufacturer,\r\n ID: ID,\r\n HardwareName: isSmartPhone ? this.deviceInfo.hardwarename : microphoneName,\r\n hardwareFamily: isSmartPhone ? this.deviceInfo.hardwarefamily : microphoneName,\r\n HardwareModel: isSmartPhone ? this.deviceInfo.hardwaremodel : microphoneName,\r\n PlatformName: isSmartPhone ? this.deviceInfo.platformname : 'N/A',\r\n PlatformVersion: isSmartPhone ? this.deviceInfo.platformversion : 'N/A',\r\n DeviceType: isSmartPhone ? this.deviceInfo.devicetype : 'N/A',\r\n ID_from_51Degrees: isSmartPhone ? this.deviceInfo.DeviceId : 'N/A',\r\n calibrateMicrophonesBool: calibrateMicrophonesBool,\r\n webAudioDeviceNames: {\r\n loudspeaker: this.webAudioDeviceNames.loudspeaker,\r\n microphone: this.webAudioDeviceNames.microphone,\r\n },\r\n userIDs: userIDs,\r\n };\r\n if (calibrateMicrophonesBool) {\r\n micInfo['authorEmails'] = authorEmails;\r\n }\r\n // if undefined in micInfo, set to empty string\r\n for (const [key, value] of Object.entries(micInfo)) {\r\n if (value === undefined) {\r\n micInfo[key] = '';\r\n }\r\n }\r\n\r\n // this.writeMicrophoneInfoToFirestore(ID, micInfo, OEM, 'default');\r\n // this.addMicrophoneInfo(ID, OEM, micInfo);\r\n if (componentIR == null) {\r\n //mode 'ir'\r\n //global variable this.componentIR must be set\r\n this.componentIR = await this.readFrqGainFromFirestore(ID, OEM, 'default').then(data => {\r\n return data;\r\n });\r\n // 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 lCalib = await this.readGainat1000HzFromFirestore(ID, OEM, 'default');\r\n micInfo['gainDBSPL'] = lCalib;\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.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 impulseResponseResults['background_noise'] = this.background_noise;\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.component.ir.Freq,\r\n // impulseResponseResults.component.ir.Gain,\r\n // OEM\r\n // );\r\n const id = await this.writeIsSmartPhoneToFirestore(ID, isSmartPhone, OEM);\r\n await this.writeMicrophoneInfoToFirestore(ID, micInfo, OEM, id);\r\n await this.writeFrqGainToFirestore(\r\n ID,\r\n impulseResponseResults.component.ir.Freq,\r\n impulseResponseResults.component.ir.Gain,\r\n OEM,\r\n id\r\n );\r\n micInfo['gainDBSPL'] = impulseResponseResults.component.gainDBSPL;\r\n await this.writeGainat1000HzToFirestore(ID, micInfo['gainDBSPL'], OEM, id);\r\n // await this.writeGainat1000Hz(ID, micInfo['gainDBSPL'], OEM);\r\n }\r\n const total_results = {...volumeResults, ...impulseResponseResults};\r\n total_results['filteredMLSRange'] = this.filteredMLSRange;\r\n total_results['micInfo'] = micInfo;\r\n total_results['audioInfo'] = {};\r\n total_results['audioInfo']['sinkSampleRate'] = this.sinkSamplingRate;\r\n total_results['audioInfo']['sourceSampleRate'] = this.sourceSamplingRate;\r\n total_results['audioInfo']['bitsPerSample'] = this.sampleSize;\r\n const timeStampresult = [...this.timeStamp].join('\\n');\r\n total_results['timeStamps'] = timeStampresult;\r\n total_results['recordingChecks'] = this.recordingChecks;\r\n console.log('total results');\r\n console.log(total_results);\r\n console.log('Time Stamps');\r\n console.log(timeStampresult);\r\n\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?");
|
|
854
|
+
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/* harmony import */ var firebase_firestore__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! firebase/firestore */ \"./node_modules/firebase/firestore/dist/esm/index.esm.js\");\n\r\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 = 2] - 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 = 2,\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 componentIROrigin = {\r\n Freq: [],\r\n Gain: [],\r\n };\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 calibrateSound1000HzPreSec = 3.5;\r\n calibrateSound1000HzSec = 1.0;\r\n calibrateSound1000HzPostSec = 0.5;\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 irLength = 0;\r\n\r\n componentInvertedImpulseResponseNoBandpass = [];\r\n\r\n componentIRInTimeDomain = [];\r\n\r\n systemInvertedImpulseResponseNoBandpass = [];\r\n\r\n _calibrateSoundBackgroundSecs;\r\n\r\n _calibrateSoundSmoothOctaves;\r\n\r\n background_noise = {};\r\n\r\n numSuccessfulBackgroundCaptured;\r\n\r\n _calibrateSoundBurstDb;\r\n\r\n webAudioDeviceNames = {loudspeaker: '', microphone: '', loudspeakerText: '', microphoneText: ''};\r\n\r\n recordingChecks = {\r\n volume: {},\r\n unfiltered: [],\r\n system: [],\r\n component: [],\r\n };\r\n\r\n inDB;\r\n\r\n soundCheck = '';\r\n\r\n filteredMLSRange = {\r\n component: {\r\n Min: null,\r\n Max: null,\r\n },\r\n system: {\r\n Min: null,\r\n Max: null,\r\n },\r\n };\r\n\r\n /** @private */\r\n timeStamp = [];\r\n\r\n /** @private */\r\n startTime;\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 reportWebAudioNames = `<br>${this.webAudioDeviceNames.loudspeakerText} <br> ${this.webAudioDeviceNames.microphoneText}`;\r\n const reportParameters = `<br> Sampling: Loudspeaker ${this.sourceSamplingRate} Hz, Microphone ${this.sinkSamplingRate} Hz, ${this.sampleSize} bits`;\r\n const template = `<div style=\"display: flex; justify-content: center; margin-top:12px;\"><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 reportWebAudioNames + reportParameters + 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 this.addTimeStamp('Get system iir');\r\n const computedIRs = await Promise.all(this.impulseResponses);\r\n const filteredComputedIRs = computedIRs.filter(element => {\r\n return element != undefined;\r\n }); //log any errors that are found in this step\r\n const mls = this.#mls;\r\n const lowHz = this.#lowHz; //gain of 1 below cutoff, need gain of 0\r\n const highHz = this.#highHz; //check error for anything other than 10 kHz\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 calibrateSoundBurstDb: this._calibrateSoundBurstDb,\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 this.systemInvertedImpulseResponseNoBandpass = res['iirNoBandpass'];\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 this.addTimeStamp('Get component iir');\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 irLength = this.irLength;\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 calibrateSoundBurstDb: this._calibrateSoundBurstDb,\r\n irLength,\r\n calibrateSoundSmoothOctaves: this._calibrateSoundSmoothOctaves,\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.componentIROrigin['Freq'] = res['frequencies'];\r\n this.componentIROrigin['Gain'] = res['irOrigin'];\r\n this.componentConvolution = res['convolution'];\r\n this.componentInvertedImpulseResponseNoBandpass = res['iirNoBandpass'];\r\n this.componentIRInTimeDomain = res['irTime'];\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 sendBackgroundRecording = () => {\r\n const allSignals = this.getAllBackgroundRecordings();\r\n const numSignals = allSignals.length;\r\n const background_rec_whole = allSignals[numSignals - 1];\r\n const fraction = 0.5 / (this._calibrateSoundBackgroundSecs + 0.5);\r\n // Calculate the starting index for slicing the array\r\n const startIndex = Math.round(fraction * background_rec_whole.length);\r\n // Slice the array from the calculated start index to the end of the array\r\n const background_rec = background_rec_whole.slice(startIndex);\r\n console.log('Sending background recording to server for processing');\r\n this.addTimeStamp('Get background PSD');\r\n this.pyServerAPI\r\n .getBackgroundNoisePSDWithRetry({\r\n background_rec,\r\n sampleRate: this.sourceSamplingRate || 96000,\r\n })\r\n .then(res => {\r\n if (this.numSuccessfulBackgroundCaptured < 1) {\r\n this.numSuccessfulBackgroundCaptured += 1;\r\n //storing all background data in background_psd object\r\n this.background_noise['x_background'] = res['x_background'];\r\n this.background_noise['y_background'] = res['y_background'];\r\n this.background_noise['recording'] = background_rec;\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 * .\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 = async signalCsv => {\r\n const allSignals = this.getAllUnfilteredRecordedSignals();\r\n console.log(\r\n 'Obtaining last all hz unfiltered recording from #allHzUnfilteredRecordings to send to server for processing'\r\n );\r\n const numSignals = allSignals.length;\r\n const mls = this.#mlsBufferView;\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 await this.pyServerAPI\r\n .allHzPowerCheck({\r\n payload,\r\n sampleRate: this.sourceSamplingRate || 96000,\r\n binDesiredSec: this._calibrateSoundPowerBinDesiredSec,\r\n burstSec: this.desired_time_per_mls,\r\n })\r\n .then(result => {\r\n this.recordingChecks['unfiltered'].push(result);\r\n if (result['sd'] < this._calibrateSoundPowerDbSDToleratedDb) {\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, //get rid of this\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 } else if (result['sd'] > this._calibrateSoundPowerDbSDToleratedDb) {\r\n this.clearLastUnfilteredRecordedSignals();\r\n console.log('unfiltered rec', this.getAllUnfilteredRecordedSignals.length);\r\n }\r\n })\r\n .catch(err => {\r\n console.error(err);\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 //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 //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 time_to_wait =\r\n (this.#currentConvolution.length / this.sourceSamplingRate) * this.numMLSPerCapture;\r\n time_to_wait = time_to_wait * 1.1;\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 * Passed to the background noise recording function, awaits the desired amount of seconds to capture the desired number\r\n * of seconds of background noise\r\n *\r\n * @example\r\n */\r\n #awaitBackgroundNoiseRecording = async () => {\r\n console.log(\r\n 'Waiting ' + this._calibrateSoundBackgroundSecs + ' second(s) to record background noise'\r\n );\r\n let time_to_wait = this._calibrateSoundBackgroundSecs + 0.5;\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 time_to_sleep =\r\n (this.#currentConvolution.length / this.sourceSamplingRate) * number_of_bursts_to_skip;\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 = async () => {\r\n await this.checkPowerVariation();\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('length databuffer');\r\n console.log(dataBuffer.length);\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\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];\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\r\n if (this.mode === 'filtered') {\r\n //used to not loop filtered\r\n this.sourceNode.loop = true;\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.addTimeStamp('Play unfiltered mls');\r\n this.calibrationNodes[0].start(0);\r\n this.status = ``;\r\n if (this.mode === 'unfiltered') {\r\n console.log('mls', this.#mls); // before multiplied by calibrateSoundBurstDb\r\n console.log('mls buffer view', this.#mlsBufferView); // after multiplied by calibrateSoundBurstDb\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 console.log('sample size');\r\n console.log(this.sampleSize);\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, convolution) => {\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(convolution), // 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 bothSoundCheck = async stream => {\r\n let iir_ir_and_plots;\r\n this.#currentConvolution = this.componentConvolution;\r\n this.filteredMLSRange.component.Min = (0,_utils__WEBPACK_IMPORTED_MODULE_1__.findMinValue)(this.#currentConvolution);\r\n this.filteredMLSRange.component.Max = (0,_utils__WEBPACK_IMPORTED_MODULE_1__.findMaxValue)(this.#currentConvolution);\r\n this.addTimeStamp('Play MLS with component IIR');\r\n this.soundCheck = 'component';\r\n await this.playMLSwithIIR(stream, this.#currentConvolution);\r\n this.#stopCalibrationAudio();\r\n let component_conv_recs = this.getAllFilteredRecordedSignals();\r\n let return_component_conv_rec = component_conv_recs[component_conv_recs.length - 1];\r\n this.clearAllFilteredRecordedSignals();\r\n // await this.checkPowerVariation(return_component_conv_rec);\r\n this.numSuccessfulCaptured = 0;\r\n this.#currentConvolution = this.systemConvolution;\r\n this.filteredMLSRange.system.Min = (0,_utils__WEBPACK_IMPORTED_MODULE_1__.findMinValue)(this.#currentConvolution);\r\n this.filteredMLSRange.system.Max = (0,_utils__WEBPACK_IMPORTED_MODULE_1__.findMaxValue)(this.#currentConvolution);\r\n this.soundCheck = 'system';\r\n this.addTimeStamp('Play MLS with system IIR');\r\n await this.playMLSwithIIR(stream, this.#currentConvolution);\r\n\r\n this.#stopCalibrationAudio();\r\n\r\n let system_conv_recs = this.getAllFilteredRecordedSignals();\r\n let return_system_conv_rec = system_conv_recs[system_conv_recs.length - 1];\r\n // await this.checkPowerVariation(return_system_conv_rec);\r\n\r\n this.clearAllFilteredRecordedSignals();\r\n\r\n this.sourceAudioContext.close();\r\n let recs = this.getAllUnfilteredRecordedSignals();\r\n let unconv_rec = recs[0];\r\n let return_unconv_rec = unconv_rec;\r\n let conv_rec = component_conv_recs[component_conv_recs.length - 1];\r\n\r\n //psd of component\r\n let knownGain = this.oldComponentIR.Gain;\r\n let knownFreq = this.oldComponentIR.Freq;\r\n let sampleRate = this.sourceSamplingRate || 96000;\r\n this.addTimeStamp('Get PSD of mls recording');\r\n let component_unconv_rec_psd = 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 this.addTimeStamp('Get PSD of filtered recording (component)');\r\n let component_conv_rec_psd = 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\r\n conv_rec = system_conv_recs[system_conv_recs.length - 1];\r\n //psd of system\r\n this.addTimeStamp('Get PSD of filtered recording (system) and unfiltered recording');\r\n let system_recs_psd = 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\r\n //iir w/ and without bandpass psd. done\r\n unconv_rec = this.componentInvertedImpulseResponseNoBandpass;\r\n conv_rec = this.componentInvertedImpulseResponse;\r\n this.addTimeStamp('Get PSD of component iir and component iir no band pass');\r\n let component_iir_psd = 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 unconv_rec = this.systemInvertedImpulseResponseNoBandpass;\r\n conv_rec = this.systemInvertedImpulseResponse;\r\n this.addTimeStamp('Get PSD of system iir and system iir no band pass');\r\n let system_iir_psd = 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\r\n this.addTimeStamp('Get PSD of mls sequence');\r\n let mls_psd = await this.pyServerAPI\r\n .getMLSPSDWithRetry({mls: this.#mlsBufferView, sampleRate: this.sourceSamplingRate || 96000})\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 this.addTimeStamp('Get PSD of filered mls (system)');\r\n let system_filtered_mls_psd = await this.pyServerAPI\r\n .getMLSPSDWithRetry({\r\n mls: this.systemConvolution,\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\r\n this.addTimeStamp('Get PSD of filered mls (component)');\r\n let component_filtered_mls_psd = await this.pyServerAPI\r\n .getMLSPSDWithRetry({\r\n mls: this.componentConvolution,\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\r\n let gainValue = this.getGainDBSPL();\r\n\r\n iir_ir_and_plots = {\r\n filtered_recording: {\r\n component: return_component_conv_rec,\r\n system: return_system_conv_rec,\r\n },\r\n unfiltered_recording: this.getAllUnfilteredRecordedSignals()[0],\r\n system: {\r\n iir: this.systemInvertedImpulseResponse,\r\n ir: this.systemIR,\r\n iir_psd: {\r\n y: system_iir_psd['y_conv'],\r\n x: system_iir_psd['x_conv'],\r\n y_no_bandpass: system_iir_psd['y_unconv'],\r\n x_no_bandpass: system_iir_psd['x_unconv'],\r\n },\r\n filtered_mls_psd: {\r\n x: system_filtered_mls_psd['x_mls'],\r\n y: system_filtered_mls_psd['y_mls'],\r\n },\r\n convolution: this.systemConvolution,\r\n psd: {\r\n unconv: {\r\n x: system_recs_psd['x_unconv'],\r\n y: system_recs_psd['y_unconv'],\r\n },\r\n conv: {\r\n x: system_recs_psd['x_conv'],\r\n y: system_recs_psd['y_conv'],\r\n },\r\n },\r\n },\r\n component: {\r\n iir: this.componentInvertedImpulseResponse,\r\n ir: this.componentIR,\r\n ir_origin: this.componentIROrigin,\r\n ir_in_time_domain: this.componentIRInTimeDomain,\r\n iir_psd: {\r\n y: component_iir_psd['y_conv'],\r\n x: component_iir_psd['x_conv'],\r\n y_no_bandpass: component_iir_psd['y_unconv'],\r\n x_no_bandpass: component_iir_psd['x_unconv'],\r\n },\r\n filtered_mls_psd: {\r\n x: component_filtered_mls_psd['x_mls'],\r\n y: component_filtered_mls_psd['y_mls'],\r\n },\r\n convolution: this.componentConvolution,\r\n psd: {\r\n unconv: {\r\n x: component_unconv_rec_psd['x'],\r\n y: component_unconv_rec_psd['y'],\r\n },\r\n conv: {\r\n x: component_conv_rec_psd['x'],\r\n y: component_conv_rec_psd['y'],\r\n },\r\n },\r\n gainDBSPL: gainValue,\r\n },\r\n mls: this.#mlsBufferView,\r\n mls_psd: {\r\n x: mls_psd['x_mls'],\r\n y: mls_psd['y_mls'],\r\n },\r\n autocorrelations: this.autocorrelations,\r\n impulseResponses: [],\r\n };\r\n\r\n return iir_ir_and_plots;\r\n };\r\n\r\n singleSoundCheck = async stream => {\r\n let iir_ir_and_plots;\r\n if (this._calibrateSoundCheck != 'system') {\r\n this.#currentConvolution = this.componentConvolution;\r\n this.filteredMLSRange.component.Min = (0,_utils__WEBPACK_IMPORTED_MODULE_1__.findMinValue)(this.#currentConvolution);\r\n this.filteredMLSRange.component.Max = (0,_utils__WEBPACK_IMPORTED_MODULE_1__.findMaxValue)(this.#currentConvolution);\r\n this.addTimeStamp('Play MLS with component IIR');\r\n this.soundCheck = 'component';\r\n await this.playMLSwithIIR(stream, this.#currentConvolution);\r\n this.#stopCalibrationAudio();\r\n } else {\r\n this.#currentConvolution = this.systemConvolution;\r\n this.filteredMLSRange.system.Min = (0,_utils__WEBPACK_IMPORTED_MODULE_1__.findMinValue)(this.#currentConvolution);\r\n this.filteredMLSRange.system.Max = (0,_utils__WEBPACK_IMPORTED_MODULE_1__.findMaxValue)(this.#currentConvolution);\r\n this.addTimeStamp('Play MLS with system IIR');\r\n this.soundCheck = 'systen';\r\n await this.playMLSwithIIR(stream, this.#currentConvolution);\r\n this.#stopCalibrationAudio();\r\n }\r\n let conv_recs = this.getAllFilteredRecordedSignals();\r\n let recs = this.getAllUnfilteredRecordedSignals();\r\n this.clearAllFilteredRecordedSignals();\r\n console.log('Obtaining unfiltered recording from #allHzUnfilteredRecordings to calculate PSD');\r\n console.log('Obtaining filtered recording from #allHzFilteredRecordings to calculate PSD');\r\n let unconv_rec = recs[0];\r\n let return_unconv_rec = unconv_rec;\r\n let conv_rec = conv_recs[conv_recs.length - 1];\r\n let return_conv_rec = conv_rec;\r\n this.sourceAudioContext.close();\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 this.addTimeStamp('Get PSD of mls recording');\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 this.addTimeStamp('Get PSD recording of filtered recording (component)');\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\r\n unconv_rec = this.componentInvertedImpulseResponseNoBandpass;\r\n conv_rec = this.componentInvertedImpulseResponse;\r\n this.addTimeStamp('Get PSD of component iir and component iir no bandpass');\r\n let component_iir_psd = 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 unconv_rec = this.systemInvertedImpulseResponseNoBandpass;\r\n conv_rec = this.systemInvertedImpulseResponse;\r\n this.addTimeStamp('Get PSD of system iir and system iir no bandpass');\r\n let system_iir_psd = 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\r\n this.addTimeStamp('Get PSD of mls sequence');\r\n let mls_psd = await this.pyServerAPI\r\n .getMLSPSDWithRetry({\r\n mls: this.#mlsBufferView,\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\r\n this.addTimeStamp('Get PSD of filtered mls (component)');\r\n let filtered_mls_psd = await this.pyServerAPI\r\n .getMLSPSDWithRetry({\r\n mls: this.componentConvolution,\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\r\n let gainValue = this.getGainDBSPL();\r\n iir_ir_and_plots = {\r\n unfiltered_recording: return_unconv_rec,\r\n filtered_recording: return_conv_rec,\r\n system: {\r\n iir: this.systemInvertedImpulseResponse,\r\n ir: this.systemIR,\r\n iir_psd: {\r\n y: system_iir_psd['y_conv'],\r\n x: system_iir_psd['y_conv'],\r\n y_no_bandpass: system_iir_psd['y_unconv'],\r\n x_no_bandpass: system_iir_psd['x_unconv'],\r\n },\r\n filtered_recording: [],\r\n filtered_mls_psd: {},\r\n convolution: this.systemConvolution,\r\n psd: {\r\n unconv: {\r\n x: [],\r\n y: [],\r\n },\r\n conv: {\r\n x: [],\r\n y: [],\r\n },\r\n },\r\n },\r\n component: {\r\n iir: this.componentInvertedImpulseResponse,\r\n ir: this.componentIR,\r\n ir_origin: this.componentIROrigin,\r\n ir_in_time_domain: this.componentIRInTimeDomain,\r\n iir_psd: {\r\n y: component_iir_psd['y_conv'],\r\n x: component_iir_psd['x_conv'],\r\n y_no_bandpass: component_iir_psd['y_unconv'],\r\n x_no_bandpass: component_iir_psd['x_unconv'],\r\n },\r\n filtered_mls_psd: {\r\n x: filtered_mls_psd['x_mls'],\r\n y: filtered_mls_psd['y_mls'],\r\n },\r\n convolution: this.componentConvolution,\r\n psd: {\r\n unconv: {\r\n x: unconv_results['x'],\r\n y: unconv_results['y'],\r\n },\r\n conv: {\r\n x: conv_results['x'],\r\n y: conv_results['y'],\r\n },\r\n },\r\n gainDBSPL: gainValue,\r\n },\r\n mls: this.#mlsBufferView,\r\n mls_psd: {\r\n x: mls_psd['x_mls'],\r\n y: mls_psd['y_mls'],\r\n },\r\n autocorrelations: this.autocorrelations,\r\n impulseResponses: [],\r\n };\r\n } else {\r\n this.addTimeStamp('Get PSD of filtered recording (system) and unfiltered recording');\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\r\n //iir w/ and without bandpass psd\r\n unconv_rec = this.componentInvertedImpulseResponseNoBandpass;\r\n conv_rec = this.componentInvertedImpulseResponse;\r\n this.addTimeStamp('Get PSD of component iir and component iir no band pass');\r\n let component_iir_psd = 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 unconv_rec = this.systemInvertedImpulseResponseNoBandpass;\r\n conv_rec = this.systemInvertedImpulseResponse;\r\n this.addTimeStamp('Get PSD of system iir and system iir no band pass');\r\n let system_iir_psd = 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\r\n this.addTimeStamp('Get PSD of mls sequence');\r\n let mls_psd = await this.pyServerAPI\r\n .getMLSPSDWithRetry({\r\n mls: this.#mlsBufferView,\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\r\n this.addTimeStamp('Get PSD of filtered mls (system)');\r\n let filtered_mls_psd = await this.pyServerAPI\r\n .getMLSPSDWithRetry({\r\n mls: this.systemConvolution,\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\r\n let gainValue = this.getGainDBSPL();\r\n iir_ir_and_plots = {\r\n unfiltered_recording: return_unconv_rec,\r\n filtered_recording: return_conv_rec,\r\n system: {\r\n iir: this.systemInvertedImpulseResponse,\r\n ir: this.systemIR,\r\n iir_psd: {\r\n y: system_iir_psd['y_conv'],\r\n x: system_iir_psd['y_conv'],\r\n y_no_bandpass: system_iir_psd['y_unconv'],\r\n x_no_bandpass: system_iir_psd['x_unconv'],\r\n },\r\n filtered_recording: [],\r\n filtered_mls_psd: {\r\n x: filtered_mls_psd['x_mls'],\r\n y: filtered_mls_psd['y_mls'],\r\n },\r\n convolution: this.systemConvolution,\r\n psd: {\r\n unconv: {\r\n x: results['x_unconv'],\r\n y: results['y_unconv'],\r\n },\r\n conv: {\r\n x: results['x_conv'],\r\n y: results['y_conv'],\r\n },\r\n },\r\n },\r\n component: {\r\n iir: this.componentInvertedImpulseResponse,\r\n ir: this.componentIR,\r\n ir_origin: this.componentIROrigin,\r\n ir_in_time_domain: this.componentIRInTimeDomain,\r\n iir_psd: {\r\n y: component_iir_psd['y_conv'],\r\n x: component_iir_psd['x_conv'],\r\n y_no_bandpass: component_iir_psd['y_unconv'],\r\n x_no_bandpass: component_iir_psd['x_unconv'],\r\n },\r\n filtered_mls_psd: {},\r\n convolution: this.componentConvolution,\r\n psd: {\r\n unconv: {\r\n x: [],\r\n y: [],\r\n },\r\n conv: {\r\n x: [],\r\n y: [],\r\n },\r\n },\r\n gainDBSPL: gainValue,\r\n },\r\n mls: this.#mlsBufferView,\r\n mls_psd: {\r\n x: mls_psd['x_mls'],\r\n y: mls_psd['y_mls'],\r\n },\r\n autocorrelations: this.autocorrelations,\r\n impulseResponses: [],\r\n };\r\n }\r\n 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 iir_ir_and_plots['impulseResponses'].push(res[i]);\r\n }\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\r\n return iir_ir_and_plots;\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 const calibrateSoundBurstDb = this._calibrateSoundBurstDb;\r\n this.addTimeStamp('Get MLS sequence');\r\n await this.pyServerAPI\r\n .getMLSWithRetry({length, calibrateSoundBurstDb})\r\n .then(res => {\r\n console.log(res);\r\n this.#mlsBufferView = res['mls'];\r\n this.#mls = res['unscaledMLS'];\r\n })\r\n .catch(err => {\r\n // this.emit('InvertedImpulseResponse', {res: false});\r\n console.error(err);\r\n });\r\n this.numSuccessfulBackgroundCaptured = 0;\r\n if (this._calibrateSoundBackgroundSecs > 0) {\r\n this.mode = 'background';\r\n this.status =\r\n `All Hz Calibration: sampling the background noise...`.toString() +\r\n this.generateTemplate().toString();\r\n this.emit('update', {message: this.status});\r\n await this.recordBackground(\r\n stream, //stream\r\n () => this.numSuccessfulBackgroundCaptured < 1, //loop condition\r\n this.#awaitBackgroundNoiseRecording, //sleep to record\r\n this.sendBackgroundRecording, //send to get PSD\r\n this.mode,\r\n checkRec\r\n );\r\n this.incrementStatusBar();\r\n }\r\n this.mode = 'unfiltered';\r\n this.numSuccessfulCaptured = 0;\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 //do single check\r\n if (this._calibrateSoundCheck == 'goal' || this._calibrateSoundCheck == 'system') {\r\n iir_ir_and_plots = await this.singleSoundCheck(stream);\r\n } else {\r\n //both\r\n iir_ir_and_plots = await this.bothSoundCheck(stream);\r\n }\r\n } else {\r\n let unconv_rec = this.componentInvertedImpulseResponseNoBandpass;\r\n let conv_rec = this.componentInvertedImpulseResponse;\r\n let component_iir_psd = 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 unconv_rec = this.systemInvertedImpulseResponseNoBandpass;\r\n conv_rec = this.systemInvertedImpulseResponse;\r\n let system_iir_psd = 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\r\n let gainValue = this.getGainDBSPL();\r\n iir_ir_and_plots = {\r\n unfiltered_recording: return_unconv_rec,\r\n filtered_recording: return_conv_rec,\r\n system: {\r\n iir: this.systemInvertedImpulseResponse,\r\n ir: this.systemIR,\r\n iir_psd: {\r\n y: system_iir_psd['y_conv'],\r\n x: system_iir_psd['y_conv'],\r\n y_no_bandpass: system_iir_psd['y_unconv'],\r\n x_no_bandpass: system_iir_psd['x_unconv'],\r\n },\r\n filtered_recording: [],\r\n convolution: this.systemConvolution,\r\n psd: {\r\n unconv: {\r\n x: [],\r\n y: [],\r\n },\r\n conv: {\r\n x: [],\r\n y: [],\r\n },\r\n },\r\n },\r\n component: {\r\n iir: this.componentInvertedImpulseResponse,\r\n ir: this.componentIR,\r\n ir_in_time_domain: this.componentIRInTimeDomain,\r\n iir_psd: {\r\n y: component_iir_psd['y_conv'],\r\n x: component_iir_psd['x_conv'],\r\n y_no_bandpass: component_iir_psd['y_unconv'],\r\n x_no_bandpass: component_iir_psd['x_unconv'],\r\n },\r\n convolution: this.componentConvolution,\r\n psd: {\r\n unconv: {\r\n x: [],\r\n y: [],\r\n },\r\n conv: {\r\n x: [],\r\n y: [],\r\n },\r\n },\r\n gainDBSPL: gainValue,\r\n },\r\n mls: this.#mlsBufferView,\r\n autocorrelations: this.autocorrelations,\r\n impulseResponses: [],\r\n };\r\n 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 iir_ir_and_plots['impulseResponses'].push(res[i]);\r\n }\r\n }\r\n });\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 // saveToJSON(iir_ir_and_plots);\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.getLastVolumeRecordedSignal().slice(start, end));\r\n console.log(\r\n 'Obtaining last 1000 hz recording from #allVolumeRecordings to send for processing'\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.getAllVolumeRecordedSignals()\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.addTimeStamp('Send volume data to server');\r\n let left = this.calibrateSound1000HzPreSec;\r\n let right = this.calibrateSound1000HzPreSec + this.calibrateSound1000HzSec;\r\n this.pyServerAPI\r\n .getVolumeCalibration({\r\n sampleRate: this.sourceSamplingRate,\r\n payload: this.#getTruncatedSignal(left, right),\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 this.pyServerAPI\r\n .volumePowerCheck({\r\n payload: this.getLastVolumeRecordedSignal(),\r\n sampleRate: this.sourceSamplingRate || 96000,\r\n binDesiredSec: this._calibrateSoundPowerBinDesiredSec,\r\n preSec: this.calibrateSound1000HzPreSec,\r\n Sec: this.calibrateSound1000HzSec,\r\n })\r\n .then(res => {\r\n if (res['sd'] < this._calibrateSoundPowerDbSDToleratedDb) {\r\n this.recordingChecks['volume'][this.inDB] = res;\r\n }\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.inDB = soundLevelToDiscard;\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 this.startTime = new Date().getTime();\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 this.inDB = inDB;\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 this.addTimeStamp('Get Volume Calibration Parameters');\r\n\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 writeFrqGainToFirestore = async (speakerID, frq, gain, OEM, documentID) => {\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 // update Microphone/OEM/speakerID/default/linear\r\n const docRef = (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.doc)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"], 'Microphone', OEM, speakerID, documentID);\r\n await (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.updateDoc)(docRef, {\r\n linear: data,\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 readFrqGainFromFirestore = async (speakerID, OEM, documentID) => {\r\n const docRef = (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.doc)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"], 'Microphone', OEM, speakerID, documentID);\r\n\r\n const docSnap = await (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.getDoc)(docRef);\r\n\r\n if (docSnap.exists()) {\r\n return docSnap.data().linear;\r\n } else {\r\n return null;\r\n }\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 readGainat1000HzFromFirestore = async (speakerID, OEM, documentID) => {\r\n const docRef = (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.doc)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"], 'Microphone', OEM, speakerID, documentID);\r\n const docSnap = await (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.getDoc)(docRef);\r\n\r\n if (docSnap.exists()) {\r\n return docSnap.data().Gain1000;\r\n } else {\r\n return null;\r\n }\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 writeGainat1000HzToFirestore = async (speakerID, gain, OEM, documentID) => {\r\n const docRef = (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.doc)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"], 'Microphone', OEM, speakerID, documentID);\r\n\r\n await (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.updateDoc)(docRef, {\r\n Gain1000: gain,\r\n });\r\n };\r\n\r\n writeGainat1000Hz = async (speakerID, gain, OEM) => {\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 writeIsSmartPhoneToFirestore = async (speakerID, isSmartPhone, OEM) => {\r\n // if Microphone/OEM/speakerID/default exists, leave it alone and create a new document at Microphone/OEM/speakerID and return the id of the new document\r\n const OEMdocRef = (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.doc)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"], 'Microphone', OEM);\r\n const OEMdocSnap = await (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.getDoc)(OEMdocRef);\r\n // if OEM does not exist, create it with dummy field\r\n if (!OEMdocSnap.exists()) {\r\n await (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.setDoc)(OEMdocRef, {dummy: 'dummy'});\r\n }\r\n // save the collectionIDs in the OEM document as a field. If the field already exists, add the new collectionID to the array\r\n await (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.updateDoc)(OEMdocRef, {\r\n collectionIDs: (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.arrayUnion)(speakerID),\r\n });\r\n const docRef = (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.doc)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"], 'Microphone', OEM, speakerID, 'default');\r\n const docSnap = await (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.getDoc)(docRef);\r\n\r\n if (docSnap.exists()) {\r\n // add new document\r\n const collectionRef = (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.collection)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"], 'Microphone', OEM, speakerID);\r\n // add the new document and return the id\r\n const docRef = await (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.addDoc)(collectionRef, {isSmartPhone: isSmartPhone});\r\n return docRef.id;\r\n } else {\r\n // create document at Microphone/OEM/speakerID/default\r\n await (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.setDoc)(docRef, {isSmartPhone: isSmartPhone});\r\n return 'default';\r\n }\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 writeMicrophoneInfoToFirestore = async (speakerID, micInfo, OEM, documentID) => {\r\n const docRef = (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.doc)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"], 'Microphone', OEM, speakerID, documentID);\r\n await (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.updateDoc)(docRef, {\r\n info: micInfo,\r\n });\r\n };\r\n\r\n doesMicrophoneExistInFirestore = async (speakerID, OEM, documentID) => {\r\n const docRef = (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.doc)(_config_firebase__WEBPACK_IMPORTED_MODULE_2__[\"default\"], 'Microphone', OEM, speakerID, documentID);\r\n const docSnap = await (0,firebase_firestore__WEBPACK_IMPORTED_MODULE_4__.getDoc)(docRef);\r\n if (docSnap.exists()) {\r\n return true;\r\n }\r\n return false;\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 addMicrophoneInfo = async (speakerID, OEM, micInfo) => {\r\n // add to database if /info does not exist\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}/info`));\r\n if (!snapshot.exists()) {\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}/info`), micInfo);\r\n }\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 // add time stamp\r\n addTimeStamp = taskName => {\r\n let startTaskTime = (new Date().getTime() - this.startTime) / 1000;\r\n this.timeStamp.push(`SOUND ${Number(startTaskTime.toFixed(1))} s. ${taskName}`);\r\n };\r\n\r\n checkPowerVariation = async () => {\r\n const recordings = this.getAllFilteredRecordedSignals();\r\n const rec = recordings[recordings.length - 1];\r\n console.log(rec);\r\n await this.pyServerAPI\r\n .allHzPowerCheck({\r\n payload: rec,\r\n sampleRate: this.sourceSamplingRate || 96000,\r\n binDesiredSec: this._calibrateSoundPowerBinDesiredSec,\r\n burstSec: this.desired_time_per_mls,\r\n })\r\n .then(result => {\r\n this.recordingChecks[this.soundCheck].push(result);\r\n if (result['sd'] > this._calibrateSoundPowerDbSDToleratedDb) {\r\n console.log('filtered recording sd too high');\r\n } else {\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 getGainDBSPL = () => {\r\n var freqIndex = this.componentIR.Freq.indexOf(1000);\r\n\r\n // If freqIndex is not -1 (meaning 1000 is found in the freq array)\r\n if (freqIndex !== -1) {\r\n // Get the corresponding gain value using the index\r\n var gainValue = this.componentIR.Gain[freqIndex];\r\n return gainValue;\r\n } else {\r\n console.log('Freq 1000 not found in the array.');\r\n return null;\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 _calibrateSoundBurstDb = 0.1,\r\n _calibrateSoundBurstRepeats = 3,\r\n _calibrateSoundBurstSec = 1,\r\n _calibrateSoundBurstsWarmup = 1,\r\n _calibrateSoundHz = 48000,\r\n _calibrateSoundIIRSec = 0.2,\r\n _calibrateSoundIRSec = 0.2,\r\n calibrateSound1000HzPreSec = 3.5,\r\n calibrateSound1000HzSec = 1.0,\r\n calibrateSound1000HzPostSec = 0.5,\r\n _calibrateSoundBackgroundSecs = 0,\r\n _calibrateSoundSmoothOctaves = 0.33,\r\n _calibrateSoundPowerBinDesiredSec = 0.2,\r\n _calibrateSoundPowerDbSDToleratedDb = 1,\r\n micManufacturer = '',\r\n micSerialNumber = '',\r\n micModelNumber = '',\r\n micModelName = '',\r\n calibrateMicrophonesBool,\r\n authorEmails,\r\n webAudioDeviceNames = {loudspeaker: '', microphone: ''},\r\n userIDs\r\n ) => {\r\n this._calibrateSoundBurstDb = _calibrateSoundBurstDb;\r\n this.CALIBRATION_TONE_DURATION =\r\n calibrateSound1000HzPreSec + calibrateSound1000HzSec + calibrateSound1000HzPostSec;\r\n this.calibrateSound1000HzPreSec = calibrateSound1000HzPreSec;\r\n this.calibrateSound1000HzSec = calibrateSound1000HzSec;\r\n this.calibrateSound1000HzPostSec = calibrateSound1000HzPostSec;\r\n this.iirLength = Math.floor(_calibrateSoundIIRSec * this.sourceSamplingRate);\r\n this.irLength = Math.floor(_calibrateSoundIRSec * 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 this._calibrateSoundBackgroundSecs = _calibrateSoundBackgroundSecs;\r\n this._calibrateSoundSmoothOctaves = _calibrateSoundSmoothOctaves;\r\n this._calibrateSoundPowerBinDesiredSec = _calibrateSoundPowerBinDesiredSec;\r\n this._calibrateSoundPowerDbSDToleratedDb = _calibrateSoundPowerDbSDToleratedDb;\r\n this.webAudioDeviceNames = webAudioDeviceNames;\r\n if (isSmartPhone) this.webAudioDeviceNames.microphone = this.deviceInfo.microphoneFromAPI;\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\r\n ? micModelName === 'umik-1' || micModelName === 'umik-2'\r\n ? 'minidsp'\r\n : this.deviceInfo.OEM.toLowerCase().split(' ').join('')\r\n : micManufacturer;\r\n // const ID = \"712-5669\";\r\n // const OEM = \"minidsp\";\r\n const micInfo = {\r\n micModelName: isSmartPhone ? micModelName : microphoneName,\r\n OEM: isSmartPhone\r\n ? micModelName === 'umik-1' || micModelName === 'umik-2'\r\n ? 'miniDSP'\r\n : this.deviceInfo.OEM\r\n : micManufacturer,\r\n ID: ID,\r\n HardwareName: isSmartPhone ? this.deviceInfo.hardwarename : microphoneName,\r\n hardwareFamily: isSmartPhone ? this.deviceInfo.hardwarefamily : microphoneName,\r\n HardwareModel: isSmartPhone ? this.deviceInfo.hardwaremodel : microphoneName,\r\n PlatformName: isSmartPhone ? this.deviceInfo.platformname : 'N/A',\r\n PlatformVersion: isSmartPhone ? this.deviceInfo.platformversion : 'N/A',\r\n DeviceType: isSmartPhone ? this.deviceInfo.devicetype : 'N/A',\r\n ID_from_51Degrees: isSmartPhone ? this.deviceInfo.DeviceId : 'N/A',\r\n calibrateMicrophonesBool: calibrateMicrophonesBool,\r\n webAudioDeviceNames: {\r\n loudspeaker: this.webAudioDeviceNames.loudspeaker,\r\n microphone: this.webAudioDeviceNames.microphone,\r\n },\r\n userIDs: userIDs,\r\n };\r\n if (calibrateMicrophonesBool) {\r\n micInfo['authorEmails'] = authorEmails;\r\n }\r\n // if undefined in micInfo, set to empty string\r\n for (const [key, value] of Object.entries(micInfo)) {\r\n if (value === undefined) {\r\n micInfo[key] = '';\r\n }\r\n }\r\n\r\n // this.writeMicrophoneInfoToFirestore(ID, micInfo, OEM, 'default');\r\n // this.addMicrophoneInfo(ID, OEM, micInfo);\r\n if (componentIR == null) {\r\n //mode 'ir'\r\n //global variable this.componentIR must be set\r\n this.componentIR = await this.readFrqGainFromFirestore(ID, OEM, 'default').then(data => {\r\n return data;\r\n });\r\n // 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 lCalib = await this.readGainat1000HzFromFirestore(ID, OEM, 'default');\r\n micInfo['gainDBSPL'] = lCalib;\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.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 impulseResponseResults['background_noise'] = this.background_noise;\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.component.ir.Freq,\r\n // impulseResponseResults.component.ir.Gain,\r\n // OEM\r\n // );\r\n const id = await this.writeIsSmartPhoneToFirestore(ID, isSmartPhone, OEM);\r\n await this.writeMicrophoneInfoToFirestore(ID, micInfo, OEM, id);\r\n await this.writeFrqGainToFirestore(\r\n ID,\r\n impulseResponseResults.component.ir.Freq,\r\n impulseResponseResults.component.ir.Gain,\r\n OEM,\r\n id\r\n );\r\n micInfo['gainDBSPL'] = impulseResponseResults.component.gainDBSPL;\r\n await this.writeGainat1000HzToFirestore(ID, micInfo['gainDBSPL'], OEM, id);\r\n // await this.writeGainat1000Hz(ID, micInfo['gainDBSPL'], OEM);\r\n }\r\n const total_results = {...volumeResults, ...impulseResponseResults};\r\n total_results['filteredMLSRange'] = this.filteredMLSRange;\r\n total_results['micInfo'] = micInfo;\r\n total_results['audioInfo'] = {};\r\n total_results['audioInfo']['sinkSampleRate'] = this.sinkSamplingRate;\r\n total_results['audioInfo']['sourceSampleRate'] = this.sourceSamplingRate;\r\n total_results['audioInfo']['bitsPerSample'] = this.sampleSize;\r\n const timeStampresult = [...this.timeStamp].join('\\n');\r\n total_results['timeStamps'] = timeStampresult;\r\n total_results['recordingChecks'] = this.recordingChecks;\r\n console.log('total results');\r\n console.log(total_results);\r\n console.log('Time Stamps');\r\n console.log(timeStampresult);\r\n\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?");
|
|
855
855
|
|
|
856
856
|
/***/ }),
|
|
857
857
|
|
package/package.json
CHANGED
|
@@ -16,6 +16,7 @@ class Listener extends AudioPeer {
|
|
|
16
16
|
*/
|
|
17
17
|
constructor(params) {
|
|
18
18
|
super(params);
|
|
19
|
+
this.microphoneFromAPI = params.microphoneFromAPI ? params.microphoneFromAPI : '';
|
|
19
20
|
// this.deviceInfoFromUser = params.deviceInfoFromUser
|
|
20
21
|
// ? params.deviceInfoFromUser
|
|
21
22
|
// : {modelNumber: '', modelName: ''};
|
|
@@ -170,6 +171,7 @@ class Listener extends AudioPeer {
|
|
|
170
171
|
// deviceInfo['deviceInfoFromUser'] = this.deviceInfoFromUser;
|
|
171
172
|
});
|
|
172
173
|
// deviceInfo['deviceInfoFromUser'] = this.deviceInfoFromUser;
|
|
174
|
+
deviceInfo['microphoneFromAPI'] = this.microphoneFromAPI;
|
|
173
175
|
this.conn.send({
|
|
174
176
|
name: 'deviceInfo',
|
|
175
177
|
payload: deviceInfo,
|
|
@@ -273,7 +275,7 @@ class Listener extends AudioPeer {
|
|
|
273
275
|
});
|
|
274
276
|
return;
|
|
275
277
|
}
|
|
276
|
-
|
|
278
|
+
|
|
277
279
|
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
|
278
280
|
navigator.mediaDevices
|
|
279
281
|
.getUserMedia({
|
|
@@ -597,13 +597,13 @@ class PythonServerAPI {
|
|
|
597
597
|
return res.data[task];
|
|
598
598
|
};
|
|
599
599
|
|
|
600
|
-
|
|
600
|
+
allHzPowerCheck = async ({
|
|
601
601
|
payload,
|
|
602
602
|
sampleRate,
|
|
603
603
|
binDesiredSec,
|
|
604
604
|
burstSec
|
|
605
605
|
}) => {
|
|
606
|
-
const task = '
|
|
606
|
+
const task = 'all-hz-check';
|
|
607
607
|
let res = null;
|
|
608
608
|
|
|
609
609
|
const data = JSON.stringify({
|
|
@@ -631,6 +631,43 @@ class PythonServerAPI {
|
|
|
631
631
|
});
|
|
632
632
|
return res.data[task];
|
|
633
633
|
};
|
|
634
|
+
|
|
635
|
+
volumePowerCheck = async ({
|
|
636
|
+
payload,
|
|
637
|
+
sampleRate,
|
|
638
|
+
preSec,
|
|
639
|
+
Sec,
|
|
640
|
+
binDesiredSec
|
|
641
|
+
}) => {
|
|
642
|
+
const task = 'volume-check';
|
|
643
|
+
let res = null;
|
|
644
|
+
|
|
645
|
+
const data = JSON.stringify({
|
|
646
|
+
payload,
|
|
647
|
+
sampleRate,
|
|
648
|
+
preSec,
|
|
649
|
+
Sec,
|
|
650
|
+
binDesiredSec
|
|
651
|
+
});
|
|
652
|
+
|
|
653
|
+
await axios({
|
|
654
|
+
method: 'post',
|
|
655
|
+
baseURL: PythonServerAPI.PYTHON_SERVER_URL, //server
|
|
656
|
+
url: `/task/${task}`,
|
|
657
|
+
headers: {
|
|
658
|
+
'Content-Type': 'application/json',
|
|
659
|
+
},
|
|
660
|
+
data,
|
|
661
|
+
})
|
|
662
|
+
.then(response => {
|
|
663
|
+
res = response;
|
|
664
|
+
console.log(res.data[task]);
|
|
665
|
+
})
|
|
666
|
+
.catch(error => {
|
|
667
|
+
throw error;
|
|
668
|
+
});
|
|
669
|
+
return res.data[task];
|
|
670
|
+
};
|
|
634
671
|
}
|
|
635
672
|
|
|
636
673
|
export default PythonServerAPI;
|
|
@@ -189,11 +189,14 @@ class Combination extends AudioCalibrator {
|
|
|
189
189
|
webAudioDeviceNames = {loudspeaker: '', microphone: '', loudspeakerText: '', microphoneText: ''};
|
|
190
190
|
|
|
191
191
|
recordingChecks = {
|
|
192
|
+
volume: {},
|
|
192
193
|
unfiltered: [],
|
|
193
194
|
system: [],
|
|
194
195
|
component: [],
|
|
195
196
|
};
|
|
196
197
|
|
|
198
|
+
inDB;
|
|
199
|
+
|
|
197
200
|
soundCheck = '';
|
|
198
201
|
|
|
199
202
|
filteredMLSRange = {
|
|
@@ -417,7 +420,7 @@ class Combination extends AudioCalibrator {
|
|
|
417
420
|
this.generateTemplate().toString();
|
|
418
421
|
this.emit('update', {message: this.status});
|
|
419
422
|
await this.pyServerAPI
|
|
420
|
-
.
|
|
423
|
+
.allHzPowerCheck({
|
|
421
424
|
payload,
|
|
422
425
|
sampleRate: this.sourceSamplingRate || 96000,
|
|
423
426
|
binDesiredSec: this._calibrateSoundPowerBinDesiredSec,
|
|
@@ -1735,6 +1738,20 @@ class Combination extends AudioCalibrator {
|
|
|
1735
1738
|
.catch(err => {
|
|
1736
1739
|
console.warn(err);
|
|
1737
1740
|
});
|
|
1741
|
+
|
|
1742
|
+
this.pyServerAPI
|
|
1743
|
+
.volumePowerCheck({
|
|
1744
|
+
payload: this.getLastVolumeRecordedSignal(),
|
|
1745
|
+
sampleRate: this.sourceSamplingRate || 96000,
|
|
1746
|
+
binDesiredSec: this._calibrateSoundPowerBinDesiredSec,
|
|
1747
|
+
preSec: this.calibrateSound1000HzPreSec,
|
|
1748
|
+
Sec: this.calibrateSound1000HzSec,
|
|
1749
|
+
})
|
|
1750
|
+
.then(res => {
|
|
1751
|
+
if (res['sd'] < this._calibrateSoundPowerDbSDToleratedDb) {
|
|
1752
|
+
this.recordingChecks['volume'][this.inDB] = res;
|
|
1753
|
+
}
|
|
1754
|
+
});
|
|
1738
1755
|
};
|
|
1739
1756
|
|
|
1740
1757
|
startCalibrationVolume = async (stream, gainValues, lCalib, componentGainDBSPL) => {
|
|
@@ -1750,6 +1767,7 @@ class Combination extends AudioCalibrator {
|
|
|
1750
1767
|
// do one calibration that will be discarded
|
|
1751
1768
|
const soundLevelToDiscard = -60;
|
|
1752
1769
|
const gainToDiscard = Math.pow(10, soundLevelToDiscard / 20);
|
|
1770
|
+
this.inDB = soundLevelToDiscard;
|
|
1753
1771
|
this.status =
|
|
1754
1772
|
`1000 Hz Calibration: Sound Level ${soundLevelToDiscard} dB`.toString() +
|
|
1755
1773
|
this.generateTemplate().toString();
|
|
@@ -1786,6 +1804,7 @@ class Combination extends AudioCalibrator {
|
|
|
1786
1804
|
inDB = Math.log10(gainValues[i]) * 20;
|
|
1787
1805
|
// precision to 1 decimal place
|
|
1788
1806
|
inDB = Math.round(inDB * 10) / 10;
|
|
1807
|
+
this.inDB = inDB;
|
|
1789
1808
|
inDBValues.push(inDB);
|
|
1790
1809
|
console.log('next update');
|
|
1791
1810
|
this.status =
|
|
@@ -2033,7 +2052,7 @@ class Combination extends AudioCalibrator {
|
|
|
2033
2052
|
const rec = recordings[recordings.length - 1];
|
|
2034
2053
|
console.log(rec);
|
|
2035
2054
|
await this.pyServerAPI
|
|
2036
|
-
.
|
|
2055
|
+
.allHzPowerCheck({
|
|
2037
2056
|
payload: rec,
|
|
2038
2057
|
sampleRate: this.sourceSamplingRate || 96000,
|
|
2039
2058
|
binDesiredSec: this._calibrateSoundPowerBinDesiredSec,
|
|
@@ -2093,7 +2112,7 @@ class Combination extends AudioCalibrator {
|
|
|
2093
2112
|
_calibrateSoundBurstsWarmup = 1,
|
|
2094
2113
|
_calibrateSoundHz = 48000,
|
|
2095
2114
|
_calibrateSoundIIRSec = 0.2,
|
|
2096
|
-
_calibrateSoundIRSec = 0.
|
|
2115
|
+
_calibrateSoundIRSec = 0.2,
|
|
2097
2116
|
calibrateSound1000HzPreSec = 3.5,
|
|
2098
2117
|
calibrateSound1000HzSec = 1.0,
|
|
2099
2118
|
calibrateSound1000HzPostSec = 0.5,
|
|
@@ -2107,7 +2126,7 @@ class Combination extends AudioCalibrator {
|
|
|
2107
2126
|
micModelName = '',
|
|
2108
2127
|
calibrateMicrophonesBool,
|
|
2109
2128
|
authorEmails,
|
|
2110
|
-
webAudioDeviceNames,
|
|
2129
|
+
webAudioDeviceNames = {loudspeaker: '', microphone: ''},
|
|
2111
2130
|
userIDs
|
|
2112
2131
|
) => {
|
|
2113
2132
|
this._calibrateSoundBurstDb = _calibrateSoundBurstDb;
|
|
@@ -2128,6 +2147,7 @@ class Combination extends AudioCalibrator {
|
|
|
2128
2147
|
this._calibrateSoundPowerBinDesiredSec = _calibrateSoundPowerBinDesiredSec;
|
|
2129
2148
|
this._calibrateSoundPowerDbSDToleratedDb = _calibrateSoundPowerDbSDToleratedDb;
|
|
2130
2149
|
this.webAudioDeviceNames = webAudioDeviceNames;
|
|
2150
|
+
if (isSmartPhone) this.webAudioDeviceNames.microphone = this.deviceInfo.microphoneFromAPI;
|
|
2131
2151
|
|
|
2132
2152
|
//feed calibration goal here
|
|
2133
2153
|
this._calibrateSoundCheck = _calibrateSoundCheck;
|
|
@@ -2142,7 +2162,7 @@ class Combination extends AudioCalibrator {
|
|
|
2142
2162
|
? 'minidsp'
|
|
2143
2163
|
: this.deviceInfo.OEM.toLowerCase().split(' ').join('')
|
|
2144
2164
|
: micManufacturer;
|
|
2145
|
-
// const ID = "
|
|
2165
|
+
// const ID = "712-5669";
|
|
2146
2166
|
// const OEM = "minidsp";
|
|
2147
2167
|
const micInfo = {
|
|
2148
2168
|
micModelName: isSmartPhone ? micModelName : microphoneName,
|