speaker-calibration 2.2.270 → 2.2.272

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 (120) hide show
  1. package/.env.example +1 -1
  2. package/.eslintignore +71 -71
  3. package/.eslintrc.json +40 -40
  4. package/.gitignore +81 -81
  5. package/.prettierignore +69 -69
  6. package/.prettierrc +14 -14
  7. package/LICENSE +21 -21
  8. package/README.md +133 -133
  9. package/__mocks__/fileMock.js +1 -1
  10. package/__mocks__/styleMock.js +1 -1
  11. package/babel.config.js +3 -3
  12. package/coverage/clover.xml +71 -71
  13. package/coverage/coverage-final.json +224 -224
  14. package/coverage/lcov-report/PythonServerInterface.js.html +265 -265
  15. package/coverage/lcov-report/base.css +354 -354
  16. package/coverage/lcov-report/block-navigation.js +82 -82
  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/sorter.js +189 -189
  21. package/coverage/lcov-report/src/index.html +121 -121
  22. package/coverage/lcov-report/src/server/PythonServerInterface.js.html +268 -268
  23. package/coverage/lcov-report/src/server/index.html +123 -123
  24. package/coverage/lcov-report/src/tasks/audioCalibrator.js.html +499 -499
  25. package/coverage/lcov-report/src/tasks/audioRecorder.js.html +412 -412
  26. package/coverage/lcov-report/src/tasks/index.html +143 -143
  27. package/coverage/lcov-report/src/tasks/volume/index.html +123 -123
  28. package/coverage/lcov-report/src/tasks/volume/volume.js.html +409 -409
  29. package/coverage/lcov-report/src/utils.js.html +172 -172
  30. package/coverage/lcov.info +91 -91
  31. package/dist/example/NoSleep.min.js +1 -1
  32. package/dist/example/index.html +47 -47
  33. package/dist/example/server.js +51 -51
  34. package/dist/example/speaker.html +145 -145
  35. package/dist/example/speakerUI.js +273 -273
  36. package/dist/example/styles.css +152 -152
  37. package/dist/main.js +3208 -2
  38. package/dist/main.js.LICENSE.txt +118 -118
  39. package/dist/mlsGen.js +6814 -6814
  40. package/dist/mlsGen.wasm +0 -0
  41. package/dist/package-lock.json +1018 -1018
  42. package/dist/package.json +18 -18
  43. package/dist/phonePeer.js +1242 -1
  44. package/doc/AudioCalibrator.html +417 -417
  45. package/doc/AudioPeer.html +251 -251
  46. package/doc/AudioRecorder.html +195 -195
  47. package/doc/ImpulseResponse.html +215 -215
  48. package/doc/Listener.html +308 -308
  49. package/doc/MlsGenInterface.html +226 -226
  50. package/doc/MyEventEmitter.html +274 -274
  51. package/doc/PythonServerAPI.html +109 -109
  52. package/doc/Speaker.html +276 -276
  53. package/doc/Takes%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +128 -128
  54. package/doc/Takes%20the%20url%20of%20the%20current%20site%0Aand%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +138 -138
  55. package/doc/Takes%20the%20url%20of%20the%20current%20site%20and%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +137 -137
  56. package/doc/Volume.html +88 -88
  57. package/doc/audioCalibrator.js.html +179 -179
  58. package/doc/audioPeer.js.html +175 -175
  59. package/doc/audioRecorder.js.html +163 -163
  60. package/doc/creates%20a%20new%20AudioRecorder%20instance.%20%0ASets%20up%20the%20audio%20context%20and%20file%20reader..html +114 -114
  61. package/doc/fonts/OpenSans-Bold-webfont.svg +1829 -1829
  62. package/doc/fonts/OpenSans-BoldItalic-webfont.svg +1829 -1829
  63. package/doc/fonts/OpenSans-Italic-webfont.svg +1829 -1829
  64. package/doc/fonts/OpenSans-Light-webfont.svg +1830 -1830
  65. package/doc/fonts/OpenSans-LightItalic-webfont.svg +1834 -1834
  66. package/doc/fonts/OpenSans-Regular-webfont.svg +1830 -1830
  67. package/doc/global.html +308 -308
  68. package/doc/index.html +58 -58
  69. package/doc/listener.js.html +170 -170
  70. package/doc/mlsGen_mlsGenInterface.js.html +117 -117
  71. package/doc/myEventEmitter.js.html +124 -124
  72. package/doc/peer-connection_audioPeer.js.html +188 -188
  73. package/doc/peer-connection_listener.js.html +311 -311
  74. package/doc/peer-connection_speaker.js.html +381 -381
  75. package/doc/scripts/linenumber.js +25 -25
  76. package/doc/scripts/prettify/Apache-License-2.0.txt +202 -202
  77. package/doc/scripts/prettify/lang-css.js +24 -24
  78. package/doc/scripts/prettify/prettify.js +640 -640
  79. package/doc/server_PythonServerAPI.js.html +160 -160
  80. package/doc/speaker.js.html +248 -248
  81. package/doc/styles/jsdoc-default.css +371 -371
  82. package/doc/styles/prettify-jsdoc.css +111 -111
  83. package/doc/styles/prettify-tomorrow.css +163 -163
  84. package/doc/tasks_audioCalibrator.js.html +207 -207
  85. package/doc/tasks_audioRecorder.js.html +190 -190
  86. package/doc/tasks_impulse-response_impulseResponse.js.html +442 -442
  87. package/doc/tasks_impulse-response_mlsGen_mlsGenInterface.js.html +175 -175
  88. package/doc/tasks_volume_volume.js.html +185 -185
  89. package/doc/utils.js.html +105 -105
  90. package/jest.config.js +173 -173
  91. package/netlify.toml +26 -26
  92. package/package.json +78 -78
  93. package/src/config/firebase.js +27 -27
  94. package/src/index.html +21 -21
  95. package/src/listener-app/PhonePeer.js +504 -504
  96. package/src/main.js +22 -22
  97. package/src/myEventEmitter.js +83 -83
  98. package/src/peer-connection/audioPeer.js +100 -100
  99. package/src/peer-connection/listener.js +298 -298
  100. package/src/peer-connection/peerErrors.js +25 -25
  101. package/src/peer-connection/speaker.js +996 -996
  102. package/src/powerCheck.js +110 -110
  103. package/src/server/PythonServerAPI.js +1001 -1001
  104. package/src/tasks/audioCalibrator.js +451 -451
  105. package/src/tasks/audioRecorder.js +351 -351
  106. package/src/tasks/combination/combination.js +3988 -3988
  107. package/src/tasks/combination/mlsGen/mlsGen.cpp +98 -98
  108. package/src/tasks/combination/mlsGen/mlsGen.hpp +303 -303
  109. package/src/tasks/combination/mlsGen/mlsGenInterface.js +131 -131
  110. package/src/tasks/combination/mlsGen/mlsGenTest.cpp +180 -180
  111. package/src/tasks/impulse-response/impulseResponse.js +610 -610
  112. package/src/tasks/impulse-response/mlsGen/mlsGen.cpp +98 -98
  113. package/src/tasks/impulse-response/mlsGen/mlsGen.hpp +303 -303
  114. package/src/tasks/impulse-response/mlsGen/mlsGenInterface.js +131 -131
  115. package/src/tasks/impulse-response/mlsGen/mlsGenTest.cpp +180 -180
  116. package/src/tasks/volume/volume.cpp +2 -2
  117. package/src/tasks/volume/volume.hpp +22 -22
  118. package/src/tasks/volume/volume.js +279 -279
  119. package/src/utils.js +220 -220
  120. package/webpack.config.js +72 -72
