speaker-calibration 2.2.212 → 2.2.214

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.
Files changed (142) hide show
  1. package/.eslintignore +71 -71
  2. package/.eslintrc.json +40 -40
  3. package/.github/workflows/update-phrases.yml +37 -0
  4. package/.prettierignore +69 -69
  5. package/.prettierrc +14 -14
  6. package/LICENSE +20 -20
  7. package/README.md +133 -133
  8. package/__mocks__/fileMock.js +1 -1
  9. package/__mocks__/styleMock.js +1 -1
  10. package/babel.config.js +3 -3
  11. package/coverage/clover.xml +71 -71
  12. package/coverage/coverage-final.json +224 -224
  13. package/coverage/lcov-report/PythonServerInterface.js.html +265 -265
  14. package/coverage/lcov-report/base.css +354 -354
  15. package/coverage/lcov-report/block-navigation.js +82 -82
  16. package/coverage/lcov-report/favicon.png +0 -0
  17. package/coverage/lcov-report/index.html +123 -123
  18. package/coverage/lcov-report/prettify.css +101 -101
  19. package/coverage/lcov-report/prettify.js +937 -937
  20. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  21. package/coverage/lcov-report/sorter.js +189 -189
  22. package/coverage/lcov-report/src/index.html +121 -121
  23. package/coverage/lcov-report/src/server/PythonServerInterface.js.html +268 -268
  24. package/coverage/lcov-report/src/server/index.html +123 -123
  25. package/coverage/lcov-report/src/tasks/audioCalibrator.js.html +499 -499
  26. package/coverage/lcov-report/src/tasks/audioRecorder.js.html +412 -412
  27. package/coverage/lcov-report/src/tasks/index.html +143 -143
  28. package/coverage/lcov-report/src/tasks/volume/index.html +123 -123
  29. package/coverage/lcov-report/src/tasks/volume/volume.js.html +409 -409
  30. package/coverage/lcov-report/src/utils.js.html +172 -172
  31. package/coverage/lcov.info +91 -91
  32. package/dist/Procfile +0 -0
  33. package/dist/example/NoSleep.min.js +1 -1
  34. package/dist/example/credentials.json.gpg +0 -0
  35. package/dist/example/fetch-languages-sheets.js +77 -77
  36. package/dist/example/i18n.js +27366 -27366
  37. package/dist/example/index.html +47 -47
  38. package/dist/example/listener.html +79 -79
  39. package/dist/example/server.js +51 -51
  40. package/dist/example/speaker.html +145 -145
  41. package/dist/example/speakerUI.js +273 -273
  42. package/dist/example/styles.css +99 -99
  43. package/dist/listener.js +3459 -0
  44. package/dist/main.js +2420 -207
  45. package/dist/main.js.LICENSE.txt +0 -0
  46. package/dist/mlsGen.js +6814 -6814
  47. package/dist/mlsGen.wasm +0 -0
  48. package/dist/package-lock.json +1018 -1018
  49. package/dist/package.json +18 -18
  50. package/doc/AudioCalibrator.html +417 -417
  51. package/doc/AudioPeer.html +251 -251
  52. package/doc/AudioRecorder.html +195 -195
  53. package/doc/ImpulseResponse.html +215 -215
  54. package/doc/Listener.html +308 -308
  55. package/doc/MlsGenInterface.html +226 -226
  56. package/doc/MyEventEmitter.html +274 -274
  57. package/doc/PythonServerAPI.html +109 -109
  58. package/doc/Speaker-Calibration-UML-Diagram.png +0 -0
  59. package/doc/Speaker.html +276 -276
  60. package/doc/Takes%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +128 -128
  61. package/doc/Takes%20the%20url%20of%20the%20current%20site%0Aand%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +138 -138
  62. package/doc/Takes%20the%20url%20of%20the%20current%20site%20and%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +137 -137
  63. package/doc/Volume.html +88 -88
  64. package/doc/audioCalibrator.js.html +179 -179
  65. package/doc/audioPeer.js.html +175 -175
  66. package/doc/audioRecorder.js.html +163 -163
  67. package/doc/creates%20a%20new%20AudioRecorder%20instance.%20%0ASets%20up%20the%20audio%20context%20and%20file%20reader..html +114 -114
  68. package/doc/fonts/OpenSans-Bold-webfont.eot +0 -0
  69. package/doc/fonts/OpenSans-Bold-webfont.svg +1829 -1829
  70. package/doc/fonts/OpenSans-Bold-webfont.woff +0 -0
  71. package/doc/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
  72. package/doc/fonts/OpenSans-BoldItalic-webfont.svg +1829 -1829
  73. package/doc/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
  74. package/doc/fonts/OpenSans-Italic-webfont.eot +0 -0
  75. package/doc/fonts/OpenSans-Italic-webfont.svg +1829 -1829
  76. package/doc/fonts/OpenSans-Italic-webfont.woff +0 -0
  77. package/doc/fonts/OpenSans-Light-webfont.eot +0 -0
  78. package/doc/fonts/OpenSans-Light-webfont.svg +1830 -1830
  79. package/doc/fonts/OpenSans-Light-webfont.woff +0 -0
  80. package/doc/fonts/OpenSans-LightItalic-webfont.eot +0 -0
  81. package/doc/fonts/OpenSans-LightItalic-webfont.svg +1834 -1834
  82. package/doc/fonts/OpenSans-LightItalic-webfont.woff +0 -0
  83. package/doc/fonts/OpenSans-Regular-webfont.eot +0 -0
  84. package/doc/fonts/OpenSans-Regular-webfont.svg +1830 -1830
  85. package/doc/fonts/OpenSans-Regular-webfont.woff +0 -0
  86. package/doc/global.html +308 -308
  87. package/doc/index.html +58 -58
  88. package/doc/listener.js.html +170 -170
  89. package/doc/mlsGen_mlsGenInterface.js.html +117 -117
  90. package/doc/myEventEmitter.js.html +124 -124
  91. package/doc/peer-connection_audioPeer.js.html +188 -188
  92. package/doc/peer-connection_listener.js.html +311 -311
  93. package/doc/peer-connection_speaker.js.html +381 -381
  94. package/doc/sc-activity-diagram.png +0 -0
  95. package/doc/scripts/linenumber.js +25 -25
  96. package/doc/scripts/prettify/Apache-License-2.0.txt +202 -202
  97. package/doc/scripts/prettify/lang-css.js +24 -24
  98. package/doc/scripts/prettify/prettify.js +640 -640
  99. package/doc/server_PythonServerAPI.js.html +160 -160
  100. package/doc/speaker.js.html +248 -248
  101. package/doc/styles/jsdoc-default.css +371 -371
  102. package/doc/styles/prettify-jsdoc.css +111 -111
  103. package/doc/styles/prettify-tomorrow.css +163 -163
  104. package/doc/tasks_audioCalibrator.js.html +207 -207
  105. package/doc/tasks_audioRecorder.js.html +190 -190
  106. package/doc/tasks_impulse-response_impulseResponse.js.html +442 -442
  107. package/doc/tasks_impulse-response_mlsGen_mlsGenInterface.js.html +175 -175
  108. package/doc/tasks_volume_volume.js.html +185 -185
  109. package/doc/utils.js.html +105 -105
  110. package/jest.config.js +173 -173
  111. package/makefile +74 -0
  112. package/netlify.toml +26 -26
  113. package/package.json +78 -73
  114. package/src/config/firebase.js +26 -26
  115. package/src/index.html +21 -21
  116. package/{dist/example → src/listener-app}/listener.js +153 -152
  117. package/src/main.js +23 -23
  118. package/src/myEventEmitter.js +83 -83
  119. package/src/peer-connection/audioPeer.js +183 -183
  120. package/src/peer-connection/listener.js +366 -367
  121. package/src/peer-connection/peerErrors.js +25 -25
  122. package/src/peer-connection/speaker.js +743 -738
  123. package/src/powerCheck.js +98 -98
  124. package/src/server/PythonServerAPI.js +869 -869
  125. package/src/tasks/audioCalibrator.js +351 -351
  126. package/src/tasks/audioRecorder.js +315 -315
  127. package/src/tasks/combination/combination.js +3160 -3031
  128. package/src/tasks/combination/mlsGen/mlsGen.cpp +98 -98
  129. package/src/tasks/combination/mlsGen/mlsGen.hpp +303 -303
  130. package/src/tasks/combination/mlsGen/mlsGenInterface.js +131 -131
  131. package/src/tasks/combination/mlsGen/mlsGenTest.cpp +180 -180
  132. package/src/tasks/impulse-response/impulseResponse.js +610 -610
  133. package/src/tasks/impulse-response/mlsGen/mlsGen.cpp +98 -98
  134. package/src/tasks/impulse-response/mlsGen/mlsGen.hpp +303 -303
  135. package/src/tasks/impulse-response/mlsGen/mlsGenInterface.js +131 -131
  136. package/src/tasks/impulse-response/mlsGen/mlsGenTest.cpp +180 -180
  137. package/src/tasks/volume/volume.cpp +2 -2
  138. package/src/tasks/volume/volume.hpp +22 -22
  139. package/src/tasks/volume/volume.js +279 -279
  140. package/src/utils.js +205 -205
  141. package/webpack.config.js +64 -37
  142. package/.gitignore +0 -81
