speaker-calibration 2.2.221 → 2.2.222

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 (123) 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/index.html +123 -123
  17. package/coverage/lcov-report/prettify.css +101 -101
  18. package/coverage/lcov-report/prettify.js +937 -937
  19. package/coverage/lcov-report/sorter.js +189 -189
  20. package/coverage/lcov-report/src/index.html +121 -121
  21. package/coverage/lcov-report/src/server/PythonServerInterface.js.html +268 -268
  22. package/coverage/lcov-report/src/server/index.html +123 -123
  23. package/coverage/lcov-report/src/tasks/audioCalibrator.js.html +499 -499
  24. package/coverage/lcov-report/src/tasks/audioRecorder.js.html +412 -412
  25. package/coverage/lcov-report/src/tasks/index.html +143 -143
  26. package/coverage/lcov-report/src/tasks/volume/index.html +123 -123
  27. package/coverage/lcov-report/src/tasks/volume/volume.js.html +409 -409
  28. package/coverage/lcov-report/src/utils.js.html +172 -172
  29. package/coverage/lcov.info +91 -91
  30. package/dist/example/NoSleep.min.js +1 -1
  31. package/dist/example/fetch-languages-sheets.js +77 -77
  32. package/dist/example/i18n.js +29082 -29082
  33. package/dist/example/index.html +47 -47
  34. package/dist/example/listener.html +81 -81
  35. package/dist/example/server.js +51 -51
  36. package/dist/example/speaker.html +145 -145
  37. package/dist/example/speakerUI.js +273 -273
  38. package/dist/example/styles.css +152 -152
  39. package/dist/listener.js +3 -3
  40. package/dist/main.js +11 -11
  41. package/dist/mlsGen.js +6814 -6814
  42. package/dist/mlsGen.wasm +0 -0
  43. package/dist/package-lock.json +1018 -1018
  44. package/dist/package.json +18 -18
  45. package/doc/AudioCalibrator.html +417 -417
  46. package/doc/AudioPeer.html +251 -251
  47. package/doc/AudioRecorder.html +195 -195
  48. package/doc/ImpulseResponse.html +215 -215
  49. package/doc/Listener.html +308 -308
  50. package/doc/MlsGenInterface.html +226 -226
  51. package/doc/MyEventEmitter.html +274 -274
  52. package/doc/PythonServerAPI.html +109 -109
  53. package/doc/Speaker.html +276 -276
  54. package/doc/Takes%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +128 -128
  55. package/doc/Takes%20the%20url%20of%20the%20current%20site%0Aand%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +138 -138
  56. package/doc/Takes%20the%20url%20of%20the%20current%20site%20and%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +137 -137
  57. package/doc/Volume.html +88 -88
  58. package/doc/audioCalibrator.js.html +179 -179
  59. package/doc/audioPeer.js.html +175 -175
  60. package/doc/audioRecorder.js.html +163 -163
  61. package/doc/creates%20a%20new%20AudioRecorder%20instance.%20%0ASets%20up%20the%20audio%20context%20and%20file%20reader..html +114 -114
  62. package/doc/fonts/OpenSans-Bold-webfont.svg +1829 -1829
  63. package/doc/fonts/OpenSans-BoldItalic-webfont.svg +1829 -1829
  64. package/doc/fonts/OpenSans-Italic-webfont.svg +1829 -1829
  65. package/doc/fonts/OpenSans-Light-webfont.svg +1830 -1830
  66. package/doc/fonts/OpenSans-LightItalic-webfont.svg +1834 -1834
  67. package/doc/fonts/OpenSans-Regular-webfont.svg +1830 -1830
  68. package/doc/global.html +308 -308
  69. package/doc/index.html +58 -58
  70. package/doc/listener.js.html +170 -170
  71. package/doc/mlsGen_mlsGenInterface.js.html +117 -117
  72. package/doc/myEventEmitter.js.html +124 -124
  73. package/doc/peer-connection_audioPeer.js.html +188 -188
  74. package/doc/peer-connection_listener.js.html +311 -311
  75. package/doc/peer-connection_speaker.js.html +381 -381
  76. package/doc/scripts/linenumber.js +25 -25
  77. package/doc/scripts/prettify/Apache-License-2.0.txt +202 -202
  78. package/doc/scripts/prettify/lang-css.js +24 -24
  79. package/doc/scripts/prettify/prettify.js +640 -640
  80. package/doc/server_PythonServerAPI.js.html +160 -160
  81. package/doc/speaker.js.html +248 -248
  82. package/doc/styles/jsdoc-default.css +371 -371
  83. package/doc/styles/prettify-jsdoc.css +111 -111
  84. package/doc/styles/prettify-tomorrow.css +163 -163
  85. package/doc/tasks_audioCalibrator.js.html +207 -207
  86. package/doc/tasks_audioRecorder.js.html +190 -190
  87. package/doc/tasks_impulse-response_impulseResponse.js.html +442 -442
  88. package/doc/tasks_impulse-response_mlsGen_mlsGenInterface.js.html +175 -175
  89. package/doc/tasks_volume_volume.js.html +185 -185
  90. package/doc/utils.js.html +105 -105
  91. package/jest.config.js +173 -173
  92. package/makefile +74 -0
  93. package/netlify.toml +26 -26
  94. package/package.json +78 -78
  95. package/src/config/firebase.js +26 -26
  96. package/src/index.html +21 -21
  97. package/src/listener-app/listener.js +377 -377
  98. package/src/main.js +22 -22
  99. package/src/myEventEmitter.js +83 -83
  100. package/src/peer-connection/audioPeer.js +148 -148
  101. package/src/peer-connection/listener.js +467 -467
  102. package/src/peer-connection/peerErrors.js +25 -25
  103. package/src/peer-connection/speaker.js +818 -812
  104. package/src/powerCheck.js +98 -98
  105. package/src/server/PythonServerAPI.js +869 -869
  106. package/src/tasks/audioCalibrator.js +360 -360
  107. package/src/tasks/audioRecorder.js +315 -315
  108. package/src/tasks/combination/combination.js +3171 -3171
  109. package/src/tasks/combination/mlsGen/mlsGen.cpp +98 -98
  110. package/src/tasks/combination/mlsGen/mlsGen.hpp +303 -303
  111. package/src/tasks/combination/mlsGen/mlsGenInterface.js +131 -131
  112. package/src/tasks/combination/mlsGen/mlsGenTest.cpp +180 -180
  113. package/src/tasks/impulse-response/impulseResponse.js +610 -610
  114. package/src/tasks/impulse-response/mlsGen/mlsGen.cpp +98 -98
  115. package/src/tasks/impulse-response/mlsGen/mlsGen.hpp +303 -303
  116. package/src/tasks/impulse-response/mlsGen/mlsGenInterface.js +131 -131
  117. package/src/tasks/impulse-response/mlsGen/mlsGenTest.cpp +180 -180
  118. package/src/tasks/volume/volume.cpp +2 -2
  119. package/src/tasks/volume/volume.hpp +22 -22
  120. package/src/tasks/volume/volume.js +279 -279
  121. package/src/utils.js +205 -205
  122. package/webpack.config.js +64 -64
  123. package/.gitignore +0 -81