@@ -1,504 +1,504 @@
1
- import Listener from '../peer-connection/listener.js';
2
-
3
- export class PhonePeer {
4
- constructor() {
5
- // Phrases are injected over the peer channel via handlePhrases (message: 'phrases').
6
- this.phrases = {};
7
- this.name = 'SoundCalibration';
8
- this.listenerParameters = {
9
- targetElementId: 'display',
10
- microphoneFromAPI: '',
11
- microphoneDeviceId: '',
12
- lang: 'en',
13
- };
14
- this.listener = null;
15
- this.connectionManager = null;
16
- }
17
-
18
- register = manager => {
19
- this.connectionManager = manager;
20
- };
21
-
22
- onMessage = (data, manager) => {
23
- if (!data || !data.message) return;
24
-
25
- switch (data.message) {
26
- case 'connectionParams':
27
- console.log('connectionParams', data.payload);
28
- this.handleConnectionParams(data.payload);
29
- break;
30
- case 'phrases':
31
- console.log('phrases', data.payload);
32
- this.handlePhrases(data.payload);
33
- break;
34
- }
35
- };
36
-
37
- handleConnectionParams = async payload => {
38
- this.isSmartPhone = payload.sp;
39
- this.listenerParameters.speakerPeerId = payload.speakerPeerId;
40
- this.listenerParameters.hz = payload.hz;
41
- this.listenerParameters.bits = payload.bits;
42
- this.listenerParameters.lang = payload.lang ?? 'en';
43
- this.listenerParameters.deviceId = payload.deviceId;
44
- this.listenerParameters.sp = payload.sp;
45
- this.listenerParameters.microphoneDeviceId = payload.deviceId;
46
-
47
- this.handleStartCalibration();
48
- };
49
-
50
- handlePhrases = payload => {
51
- this.phrases = payload;
52
- };
53
-
54
- createUI = () => {
55
- const container = document.createElement('div');
56
- container.id = 'listenerContainer';
57
- container.className = 'container';
58
- container.innerHTML = `
59
- <p id="turnMeToReadBelow"></p>
60
- <div id="updateDisplay">
61
- <h1 id="recordingInProgress"></h1>
62
- <p id="allowMicrophone"></p>
63
- <button id="calibrationBeginButton" class="btn btn-success">Proceed</button>
64
- </div>
65
- <div id="display"></div>
66
- <div id="message"></div>
67
- <div id="phrases"></div>
68
- `;
69
- const target = document.getElementById('target');
70
- if (target) {
71
- console.log('target found');
72
- //clear the target
73
- target.innerHTML = '';
74
- target.appendChild(container);
75
- } else {
76
- console.log('target not found');
77
- //create a new target
78
- const newTarget = document.createElement('div');
79
- newTarget.id = 'target';
80
- document.body.appendChild(newTarget);
81
- newTarget.appendChild(container);
82
- }
83
- console.log('UI created');
84
- };
85
-
86
- handleStartCalibration = async () => {
87
- this.createUI();
88
- if (this.isSmartPhone) {
89
- await this.initializeSmartPhoneMode();
90
- } else {
91
- await this.initializeDesktopMode();
92
- }
93
- };
94
-
95
- initializeListener = async () => {
96
- this.listener = new Listener(this.listenerParameters);
97
- this.listener.connectionManager = this.connectionManager;
98
- };
99
-
100
- waitForConnection = async (timeout = 30000) => {
101
- const startTime = Date.now();
102
-
103
- while (Date.now() - startTime < timeout) {
104
- if (
105
- this.listener.connectionManager.peer.id !== null &&
106
- this.listener.connectionManager.conn !== null &&
107
- this.listener.connectionManager.connOpen
108
- ) {
109
- console.log('Connection established successfully');
110
- return true;
111
- }
112
- console.log('Waiting for connection setup...');
113
- await new Promise(resolve => setTimeout(resolve, 100));
114
- }
115
-
116
- console.error('Connection setup timed out after 30 seconds');
117
- return false;
118
- };
119
-
120
- initializeSmartPhoneMode = async () => {
121
- // Hide target element
122
- const targetElement = document.getElementById('display');
123
- targetElement.style.display = 'none';
124
-
125
- // Initialize listener
126
- await this.initializeListener();
127
-
128
- if (await this.waitForConnection()) {
129
- await this.checkAndRequestMicrophonePermission();
130
- } else {
131
- const allowMicrophoneElement = document.getElementById('allowMicrophone');
132
- let language = this.listenerParameters.lang;
133
- allowMicrophoneElement.innerText = this.phrases.RC_microphonePermissionDenied[language];
134
- this.listener.sendPermissionStatus({
135
- type: 'error',
136
- error: 'Connection setup timed out after 30 seconds',
137
- });
138
- }
139
- };
140
-
141
- checkAndRequestMicrophonePermission = async () => {
142
- const container = document.getElementById('listenerContainer');
143
- const allowMicrophoneElement = document.getElementById('allowMicrophone');
144
-
145
- // Show permission request message
146
- let language = this.listenerParameters.lang;
147
- allowMicrophoneElement.innerText = this.phrases.RC_microphonePermission[language];
148
- container.style.display = 'block';
149
-
150
- // Function to request microphone access
151
- const requestMicAccess = async (attempt = 1) => {
152
- try {
153
- await navigator.mediaDevices.getUserMedia({audio: true});
154
- // Permission granted, proceed to normal flow
155
- this.initializeSmartPhoneDisplay();
156
- } catch (err) {
157
- if (err.name === 'NotAllowedError') {
158
- console.log('Permission explicitly denied');
159
- // Permission explicitly denied
160
- allowMicrophoneElement.innerText = this.phrases.RC_microphonePermissionDenied[language];
161
- // Send denied status and end study
162
- let error = JSON.stringify(err);
163
- this.listener.sendPermissionStatus({type: 'denied', error: error});
164
- return;
165
- }
166
-
167
- // If 10 seconds passed, try again
168
- if (attempt < 3) {
169
- console.log('Retrying microphone access');
170
- // Limit retries
171
- setTimeout(() => requestMicAccess(attempt + 1), 10000);
172
- } else {
173
- console.log('All retries failed, treating as denied');
174
- // After all retries failed, treat as denied
175
- allowMicrophoneElement.innerText = this.phrases.RC_microphonePermissionDenied[language];
176
- let error = JSON.stringify(err);
177
- this.listener.sendPermissionStatus({type: 'error', error: error});
178
- }
179
- }
180
- };
181
-
182
- try {
183
- await requestMicAccess();
184
- } catch (err) {
185
- console.error('Error requesting microphone permission:', err);
186
- allowMicrophoneElement.innerText = this.phrases.RC_microphonePermissionDenied[language];
187
- let error = JSON.stringify(err);
188
- this.listener.sendPermissionStatus({type: 'error', error: error});
189
- }
190
- };
191
-
192
- initializeSmartPhoneDisplay = () => {
193
- const container = document.getElementById('listenerContainer');
194
- const allowMicrophoneElement = document.getElementById('allowMicrophone');
195
- const turnMessageElement = document.getElementById('turnMeToReadBelow');
196
- let language = this.listenerParameters.lang;
197
- const placeSmartphoneMicrophone = this.phrases.RC_placeSmartphoneMicrophone[language];
198
- const turnMeToReadBelow = this.phrases.RC_turnMeToReadBelow[language];
199
-
200
- allowMicrophoneElement.innerText = placeSmartphoneMicrophone;
201
- allowMicrophoneElement.style.lineHeight = '1.2rem';
202
- allowMicrophoneElement.style.fontSize = '14px';
203
- turnMessageElement.innerText = turnMeToReadBelow;
204
- turnMessageElement.style.fontSize = '14px';
205
- turnMessageElement.style.position = 'fixed';
206
- turnMessageElement.style.top = '20px';
207
- turnMessageElement.style.left = '20px';
208
- turnMessageElement.style.lineHeight = '1.2rem';
209
- turnMessageElement.style.textAlign = 'left';
210
-
211
- // Show the html upsidedown and adjust layout
212
- const phrasesContainer = document.getElementById('phrases');
213
- phrasesContainer.classList.add('phrases');
214
-
215
- // Hide all elements except what's needed for calibration
216
- const html = document.querySelector('html');
217
- html.style.overflow = 'hidden';
218
-
219
- // Adjust the display container
220
- const display = document.getElementById('updateDisplay');
221
- display.classList.add('updateDisplay');
222
- display.style.position = 'absolute';
223
- display.style.top = '70%';
224
- display.style.left = '50%';
225
- display.style.transform = 'translate(-50%, -50%) rotate(180deg)';
226
- display.style.width = '100%';
227
- display.style.textAlign = 'left';
228
- display.style.padding = '15px';
229
-
230
- container.style.display = 'block';
231
-
232
- // event listener for id calibrationBeginButton
233
- const calibrationBeginButton = document.getElementById('calibrationBeginButton');
234
- console.log('Waiting for proceed button click');
235
-
236
- calibrationBeginButton.addEventListener('click', async () => {
237
- console.log('Proceed button clicked');
238
- this.handleCalibrationButtonClick();
239
- });
240
- };
241
-
242
- handleCalibrationButtonClick = async () => {
243
- const calibrationBeginButton = document.getElementById('calibrationBeginButton');
244
- const turnMessageElement = document.getElementById('turnMeToReadBelow');
245
- const allowMicrophoneElement = document.getElementById('allowMicrophone');
246
- const container = document.getElementById('listenerContainer');
247
- const targetElement = document.getElementById('display');
248
- let language = this.listenerParameters.lang;
249
- // Clear unnecessary elements
250
- calibrationBeginButton.remove();
251
- turnMessageElement.remove();
252
-
253
- // Create a header container for fixed elements
254
- const headerContainer = document.createElement('div');
255
- headerContainer.id = 'headerContainer';
256
- headerContainer.style.position = 'fixed';
257
- headerContainer.style.bottom = '0';
258
- headerContainer.style.left = '0';
259
- headerContainer.style.width = '100%';
260
- headerContainer.style.background = 'white';
261
- headerContainer.style.padding = '10px';
262
- headerContainer.style.zIndex = '1000';
263
- headerContainer.style.transform = 'rotate(180deg)';
264
- container.appendChild(headerContainer);
265
-
266
- // Set title based on screen width
267
- const title = document.createElement('h1');
268
- const titleText =
269
- window.innerWidth >= 1366
270
- ? this.phrases.RC_soundRecording[language]
271
- : this.phrases.RC_soundRecordingSmallScreen[language];
272
-
273
- // Split small screen title into lines if needed
274
- if (window.innerWidth < 1366 && titleText.includes('\n')) {
275
- const lines = titleText.split('\n');
276
-
277
- // Create container for title lines
278
- const titleContainer = document.createElement('div');
279
- titleContainer.style.display = 'flex';
280
- titleContainer.style.flexDirection = 'column';
281
- titleContainer.style.alignItems = 'left';
282
- titleContainer.style.lineHeight = '1.2';
283
-
284
- // Add each line
285
- lines.forEach(line => {
286
- const lineDiv = document.createElement('p');
287
- lineDiv.textContent = line;
288
- lineDiv.style.width = 'fit-content';
289
- titleContainer.appendChild(lineDiv);
290
- });
291
-
292
- title.appendChild(titleContainer);
293
- } else {
294
- title.textContent = titleText;
295
- title.style.lineHeight = '1.2';
296
- }
297
-
298
- title.style.margin = '0';
299
- title.style.whiteSpace = 'pre-line'; // Preserve line breaks
300
- headerContainer.appendChild(title);
301
-
302
- // Function to adjust font size to fill width
303
- const adjustFontSize = (element, maxWidth) => {
304
- let fontSize = 20; // Start with a reasonable minimum size
305
- element.style.fontSize = fontSize + 'px';
306
- // Increase font size until text fills width (minus margins)
307
- while (element.scrollWidth < maxWidth - 40 && fontSize < 200) {
308
- fontSize++;
309
- element.style.fontSize = fontSize + 'px';
310
- }
311
-
312
- // Step back one to ensure we don't overflow
313
- fontSize--;
314
- element.style.fontSize = fontSize + 'px';
315
- return fontSize;
316
- };
317
-
318
- // For small screen, ensure all lines use same font size
319
- if (window.innerWidth < 1366 && titleText.includes('\n')) {
320
- const lines = title.querySelectorAll('p');
321
- let minFontSize = Infinity;
322
-
323
- // First pass: find the smallest font size that fits for any line
324
- lines.forEach(line => {
325
- const fontSize = adjustFontSize(line, window.innerWidth);
326
- minFontSize = Math.min(minFontSize, fontSize);
327
- });
328
-
329
- // Second pass: apply the smallest font size to all lines
330
- lines.forEach(line => {
331
- line.style.fontSize = minFontSize + 'px';
332
- });
333
- } else {
334
- // For single line title, just adjust to fill width
335
- adjustFontSize(title, window.innerWidth);
336
- }
337
-
338
- // Get the header height after text is added and sized
339
- const headerHeight = headerContainer.getBoundingClientRect().height;
340
-
341
- // Adjust the display container to start after header
342
- const display = document.getElementById('updateDisplay');
343
- display.classList.add('updateDisplay');
344
- display.style.position = 'fixed';
345
- display.style.bottom = `${headerHeight}px`; // Start after header
346
- display.style.left = '0';
347
- display.style.right = '0';
348
- display.style.top = '0';
349
- display.style.transform = 'rotate(180deg)';
350
- display.style.overflowY = 'auto';
351
- display.style.padding = '20px';
352
- display.style.background = 'white';
353
-
354
- // Position microphone instruction at the top (appears at bottom due to rotation)
355
- allowMicrophoneElement.innerText = '';
356
- allowMicrophoneElement.style.position = 'fixed';
357
- allowMicrophoneElement.style.top = '20px';
358
- allowMicrophoneElement.style.left = '50%';
359
- allowMicrophoneElement.style.transform = 'translateX(-50%) rotate(180deg)';
360
- allowMicrophoneElement.style.width = '90%';
361
- allowMicrophoneElement.style.textAlign = 'left';
362
- allowMicrophoneElement.style.zIndex = '1000';
363
-
364
- const webAudioDeviceNames = {microphone: '', deviceID: ''};
365
- const externalMicList = ['UMIK', 'Airpods', 'Bluetooth'];
366
- try {
367
- const stream = await navigator.mediaDevices.getUserMedia({audio: true});
368
- if (stream) {
369
- const devices = await navigator.mediaDevices.enumerateDevices();
370
- const mics = devices.filter(device => device.kind === 'audioinput');
371
- mics.forEach(mic => {
372
- if (externalMicList.some(externalMic => mic.label.includes(externalMic))) {
373
- webAudioDeviceNames.microphone = mic.label;
374
- webAudioDeviceNames.deviceID = mic.deviceId;
375
- }
376
- });
377
- if (webAudioDeviceNames.microphone === '') {
378
- webAudioDeviceNames.microphone = mics[0].label;
379
- webAudioDeviceNames.deviceID = mics[0].deviceId;
380
- }
381
- }
382
- } catch (err) {
383
- console.log(err);
384
- }
385
-
386
- let lock = null;
387
- try {
388
- if ('wakeLock' in navigator) {
389
- lock = await navigator.wakeLock.request('screen');
390
- }
391
- } catch (err) {
392
- console.log(err);
393
- }
394
-
395
- this.setupMicrophone();
396
-
397
- // show target element
398
- targetElement.style.display = 'block';
399
- await this.listener.startCalibration({
400
- microphoneFromAPI: webAudioDeviceNames.microphone,
401
- microphoneDeviceId: webAudioDeviceNames.deviceID,
402
- });
403
- if (lock) {
404
- lock.release();
405
- }
406
- };
407
-
408
- setupMicrophone = async () => {
409
- const webAudioDeviceNames = {microphone: '', deviceID: ''};
410
- const externalMicList = ['UMIK', 'Airpods', 'Bluetooth'];
411
- try {
412
- const stream = await navigator.mediaDevices.getUserMedia({audio: true});
413
- if (stream) {
414
- const devices = await navigator.mediaDevices.enumerateDevices();
415
- const mics = devices.filter(device => device.kind === 'audioinput');
416
- mics.forEach(mic => {
417
- if (externalMicList.some(externalMic => mic.label.includes(externalMic))) {
418
- webAudioDeviceNames.microphone = mic.label;
419
- webAudioDeviceNames.deviceID = mic.deviceId;
420
- }
421
- });
422
- if (webAudioDeviceNames.microphone === '') {
423
- webAudioDeviceNames.microphone = mics[0].label;
424
- webAudioDeviceNames.deviceID = mics[0].deviceId;
425
- }
426
- }
427
- } catch (err) {
428
- console.log(err);
429
- }
430
- this.listener.setMicrophoneFromAPI(webAudioDeviceNames.microphone);
431
- this.listener.setMicrophoneDeviceId(webAudioDeviceNames.microphone);
432
- };
433
-
434
- initializeDesktopMode = async () => {
435
- await this.initializeListener();
436
-
437
- if (await this.waitForConnection()) {
438
- await this.setupDesktopUI();
439
- } else {
440
- console.error('Connection setup timed out after 30 seconds');
441
- }
442
- };
443
-
444
- setupDesktopUI = async () => {
445
- const container = document.getElementById('listenerContainer');
446
- const recordingInProgressElement = document.getElementById('recordingInProgress');
447
- const allowMicrophoneElement = document.getElementById('allowMicrophone');
448
- let language = this.listenerParameters.lang;
449
- const recordingInProgress = this.phrases.RC_soundRecording[language];
450
- const backToExperimentWindow = this.phrases.RC_backToExperimentWindow[language];
451
- const allowMicrophone = this.phrases.RC_allowMicrophoneUse[language];
452
-
453
- // remove the button
454
- const calibrationBeginButton = document.getElementById('calibrationBeginButton');
455
- calibrationBeginButton.remove();
456
- container.style.display = 'block';
457
-
458
- //update the display to be
459
- const display = document.getElementById('display');
460
- if (display) {
461
- display.style.textAlign = 'left';
462
- display.style.fontSize = '11px';
463
- }
464
-
465
- // set the text of the html elements
466
- recordingInProgressElement.innerText = recordingInProgress;
467
- allowMicrophoneElement.innerText = allowMicrophone;
468
- allowMicrophoneElement.style.marginBottom = '0.5rem';
469
- allowMicrophoneElement.style.fontSize = '17px';
470
-
471
- recordingInProgressElement.style.whiteSpace = 'nowrap';
472
- recordingInProgressElement.style.fontWeight = 'bold';
473
-
474
- // fit content
475
- recordingInProgressElement.style.width = 'fit-content';
476
- let fontSize = 100;
477
- recordingInProgressElement.style.fontSize = fontSize + 'px';
478
-
479
- while (recordingInProgressElement.scrollWidth > window.innerWidth * 0.9 && fontSize > 10) {
480
- fontSize--;
481
- recordingInProgressElement.style.fontSize = fontSize + 'px';
482
- }
483
-
484
- const message = document.getElementById('message');
485
- message.style.lineHeight = '2.5rem';
486
- const p = document.createElement('p');
487
- p.innerText = backToExperimentWindow;
488
- p.style.fontSize = '17px';
489
- message.appendChild(p);
490
-
491
- await this.listener.startCalibration();
492
- };
493
- }
494
-
495
- async function main() {
496
- const pp = new PhonePeer();
497
- if (window.phoneApp) {
498
- window.phoneApp.registerSubmodule(pp);
499
- } else {
500
- console.log('PhoneApp not found');
501
- }
502
- }
503
-
504
- main().catch(err => console.error(err));
1
+ import Listener from '../peer-connection/listener.js';
2
+
3
+ export class PhonePeer {
4
+ constructor() {
5
+ // Phrases are injected over the peer channel via handlePhrases (message: 'phrases').
6
+ this.phrases = {};
7
+ this.name = 'SoundCalibration';
8
+ this.listenerParameters = {
9
+ targetElementId: 'display',
10
+ microphoneFromAPI: '',
11
+ microphoneDeviceId: '',
12
+ lang: 'en',
13
+ };
14
+ this.listener = null;
15
+ this.connectionManager = null;
16
+ }
17
+
18
+ register = manager => {
19
+ this.connectionManager = manager;
20
+ };
21
+
22
+ onMessage = (data, manager) => {
23
+ if (!data || !data.message) return;
24
+
25
+ switch (data.message) {
26
+ case 'connectionParams':
27
+ console.log('connectionParams', data.payload);
28
+ this.handleConnectionParams(data.payload);
29
+ break;
30
+ case 'phrases':
31
+ console.log('phrases', data.payload);
32
+ this.handlePhrases(data.payload);
33
+ break;
34
+ }
35
+ };
36
+
37
+ handleConnectionParams = async payload => {
38
+ this.isSmartPhone = payload.sp;
39
+ this.listenerParameters.speakerPeerId = payload.speakerPeerId;
40
+ this.listenerParameters.hz = payload.hz;
41
+ this.listenerParameters.bits = payload.bits;
42
+ this.listenerParameters.lang = payload.lang ?? 'en';
43
+ this.listenerParameters.deviceId = payload.deviceId;
44
+ this.listenerParameters.sp = payload.sp;
45
+ this.listenerParameters.microphoneDeviceId = payload.deviceId;
46
+
47
+ this.handleStartCalibration();
48
+ };
49
+
50
+ handlePhrases = payload => {
51
+ this.phrases = payload;
52
+ };
53
+
54
+ createUI = () => {
55
+ const container = document.createElement('div');
56
+ container.id = 'listenerContainer';
57
+ container.className = 'container';
58
+ container.innerHTML = `
59
+ <p id="turnMeToReadBelow"></p>
60
+ <div id="updateDisplay">
61
+ <h1 id="recordingInProgress"></h1>
62
+ <p id="allowMicrophone"></p>
63
+ <button id="calibrationBeginButton" class="btn btn-success">Proceed</button>
64
+ </div>
65
+ <div id="display"></div>
66
+ <div id="message"></div>
67
+ <div id="phrases"></div>
68
+ `;
69
+ const target = document.getElementById('target');
70
+ if (target) {
71
+ console.log('target found');
72
+ //clear the target
73
+ target.innerHTML = '';
74
+ target.appendChild(container);
75
+ } else {
76
+ console.log('target not found');
77
+ //create a new target
78
+ const newTarget = document.createElement('div');
79
+ newTarget.id = 'target';
80
+ document.body.appendChild(newTarget);
81
+ newTarget.appendChild(container);
82
+ }
83
+ console.log('UI created');
84
+ };
85
+
86
+ handleStartCalibration = async () => {
87
+ this.createUI();
88
+ if (this.isSmartPhone) {
89
+ await this.initializeSmartPhoneMode();
90
+ } else {
91
+ await this.initializeDesktopMode();
92
+ }
93
+ };
94
+
95
+ initializeListener = async () => {
96
+ this.listener = new Listener(this.listenerParameters);
97
+ this.listener.connectionManager = this.connectionManager;
98
+ };
99
+
100
+ waitForConnection = async (timeout = 30000) => {
101
+ const startTime = Date.now();
102
+
103
+ while (Date.now() - startTime < timeout) {
104
+ if (
105
+ this.listener.connectionManager.peer.id !== null &&
106
+ this.listener.connectionManager.conn !== null &&
107
+ this.listener.connectionManager.connOpen
108
+ ) {
109
+ console.log('Connection established successfully');
110
+ return true;
111
+ }
112
+ console.log('Waiting for connection setup...');
113
+ await new Promise(resolve => setTimeout(resolve, 100));
114
+ }
115
+
116
+ console.error('Connection setup timed out after 30 seconds');
117
+ return false;
118
+ };
119
+
120
+ initializeSmartPhoneMode = async () => {
121
+ // Hide target element
122
+ const targetElement = document.getElementById('display');
123
+ targetElement.style.display = 'none';
124
+
125
+ // Initialize listener
126
+ await this.initializeListener();
127
+
128
+ if (await this.waitForConnection()) {
129
+ await this.checkAndRequestMicrophonePermission();
130
+ } else {
131
+ const allowMicrophoneElement = document.getElementById('allowMicrophone');
132
+ let language = this.listenerParameters.lang;
133
+ allowMicrophoneElement.innerText = this.phrases.RC_microphonePermissionDenied[language];
134
+ this.listener.sendPermissionStatus({
135
+ type: 'error',
136
+ error: 'Connection setup timed out after 30 seconds',
137
+ });
138
+ }
139
+ };
140
+
141
+ checkAndRequestMicrophonePermission = async () => {
142
+ const container = document.getElementById('listenerContainer');
143
+ const allowMicrophoneElement = document.getElementById('allowMicrophone');
144
+
145
+ // Show permission request message
146
+ let language = this.listenerParameters.lang;
147
+ allowMicrophoneElement.innerText = this.phrases.RC_microphonePermission[language];
148
+ container.style.display = 'block';
149
+
150
+ // Function to request microphone access
151
+ const requestMicAccess = async (attempt = 1) => {
152
+ try {
153
+ await navigator.mediaDevices.getUserMedia({audio: true});
154
+ // Permission granted, proceed to normal flow
155
+ this.initializeSmartPhoneDisplay();
156
+ } catch (err) {
157
+ if (err.name === 'NotAllowedError') {
158
+ console.log('Permission explicitly denied');
159
+ // Permission explicitly denied
160
+ allowMicrophoneElement.innerText = this.phrases.RC_microphonePermissionDenied[language];
161
+ // Send denied status and end study
162
+ let error = JSON.stringify(err);
163
+ this.listener.sendPermissionStatus({type: 'denied', error: error});
164
+ return;
165
+ }
166
+
167
+ // If 10 seconds passed, try again
168
+ if (attempt < 3) {
169
+ console.log('Retrying microphone access');
170
+ // Limit retries
171
+ setTimeout(() => requestMicAccess(attempt + 1), 10000);
172
+ } else {
173
+ console.log('All retries failed, treating as denied');
174
+ // After all retries failed, treat as denied
175
+ allowMicrophoneElement.innerText = this.phrases.RC_microphonePermissionDenied[language];
176
+ let error = JSON.stringify(err);
177
+ this.listener.sendPermissionStatus({type: 'error', error: error});
178
+ }
179
+ }
180
+ };
181
+
182
+ try {
183
+ await requestMicAccess();
184
+ } catch (err) {
185
+ console.error('Error requesting microphone permission:', err);
186
+ allowMicrophoneElement.innerText = this.phrases.RC_microphonePermissionDenied[language];
187
+ let error = JSON.stringify(err);
188
+ this.listener.sendPermissionStatus({type: 'error', error: error});
189
+ }
190
+ };
191
+
192
+ initializeSmartPhoneDisplay = () => {
193
+ const container = document.getElementById('listenerContainer');
194
+ const allowMicrophoneElement = document.getElementById('allowMicrophone');
195
+ const turnMessageElement = document.getElementById('turnMeToReadBelow');
196
+ let language = this.listenerParameters.lang;
197
+ const placeSmartphoneMicrophone = this.phrases.RC_placeSmartphoneMicrophone[language];
198
+ const turnMeToReadBelow = this.phrases.RC_turnMeToReadBelow[language];
199
+
200
+ allowMicrophoneElement.innerText = placeSmartphoneMicrophone;
201
+ allowMicrophoneElement.style.lineHeight = '1.2rem';
202
+ allowMicrophoneElement.style.fontSize = '14px';
203
+ turnMessageElement.innerText = turnMeToReadBelow;
204
+ turnMessageElement.style.fontSize = '14px';
205
+ turnMessageElement.style.position = 'fixed';
206
+ turnMessageElement.style.top = '20px';
207
+ turnMessageElement.style.left = '20px';
208
+ turnMessageElement.style.lineHeight = '1.2rem';
209
+ turnMessageElement.style.textAlign = 'left';
210
+
211
+ // Show the html upsidedown and adjust layout
212
+ const phrasesContainer = document.getElementById('phrases');
213
+ phrasesContainer.classList.add('phrases');
214
+
215
+ // Hide all elements except what's needed for calibration
216
+ const html = document.querySelector('html');
217
+ html.style.overflow = 'hidden';
218
+
219
+ // Adjust the display container
220
+ const display = document.getElementById('updateDisplay');
221
+ display.classList.add('updateDisplay');
222
+ display.style.position = 'absolute';
223
+ display.style.top = '70%';
224
+ display.style.left = '50%';
225
+ display.style.transform = 'translate(-50%, -50%) rotate(180deg)';
226
+ display.style.width = '100%';
227
+ display.style.textAlign = 'left';
228
+ display.style.padding = '15px';
229
+
230
+ container.style.display = 'block';
231
+
232
+ // event listener for id calibrationBeginButton
233
+ const calibrationBeginButton = document.getElementById('calibrationBeginButton');
234
+ console.log('Waiting for proceed button click');
235
+
236
+ calibrationBeginButton.addEventListener('click', async () => {
237
+ console.log('Proceed button clicked');
238
+ this.handleCalibrationButtonClick();
239
+ });
240
+ };
241
+
242
+ handleCalibrationButtonClick = async () => {
243
+ const calibrationBeginButton = document.getElementById('calibrationBeginButton');
244
+ const turnMessageElement = document.getElementById('turnMeToReadBelow');
245
+ const allowMicrophoneElement = document.getElementById('allowMicrophone');
246
+ const container = document.getElementById('listenerContainer');
247
+ const targetElement = document.getElementById('display');
248
+ let language = this.listenerParameters.lang;
249
+ // Clear unnecessary elements
250
+ calibrationBeginButton.remove();
251
+ turnMessageElement.remove();
252
+
253
+ // Create a header container for fixed elements
254
+ const headerContainer = document.createElement('div');
255
+ headerContainer.id = 'headerContainer';
256
+ headerContainer.style.position = 'fixed';
257
+ headerContainer.style.bottom = '0';
258
+ headerContainer.style.left = '0';
259
+ headerContainer.style.width = '100%';
260
+ headerContainer.style.background = 'white';
261
+ headerContainer.style.padding = '10px';
262
+ headerContainer.style.zIndex = '1000';
263
+ headerContainer.style.transform = 'rotate(180deg)';
264
+ container.appendChild(headerContainer);
265
+
266
+ // Set title based on screen width
267
+ const title = document.createElement('h1');
268
+ const titleText =
269
+ window.innerWidth >= 1366
270
+ ? this.phrases.RC_soundRecording[language]
271
+ : this.phrases.RC_soundRecordingSmallScreen[language];
272
+
273
+ // Split small screen title into lines if needed
274
+ if (window.innerWidth < 1366 && titleText.includes('\n')) {
275
+ const lines = titleText.split('\n');
276
+
277
+ // Create container for title lines
278
+ const titleContainer = document.createElement('div');
279
+ titleContainer.style.display = 'flex';
280
+ titleContainer.style.flexDirection = 'column';
281
+ titleContainer.style.alignItems = 'left';
282
+ titleContainer.style.lineHeight = '1.2';
283
+
284
+ // Add each line
285
+ lines.forEach(line => {
286
+ const lineDiv = document.createElement('p');
287
+ lineDiv.textContent = line;
288
+ lineDiv.style.width = 'fit-content';
289
+ titleContainer.appendChild(lineDiv);
290
+ });
291
+
292
+ title.appendChild(titleContainer);
293
+ } else {
294
+ title.textContent = titleText;
295
+ title.style.lineHeight = '1.2';
296
+ }
297
+
298
+ title.style.margin = '0';
299
+ title.style.whiteSpace = 'pre-line'; // Preserve line breaks
300
+ headerContainer.appendChild(title);
301
+
302
+ // Function to adjust font size to fill width
303
+ const adjustFontSize = (element, maxWidth) => {
304
+ let fontSize = 20; // Start with a reasonable minimum size
305
+ element.style.fontSize = fontSize + 'px';
306
+ // Increase font size until text fills width (minus margins)
307
+ while (element.scrollWidth < maxWidth - 40 && fontSize < 200) {
308
+ fontSize++;
309
+ element.style.fontSize = fontSize + 'px';
310
+ }
311
+
312
+ // Step back one to ensure we don't overflow
313
+ fontSize--;
314
+ element.style.fontSize = fontSize + 'px';
315
+ return fontSize;
316
+ };
317
+
318
+ // For small screen, ensure all lines use same font size
319
+ if (window.innerWidth < 1366 && titleText.includes('\n')) {
320
+ const lines = title.querySelectorAll('p');
321
+ let minFontSize = Infinity;
322
+
323
+ // First pass: find the smallest font size that fits for any line
324
+ lines.forEach(line => {
325
+ const fontSize = adjustFontSize(line, window.innerWidth);
326
+ minFontSize = Math.min(minFontSize, fontSize);
327
+ });
328
+
329
+ // Second pass: apply the smallest font size to all lines
330
+ lines.forEach(line => {
331
+ line.style.fontSize = minFontSize + 'px';
332
+ });
333
+ } else {
334
+ // For single line title, just adjust to fill width
335
+ adjustFontSize(title, window.innerWidth);
336
+ }
337
+
338
+ // Get the header height after text is added and sized
339
+ const headerHeight = headerContainer.getBoundingClientRect().height;
340
+
341
+ // Adjust the display container to start after header
342
+ const display = document.getElementById('updateDisplay');
343
+ display.classList.add('updateDisplay');
344
+ display.style.position = 'fixed';
345
+ display.style.bottom = `${headerHeight}px`; // Start after header
346
+ display.style.left = '0';
347
+ display.style.right = '0';
348
+ display.style.top = '0';
349
+ display.style.transform = 'rotate(180deg)';
350
+ display.style.overflowY = 'auto';
351
+ display.style.padding = '20px';
352
+ display.style.background = 'white';
353
+
354
+ // Position microphone instruction at the top (appears at bottom due to rotation)
355
+ allowMicrophoneElement.innerText = '';
356
+ allowMicrophoneElement.style.position = 'fixed';
357
+ allowMicrophoneElement.style.top = '20px';
358
+ allowMicrophoneElement.style.left = '50%';
359
+ allowMicrophoneElement.style.transform = 'translateX(-50%) rotate(180deg)';
360
+ allowMicrophoneElement.style.width = '90%';
361
+ allowMicrophoneElement.style.textAlign = 'left';
362
+ allowMicrophoneElement.style.zIndex = '1000';
363
+
364
+ const webAudioDeviceNames = {microphone: '', deviceID: ''};
365
+ const externalMicList = ['UMIK', 'Airpods', 'Bluetooth'];
366
+ try {
367
+ const stream = await navigator.mediaDevices.getUserMedia({audio: true});
368
+ if (stream) {
369
+ const devices = await navigator.mediaDevices.enumerateDevices();
370
+ const mics = devices.filter(device => device.kind === 'audioinput');
371
+ mics.forEach(mic => {
372
+ if (externalMicList.some(externalMic => mic.label.includes(externalMic))) {
373
+ webAudioDeviceNames.microphone = mic.label;
374
+ webAudioDeviceNames.deviceID = mic.deviceId;
375
+ }
376
+ });
377
+ if (webAudioDeviceNames.microphone === '') {
378
+ webAudioDeviceNames.microphone = mics[0].label;
379
+ webAudioDeviceNames.deviceID = mics[0].deviceId;
380
+ }
381
+ }
382
+ } catch (err) {
383
+ console.log(err);
384
+ }
385
+
386
+ let lock = null;
387
+ try {
388
+ if ('wakeLock' in navigator) {
389
+ lock = await navigator.wakeLock.request('screen');
390
+ }
391
+ } catch (err) {
392
+ console.log(err);
393
+ }
394
+
395
+ this.setupMicrophone();
396
+
397
+ // show target element
398
+ targetElement.style.display = 'block';
399
+ await this.listener.startCalibration({
400
+ microphoneFromAPI: webAudioDeviceNames.microphone,
401
+ microphoneDeviceId: webAudioDeviceNames.deviceID,
402
+ });
403
+ if (lock) {
404
+ lock.release();
405
+ }
406
+ };
407
+
408
+ setupMicrophone = async () => {
409
+ const webAudioDeviceNames = {microphone: '', deviceID: ''};
410
+ const externalMicList = ['UMIK', 'Airpods', 'Bluetooth'];
411
+ try {
412
+ const stream = await navigator.mediaDevices.getUserMedia({audio: true});
413
+ if (stream) {
414
+ const devices = await navigator.mediaDevices.enumerateDevices();
415
+ const mics = devices.filter(device => device.kind === 'audioinput');
416
+ mics.forEach(mic => {
417
+ if (externalMicList.some(externalMic => mic.label.includes(externalMic))) {
418
+ webAudioDeviceNames.microphone = mic.label;
419
+ webAudioDeviceNames.deviceID = mic.deviceId;
420
+ }
421
+ });
422
+ if (webAudioDeviceNames.microphone === '') {
423
+ webAudioDeviceNames.microphone = mics[0].label;
424
+ webAudioDeviceNames.deviceID = mics[0].deviceId;
425
+ }
426
+ }
427
+ } catch (err) {
428
+ console.log(err);
429
+ }
430
+ this.listener.setMicrophoneFromAPI(webAudioDeviceNames.microphone);
431
+ this.listener.setMicrophoneDeviceId(webAudioDeviceNames.microphone);
432
+ };
433
+
434
+ initializeDesktopMode = async () => {
435
+ await this.initializeListener();
436
+
437
+ if (await this.waitForConnection()) {
438
+ await this.setupDesktopUI();
439
+ } else {
440
+ console.error('Connection setup timed out after 30 seconds');
441
+ }
442
+ };
443
+
444
+ setupDesktopUI = async () => {
445
+ const container = document.getElementById('listenerContainer');
446
+ const recordingInProgressElement = document.getElementById('recordingInProgress');
447
+ const allowMicrophoneElement = document.getElementById('allowMicrophone');
448
+ let language = this.listenerParameters.lang;
449
+ const recordingInProgress = this.phrases.RC_soundRecording[language];
450
+ const backToExperimentWindow = this.phrases.RC_backToStudyWindow[language];
451
+ const allowMicrophone = this.phrases.RC_allowMicrophoneUse[language];
452
+
453
+ // remove the button
454
+ const calibrationBeginButton = document.getElementById('calibrationBeginButton');
455
+ calibrationBeginButton.remove();
456
+ container.style.display = 'block';
457
+
458
+ //update the display to be
459
+ const display = document.getElementById('display');
460
+ if (display) {
461
+ display.style.textAlign = 'left';
462
+ display.style.fontSize = '11px';
463
+ }
464
+
465
+ // set the text of the html elements
466
+ recordingInProgressElement.innerText = recordingInProgress;
467
+ allowMicrophoneElement.innerText = allowMicrophone;
468
+ allowMicrophoneElement.style.marginBottom = '0.5rem';
469
+ allowMicrophoneElement.style.fontSize = '17px';
470
+
471
+ recordingInProgressElement.style.whiteSpace = 'nowrap';
472
+ recordingInProgressElement.style.fontWeight = 'bold';
473
+
474
+ // fit content
475
+ recordingInProgressElement.style.width = 'fit-content';
476
+ let fontSize = 100;
477
+ recordingInProgressElement.style.fontSize = fontSize + 'px';
478
+
479
+ while (recordingInProgressElement.scrollWidth > window.innerWidth * 0.9 && fontSize > 10) {
480
+ fontSize--;
481
+ recordingInProgressElement.style.fontSize = fontSize + 'px';
482
+ }
483
+
484
+ const message = document.getElementById('message');
485
+ message.style.lineHeight = '2.5rem';
486
+ const p = document.createElement('p');
487
+ p.innerText = backToExperimentWindow;
488
+ p.style.fontSize = '17px';
489
+ message.appendChild(p);
490
+
491
+ await this.listener.startCalibration();
492
+ };
493
+ }
494
+
495
+ async function main() {
496
+ const pp = new PhonePeer();
497
+ if (window.phoneApp) {
498
+ window.phoneApp.registerSubmodule(pp);
499
+ } else {
500
+ console.log('PhoneApp not found');
501
+ }
502
+ }
503
+
504
+ main().catch(err => console.error(err));