speaker-calibration 2.2.256 → 2.2.257
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +71 -71
- package/.eslintrc.json +40 -40
- package/.gitignore +81 -0
- package/.prettierignore +69 -69
- package/.prettierrc +14 -14
- package/LICENSE +20 -20
- package/README.md +133 -133
- package/__mocks__/fileMock.js +1 -1
- package/__mocks__/styleMock.js +1 -1
- package/babel.config.js +3 -3
- package/coverage/clover.xml +71 -71
- package/coverage/coverage-final.json +224 -224
- package/coverage/lcov-report/PythonServerInterface.js.html +265 -265
- package/coverage/lcov-report/base.css +354 -354
- package/coverage/lcov-report/block-navigation.js +82 -82
- package/coverage/lcov-report/index.html +123 -123
- package/coverage/lcov-report/prettify.css +101 -101
- package/coverage/lcov-report/prettify.js +937 -937
- package/coverage/lcov-report/sorter.js +189 -189
- package/coverage/lcov-report/src/index.html +121 -121
- package/coverage/lcov-report/src/server/PythonServerInterface.js.html +268 -268
- package/coverage/lcov-report/src/server/index.html +123 -123
- package/coverage/lcov-report/src/tasks/audioCalibrator.js.html +499 -499
- package/coverage/lcov-report/src/tasks/audioRecorder.js.html +412 -412
- package/coverage/lcov-report/src/tasks/index.html +143 -143
- package/coverage/lcov-report/src/tasks/volume/index.html +123 -123
- package/coverage/lcov-report/src/tasks/volume/volume.js.html +409 -409
- package/coverage/lcov-report/src/utils.js.html +172 -172
- package/coverage/lcov.info +91 -91
- package/dist/example/NoSleep.min.js +1 -1
- package/dist/example/fetch-languages-sheets.js +77 -77
- package/dist/example/i18n.js +29038 -29654
- package/dist/example/index.html +47 -47
- package/dist/example/listener.html +81 -81
- package/dist/example/server.js +51 -51
- package/dist/example/speaker.html +145 -145
- package/dist/example/speakerUI.js +273 -273
- package/dist/example/styles.css +152 -152
- package/dist/listener.js +3 -3
- package/dist/main.js +9 -9
- package/dist/mlsGen.js +6814 -6814
- package/dist/mlsGen.wasm +0 -0
- package/dist/package-lock.json +1018 -1018
- package/dist/package.json +18 -18
- package/dist/phonePeer.js +3 -3
- package/doc/AudioCalibrator.html +417 -417
- package/doc/AudioPeer.html +251 -251
- package/doc/AudioRecorder.html +195 -195
- package/doc/ImpulseResponse.html +215 -215
- package/doc/Listener.html +308 -308
- package/doc/MlsGenInterface.html +226 -226
- package/doc/MyEventEmitter.html +274 -274
- package/doc/PythonServerAPI.html +109 -109
- package/doc/Speaker.html +276 -276
- package/doc/Takes%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +128 -128
- package/doc/Takes%20the%20url%20of%20the%20current%20site%0Aand%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +138 -138
- package/doc/Takes%20the%20url%20of%20the%20current%20site%20and%20a%20target%20element%20where%20html%20elements%20will%20be%20appended..html +137 -137
- package/doc/Volume.html +88 -88
- package/doc/audioCalibrator.js.html +179 -179
- package/doc/audioPeer.js.html +175 -175
- package/doc/audioRecorder.js.html +163 -163
- package/doc/creates%20a%20new%20AudioRecorder%20instance.%20%0ASets%20up%20the%20audio%20context%20and%20file%20reader..html +114 -114
- package/doc/fonts/OpenSans-Bold-webfont.svg +1829 -1829
- package/doc/fonts/OpenSans-BoldItalic-webfont.svg +1829 -1829
- package/doc/fonts/OpenSans-Italic-webfont.svg +1829 -1829
- package/doc/fonts/OpenSans-Light-webfont.svg +1830 -1830
- package/doc/fonts/OpenSans-LightItalic-webfont.svg +1834 -1834
- package/doc/fonts/OpenSans-Regular-webfont.svg +1830 -1830
- package/doc/global.html +308 -308
- package/doc/index.html +58 -58
- package/doc/listener.js.html +170 -170
- package/doc/mlsGen_mlsGenInterface.js.html +117 -117
- package/doc/myEventEmitter.js.html +124 -124
- package/doc/peer-connection_audioPeer.js.html +188 -188
- package/doc/peer-connection_listener.js.html +311 -311
- package/doc/peer-connection_speaker.js.html +381 -381
- package/doc/scripts/linenumber.js +25 -25
- package/doc/scripts/prettify/Apache-License-2.0.txt +202 -202
- package/doc/scripts/prettify/lang-css.js +24 -24
- package/doc/scripts/prettify/prettify.js +640 -640
- package/doc/server_PythonServerAPI.js.html +160 -160
- package/doc/speaker.js.html +248 -248
- package/doc/styles/jsdoc-default.css +371 -371
- package/doc/styles/prettify-jsdoc.css +111 -111
- package/doc/styles/prettify-tomorrow.css +163 -163
- package/doc/tasks_audioCalibrator.js.html +207 -207
- package/doc/tasks_audioRecorder.js.html +190 -190
- package/doc/tasks_impulse-response_impulseResponse.js.html +442 -442
- package/doc/tasks_impulse-response_mlsGen_mlsGenInterface.js.html +175 -175
- package/doc/tasks_volume_volume.js.html +185 -185
- package/doc/utils.js.html +105 -105
- package/jest.config.js +173 -173
- package/netlify.toml +26 -26
- package/package.json +78 -78
- package/src/config/firebase.js +26 -26
- package/src/index.html +21 -21
- package/src/listener-app/PhonePeer.js +499 -499
- package/src/listener-app/listener.js +380 -380
- package/src/main.js +22 -22
- package/src/myEventEmitter.js +83 -83
- package/src/peer-connection/audioPeer.js +100 -100
- package/src/peer-connection/listener.js +298 -298
- package/src/peer-connection/peerErrors.js +25 -25
- package/src/peer-connection/speaker.js +983 -983
- package/src/powerCheck.js +110 -110
- package/src/server/PythonServerAPI.js +1001 -1001
- package/src/tasks/combination/combination.js +3927 -3918
- package/src/tasks/combination/mlsGen/mlsGen.cpp +98 -98
- package/src/tasks/combination/mlsGen/mlsGen.hpp +303 -303
- package/src/tasks/combination/mlsGen/mlsGenInterface.js +131 -131
- package/src/tasks/combination/mlsGen/mlsGenTest.cpp +180 -180
- package/src/tasks/impulse-response/impulseResponse.js +610 -610
- package/src/tasks/impulse-response/mlsGen/mlsGen.cpp +98 -98
- package/src/tasks/impulse-response/mlsGen/mlsGen.hpp +303 -303
- package/src/tasks/impulse-response/mlsGen/mlsGenInterface.js +131 -131
- package/src/tasks/impulse-response/mlsGen/mlsGenTest.cpp +180 -180
- package/src/tasks/volume/volume.cpp +2 -2
- package/src/tasks/volume/volume.hpp +22 -22
- package/src/tasks/volume/volume.js +279 -279
- package/src/utils.js +215 -205
- package/webpack.config.js +65 -65
- package/.github/workflows/update-phrases.yml +0 -37
- package/makefile +0 -74
|
@@ -1,499 +1,499 @@
|
|
|
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
|
-
const webAudioDeviceNames = {microphone: '', deviceID: ''};
|
|
361
|
-
const externalMicList = ['UMIK', 'Airpods', 'Bluetooth'];
|
|
362
|
-
try {
|
|
363
|
-
const stream = await navigator.mediaDevices.getUserMedia({audio: true});
|
|
364
|
-
if (stream) {
|
|
365
|
-
const devices = await navigator.mediaDevices.enumerateDevices();
|
|
366
|
-
const mics = devices.filter(device => device.kind === 'audioinput');
|
|
367
|
-
mics.forEach(mic => {
|
|
368
|
-
if (externalMicList.some(externalMic => mic.label.includes(externalMic))) {
|
|
369
|
-
webAudioDeviceNames.microphone = mic.label;
|
|
370
|
-
webAudioDeviceNames.deviceID = mic.deviceId;
|
|
371
|
-
}
|
|
372
|
-
});
|
|
373
|
-
if (webAudioDeviceNames.microphone === '') {
|
|
374
|
-
webAudioDeviceNames.microphone = mics[0].label;
|
|
375
|
-
webAudioDeviceNames.deviceID = mics[0].deviceId;
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
} catch (err) {
|
|
379
|
-
console.log(err);
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
let lock = null;
|
|
383
|
-
try {
|
|
384
|
-
if ('wakeLock' in navigator) {
|
|
385
|
-
lock = await navigator.wakeLock.request('screen');
|
|
386
|
-
}
|
|
387
|
-
} catch (err) {
|
|
388
|
-
console.log(err);
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
this.setupMicrophone();
|
|
392
|
-
|
|
393
|
-
// show target element
|
|
394
|
-
targetElement.style.display = 'block';
|
|
395
|
-
await this.listener.startCalibration({
|
|
396
|
-
microphoneFromAPI: webAudioDeviceNames.microphone,
|
|
397
|
-
microphoneDeviceId: webAudioDeviceNames.deviceID,
|
|
398
|
-
});
|
|
399
|
-
if (lock) {
|
|
400
|
-
lock.release();
|
|
401
|
-
}
|
|
402
|
-
};
|
|
403
|
-
|
|
404
|
-
setupMicrophone = async () => {
|
|
405
|
-
const webAudioDeviceNames = {microphone: '', deviceID: ''};
|
|
406
|
-
const externalMicList = ['UMIK', 'Airpods', 'Bluetooth'];
|
|
407
|
-
try {
|
|
408
|
-
const stream = await navigator.mediaDevices.getUserMedia({audio: true});
|
|
409
|
-
if (stream) {
|
|
410
|
-
const devices = await navigator.mediaDevices.enumerateDevices();
|
|
411
|
-
const mics = devices.filter(device => device.kind === 'audioinput');
|
|
412
|
-
mics.forEach(mic => {
|
|
413
|
-
if (externalMicList.some(externalMic => mic.label.includes(externalMic))) {
|
|
414
|
-
webAudioDeviceNames.microphone = mic.label;
|
|
415
|
-
webAudioDeviceNames.deviceID = mic.deviceId;
|
|
416
|
-
}
|
|
417
|
-
});
|
|
418
|
-
if (webAudioDeviceNames.microphone === '') {
|
|
419
|
-
webAudioDeviceNames.microphone = mics[0].label;
|
|
420
|
-
webAudioDeviceNames.deviceID = mics[0].deviceId;
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
} catch (err) {
|
|
424
|
-
console.log(err);
|
|
425
|
-
}
|
|
426
|
-
this.listener.setMicrophoneFromAPI(webAudioDeviceNames.microphone);
|
|
427
|
-
this.listener.setMicrophoneDeviceId(webAudioDeviceNames.microphone);
|
|
428
|
-
};
|
|
429
|
-
|
|
430
|
-
initializeDesktopMode = async () => {
|
|
431
|
-
await this.initializeListener();
|
|
432
|
-
|
|
433
|
-
if (await this.waitForConnection()) {
|
|
434
|
-
await this.setupDesktopUI();
|
|
435
|
-
} else {
|
|
436
|
-
console.error('Connection setup timed out after 30 seconds');
|
|
437
|
-
}
|
|
438
|
-
};
|
|
439
|
-
|
|
440
|
-
setupDesktopUI = async () => {
|
|
441
|
-
const container = document.getElementById('listenerContainer');
|
|
442
|
-
const recordingInProgressElement = document.getElementById('recordingInProgress');
|
|
443
|
-
const allowMicrophoneElement = document.getElementById('allowMicrophone');
|
|
444
|
-
const recordingInProgress = this.phrases.RC_soundRecording['en-US'];
|
|
445
|
-
const backToExperimentWindow = this.phrases.RC_backToExperimentWindow['en-US'];
|
|
446
|
-
const allowMicrophone = this.phrases.RC_allowMicrophoneUse['en-US'];
|
|
447
|
-
|
|
448
|
-
// remove the button
|
|
449
|
-
const calibrationBeginButton = document.getElementById('calibrationBeginButton');
|
|
450
|
-
calibrationBeginButton.remove();
|
|
451
|
-
container.style.display = 'block';
|
|
452
|
-
|
|
453
|
-
//update the display to be
|
|
454
|
-
const display = document.getElementById('display');
|
|
455
|
-
if (display) {
|
|
456
|
-
display.style.textAlign = 'left';
|
|
457
|
-
display.style.fontSize = '11px';
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
// set the text of the html elements
|
|
461
|
-
recordingInProgressElement.innerText = recordingInProgress;
|
|
462
|
-
allowMicrophoneElement.innerText = allowMicrophone;
|
|
463
|
-
allowMicrophoneElement.style.marginBottom = '0.5rem';
|
|
464
|
-
allowMicrophoneElement.style.fontSize = '17px';
|
|
465
|
-
|
|
466
|
-
recordingInProgressElement.style.whiteSpace = 'nowrap';
|
|
467
|
-
recordingInProgressElement.style.fontWeight = 'bold';
|
|
468
|
-
|
|
469
|
-
// fit content
|
|
470
|
-
recordingInProgressElement.style.width = 'fit-content';
|
|
471
|
-
let fontSize = 100;
|
|
472
|
-
recordingInProgressElement.style.fontSize = fontSize + 'px';
|
|
473
|
-
|
|
474
|
-
while (recordingInProgressElement.scrollWidth > window.innerWidth * 0.9 && fontSize > 10) {
|
|
475
|
-
fontSize--;
|
|
476
|
-
recordingInProgressElement.style.fontSize = fontSize + 'px';
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
const message = document.getElementById('message');
|
|
480
|
-
message.style.lineHeight = '2.5rem';
|
|
481
|
-
const p = document.createElement('p');
|
|
482
|
-
p.innerText = backToExperimentWindow;
|
|
483
|
-
p.style.fontSize = '17px';
|
|
484
|
-
message.appendChild(p);
|
|
485
|
-
|
|
486
|
-
await this.listener.startCalibration();
|
|
487
|
-
};
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
async function main() {
|
|
491
|
-
const pp = new PhonePeer();
|
|
492
|
-
if (window.phoneApp) {
|
|
493
|
-
window.phoneApp.registerSubmodule(pp);
|
|
494
|
-
} else {
|
|
495
|
-
console.log('PhoneApp not found');
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
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
|
+
const webAudioDeviceNames = {microphone: '', deviceID: ''};
|
|
361
|
+
const externalMicList = ['UMIK', 'Airpods', 'Bluetooth'];
|
|
362
|
+
try {
|
|
363
|
+
const stream = await navigator.mediaDevices.getUserMedia({audio: true});
|
|
364
|
+
if (stream) {
|
|
365
|
+
const devices = await navigator.mediaDevices.enumerateDevices();
|
|
366
|
+
const mics = devices.filter(device => device.kind === 'audioinput');
|
|
367
|
+
mics.forEach(mic => {
|
|
368
|
+
if (externalMicList.some(externalMic => mic.label.includes(externalMic))) {
|
|
369
|
+
webAudioDeviceNames.microphone = mic.label;
|
|
370
|
+
webAudioDeviceNames.deviceID = mic.deviceId;
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
if (webAudioDeviceNames.microphone === '') {
|
|
374
|
+
webAudioDeviceNames.microphone = mics[0].label;
|
|
375
|
+
webAudioDeviceNames.deviceID = mics[0].deviceId;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
} catch (err) {
|
|
379
|
+
console.log(err);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
let lock = null;
|
|
383
|
+
try {
|
|
384
|
+
if ('wakeLock' in navigator) {
|
|
385
|
+
lock = await navigator.wakeLock.request('screen');
|
|
386
|
+
}
|
|
387
|
+
} catch (err) {
|
|
388
|
+
console.log(err);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
this.setupMicrophone();
|
|
392
|
+
|
|
393
|
+
// show target element
|
|
394
|
+
targetElement.style.display = 'block';
|
|
395
|
+
await this.listener.startCalibration({
|
|
396
|
+
microphoneFromAPI: webAudioDeviceNames.microphone,
|
|
397
|
+
microphoneDeviceId: webAudioDeviceNames.deviceID,
|
|
398
|
+
});
|
|
399
|
+
if (lock) {
|
|
400
|
+
lock.release();
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
setupMicrophone = async () => {
|
|
405
|
+
const webAudioDeviceNames = {microphone: '', deviceID: ''};
|
|
406
|
+
const externalMicList = ['UMIK', 'Airpods', 'Bluetooth'];
|
|
407
|
+
try {
|
|
408
|
+
const stream = await navigator.mediaDevices.getUserMedia({audio: true});
|
|
409
|
+
if (stream) {
|
|
410
|
+
const devices = await navigator.mediaDevices.enumerateDevices();
|
|
411
|
+
const mics = devices.filter(device => device.kind === 'audioinput');
|
|
412
|
+
mics.forEach(mic => {
|
|
413
|
+
if (externalMicList.some(externalMic => mic.label.includes(externalMic))) {
|
|
414
|
+
webAudioDeviceNames.microphone = mic.label;
|
|
415
|
+
webAudioDeviceNames.deviceID = mic.deviceId;
|
|
416
|
+
}
|
|
417
|
+
});
|
|
418
|
+
if (webAudioDeviceNames.microphone === '') {
|
|
419
|
+
webAudioDeviceNames.microphone = mics[0].label;
|
|
420
|
+
webAudioDeviceNames.deviceID = mics[0].deviceId;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
} catch (err) {
|
|
424
|
+
console.log(err);
|
|
425
|
+
}
|
|
426
|
+
this.listener.setMicrophoneFromAPI(webAudioDeviceNames.microphone);
|
|
427
|
+
this.listener.setMicrophoneDeviceId(webAudioDeviceNames.microphone);
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
initializeDesktopMode = async () => {
|
|
431
|
+
await this.initializeListener();
|
|
432
|
+
|
|
433
|
+
if (await this.waitForConnection()) {
|
|
434
|
+
await this.setupDesktopUI();
|
|
435
|
+
} else {
|
|
436
|
+
console.error('Connection setup timed out after 30 seconds');
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
|
|
440
|
+
setupDesktopUI = async () => {
|
|
441
|
+
const container = document.getElementById('listenerContainer');
|
|
442
|
+
const recordingInProgressElement = document.getElementById('recordingInProgress');
|
|
443
|
+
const allowMicrophoneElement = document.getElementById('allowMicrophone');
|
|
444
|
+
const recordingInProgress = this.phrases.RC_soundRecording['en-US'];
|
|
445
|
+
const backToExperimentWindow = this.phrases.RC_backToExperimentWindow['en-US'];
|
|
446
|
+
const allowMicrophone = this.phrases.RC_allowMicrophoneUse['en-US'];
|
|
447
|
+
|
|
448
|
+
// remove the button
|
|
449
|
+
const calibrationBeginButton = document.getElementById('calibrationBeginButton');
|
|
450
|
+
calibrationBeginButton.remove();
|
|
451
|
+
container.style.display = 'block';
|
|
452
|
+
|
|
453
|
+
//update the display to be
|
|
454
|
+
const display = document.getElementById('display');
|
|
455
|
+
if (display) {
|
|
456
|
+
display.style.textAlign = 'left';
|
|
457
|
+
display.style.fontSize = '11px';
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// set the text of the html elements
|
|
461
|
+
recordingInProgressElement.innerText = recordingInProgress;
|
|
462
|
+
allowMicrophoneElement.innerText = allowMicrophone;
|
|
463
|
+
allowMicrophoneElement.style.marginBottom = '0.5rem';
|
|
464
|
+
allowMicrophoneElement.style.fontSize = '17px';
|
|
465
|
+
|
|
466
|
+
recordingInProgressElement.style.whiteSpace = 'nowrap';
|
|
467
|
+
recordingInProgressElement.style.fontWeight = 'bold';
|
|
468
|
+
|
|
469
|
+
// fit content
|
|
470
|
+
recordingInProgressElement.style.width = 'fit-content';
|
|
471
|
+
let fontSize = 100;
|
|
472
|
+
recordingInProgressElement.style.fontSize = fontSize + 'px';
|
|
473
|
+
|
|
474
|
+
while (recordingInProgressElement.scrollWidth > window.innerWidth * 0.9 && fontSize > 10) {
|
|
475
|
+
fontSize--;
|
|
476
|
+
recordingInProgressElement.style.fontSize = fontSize + 'px';
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
const message = document.getElementById('message');
|
|
480
|
+
message.style.lineHeight = '2.5rem';
|
|
481
|
+
const p = document.createElement('p');
|
|
482
|
+
p.innerText = backToExperimentWindow;
|
|
483
|
+
p.style.fontSize = '17px';
|
|
484
|
+
message.appendChild(p);
|
|
485
|
+
|
|
486
|
+
await this.listener.startCalibration();
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
async function main() {
|
|
491
|
+
const pp = new PhonePeer();
|
|
492
|
+
if (window.phoneApp) {
|
|
493
|
+
window.phoneApp.registerSubmodule(pp);
|
|
494
|
+
} else {
|
|
495
|
+
console.log('PhoneApp not found');
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
main().catch(err => console.error(err));
|