@@ -1,367 +1,366 @@
1
- import AudioPeer from './audioPeer';
2
- import {UnsupportedDeviceError, MissingSpeakerIdError} from './peerErrors';
3
- import axios from 'axios';
4
-
5
- /**
6
- * @class Handles the listener's side of the connection. Responsible for getting access to user's microphone,
7
- * and initiating a call to the Speaker.
8
- * @augments AudioPeer
9
- */
10
- class Listener extends AudioPeer {
11
- /**
12
- * Takes a target element where html elements will be appended.
13
- *
14
- * @param params - See type definition for initParameters.
15
- * @example
16
- */
17
- constructor(params) {
18
- super(params);
19
- this.microphoneFromAPI = params.microphoneFromAPI ? params.microphoneFromAPI : '';
20
- this.microphoneDeviceId = params.microphoneDeviceId ? params.microphoneDeviceId : '';
21
- // this.deviceInfoFromUser = params.deviceInfoFromUser
22
- // ? params.deviceInfoFromUser
23
- // : {modelNumber: '', modelName: ''};
24
- this.startTime = Date.now();
25
- this.receiverPeerId = null;
26
-
27
- const urlParameters = this.parseURLSearchParams();
28
- this.calibrateSoundHz =
29
- // previous calibrateSoundHz
30
- urlParameters.hz !== null && urlParameters.hz !== undefined ? urlParameters.hz : 48000;
31
- this.calibrateSoundSamplingDesiredBits =
32
- // previous calibrateSoundSamplingDesiredBits
33
- urlParameters.bits !== null && urlParameters.bits !== undefined ? urlParameters.bits : 24;
34
- this.speakerPeerId = urlParameters.speakerPeerId;
35
-
36
- this.peer.on('open', this.onPeerOpen);
37
- this.peer.on('connection', this.onPeerConnection);
38
- this.peer.on('disconnected', this.onPeerDisconnected);
39
- this.peer.on('close', this.onPeerClose);
40
- this.peer.on('error', this.onPeerError);
41
- }
42
-
43
- onPeerOpen = id => {
44
- this.displayUpdate('Listener - onPeerOpen');
45
- // Workaround for peer.reconnect deleting previous id
46
-
47
- if (id === null) {
48
- this.displayUpdate('Received null id from peer open');
49
- this.peer.id = this.lastPeerId;
50
- } else {
51
- this.lastPeerId = this.peer.id;
52
- }
53
-
54
- this.join();
55
- };
56
-
57
- onPeerConnection = connection => {
58
- this.displayUpdate('Listener - onPeerConnection');
59
- // Disallow incoming connections
60
- connection.on('open', () => {
61
- connection.send('Sender does not accept incoming connections');
62
- setTimeout(() => {
63
- connection.close();
64
- }, 500);
65
- });
66
- };
67
-
68
- onConnData = data => {
69
- this.displayUpdate('Listener - onConnData');
70
- const hasSpeakerID = Object.prototype.hasOwnProperty.call(data, 'speakerPeerId');
71
- if (!hasSpeakerID) {
72
- this.displayUpdate('Error in parsing data received! Must set "speakerPeerId" property');
73
- throw new MissingSpeakerIdError('Must set "speakerPeerId" property');
74
- } else {
75
- // this.conn.close();
76
- this.displayUpdate(this.speakerPeerId);
77
- this.speakerPeerId = data.speakerPeerId;
78
- const newParams = {
79
- speakerPeerId: this.speakerPeerId,
80
- };
81
- /*
82
- FUTURE does this limit usable environments?
83
- ie does this work if internet is lost after initial page load?
84
- */
85
- window.location.search = this.queryStringFromObject(newParams); // Redirect to correctly constructed keypad page
86
- }
87
- };
88
-
89
- join = async () => {
90
- this.displayUpdate('Listener - join');
91
- /**
92
- * Create the connection between the two Peers.
93
- *
94
- * Sets up callbacks that handle any events related to the
95
- * connection and data received on it.
96
- */
97
- // Close old connection
98
- if (this.conn) {
99
- this.displayUpdate('Closing old connection');
100
- this.conn.close();
101
- }
102
-
103
- // Create connection to destination peer specified by the query param
104
- this.displayUpdate(`Creating connection to: ${this.speakerPeerId}`);
105
- this.conn = this.peer.connect(this.speakerPeerId, {
106
- reliable: true,
107
- });
108
-
109
- this.displayUpdate('Created connection');
110
- this.conn.on('open', async () => {
111
- this.displayUpdate('Listener - conn open');
112
- await this.getDeviceInfo();
113
- // this.sendSamplingRate();
114
- await this.openAudioStream();
115
- });
116
-
117
- // Handle incoming data (messages only since this is the signal sender)
118
- this.conn.on('data', this.onConnData);
119
- this.conn.on('close', () => {
120
- console.log('Connection closed');
121
- });
122
- };
123
-
124
- getMobileOS = () => {
125
- const ua = navigator.userAgent;
126
- if (/android/i.test(ua)) {
127
- return 'Android';
128
- }
129
- if (
130
- /iPad|iPhone|iPod/.test(ua) ||
131
- ((navigator?.userAgentData?.platform || navigator?.platform) === 'MacIntel' &&
132
- navigator.maxTouchPoints > 1)
133
- ) {
134
- return 'iOS';
135
- }
136
- return 'Other';
137
- };
138
-
139
- sendSamplingRate = sampleRate => {
140
- this.displayUpdate('Listener - sendSamplingRate');
141
- this.conn.send({
142
- name: 'samplingRate',
143
- payload: sampleRate,
144
- });
145
- };
146
-
147
- sendSampleSize = sampleSize => {
148
- this.displayUpdate('Listener - sendSampleSize');
149
- this.conn.send({
150
- name: 'sampleSize',
151
- payload: sampleSize,
152
- });
153
- };
154
-
155
- sendFlags = flags => {
156
- this.displayUpdate('Listener - sendFlags');
157
- this.conn.send({
158
- name: 'flags',
159
- payload: flags,
160
- });
161
- };
162
-
163
- getDeviceInfo = async () => {
164
- try {
165
- const deviceInfo = {};
166
- fod.complete(function (data) {
167
- deviceInfo['IsMobile'] = data.device['ismobile'];
168
- deviceInfo['HardwareName'] = data.device['hardwarename'];
169
- deviceInfo['HardwareFamily'] = data.device['hardwarefamily'];
170
- deviceInfo['HardwareModel'] = data.device['hardwaremodel'];
171
- deviceInfo['OEM'] = data.device['oem'];
172
- deviceInfo['HardwareModelVariants'] = data.device['hardwaremodelvariants'];
173
- deviceInfo['DeviceId'] = data.device['deviceid'];
174
- deviceInfo['PlatformName'] = data.device['platformname'];
175
- deviceInfo['PlatformVersion'] = data.device['platformversion'];
176
- deviceInfo['DeviceType'] = data.device['devicetype'];
177
- // deviceInfo['deviceInfoFromUser'] = this.deviceInfoFromUser;
178
- });
179
- // deviceInfo['deviceInfoFromUser'] = this.deviceInfoFromUser;
180
- deviceInfo['microphoneFromAPI'] = this.microphoneFromAPI;
181
- deviceInfo['microphoneDeviceId'] = this.microphoneDeviceId;
182
- deviceInfo['screenWidth'] = window.screen.width;
183
- deviceInfo['screenHeight'] = window.screen.height;
184
- console.log('deviceInfo Inside getDeviceInfo: ', deviceInfo);
185
- this.conn.send({
186
- name: 'deviceInfo',
187
- payload: deviceInfo,
188
- });
189
- return deviceInfo;
190
- } catch (error) {
191
- console.error('Error fetching or executing script:', error.message);
192
- return null;
193
- }
194
- };
195
-
196
- applyHQTrackConstraints = async stream => {
197
- // Contraint the incoming audio to the sampling rate we want
198
- stream.getAudioTracks().forEach(track => {
199
- console.log(track, track.enabled);
200
- });
201
- const track = stream.getAudioTracks()[0];
202
- console.log(track);
203
- const capabilities = track.getCapabilities();
204
-
205
- this.displayUpdate(
206
- `Listener Track Capabilities - ${JSON.stringify(capabilities, undefined, 2)}`
207
- );
208
-
209
- const constraints = track.getConstraints();
210
-
211
- if (capabilities.echoCancellation) {
212
- constraints.echoCancellation = false;
213
- }
214
-
215
- if (capabilities.sampleRate) {
216
- constraints.sampleRate = this.calibrateSoundHz;
217
- }
218
-
219
- if (capabilities.sampleSize) {
220
- constraints.sampleSize = this.calibrateSoundSamplingDesiredBits;
221
- }
222
-
223
- if (capabilities.channelCount) {
224
- constraints.channelCount = 1;
225
- }
226
-
227
- this.displayUpdate(`Listener Track Constraints - ${JSON.stringify(constraints, undefined, 2)}`);
228
-
229
- // await the promise
230
- try {
231
- await track.applyConstraints(constraints);
232
- } catch (err) {
233
- console.error(err);
234
- this.displayUpdate(`Error applying constraints to track: ${err}`);
235
- }
236
-
237
- const settings = track.getSettings();
238
- this.displayUpdate(`Listener Track Settings - ${JSON.stringify(settings, undefined, 2)}`);
239
- return settings;
240
- };
241
-
242
- getMediaDevicesAudioContraints = async () => {
243
- const availableConstraints = navigator.mediaDevices.getSupportedConstraints();
244
-
245
- const contraints = {
246
- // ...(availableConstraints.echoCancellation && availableConstraints.echoCancellation == true
247
- // ? {echoCancellation: {exact: false}}
248
- // : {}),
249
- // ...(availableConstraints.sampleRate && availableConstraints.sampleRate == true
250
- // ? {sampleRate: {ideal: this.calibrateSoundHz}}
251
- // : {}),
252
- // ...(availableConstraints.sampleSize && availableConstraints.sampleSize == true
253
- // ? {sampleSize: {ideal: this.calibrateSoundSamplingDesiredBits}}
254
- // : {}),
255
- // ...(availableConstraints.channelCount && availableConstraints.channelCount == true
256
- // ? {channelCount: {exact: 1}}
257
- // : {}),
258
- autoGainControl: false,
259
- noiseSuppression: false,
260
- echoCancellation: false,
261
- channelCount: 1,
262
- };
263
-
264
- if (this.microphoneDeviceId !== '') {
265
- contraints.deviceId = {exact: await this.getDeviceIdByLabel(this.microphoneDeviceId)};
266
- }
267
-
268
- console.log(contraints);
269
-
270
- return contraints;
271
- };
272
- getDeviceIdByLabel = async targetLabel => {
273
- try {
274
- //get permission to use audio first. (Returns empty labels on some computers if not done first)
275
- await navigator.mediaDevices.getUserMedia({audio: true});
276
- // Enumerate available media devices
277
- const devices = await navigator.mediaDevices.enumerateDevices();
278
-
279
- // Find the device with the matching label
280
- const matchingDevice = devices.find(
281
- device => device.kind === 'audioinput' && device.label === targetLabel
282
- );
283
-
284
- if (matchingDevice) {
285
- return matchingDevice.deviceId; // Return the deviceId if found
286
- } else {
287
- throw new Error(`No audio input device found with label: "${targetLabel}"`);
288
- }
289
- } catch (error) {
290
- console.error('Error finding device ID:', error);
291
- return null;
292
- }
293
- };
294
-
295
- openAudioStream = async () => {
296
- this.displayUpdate('Listener - openAudioStream');
297
- const mobileOS = this.getMobileOS();
298
- if (process.env.NODE_ENV !== 'development' && mobileOS !== 'iOS') {
299
- const err = new UnsupportedDeviceError(`${mobileOS} is not supported`);
300
- this.conn.send({
301
- name: err.name,
302
- payload: err,
303
- });
304
- return;
305
- }
306
- const constraints = await this.getMediaDevicesAudioContraints();
307
- console.log('Constraints right before getUserMedia:', constraints);
308
- navigator.mediaDevices
309
- .getUserMedia({
310
- audio: constraints,
311
- video: false,
312
- //audio: {echoCancellation: false, noiseSuppression: false, autoGainControl: false, deviceId: {exact: await this.getDeviceIdByLabel(this.microphoneDeviceId) }},
313
- })
314
- .then(stream => {
315
- this.displayUpdate(
316
- `Listener Track settings before applied constraints - ${JSON.stringify(
317
- stream.getAudioTracks()[0].getSettings(),
318
- undefined,
319
- 2
320
- )}`
321
- );
322
- this.applyHQTrackConstraints(stream)
323
- .then(settings => {
324
- console.log(settings);
325
- this.sendSamplingRate(settings.sampleRate);
326
- //let sampleSize = settings.sampleSize;
327
- let sampleSize = this.calibrateSoundSamplingDesiredBits;
328
- /*
329
- if (!sampleSize) {
330
- sampleSize = this.calibrateSoundSamplingDesiredBits;
331
- }
332
- */
333
- this.sendSampleSize(sampleSize);
334
- this.sendFlags({
335
- autoGainControl: settings.autoGainControl,
336
- noiseSuppression: settings.noiseSuppression,
337
- echoCancellation: settings.echoCancellation,
338
- });
339
- this.peer.call(this.speakerPeerId, stream); // one-way call
340
- this.displayUpdate('Listener - openAudioStream');
341
- })
342
- .catch(err => {
343
- console.log(err);
344
- this.displayUpdate(
345
- `Listener - Error in applyHQTrackConstraints - ${JSON.stringify(err, undefined, 2)}`
346
- );
347
- });
348
- })
349
- .catch(err => {
350
- console.error(err);
351
- if (err.name === 'OverconstrainedError') {
352
- const constraint = err.constraint;
353
- const message = `The constraint "${constraint}" cannot be satisfied by the selected microphone. Please adjust your calibration settings or choose a different microphone.`;
354
-
355
- this.displayUpdate(`Listener - OverconstrainedError: ${message}`);
356
- console.error(message);
357
-
358
- alert(`Overconstrained Error: ${message}`);
359
- }
360
- this.displayUpdate(
361
- `Listener - Error in getUserMedia - ${JSON.stringify(err, undefined, 2)}`
362
- );
363
- });
364
- };
365
- }
366
-
367
- export default Listener;
1
+ import AudioPeer from './audioPeer';
2
+ import {UnsupportedDeviceError, MissingSpeakerIdError} from './peerErrors';
3
+ import axios from 'axios';
4
+
5
+ /**
6
+ * @class Handles the listener's side of the connection. Responsible for getting access to user's microphone,
7
+ * and initiating a call to the Speaker.
8
+ * @augments AudioPeer
9
+ */
10
+ class Listener extends AudioPeer {
11
+ /**
12
+ * Takes a target element where html elements will be appended.
13
+ *
14
+ * @param params - See type definition for initParameters.
15
+ * @example
16
+ */
17
+ constructor(params) {
18
+ super(params);
19
+ this.microphoneFromAPI = params.microphoneFromAPI ? params.microphoneFromAPI : '';
20
+ this.microphoneDeviceId = params.microphoneDeviceId ? params.microphoneDeviceId : '';
21
+ // this.deviceInfoFromUser = params.deviceInfoFromUser
22
+ // ? params.deviceInfoFromUser
23
+ // : {modelNumber: '', modelName: ''};
24
+ this.startTime = Date.now();
25
+ this.receiverPeerId = null;
26
+
27
+ const urlParameters = this.parseURLSearchParams();
28
+ this.calibrateSoundHz =
29
+ // previous calibrateSoundHz
30
+ urlParameters.hz !== null && urlParameters.hz !== undefined ? urlParameters.hz : 48000;
31
+ this.calibrateSoundSamplingDesiredBits =
32
+ // previous calibrateSoundSamplingDesiredBits
33
+ urlParameters.bits !== null && urlParameters.bits !== undefined ? urlParameters.bits : 24;
34
+ this.speakerPeerId = urlParameters.speakerPeerId;
35
+
36
+ this.peer.on('open', this.onPeerOpen);
37
+ this.peer.on('connection', this.onPeerConnection);
38
+ this.peer.on('disconnected', this.onPeerDisconnected);
39
+ this.peer.on('close', this.onPeerClose);
40
+ this.peer.on('error', this.onPeerError);
41
+ }
42
+
43
+ onPeerOpen = id => {
44
+ this.displayUpdate('Listener - onPeerOpen');
45
+ // Workaround for peer.reconnect deleting previous id
46
+
47
+ if (id === null) {
48
+ this.displayUpdate('Received null id from peer open');
49
+ this.peer.id = this.lastPeerId;
50
+ } else {
51
+ this.lastPeerId = this.peer.id;
52
+ }
53
+
54
+ this.join();
55
+ };
56
+
57
+ onPeerConnection = connection => {
58
+ this.displayUpdate('Listener - onPeerConnection');
59
+ // Disallow incoming connections
60
+ connection.on('open', () => {
61
+ connection.send('Sender does not accept incoming connections');
62
+ setTimeout(() => {
63
+ connection.close();
64
+ }, 500);
65
+ });
66
+ };
67
+
68
+ onConnData = data => {
69
+ this.displayUpdate('Listener - onConnData');
70
+ const hasSpeakerID = Object.prototype.hasOwnProperty.call(data, 'speakerPeerId');
71
+ if (!hasSpeakerID) {
72
+ this.displayUpdate('Error in parsing data received! Must set "speakerPeerId" property');
73
+ throw new MissingSpeakerIdError('Must set "speakerPeerId" property');
74
+ } else {
75
+ // this.conn.close();
76
+ this.displayUpdate(this.speakerPeerId);
77
+ this.speakerPeerId = data.speakerPeerId;
78
+ const newParams = {
79
+ speakerPeerId: this.speakerPeerId,
80
+ };
81
+ /*
82
+ FUTURE does this limit usable environments?
83
+ ie does this work if internet is lost after initial page load?
84
+ */
85
+ window.location.search = this.queryStringFromObject(newParams); // Redirect to correctly constructed keypad page
86
+ }
87
+ };
88
+
89
+ join = async () => {
90
+ this.displayUpdate('Listener - join');
91
+ /**
92
+ * Create the connection between the two Peers.
93
+ *
94
+ * Sets up callbacks that handle any events related to the
95
+ * connection and data received on it.
96
+ */
97
+ // Close old connection
98
+ if (this.conn) {
99
+ this.displayUpdate('Closing old connection');
100
+ this.conn.close();
101
+ }
102
+
103
+ // Create connection to destination peer specified by the query param
104
+ this.displayUpdate(`Creating connection to: ${this.speakerPeerId}`);
105
+ this.conn = this.peer.connect(this.speakerPeerId, {
106
+ reliable: true,
107
+ });
108
+
109
+ this.displayUpdate('Created connection');
110
+ this.conn.on('open', async () => {
111
+ this.displayUpdate('Listener - conn open');
112
+ await this.getDeviceInfo();
113
+ // this.sendSamplingRate();
114
+ await this.openAudioStream();
115
+ });
116
+
117
+ // Handle incoming data (messages only since this is the signal sender)
118
+ this.conn.on('data', this.onConnData);
119
+ this.conn.on('close', () => {
120
+ console.log('Connection closed');
121
+ });
122
+ };
123
+
124
+ getMobileOS = () => {
125
+ const ua = navigator.userAgent;
126
+ if (/android/i.test(ua)) {
127
+ return 'Android';
128
+ }
129
+ if (
130
+ /iPad|iPhone|iPod/.test(ua) ||
131
+ ((navigator?.userAgentData?.platform || navigator?.platform) === 'MacIntel' &&
132
+ navigator.maxTouchPoints > 1)
133
+ ) {
134
+ return 'iOS';
135
+ }
136
+ return 'Other';
137
+ };
138
+
139
+ sendSamplingRate = sampleRate => {
140
+ this.displayUpdate('Listener - sendSamplingRate');
141
+ this.conn.send({
142
+ name: 'samplingRate',
143
+ payload: sampleRate,
144
+ });
145
+ };
146
+
147
+ sendSampleSize = sampleSize => {
148
+ this.displayUpdate('Listener - sendSampleSize');
149
+ this.conn.send({
150
+ name: 'sampleSize',
151
+ payload: sampleSize,
152
+ });
153
+ };
154
+
155
+ sendFlags = flags => {
156
+ this.displayUpdate('Listener - sendFlags');
157
+ this.conn.send({
158
+ name: 'flags',
159
+ payload: flags,
160
+ });
161
+ };
162
+
163
+ getDeviceInfo = async () => {
164
+ const deviceInfo = {};
165
+ try {
166
+ fod.complete(function (data) {
167
+ deviceInfo['IsMobile'] = data.device['ismobile'];
168
+ deviceInfo['HardwareName'] = data.device['hardwarename'];
169
+ deviceInfo['HardwareFamily'] = data.device['hardwarefamily'];
170
+ deviceInfo['HardwareModel'] = data.device['hardwaremodel'];
171
+ deviceInfo['OEM'] = data.device['oem'];
172
+ deviceInfo['HardwareModelVariants'] = data.device['hardwaremodelvariants'];
173
+ deviceInfo['DeviceId'] = data.device['deviceid'];
174
+ deviceInfo['PlatformName'] = data.device['platformname'];
175
+ deviceInfo['PlatformVersion'] = data.device['platformversion'];
176
+ deviceInfo['DeviceType'] = data.device['devicetype'];
177
+ // deviceInfo['deviceInfoFromUser'] = this.deviceInfoFromUser;
178
+ });
179
+ } catch (error) {
180
+ console.error('Error fetching or executing script:', error.message);
181
+ }
182
+ // deviceInfo['deviceInfoFromUser'] = this.deviceInfoFromUser;
183
+ deviceInfo['microphoneFromAPI'] = this.microphoneFromAPI;
184
+ deviceInfo['microphoneDeviceId'] = this.microphoneDeviceId;
185
+ deviceInfo['screenWidth'] = window.screen.width;
186
+ deviceInfo['screenHeight'] = window.screen.height;
187
+ console.log('deviceInfo Inside getDeviceInfo: ', deviceInfo);
188
+ this.conn.send({
189
+ name: 'deviceInfo',
190
+ payload: deviceInfo,
191
+ });
192
+ return deviceInfo;
193
+ };
194
+
195
+ applyHQTrackConstraints = async stream => {
196
+ // Contraint the incoming audio to the sampling rate we want
197
+ stream.getAudioTracks().forEach(track => {
198
+ console.log(track, track.enabled);
199
+ });
200
+ const track = stream.getAudioTracks()[0];
201
+ console.log(track);
202
+ const capabilities = track.getCapabilities();
203
+
204
+ this.displayUpdate(
205
+ `Listener Track Capabilities - ${JSON.stringify(capabilities, undefined, 2)}`
206
+ );
207
+
208
+ const constraints = track.getConstraints();
209
+
210
+ if (capabilities.echoCancellation) {
211
+ constraints.echoCancellation = false;
212
+ }
213
+
214
+ if (capabilities.sampleRate) {
215
+ constraints.sampleRate = this.calibrateSoundHz;
216
+ }
217
+
218
+ if (capabilities.sampleSize) {
219
+ constraints.sampleSize = this.calibrateSoundSamplingDesiredBits;
220
+ }
221
+
222
+ if (capabilities.channelCount) {
223
+ constraints.channelCount = 1;
224
+ }
225
+
226
+ this.displayUpdate(`Listener Track Constraints - ${JSON.stringify(constraints, undefined, 2)}`);
227
+
228
+ // await the promise
229
+ try {
230
+ await track.applyConstraints(constraints);
231
+ } catch (err) {
232
+ console.error(err);
233
+ this.displayUpdate(`Error applying constraints to track: ${err}`);
234
+ }
235
+
236
+ const settings = track.getSettings();
237
+ this.displayUpdate(`Listener Track Settings - ${JSON.stringify(settings, undefined, 2)}`);
238
+ return settings;
239
+ };
240
+
241
+ getMediaDevicesAudioContraints = async () => {
242
+ const availableConstraints = navigator.mediaDevices.getSupportedConstraints();
243
+
244
+ const contraints = {
245
+ // ...(availableConstraints.echoCancellation && availableConstraints.echoCancellation == true
246
+ // ? {echoCancellation: {exact: false}}
247
+ // : {}),
248
+ // ...(availableConstraints.sampleRate && availableConstraints.sampleRate == true
249
+ // ? {sampleRate: {ideal: this.calibrateSoundHz}}
250
+ // : {}),
251
+ // ...(availableConstraints.sampleSize && availableConstraints.sampleSize == true
252
+ // ? {sampleSize: {ideal: this.calibrateSoundSamplingDesiredBits}}
253
+ // : {}),
254
+ // ...(availableConstraints.channelCount && availableConstraints.channelCount == true
255
+ // ? {channelCount: {exact: 1}}
256
+ // : {}),
257
+ autoGainControl: false,
258
+ noiseSuppression: false,
259
+ echoCancellation: false,
260
+ channelCount: 1,
261
+ };
262
+
263
+ if (this.microphoneDeviceId !== '') {
264
+ contraints.deviceId = {exact: await this.getDeviceIdByLabel(this.microphoneDeviceId)};
265
+ }
266
+
267
+ console.log(contraints);
268
+
269
+ return contraints;
270
+ };
271
+ getDeviceIdByLabel = async targetLabel => {
272
+ try {
273
+ //get permission to use audio first. (Returns empty labels on some computers if not done first)
274
+ await navigator.mediaDevices.getUserMedia({audio: true});
275
+ // Enumerate available media devices
276
+ const devices = await navigator.mediaDevices.enumerateDevices();
277
+
278
+ // Find the device with the matching label
279
+ const matchingDevice = devices.find(
280
+ device => device.kind === 'audioinput' && device.label === targetLabel
281
+ );
282
+
283
+ if (matchingDevice) {
284
+ return matchingDevice.deviceId; // Return the deviceId if found
285
+ } else {
286
+ throw new Error(`No audio input device found with label: "${targetLabel}"`);
287
+ }
288
+ } catch (error) {
289
+ console.error('Error finding device ID:', error);
290
+ return null;
291
+ }
292
+ };
293
+
294
+ openAudioStream = async () => {
295
+ this.displayUpdate('Listener - openAudioStream');
296
+ const mobileOS = this.getMobileOS();
297
+ if (process.env.NODE_ENV !== 'development' && mobileOS !== 'iOS') {
298
+ const err = new UnsupportedDeviceError(`${mobileOS} is not supported`);
299
+ this.conn.send({
300
+ name: err.name,
301
+ payload: err,
302
+ });
303
+ return;
304
+ }
305
+ const constraints = await this.getMediaDevicesAudioContraints();
306
+ console.log('Constraints right before getUserMedia:', constraints);
307
+ navigator.mediaDevices
308
+ .getUserMedia({
309
+ audio: constraints,
310
+ video: false,
311
+ //audio: {echoCancellation: false, noiseSuppression: false, autoGainControl: false, deviceId: {exact: await this.getDeviceIdByLabel(this.microphoneDeviceId) }},
312
+ })
313
+ .then(stream => {
314
+ this.displayUpdate(
315
+ `Listener Track settings before applied constraints - ${JSON.stringify(
316
+ stream.getAudioTracks()[0].getSettings(),
317
+ undefined,
318
+ 2
319
+ )}`
320
+ );
321
+ this.applyHQTrackConstraints(stream)
322
+ .then(settings => {
323
+ console.log(settings);
324
+ this.sendSamplingRate(settings.sampleRate);
325
+ //let sampleSize = settings.sampleSize;
326
+ let sampleSize = this.calibrateSoundSamplingDesiredBits;
327
+ /*
328
+ if (!sampleSize) {
329
+ sampleSize = this.calibrateSoundSamplingDesiredBits;
330
+ }
331
+ */
332
+ this.sendSampleSize(sampleSize);
333
+ this.sendFlags({
334
+ autoGainControl: settings.autoGainControl,
335
+ noiseSuppression: settings.noiseSuppression,
336
+ echoCancellation: settings.echoCancellation,
337
+ });
338
+ this.peer.call(this.speakerPeerId, stream); // one-way call
339
+ this.displayUpdate('Listener - openAudioStream');
340
+ })
341
+ .catch(err => {
342
+ console.log(err);
343
+ this.displayUpdate(
344
+ `Listener - Error in applyHQTrackConstraints - ${JSON.stringify(err, undefined, 2)}`
345
+ );
346
+ });
347
+ })
348
+ .catch(err => {
349
+ console.error(err);
350
+ if (err.name === 'OverconstrainedError') {
351
+ const constraint = err.constraint;
352
+ const message = `The constraint "${constraint}" cannot be satisfied by the selected microphone. Please adjust your calibration settings or choose a different microphone.`;
353
+
354
+ this.displayUpdate(`Listener - OverconstrainedError: ${message}`);
355
+ console.error(message);
356
+
357
+ alert(`Overconstrained Error: ${message}`);
358
+ }
359
+ this.displayUpdate(
360
+ `Listener - Error in getUserMedia - ${JSON.stringify(err, undefined, 2)}`
361
+ );
362
+ });
363
+ };
364
+ }
365
+
366
+ export default Listener;