speaker-calibration 2.2.78 → 2.2.80

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.2.78",
3
+ "version": "2.2.80",
4
4
  "description": "Speaker calibration library for auditory testing",
5
5
  "main": "dist/main.js",
6
6
  "directories": {
@@ -1,7 +1,7 @@
1
1
  // Import the functions you need from the SDKs you need
2
2
  import {initializeApp} from 'firebase/app';
3
3
  import {getDatabase} from 'firebase/database';
4
-
4
+ import {getFirestore} from 'firebase/firestore';
5
5
  // TODO: Add SDKs for Firebase products that you want to use
6
6
  // https://firebase.google.com/docs/web/setup#available-libraries
7
7
 
@@ -20,6 +20,7 @@ const firebaseConfig = {
20
20
 
21
21
  // Initialize Firebase
22
22
  const app = initializeApp(firebaseConfig);
23
- const database = getDatabase();
23
+ // const database = getDatabase();
24
+ const database = getFirestore(app);
24
25
 
25
26
  export default database;
@@ -7,8 +7,6 @@ import {
7
7
  CalibrationTimedOutError,
8
8
  } from './peerErrors';
9
9
 
10
- import database from '../config/firebase';
11
- import {ref, set, get, child} from 'firebase/database';
12
10
  import {phrases} from '../../dist/example/i18n';
13
11
 
14
12
  /**
@@ -48,25 +46,6 @@ class Speaker extends AudioPeer {
48
46
  this.peer.on('disconnected', this.#onPeerDisconnected);
49
47
  this.peer.on('error', this.#onPeerError);
50
48
  }
51
-
52
- static getMicrophoneNamesFromDatabase = async () => {
53
- const dbRef = ref(database);
54
- const snapshot = await get(child(dbRef, 'Microphone'));
55
- if (snapshot.exists()) {
56
- return Object.keys(snapshot.val());
57
- }
58
- return null;
59
- };
60
-
61
- static doesMicrophoneExist = async (speakerID, OEM) => {
62
- const dbRef = ref(database);
63
- const snapshot = await get(child(dbRef, `Microphone2/${OEM}/${speakerID}`));
64
- if (snapshot.exists()) {
65
- return true;
66
- }
67
- return false;
68
- };
69
-
70
49
  /**
71
50
  * Async factory method that creates the Speaker object, and returns a promise that resolves to the result of the calibration.
72
51
  *
@@ -3,6 +3,7 @@ import AudioCalibrator from '../audioCalibrator';
3
3
  import {sleep, csvToArray, saveToCSV, saveToJSON, findMinValue, findMaxValue} from '../../utils';
4
4
  import database from '../../config/firebase';
5
5
  import {ref, set, get, child} from 'firebase/database';
6
+ import {doc, getDoc, collection, addDoc, updateDoc, setDoc} from 'firebase/firestore';
6
7
 
7
8
  /**
8
9
  *
@@ -163,6 +164,8 @@ class Combination extends AudioCalibrator {
163
164
 
164
165
  componentInvertedImpulseResponseNoBandpass = [];
165
166
 
167
+ componentIRInTimeDomain = [];
168
+
166
169
  systemInvertedImpulseResponseNoBandpass = [];
167
170
 
168
171
  _calibrateSoundBackgroundSecs;
@@ -325,6 +328,7 @@ class Combination extends AudioCalibrator {
325
328
  this.componentIR['Freq'] = res['frequencies'];
326
329
  this.componentConvolution = res['convolution'];
327
330
  this.componentInvertedImpulseResponseNoBandpass = res['iirNoBandpass'];
331
+ this.componentIRInTimeDomain = res['irTime'];
328
332
  })
329
333
  .catch(err => {
330
334
  // this.emit('InvertedImpulseResponse', {res: false});
@@ -926,6 +930,7 @@ class Combination extends AudioCalibrator {
926
930
  component: {
927
931
  iir: this.componentInvertedImpulseResponse,
928
932
  ir: this.componentIR,
933
+ ir_in_time_domain: this.componentIRInTimeDomain,
929
934
  iir_psd: {
930
935
  y: component_iir_psd['y_conv'],
931
936
  x: component_iir_psd['x_conv'],
@@ -1127,6 +1132,7 @@ class Combination extends AudioCalibrator {
1127
1132
  component: {
1128
1133
  iir: this.componentInvertedImpulseResponse,
1129
1134
  ir: this.componentIR,
1135
+ ir_in_time_domain: this.componentIRInTimeDomain,
1130
1136
  iir_psd: {
1131
1137
  y: component_iir_psd['y_conv'],
1132
1138
  x: component_iir_psd['x_conv'],
@@ -1289,6 +1295,7 @@ class Combination extends AudioCalibrator {
1289
1295
  component: {
1290
1296
  iir: this.componentInvertedImpulseResponse,
1291
1297
  ir: this.componentIR,
1298
+ ir_in_time_domain: this.componentIRInTimeDomain,
1292
1299
  iir_psd: {
1293
1300
  y: component_iir_psd['y_conv'],
1294
1301
  x: component_iir_psd['x_conv'],
@@ -1499,6 +1506,7 @@ class Combination extends AudioCalibrator {
1499
1506
  component: {
1500
1507
  iir: this.componentInvertedImpulseResponse,
1501
1508
  ir: this.componentIR,
1509
+ ir_in_time_domain: this.componentIRInTimeDomain,
1502
1510
  iir_psd: {
1503
1511
  y: component_iir_psd['y_conv'],
1504
1512
  x: component_iir_psd['x_conv'],
@@ -1801,6 +1809,23 @@ class Combination extends AudioCalibrator {
1801
1809
  return result;
1802
1810
  };
1803
1811
 
1812
+ writeFrqGainToFirestore = async (speakerID, frq, gain, OEM, documentID) => {
1813
+ // freq and gain are too large to take samples 1 in every 100 samples
1814
+
1815
+ const sampledFrq = [];
1816
+ const sampledGain = [];
1817
+ for (let i = 0; i < frq.length; i += 100) {
1818
+ sampledFrq.push(frq[i]);
1819
+ sampledGain.push(gain[i]);
1820
+ }
1821
+
1822
+ const data = {Freq: sampledFrq, Gain: sampledGain};
1823
+ // update Microphone/OEM/speakerID/default/linear
1824
+ const docRef = doc(database, 'Microphone', OEM, speakerID, documentID);
1825
+ await updateDoc(docRef, {
1826
+ linear: data,
1827
+ });
1828
+ };
1804
1829
  // function to write frq and gain to firebase database given speakerID
1805
1830
  writeFrqGain = async (speakerID, frq, gain, OEM) => {
1806
1831
  // freq and gain are too large to take samples 1 in every 100 samples
@@ -1819,7 +1844,16 @@ class Combination extends AudioCalibrator {
1819
1844
 
1820
1845
  // Function to Read frq and gain from firebase database given speakerID
1821
1846
  // returns an array of frq and gain if speakerID exists, returns null otherwise
1847
+ readFrqGainFromFirestore = async (speakerID, OEM, documentID) => {
1848
+ const docRef = doc(database, 'Microphone', OEM, speakerID, documentID);
1849
+ const docSnap = await getDoc(docRef);
1822
1850
 
1851
+ if (docSnap.exists()) {
1852
+ return docSnap.data().linear;
1853
+ } else {
1854
+ return null;
1855
+ }
1856
+ };
1823
1857
  readFrqGain = async (speakerID, OEM) => {
1824
1858
  const dbRef = ref(database);
1825
1859
  const snapshot = await get(child(dbRef, `Microphone2/${OEM}/${speakerID}/linear`));
@@ -1828,6 +1862,16 @@ class Combination extends AudioCalibrator {
1828
1862
  }
1829
1863
  return null;
1830
1864
  };
1865
+ readGainat1000HzFromFirestore = async (speakerID, OEM, documentID) => {
1866
+ const docRef = doc(database, 'Microphone', OEM, speakerID, documentID);
1867
+ const docSnap = await getDoc(docRef);
1868
+
1869
+ if (docSnap.exists()) {
1870
+ return docSnap.data().Gain1000;
1871
+ } else {
1872
+ return null;
1873
+ }
1874
+ };
1831
1875
 
1832
1876
  readGainat1000Hz = async (speakerID, OEM) => {
1833
1877
  const dbRef = ref(database);
@@ -1838,16 +1882,58 @@ class Combination extends AudioCalibrator {
1838
1882
  return null;
1839
1883
  };
1840
1884
 
1885
+ writeGainat1000HzToFirestore = async (speakerID, gain, OEM, documentID) => {
1886
+ const docRef = doc(database, 'Microphone', OEM, speakerID, documentID);
1887
+
1888
+ await updateDoc(docRef, {
1889
+ Gain1000: gain,
1890
+ });
1891
+ };
1892
+
1841
1893
  writeGainat1000Hz = async (speakerID, gain, OEM) => {
1842
- const data = {Gain: gain};
1843
1894
  await set(ref(database, `Microphone2/${OEM}/${speakerID}/Gain1000`), gain);
1844
1895
  };
1845
1896
 
1897
+ writeIsSmartPhoneToFirestore = async (speakerID, isSmartPhone, OEM) => {
1898
+ // 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
1899
+
1900
+ const docRef = doc(database, 'Microphone', OEM, speakerID, 'default');
1901
+ const docSnap = await getDoc(docRef);
1902
+
1903
+ if (docSnap.exists()) {
1904
+ // add new document
1905
+ const collectionRef = collection(database, 'Microphone', OEM, speakerID);
1906
+ // add the new document and return the id
1907
+ const docRef = await addDoc(collectionRef, {isSmartPhone: isSmartPhone});
1908
+ return docRef.id;
1909
+ } else {
1910
+ // create document at Microphone/OEM/speakerID/default
1911
+ await setDoc(docRef, {isSmartPhone: isSmartPhone});
1912
+ return 'default';
1913
+ }
1914
+ };
1915
+
1846
1916
  writeIsSmartPhone = async (speakerID, isSmartPhone, OEM) => {
1847
1917
  const data = {isSmartPhone: isSmartPhone};
1848
1918
  await set(ref(database, `Microphone2/${OEM}/${speakerID}/isSmartPhone`), isSmartPhone);
1849
1919
  };
1850
1920
 
1921
+ writeMicrophoneInfoToFirestore = async (speakerID, micInfo, OEM, documentID) => {
1922
+ const docRef = doc(database, 'Microphone', OEM, speakerID, documentID);
1923
+ await updateDoc(docRef, {
1924
+ info: micInfo,
1925
+ });
1926
+ };
1927
+
1928
+ doesMicrophoneExistInFirestore = async (speakerID, OEM, documentID) => {
1929
+ const docRef = doc(database, 'Microphone', OEM, speakerID, documentID);
1930
+ const docSnap = await getDoc(docRef);
1931
+ if (docSnap.exists()) {
1932
+ return true;
1933
+ }
1934
+ return false;
1935
+ };
1936
+
1851
1937
  doesMicrophoneExist = async (speakerID, OEM) => {
1852
1938
  const dbRef = ref(database);
1853
1939
  const snapshot = await get(child(dbRef, `Microphone2/${OEM}/${speakerID}`));
@@ -1990,15 +2076,20 @@ class Combination extends AudioCalibrator {
1990
2076
  }
1991
2077
  }
1992
2078
 
1993
- this.addMicrophoneInfo(ID, OEM, micInfo);
2079
+ // this.writeMicrophoneInfoToFirestore(ID, micInfo, OEM, 'default');
2080
+ // this.addMicrophoneInfo(ID, OEM, micInfo);
1994
2081
  if (componentIR == null) {
1995
2082
  //mode 'ir'
1996
2083
  //global variable this.componentIR must be set
1997
- this.componentIR = await this.readFrqGain(ID, OEM).then(data => {
2084
+ this.componentIR = await this.readFrqGainFromFirestore(ID, OEM, 'default').then(data => {
1998
2085
  return data;
1999
2086
  });
2087
+ // await this.readFrqGain(ID, OEM).then(data => {
2088
+ // return data;
2089
+ // });
2000
2090
 
2001
- lCalib = await this.readGainat1000Hz(ID, OEM);
2091
+ // lCalib = await this.readGainat1000Hz(ID, OEM);
2092
+ lCalib = await this.readGainat1000HzFromFirestore(ID, OEM, 'default');
2002
2093
  micInfo['gainDBSPL'] = lCalib;
2003
2094
  // this.componentGainDBSPL = this.convertToDB(lCalib);
2004
2095
  this.componentGainDBSPL = lCalib;
@@ -2014,7 +2105,7 @@ class Combination extends AudioCalibrator {
2014
2105
  lCalib = this.findGainatFrequency(this.componentIR.Freq, this.componentIR.Gain, 1000);
2015
2106
  // this.componentGainDBSPL = this.convertToDB(lCalib);
2016
2107
  this.componentGainDBSPL = lCalib;
2017
- await this.writeIsSmartPhone(ID, isSmartPhone, OEM);
2108
+ // await this.writeIsSmartPhone(ID, isSmartPhone, OEM);
2018
2109
  }
2019
2110
 
2020
2111
  this.oldComponentIR = this.componentIR;
@@ -2036,10 +2127,19 @@ class Combination extends AudioCalibrator {
2036
2127
  // impulseResponseResults.component.ir.Gain,
2037
2128
  // OEM
2038
2129
  // );
2130
+ const id = await this.writeIsSmartPhoneToFirestore(ID, isSmartPhone, OEM);
2131
+ await this.writeMicrophoneInfoToFirestore(ID, micInfo, OEM, id);
2132
+ await this.writeFrqGainToFirestore(
2133
+ ID,
2134
+ impulseResponseResults.component.ir.Freq,
2135
+ impulseResponseResults.component.ir.Gain,
2136
+ OEM,
2137
+ id
2138
+ );
2139
+ await this.writeGainat1000HzToFirestore(ID, micInfo['gainDBSPL'], OEM, id);
2039
2140
  micInfo['gainDBSPL'] = impulseResponseResults.component.gainDBSPL;
2040
2141
  // await this.writeGainat1000Hz(ID, micInfo['gainDBSPL'], OEM);
2041
2142
  }
2042
-
2043
2143
  const total_results = {...volumeResults, ...impulseResponseResults};
2044
2144
  total_results['filteredMLSRange'] = this.filteredMLSRange;
2045
2145
  total_results['micInfo'] = micInfo;