speaker-calibration 2.2.248 → 2.2.249

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