@@ -1,467 +1,467 @@
1
- import AudioPeer from './audioPeer';
2
- import {UnsupportedDeviceError, MissingSpeakerIdError} from './peerErrors';
3
- import axios from 'axios';
4
- import Peer from 'peerjs';
5
-
6
- /**
7
- * @class Handles the listener's side of the connection. Responsible for getting access to user's microphone,
8
- * and initiating a call to the Speaker.
9
- * @augments AudioPeer
10
- */
11
- class Listener extends AudioPeer {
12
- /**
13
- * Takes a target element where html elements will be appended.
14
- *
15
- * @param params - See type definition for initParameters.
16
- * @example
17
- */
18
- constructor(params) {
19
- super(params);
20
- console.log('Listener constructor', this.peer);
21
- this.microphoneFromAPI = params.microphoneFromAPI ? params.microphoneFromAPI : '';
22
- this.microphoneDeviceId = params.microphoneDeviceId ? params.microphoneDeviceId : '';
23
- // this.deviceInfoFromUser = params.deviceInfoFromUser
24
- // ? params.deviceInfoFromUser
25
- // : {modelNumber: '', modelName: ''};
26
- this.startTime = Date.now();
27
- this.receiverPeerId = null;
28
-
29
- const urlParameters = this.parseURLSearchParams();
30
- this.calibrateSoundHz =
31
- // previous calibrateSoundHz
32
- urlParameters.hz !== null && urlParameters.hz !== undefined ? urlParameters.hz : 48000;
33
- this.calibrateSoundSamplingDesiredBits =
34
- // previous calibrateSoundSamplingDesiredBits
35
- urlParameters.bits !== null && urlParameters.bits !== undefined ? urlParameters.bits : 24;
36
- this.speakerPeerId = urlParameters.speakerPeerId;
37
- this.lastPeerId = this.speakerPeerId;
38
- this.connOpen = false;
39
- }
40
- generateTimeBasedPeerID = async () => {
41
- const now = new Date().getTime();
42
- const randomBuffer = new Uint8Array(10);
43
- crypto.getRandomValues(randomBuffer);
44
- const randomPart = Array.from(randomBuffer)
45
- .map(b => b.toString(36))
46
- .join('');
47
- const toHash = `${now}-${randomPart}`;
48
- const encoder = new TextEncoder();
49
- const data = encoder.encode(toHash);
50
- const hash = await crypto.subtle.digest('SHA-256', data);
51
- const hashArray = Array.from(new Uint8Array(hash));
52
- const hashString = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
53
- const shortHash = hashString.substring(0, 12);
54
- return this.encodeBase62(parseInt(shortHash, 16));
55
- };
56
-
57
- encodeBase62 = num => {
58
- const base = 26;
59
- const characters = 'abcdefghijklmnopqrstuvwxyz';
60
- let result = '';
61
- while (num > 0) {
62
- result = characters[num % base] + result;
63
- num = Math.floor(num / base);
64
- }
65
- return result || 'a';
66
- };
67
-
68
- initializePeer = async () => {
69
- console.log('Initializing PeerJS connection...');
70
- const id = await this.generateTimeBasedPeerID();
71
- console.log('Generated Peer ID:', id);
72
-
73
- try {
74
- this.peer = new Peer(id, {
75
- debug: 2,
76
- host: 'easyeyes-peer-server.herokuapp.com',
77
- port: 443,
78
- secure: true,
79
- config: {
80
- iceServers: [
81
- {
82
- urls: 'stun:stun.relay.metered.ca:80',
83
- },
84
- {
85
- urls: 'turn:global.relay.metered.ca:80',
86
- username: 'de884cfc34189cdf1a5dd616',
87
- credential: 'IcOpouU9/TYBmpHU',
88
- },
89
- {
90
- urls: 'turn:global.relay.metered.ca:80?transport=tcp',
91
- username: 'de884cfc34189cdf1a5dd616',
92
- credential: 'IcOpouU9/TYBmpHU',
93
- },
94
- {
95
- urls: 'turn:global.relay.metered.ca:443',
96
- username: 'de884cfc34189cdf1a5dd616',
97
- credential: 'IcOpouU9/TYBmpHU',
98
- },
99
- {
100
- urls: 'turns:global.relay.metered.ca:443?transport=tcp',
101
- username: 'de884cfc34189cdf1a5dd616',
102
- credential: 'IcOpouU9/TYBmpHU',
103
- },
104
- ],
105
- },
106
- });
107
-
108
- this.peer.on('open', this.onPeerOpen);
109
- this.peer.on('connection', this.onPeerConnection);
110
- this.peer.on('disconnected', this.onPeerDisconnected);
111
- this.peer.on('close', this.onPeerClose);
112
- this.peer.on('error', this.onPeerError);
113
-
114
- console.log('Peer object created:', this.peer);
115
- } catch (error) {
116
- console.error('Failed to initialize PeerJS:', error);
117
- }
118
- };
119
-
120
- onPeerOpen = id => {
121
- this.displayUpdate('Listener - onPeerOpen');
122
- console.log('onPeerOpen: ', id);
123
- // Workaround for peer.reconnect deleting previous id
124
- try {
125
- if (id === null) {
126
- this.displayUpdate('Received null id from peer open');
127
- this.peer.id = this.lastPeerId;
128
- } else {
129
- this.lastPeerId = this.peer.id;
130
- }
131
- } catch (error) {
132
- console.error('Error in onPeerOpen: ', error);
133
- }
134
- this.join();
135
- };
136
-
137
- onPeerConnection = connection => {
138
- this.displayUpdate('Listener - onPeerConnection');
139
- console.log('onPeerConnection: ', connection);
140
- // Disallow incoming connections
141
- connection.on('open', () => {
142
- connection.send('Sender does not accept incoming connections');
143
- setTimeout(() => {
144
- connection.close();
145
- }, 500);
146
- });
147
- };
148
-
149
- onConnData = data => {
150
- this.displayUpdate('Listener - onConnData');
151
- const hasSpeakerID = Object.prototype.hasOwnProperty.call(data, 'speakerPeerId');
152
- // if (!hasSpeakerID) {
153
- // this.displayUpdate('Error in parsing data received! Must set "speakerPeerId" property');
154
- // throw new MissingSpeakerIdError('Must set "speakerPeerId" property');
155
- // } else {
156
- // // this.conn.close();
157
- // this.displayUpdate(this.speakerPeerId);
158
- // this.speakerPeerId = data.speakerPeerId;
159
- // const newParams = {
160
- // speakerPeerId: this.speakerPeerId,
161
- // };
162
- // /*
163
- // FUTURE does this limit usable environments?
164
- // ie does this work if internet is lost after initial page load?
165
- // */
166
- // window.location.search = this.queryStringFromObject(newParams); // Redirect to correctly constructed keypad page
167
- // }
168
- };
169
-
170
- join = async () => {
171
- this.displayUpdate('Listener - join');
172
- console.log(' Creating connection to: ', this.speakerPeerId);
173
- /**
174
- * Create the connection between the two Peers.
175
- *
176
- * Sets up callbacks that handle any events related to the
177
- * connection and data received on it.
178
- */
179
- // Close old connection
180
- if (this.conn) {
181
- this.displayUpdate('Closing old connection');
182
- this.conn.close();
183
- }
184
-
185
- // Create connection to destination peer specified by the query param
186
- this.displayUpdate(`Creating connection to: ${this.speakerPeerId}`);
187
- this.conn = this.peer.connect(this.speakerPeerId, {
188
- reliable: true,
189
- });
190
-
191
- this.displayUpdate('Created connection');
192
- this.conn.on('open', async () => {
193
- this.displayUpdate('Listener - conn open');
194
- this.connOpen = true;
195
- // this.sendSamplingRate();
196
- });
197
-
198
- // Handle incoming data (messages only since this is the signal sender)
199
- this.conn.on('data', this.onConnData);
200
- this.conn.on('close', () => {
201
- console.log('Connection closed');
202
- this.connOpen = false;
203
- });
204
- };
205
-
206
- startCalibration = async () => {
207
- await this.getDeviceInfo();
208
- await this.openAudioStream();
209
- };
210
-
211
- getMobileOS = () => {
212
- const ua = navigator.userAgent;
213
- if (/android/i.test(ua)) {
214
- return 'Android';
215
- }
216
- if (
217
- /iPad|iPhone|iPod/.test(ua) ||
218
- ((navigator?.userAgentData?.platform || navigator?.platform) === 'MacIntel' &&
219
- navigator.maxTouchPoints > 1)
220
- ) {
221
- return 'iOS';
222
- }
223
- return 'Other';
224
- };
225
-
226
- sendSamplingRate = sampleRate => {
227
- this.displayUpdate('Listener - sendSamplingRate');
228
- this.conn.send({
229
- name: 'samplingRate',
230
- payload: sampleRate,
231
- });
232
- };
233
-
234
- sendSampleSize = sampleSize => {
235
- this.displayUpdate('Listener - sendSampleSize');
236
- this.conn.send({
237
- name: 'sampleSize',
238
- payload: sampleSize,
239
- });
240
- };
241
-
242
- sendFlags = flags => {
243
- this.displayUpdate('Listener - sendFlags');
244
- this.conn.send({
245
- name: 'flags',
246
- payload: flags,
247
- });
248
- };
249
-
250
- sendPermissionStatus = status => {
251
- // this.displayUpdate('Listener - sendPermissionStatus');
252
- this.conn.send({
253
- name: 'permissionStatus',
254
- payload: status,
255
- });
256
- };
257
-
258
- getDeviceInfo = async () => {
259
- const deviceInfo = {};
260
- try {
261
- fod.complete(function (data) {
262
- deviceInfo['IsMobile'] = data.device['ismobile'];
263
- deviceInfo['HardwareName'] = data.device['hardwarename'];
264
- deviceInfo['HardwareFamily'] = data.device['hardwarefamily'];
265
- deviceInfo['HardwareModel'] = data.device['hardwaremodel'];
266
- deviceInfo['OEM'] = data.device['oem'];
267
- deviceInfo['HardwareModelVariants'] = data.device['hardwaremodelvariants'];
268
- deviceInfo['DeviceId'] = data.device['deviceid'];
269
- deviceInfo['PlatformName'] = data.device['platformname'];
270
- deviceInfo['PlatformVersion'] = data.device['platformversion'];
271
- deviceInfo['DeviceType'] = data.device['devicetype'];
272
- // deviceInfo['deviceInfoFromUser'] = this.deviceInfoFromUser;
273
- });
274
- } catch (error) {
275
- console.error('Error fetching or executing script:', error.message);
276
- }
277
- // deviceInfo['deviceInfoFromUser'] = this.deviceInfoFromUser;
278
- deviceInfo['microphoneFromAPI'] = this.microphoneFromAPI;
279
- deviceInfo['microphoneDeviceId'] = this.microphoneDeviceId;
280
- deviceInfo['screenWidth'] = window.screen.width;
281
- deviceInfo['screenHeight'] = window.screen.height;
282
- console.log('deviceInfo Inside getDeviceInfo: ', deviceInfo);
283
- this.conn.send({
284
- name: 'deviceInfo',
285
- payload: deviceInfo,
286
- });
287
- return deviceInfo;
288
- };
289
-
290
- applyHQTrackConstraints = async stream => {
291
- // Contraint the incoming audio to the sampling rate we want
292
- stream.getAudioTracks().forEach(track => {
293
- console.log(track, track.enabled);
294
- });
295
- const track = stream.getAudioTracks()[0];
296
- console.log(track);
297
- const capabilities = track.getCapabilities();
298
-
299
- this.displayUpdate(
300
- `Listener Track Capabilities - ${JSON.stringify(capabilities, undefined, 2)}`
301
- );
302
-
303
- const constraints = track.getConstraints();
304
-
305
- if (capabilities.echoCancellation) {
306
- constraints.echoCancellation = false;
307
- }
308
-
309
- if (capabilities.sampleRate) {
310
- constraints.sampleRate = this.calibrateSoundHz;
311
- }
312
-
313
- if (capabilities.sampleSize) {
314
- constraints.sampleSize = this.calibrateSoundSamplingDesiredBits;
315
- }
316
-
317
- if (capabilities.channelCount) {
318
- constraints.channelCount = 1;
319
- }
320
-
321
- this.displayUpdate(`Listener Track Constraints - ${JSON.stringify(constraints, undefined, 2)}`);
322
-
323
- // await the promise
324
- try {
325
- await track.applyConstraints(constraints);
326
- } catch (err) {
327
- console.error(err);
328
- this.displayUpdate(`Error applying constraints to track: ${err}`);
329
- }
330
-
331
- const settings = track.getSettings();
332
- this.displayUpdate(`Listener Track Settings - ${JSON.stringify(settings, undefined, 2)}`);
333
- return settings;
334
- };
335
-
336
- getMediaDevicesAudioContraints = async () => {
337
- const availableConstraints = navigator.mediaDevices.getSupportedConstraints();
338
-
339
- const contraints = {
340
- // ...(availableConstraints.echoCancellation && availableConstraints.echoCancellation == true
341
- // ? {echoCancellation: {exact: false}}
342
- // : {}),
343
- // ...(availableConstraints.sampleRate && availableConstraints.sampleRate == true
344
- // ? {sampleRate: {ideal: this.calibrateSoundHz}}
345
- // : {}),
346
- // ...(availableConstraints.sampleSize && availableConstraints.sampleSize == true
347
- // ? {sampleSize: {ideal: this.calibrateSoundSamplingDesiredBits}}
348
- // : {}),
349
- // ...(availableConstraints.channelCount && availableConstraints.channelCount == true
350
- // ? {channelCount: {exact: 1}}
351
- // : {}),
352
- autoGainControl: false,
353
- noiseSuppression: false,
354
- echoCancellation: false,
355
- channelCount: 1,
356
- };
357
-
358
- if (this.microphoneDeviceId !== '') {
359
- contraints.deviceId = {exact: await this.getDeviceIdByLabel(this.microphoneDeviceId)};
360
- }
361
-
362
- console.log(contraints);
363
-
364
- return contraints;
365
- };
366
- setMicrophoneFromAPI = microphoneFromAPI => {
367
- this.microphoneFromAPI = microphoneFromAPI;
368
- };
369
- setMicrophoneDeviceId = microphoneDeviceId => {
370
- this.microphoneDeviceId = microphoneDeviceId;
371
- };
372
- getDeviceIdByLabel = async targetLabel => {
373
- try {
374
- //get permission to use audio first. (Returns empty labels on some computers if not done first)
375
- await navigator.mediaDevices.getUserMedia({audio: true});
376
- // Enumerate available media devices
377
- const devices = await navigator.mediaDevices.enumerateDevices();
378
-
379
- // Find the device with the matching label
380
- const matchingDevice = devices.find(
381
- device => device.kind === 'audioinput' && device.label === targetLabel
382
- );
383
-
384
- if (matchingDevice) {
385
- return matchingDevice.deviceId; // Return the deviceId if found
386
- } else {
387
- throw new Error(`No audio input device found with label: "${targetLabel}"`);
388
- }
389
- } catch (error) {
390
- console.error('Error finding device ID:', error);
391
- return null;
392
- }
393
- };
394
-
395
- openAudioStream = async () => {
396
- this.displayUpdate('Listener - openAudioStream');
397
- const mobileOS = this.getMobileOS();
398
- if (process.env.NODE_ENV !== 'development' && mobileOS !== 'iOS') {
399
- const err = new UnsupportedDeviceError(`${mobileOS} is not supported`);
400
- this.conn.send({
401
- name: err.name,
402
- payload: err,
403
- });
404
- return;
405
- }
406
- const constraints = await this.getMediaDevicesAudioContraints();
407
- console.log('Constraints right before getUserMedia:', constraints);
408
- navigator.mediaDevices
409
- .getUserMedia({
410
- audio: constraints,
411
- video: false,
412
- //audio: {echoCancellation: false, noiseSuppression: false, autoGainControl: false, deviceId: {exact: await this.getDeviceIdByLabel(this.microphoneDeviceId) }},
413
- })
414
- .then(stream => {
415
- this.displayUpdate(
416
- `Listener Track settings before applied constraints - ${JSON.stringify(
417
- stream.getAudioTracks()[0].getSettings(),
418
- undefined,
419
- 2
420
- )}`
421
- );
422
- this.applyHQTrackConstraints(stream)
423
- .then(settings => {
424
- console.log(settings);
425
- this.sendSamplingRate(settings.sampleRate);
426
- //let sampleSize = settings.sampleSize;
427
- let sampleSize = this.calibrateSoundSamplingDesiredBits;
428
- /*
429
- if (!sampleSize) {
430
- sampleSize = this.calibrateSoundSamplingDesiredBits;
431
- }
432
- */
433
- this.sendSampleSize(sampleSize);
434
- this.sendFlags({
435
- autoGainControl: settings.autoGainControl,
436
- noiseSuppression: settings.noiseSuppression,
437
- echoCancellation: settings.echoCancellation,
438
- });
439
- this.peer.call(this.speakerPeerId, stream); // one-way call
440
- this.displayUpdate('Listener - openAudioStream');
441
- })
442
- .catch(err => {
443
- console.log(err);
444
- this.displayUpdate(
445
- `Listener - Error in applyHQTrackConstraints - ${JSON.stringify(err, undefined, 2)}`
446
- );
447
- });
448
- })
449
- .catch(err => {
450
- console.error(err);
451
- if (err.name === 'OverconstrainedError') {
452
- const constraint = err.constraint;
453
- const message = `The constraint "${constraint}" cannot be satisfied by the selected microphone. Please adjust your calibration settings or choose a different microphone.`;
454
-
455
- this.displayUpdate(`Listener - OverconstrainedError: ${message}`);
456
- console.error(message);
457
-
458
- alert(`Overconstrained Error: ${message}`);
459
- }
460
- this.displayUpdate(
461
- `Listener - Error in getUserMedia - ${JSON.stringify(err, undefined, 2)}`
462
- );
463
- });
464
- };
465
- }
466
-
467
- export default Listener;
1
+ import AudioPeer from './audioPeer';
2
+ import {UnsupportedDeviceError, MissingSpeakerIdError} from './peerErrors';
3
+ import axios from 'axios';
4
+ import Peer from 'peerjs';
5
+
6
+ /**
7
+ * @class Handles the listener's side of the connection. Responsible for getting access to user's microphone,
8
+ * and initiating a call to the Speaker.
9
+ * @augments AudioPeer
10
+ */
11
+ class Listener extends AudioPeer {
12
+ /**
13
+ * Takes a target element where html elements will be appended.
14
+ *
15
+ * @param params - See type definition for initParameters.
16
+ * @example
17
+ */
18
+ constructor(params) {
19
+ super(params);
20
+ console.log('Listener constructor', this.peer);
21
+ this.microphoneFromAPI = params.microphoneFromAPI ? params.microphoneFromAPI : '';
22
+ this.microphoneDeviceId = params.microphoneDeviceId ? params.microphoneDeviceId : '';
23
+ // this.deviceInfoFromUser = params.deviceInfoFromUser
24
+ // ? params.deviceInfoFromUser
25
+ // : {modelNumber: '', modelName: ''};
26
+ this.startTime = Date.now();
27
+ this.receiverPeerId = null;
28
+
29
+ const urlParameters = this.parseURLSearchParams();
30
+ this.calibrateSoundHz =
31
+ // previous calibrateSoundHz
32
+ urlParameters.hz !== null && urlParameters.hz !== undefined ? urlParameters.hz : 48000;
33
+ this.calibrateSoundSamplingDesiredBits =
34
+ // previous calibrateSoundSamplingDesiredBits
35
+ urlParameters.bits !== null && urlParameters.bits !== undefined ? urlParameters.bits : 24;
36
+ this.speakerPeerId = urlParameters.speakerPeerId;
37
+ this.lastPeerId = this.speakerPeerId;
38
+ this.connOpen = false;
39
+ }
40
+ generateTimeBasedPeerID = async () => {
41
+ const now = new Date().getTime();
42
+ const randomBuffer = new Uint8Array(10);
43
+ crypto.getRandomValues(randomBuffer);
44
+ const randomPart = Array.from(randomBuffer)
45
+ .map(b => b.toString(36))
46
+ .join('');
47
+ const toHash = `${now}-${randomPart}`;
48
+ const encoder = new TextEncoder();
49
+ const data = encoder.encode(toHash);
50
+ const hash = await crypto.subtle.digest('SHA-256', data);
51
+ const hashArray = Array.from(new Uint8Array(hash));
52
+ const hashString = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
53
+ const shortHash = hashString.substring(0, 12);
54
+ return this.encodeBase62(parseInt(shortHash, 16));
55
+ };
56
+
57
+ encodeBase62 = num => {
58
+ const base = 26;
59
+ const characters = 'abcdefghijklmnopqrstuvwxyz';
60
+ let result = '';
61
+ while (num > 0) {
62
+ result = characters[num % base] + result;
63
+ num = Math.floor(num / base);
64
+ }
65
+ return result || 'a';
66
+ };
67
+
68
+ initializePeer = async () => {
69
+ console.log('Initializing PeerJS connection...');
70
+ const id = await this.generateTimeBasedPeerID();
71
+ console.log('Generated Peer ID:', id);
72
+
73
+ try {
74
+ this.peer = new Peer(id, {
75
+ debug: 2,
76
+ host: 'easyeyes-peer-server.herokuapp.com',
77
+ port: 443,
78
+ secure: true,
79
+ config: {
80
+ iceServers: [
81
+ {
82
+ urls: 'stun:stun.relay.metered.ca:80',
83
+ },
84
+ {
85
+ urls: 'turn:global.relay.metered.ca:80',
86
+ username: 'de884cfc34189cdf1a5dd616',
87
+ credential: 'IcOpouU9/TYBmpHU',
88
+ },
89
+ {
90
+ urls: 'turn:global.relay.metered.ca:80?transport=tcp',
91
+ username: 'de884cfc34189cdf1a5dd616',
92
+ credential: 'IcOpouU9/TYBmpHU',
93
+ },
94
+ {
95
+ urls: 'turn:global.relay.metered.ca:443',
96
+ username: 'de884cfc34189cdf1a5dd616',
97
+ credential: 'IcOpouU9/TYBmpHU',
98
+ },
99
+ {
100
+ urls: 'turns:global.relay.metered.ca:443?transport=tcp',
101
+ username: 'de884cfc34189cdf1a5dd616',
102
+ credential: 'IcOpouU9/TYBmpHU',
103
+ },
104
+ ],
105
+ },
106
+ });
107
+
108
+ this.peer.on('open', this.onPeerOpen);
109
+ this.peer.on('connection', this.onPeerConnection);
110
+ this.peer.on('disconnected', this.onPeerDisconnected);
111
+ this.peer.on('close', this.onPeerClose);
112
+ this.peer.on('error', this.onPeerError);
113
+
114
+ console.log('Peer object created:', this.peer);
115
+ } catch (error) {
116
+ console.error('Failed to initialize PeerJS:', error);
117
+ }
118
+ };
119
+
120
+ onPeerOpen = id => {
121
+ this.displayUpdate('Listener - onPeerOpen');
122
+ console.log('onPeerOpen: ', id);
123
+ // Workaround for peer.reconnect deleting previous id
124
+ try {
125
+ if (id === null) {
126
+ this.displayUpdate('Received null id from peer open');
127
+ this.peer.id = this.lastPeerId;
128
+ } else {
129
+ this.lastPeerId = this.peer.id;
130
+ }
131
+ } catch (error) {
132
+ console.error('Error in onPeerOpen: ', error);
133
+ }
134
+ this.join();
135
+ };
136
+
137
+ onPeerConnection = connection => {
138
+ this.displayUpdate('Listener - onPeerConnection');
139
+ console.log('onPeerConnection: ', connection);
140
+ // Disallow incoming connections
141
+ connection.on('open', () => {
142
+ connection.send('Sender does not accept incoming connections');
143
+ setTimeout(() => {
144
+ connection.close();
145
+ }, 500);
146
+ });
147
+ };
148
+
149
+ onConnData = data => {
150
+ this.displayUpdate('Listener - onConnData');
151
+ const hasSpeakerID = Object.prototype.hasOwnProperty.call(data, 'speakerPeerId');
152
+ // if (!hasSpeakerID) {
153
+ // this.displayUpdate('Error in parsing data received! Must set "speakerPeerId" property');
154
+ // throw new MissingSpeakerIdError('Must set "speakerPeerId" property');
155
+ // } else {
156
+ // // this.conn.close();
157
+ // this.displayUpdate(this.speakerPeerId);
158
+ // this.speakerPeerId = data.speakerPeerId;
159
+ // const newParams = {
160
+ // speakerPeerId: this.speakerPeerId,
161
+ // };
162
+ // /*
163
+ // FUTURE does this limit usable environments?
164
+ // ie does this work if internet is lost after initial page load?
165
+ // */
166
+ // window.location.search = this.queryStringFromObject(newParams); // Redirect to correctly constructed keypad page
167
+ // }
168
+ };
169
+
170
+ join = async () => {
171
+ this.displayUpdate('Listener - join');
172
+ console.log(' Creating connection to: ', this.speakerPeerId);
173
+ /**
174
+ * Create the connection between the two Peers.
175
+ *
176
+ * Sets up callbacks that handle any events related to the
177
+ * connection and data received on it.
178
+ */
179
+ // Close old connection
180
+ if (this.conn) {
181
+ this.displayUpdate('Closing old connection');
182
+ this.conn.close();
183
+ }
184
+
185
+ // Create connection to destination peer specified by the query param
186
+ this.displayUpdate(`Creating connection to: ${this.speakerPeerId}`);
187
+ this.conn = this.peer.connect(this.speakerPeerId, {
188
+ reliable: true,
189
+ });
190
+
191
+ this.displayUpdate('Created connection');
192
+ this.conn.on('open', async () => {
193
+ this.displayUpdate('Listener - conn open');
194
+ this.connOpen = true;
195
+ // this.sendSamplingRate();
196
+ });
197
+
198
+ // Handle incoming data (messages only since this is the signal sender)
199
+ this.conn.on('data', this.onConnData);
200
+ this.conn.on('close', () => {
201
+ console.log('Connection closed');
202
+ this.connOpen = false;
203
+ });
204
+ };
205
+
206
+ startCalibration = async () => {
207
+ await this.getDeviceInfo();
208
+ await this.openAudioStream();
209
+ };
210
+
211
+ getMobileOS = () => {
212
+ const ua = navigator.userAgent;
213
+ if (/android/i.test(ua)) {
214
+ return 'Android';
215
+ }
216
+ if (
217
+ /iPad|iPhone|iPod/.test(ua) ||
218
+ ((navigator?.userAgentData?.platform || navigator?.platform) === 'MacIntel' &&
219
+ navigator.maxTouchPoints > 1)
220
+ ) {
221
+ return 'iOS';
222
+ }
223
+ return 'Other';
224
+ };
225
+
226
+ sendSamplingRate = sampleRate => {
227
+ this.displayUpdate('Listener - sendSamplingRate');
228
+ this.conn.send({
229
+ name: 'samplingRate',
230
+ payload: sampleRate,
231
+ });
232
+ };
233
+
234
+ sendSampleSize = sampleSize => {
235
+ this.displayUpdate('Listener - sendSampleSize');
236
+ this.conn.send({
237
+ name: 'sampleSize',
238
+ payload: sampleSize,
239
+ });
240
+ };
241
+
242
+ sendFlags = flags => {
243
+ this.displayUpdate('Listener - sendFlags');
244
+ this.conn.send({
245
+ name: 'flags',
246
+ payload: flags,
247
+ });
248
+ };
249
+
250
+ sendPermissionStatus = status => {
251
+ // this.displayUpdate('Listener - sendPermissionStatus');
252
+ this.conn.send({
253
+ name: 'permissionStatus',
254
+ payload: status,
255
+ });
256
+ };
257
+
258
+ getDeviceInfo = async () => {
259
+ const deviceInfo = {};
260
+ try {
261
+ fod.complete(function (data) {
262
+ deviceInfo['IsMobile'] = data.device['ismobile'];
263
+ deviceInfo['HardwareName'] = data.device['hardwarename'];
264
+ deviceInfo['HardwareFamily'] = data.device['hardwarefamily'];
265
+ deviceInfo['HardwareModel'] = data.device['hardwaremodel'];
266
+ deviceInfo['OEM'] = data.device['oem'];
267
+ deviceInfo['HardwareModelVariants'] = data.device['hardwaremodelvariants'];
268
+ deviceInfo['DeviceId'] = data.device['deviceid'];
269
+ deviceInfo['PlatformName'] = data.device['platformname'];
270
+ deviceInfo['PlatformVersion'] = data.device['platformversion'];
271
+ deviceInfo['DeviceType'] = data.device['devicetype'];
272
+ // deviceInfo['deviceInfoFromUser'] = this.deviceInfoFromUser;
273
+ });
274
+ } catch (error) {
275
+ console.error('Error fetching or executing script:', error.message);
276
+ }
277
+ // deviceInfo['deviceInfoFromUser'] = this.deviceInfoFromUser;
278
+ deviceInfo['microphoneFromAPI'] = this.microphoneFromAPI;
279
+ deviceInfo['microphoneDeviceId'] = this.microphoneDeviceId;
280
+ deviceInfo['screenWidth'] = window.screen.width;
281
+ deviceInfo['screenHeight'] = window.screen.height;
282
+ console.log('deviceInfo Inside getDeviceInfo: ', deviceInfo);
283
+ this.conn.send({
284
+ name: 'deviceInfo',
285
+ payload: deviceInfo,
286
+ });
287
+ return deviceInfo;
288
+ };
289
+
290
+ applyHQTrackConstraints = async stream => {
291
+ // Contraint the incoming audio to the sampling rate we want
292
+ stream.getAudioTracks().forEach(track => {
293
+ console.log(track, track.enabled);
294
+ });
295
+ const track = stream.getAudioTracks()[0];
296
+ console.log(track);
297
+ const capabilities = track.getCapabilities();
298
+
299
+ this.displayUpdate(
300
+ `Listener Track Capabilities - ${JSON.stringify(capabilities, undefined, 2)}`
301
+ );
302
+
303
+ const constraints = track.getConstraints();
304
+
305
+ if (capabilities.echoCancellation) {
306
+ constraints.echoCancellation = false;
307
+ }
308
+
309
+ if (capabilities.sampleRate) {
310
+ constraints.sampleRate = this.calibrateSoundHz;
311
+ }
312
+
313
+ if (capabilities.sampleSize) {
314
+ constraints.sampleSize = this.calibrateSoundSamplingDesiredBits;
315
+ }
316
+
317
+ if (capabilities.channelCount) {
318
+ constraints.channelCount = 1;
319
+ }
320
+
321
+ this.displayUpdate(`Listener Track Constraints - ${JSON.stringify(constraints, undefined, 2)}`);
322
+
323
+ // await the promise
324
+ try {
325
+ await track.applyConstraints(constraints);
326
+ } catch (err) {
327
+ console.error(err);
328
+ this.displayUpdate(`Error applying constraints to track: ${err}`);
329
+ }
330
+
331
+ const settings = track.getSettings();
332
+ this.displayUpdate(`Listener Track Settings - ${JSON.stringify(settings, undefined, 2)}`);
333
+ return settings;
334
+ };
335
+
336
+ getMediaDevicesAudioContraints = async () => {
337
+ const availableConstraints = navigator.mediaDevices.getSupportedConstraints();
338
+
339
+ const contraints = {
340
+ // ...(availableConstraints.echoCancellation && availableConstraints.echoCancellation == true
341
+ // ? {echoCancellation: {exact: false}}
342
+ // : {}),
343
+ // ...(availableConstraints.sampleRate && availableConstraints.sampleRate == true
344
+ // ? {sampleRate: {ideal: this.calibrateSoundHz}}
345
+ // : {}),
346
+ // ...(availableConstraints.sampleSize && availableConstraints.sampleSize == true
347
+ // ? {sampleSize: {ideal: this.calibrateSoundSamplingDesiredBits}}
348
+ // : {}),
349
+ // ...(availableConstraints.channelCount && availableConstraints.channelCount == true
350
+ // ? {channelCount: {exact: 1}}
351
+ // : {}),
352
+ autoGainControl: false,
353
+ noiseSuppression: false,
354
+ echoCancellation: false,
355
+ channelCount: 1,
356
+ };
357
+
358
+ if (this.microphoneDeviceId !== '') {
359
+ contraints.deviceId = {exact: await this.getDeviceIdByLabel(this.microphoneDeviceId)};
360
+ }
361
+
362
+ console.log(contraints);
363
+
364
+ return contraints;
365
+ };
366
+ setMicrophoneFromAPI = microphoneFromAPI => {
367
+ this.microphoneFromAPI = microphoneFromAPI;
368
+ };
369
+ setMicrophoneDeviceId = microphoneDeviceId => {
370
+ this.microphoneDeviceId = microphoneDeviceId;
371
+ };
372
+ getDeviceIdByLabel = async targetLabel => {
373
+ try {
374
+ //get permission to use audio first. (Returns empty labels on some computers if not done first)
375
+ await navigator.mediaDevices.getUserMedia({audio: true});
376
+ // Enumerate available media devices
377
+ const devices = await navigator.mediaDevices.enumerateDevices();
378
+
379
+ // Find the device with the matching label
380
+ const matchingDevice = devices.find(
381
+ device => device.kind === 'audioinput' && device.label === targetLabel
382
+ );
383
+
384
+ if (matchingDevice) {
385
+ return matchingDevice.deviceId; // Return the deviceId if found
386
+ } else {
387
+ throw new Error(`No audio input device found with label: "${targetLabel}"`);
388
+ }
389
+ } catch (error) {
390
+ console.error('Error finding device ID:', error);
391
+ return null;
392
+ }
393
+ };
394
+
395
+ openAudioStream = async () => {
396
+ this.displayUpdate('Listener - openAudioStream');
397
+ const mobileOS = this.getMobileOS();
398
+ if (process.env.NODE_ENV !== 'development' && mobileOS !== 'iOS') {
399
+ const err = new UnsupportedDeviceError(`${mobileOS} is not supported`);
400
+ this.conn.send({
401
+ name: err.name,
402
+ payload: err,
403
+ });
404
+ return;
405
+ }
406
+ const constraints = await this.getMediaDevicesAudioContraints();
407
+ console.log('Constraints right before getUserMedia:', constraints);
408
+ navigator.mediaDevices
409
+ .getUserMedia({
410
+ audio: constraints,
411
+ video: false,
412
+ //audio: {echoCancellation: false, noiseSuppression: false, autoGainControl: false, deviceId: {exact: await this.getDeviceIdByLabel(this.microphoneDeviceId) }},
413
+ })
414
+ .then(stream => {
415
+ this.displayUpdate(
416
+ `Listener Track settings before applied constraints - ${JSON.stringify(
417
+ stream.getAudioTracks()[0].getSettings(),
418
+ undefined,
419
+ 2
420
+ )}`
421
+ );
422
+ this.applyHQTrackConstraints(stream)
423
+ .then(settings => {
424
+ console.log(settings);
425
+ this.sendSamplingRate(settings.sampleRate);
426
+ //let sampleSize = settings.sampleSize;
427
+ let sampleSize = this.calibrateSoundSamplingDesiredBits;
428
+ /*
429
+ if (!sampleSize) {
430
+ sampleSize = this.calibrateSoundSamplingDesiredBits;
431
+ }
432
+ */
433
+ this.sendSampleSize(sampleSize);
434
+ this.sendFlags({
435
+ autoGainControl: settings.autoGainControl,
436
+ noiseSuppression: settings.noiseSuppression,
437
+ echoCancellation: settings.echoCancellation,
438
+ });
439
+ this.peer.call(this.speakerPeerId, stream); // one-way call
440
+ this.displayUpdate('Listener - openAudioStream');
441
+ })
442
+ .catch(err => {
443
+ console.log(err);
444
+ this.displayUpdate(
445
+ `Listener - Error in applyHQTrackConstraints - ${JSON.stringify(err, undefined, 2)}`
446
+ );
447
+ });
448
+ })
449
+ .catch(err => {
450
+ console.error(err);
451
+ if (err.name === 'OverconstrainedError') {
452
+ const constraint = err.constraint;
453
+ const message = `The constraint "${constraint}" cannot be satisfied by the selected microphone. Please adjust your calibration settings or choose a different microphone.`;
454
+
455
+ this.displayUpdate(`Listener - OverconstrainedError: ${message}`);
456
+ console.error(message);
457
+
458
+ alert(`Overconstrained Error: ${message}`);
459
+ }
460
+ this.displayUpdate(
461
+ `Listener - Error in getUserMedia - ${JSON.stringify(err, undefined, 2)}`
462
+ );
463
+ });
464
+ };
465
+ }
466
+
467
+ export default Listener;