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