speaker-calibration 2.1.2 → 2.1.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "speaker-calibration",
3
- "version": "2.1.2",
3
+ "version": "2.1.4",
4
4
  "description": "Speaker calibration library for auditory testing",
5
5
  "main": "dist/main.js",
6
6
  "directories": {
@@ -44,7 +44,7 @@ class AudioPeer {
44
44
  this.targetElement = param.targetElementId;
45
45
 
46
46
  this.siteUrl = param.siteUrl;
47
-
47
+ this.debug = false;
48
48
  // Store for all incoming data
49
49
  this.dataStore = [];
50
50
 
@@ -23,10 +23,10 @@ class Speaker extends AudioPeer {
23
23
  */
24
24
  constructor(params, CalibratorInstance) {
25
25
  super(params);
26
-
27
26
  this.siteUrl += '/listener?';
28
27
  this.ac = CalibratorInstance;
29
28
  this.result = null;
29
+ this.debug = params?.debug ?? false;
30
30
 
31
31
  /* Set up callbacks that handle any events related to our peer object. */
32
32
  this.peer.on('open', this.#onPeerOpen);
@@ -71,7 +71,11 @@ class Speaker extends AudioPeer {
71
71
  await sleep(1);
72
72
  }
73
73
  // resolve when we have a result
74
- speaker.result = await speaker.ac.startCalibration(stream, params.gainValues);
74
+ speaker.result = await speaker.ac.startCalibration(
75
+ stream,
76
+ params.gainValues,
77
+ params.ICalib
78
+ );
75
79
  speaker.#removeUIElems();
76
80
  resolve(speaker.result);
77
81
  });
@@ -158,7 +162,7 @@ class Speaker extends AudioPeer {
158
162
  // If specified HTML Id is available, show QR code there
159
163
  if (document.getElementById(this.targetElement)) {
160
164
  if (document.getElementById(this.targetElement)) {
161
- if (process.env.NODE_ENV === 'development') {
165
+ if (this.debug) {
162
166
  const linkTag = document.createElement('a');
163
167
  linkTag.setAttribute('href', uri);
164
168
  linkTag.innerHTML = "Click here to connect to the speaker's microphone";
@@ -82,7 +82,7 @@ class PythonServerAPI {
82
82
  return res.data[task];
83
83
  };
84
84
 
85
- getVolumeCalibration = async ({payload, sampleRate}) => {
85
+ getVolumeCalibration = async ({payload, sampleRate, lCalib}) => {
86
86
  const task = 'volume';
87
87
  let res = null;
88
88
 
@@ -92,6 +92,7 @@ class PythonServerAPI {
92
92
  task,
93
93
  payload,
94
94
  'sample-rate': sampleRate,
95
+ lCalib,
95
96
  });
96
97
 
97
98
  await axios({
@@ -113,7 +114,7 @@ class PythonServerAPI {
113
114
  return res.data[task];
114
115
  };
115
116
 
116
- getVolumeCalibrationParameters = async ({inDBValues, outDBSPLValues}) => {
117
+ getVolumeCalibrationParameters = async ({inDBValues, outDBSPLValues, lCalib}) => {
117
118
  const task = 'volume-parameters';
118
119
  let res = null;
119
120
 
@@ -121,6 +122,7 @@ class PythonServerAPI {
121
122
  task,
122
123
  inDBValues,
123
124
  outDBSPLValues,
125
+ lCalib,
124
126
  });
125
127
 
126
128
  await axios({
@@ -143,6 +145,7 @@ class PythonServerAPI {
143
145
  //below is an example of res.data[task]
144
146
  //{
145
147
  // R: 16.56981076554259,
148
+ // RMSError: 1.9289162528535229
146
149
  // T: -47.79799120884434,
147
150
  // W: 61.0485247483732,
148
151
  // backgroundDBSPL: 43.88233142069752,
@@ -128,7 +128,8 @@ class AudioCalibrator extends AudioRecorder {
128
128
  playCalibrationAudio,
129
129
  beforeRecord = () => {},
130
130
  afterRecord = () => {},
131
- gainValue
131
+ gainValue,
132
+ lCalib = 104.92978421490648
132
133
  ) => {
133
134
  this.numCalibratingRoundsCompleted = 0;
134
135
 
@@ -149,7 +150,7 @@ class AudioCalibrator extends AudioRecorder {
149
150
  await this.stopRecording();
150
151
 
151
152
  // after recording
152
- await afterRecord();
153
+ await afterRecord(lCalib);
153
154
 
154
155
  this.calibrationNodes = [];
155
156
 
@@ -25,8 +25,9 @@ class Volume extends AudioCalibrator {
25
25
  #CALIBRATION_TONE_DURATION = 5; // seconds
26
26
 
27
27
  /** @private */
28
- soundGainDBSPL = null;
29
28
  outDBSPL = null;
29
+ THD = null;
30
+ outDBSPL1000 = null;
30
31
 
31
32
  handleIncomingData = data => {
32
33
  console.log('Received data: ', data);
@@ -114,17 +115,19 @@ class Volume extends AudioCalibrator {
114
115
  await sleep(totalDuration);
115
116
  };
116
117
 
117
- #sendToServerForProcessing = () => {
118
+ #sendToServerForProcessing = (lCalib = 104.92978421490648) => {
118
119
  console.log('Sending data to server');
119
120
  this.pyServerAPI
120
121
  .getVolumeCalibration({
121
122
  sampleRate: this.sourceSamplingRate,
122
123
  payload: this.#getTruncatedSignal(),
124
+ lCalib: lCalib,
123
125
  })
124
126
  .then(res => {
125
127
  if (this.outDBSPL === null) {
126
128
  this.outDBSPL = res['outDbSPL'];
127
- this.soundGainDBSPL = res['soundGainDbSPL'];
129
+ this.outDBSPL1000 = res['outDbSPL1000'];
130
+ this.THD = res['thd'];
128
131
  }
129
132
  })
130
133
  .catch(err => {
@@ -132,13 +135,13 @@ class Volume extends AudioCalibrator {
132
135
  });
133
136
  };
134
137
 
135
- startCalibration = async (stream, gainValues) => {
138
+ startCalibration = async (stream, gainValues, lCalib = 104.92978421490648) => {
136
139
  const trialIterations = gainValues.length;
137
- const soundGainDBSPLValues = [];
140
+ const thdValues = [];
138
141
  const inDBValues = [];
139
142
  let inDB = 0;
140
143
  const outDBSPLValues = [];
141
-
144
+ const outDBSPL1000Values = [];
142
145
  // run the calibration at different gain values provided by the user
143
146
  for (let i = 0; i < trialIterations; i++) {
144
147
  //convert gain to DB and add to inDB
@@ -152,31 +155,37 @@ class Volume extends AudioCalibrator {
152
155
  this.#playCalibrationAudio,
153
156
  this.#createCalibrationToneWithGainValue,
154
157
  this.#sendToServerForProcessing,
155
- gainValues[i]
158
+ gainValues[i],
159
+ lCalib //todo make this a class parameter
156
160
  );
157
161
  } while (this.outDBSPL === null);
162
+ outDBSPL1000Values.push(this.outDBSPL1000);
163
+ thdValues.push(this.THD);
158
164
  outDBSPLValues.push(this.outDBSPL);
159
- soundGainDBSPLValues.push(this.soundGainDBSPL);
165
+
160
166
  this.outDBSPL = null;
161
- this.soundGainDBSPL = null;
167
+ this.outDBSPL1000 = null;
168
+ this.THD = null;
162
169
  }
163
170
 
164
171
  // get the volume calibration parameters from the server
165
172
  const parameters = await this.pyServerAPI
166
- .getVolumeCalibrationParameters({inDBValues: inDBValues, outDBSPLValues: outDBSPLValues})
173
+ .getVolumeCalibrationParameters({
174
+ inDBValues: inDBValues,
175
+ outDBSPLValues: outDBSPL1000Values,
176
+ lCalib: lCalib,
177
+ })
167
178
  .then(res => {
168
- // console.log(res);
169
179
  return res;
170
180
  });
171
- // console.log('Parameters: ', parameters);
172
- // return soundGainDBSPLValues;
173
181
  const result = {
174
182
  parameters: parameters,
175
183
  inDBValues: inDBValues,
176
184
  outDBSPLValues: outDBSPLValues,
177
- soundGainDBSPLValues: soundGainDBSPLValues,
185
+ outDBSPL1000Values: outDBSPL1000Values,
186
+ thdValues: thdValues,
178
187
  };
179
- // console.log('Result: ', result);
188
+
180
189
  return result;
181
190
  };
182
191
  }