speaker-calibration 2.2.173 → 2.2.175

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "speaker-calibration",
3
- "version": "2.2.173",
3
+ "version": "2.2.175",
4
4
  "description": "Speaker calibration library for auditory testing",
5
5
  "main": "dist/main.js",
6
6
  "directories": {
@@ -26,13 +26,15 @@ class Listener extends AudioPeer {
26
26
 
27
27
  const urlParameters = this.parseURLSearchParams();
28
28
  this.calibrateSoundHz =
29
- urlParameters.calibrateSoundHz !== null && urlParameters.calibrateSoundHz !== undefined
30
- ? urlParameters.calibrateSoundHz
29
+ // previous calibrateSoundHz
30
+ urlParameters.hz !== null && urlParameters.hz !== undefined
31
+ ? urlParameters.hz
31
32
  : 48000;
32
33
  this.calibrateSoundSamplingDesiredBits =
33
- urlParameters.calibrateSoundSamplingDesiredBits !== null &&
34
- urlParameters.calibrateSoundSamplingDesiredBits !== undefined
35
- ? urlParameters.calibrateSoundSamplingDesiredBits
34
+ // previous calibrateSoundSamplingDesiredBits
35
+ urlParameters.bits !== null &&
36
+ urlParameters.bits !== undefined
37
+ ? urlParameters.bits
36
38
  : 24;
37
39
  this.speakerPeerId = urlParameters.speakerPeerId;
38
40
 
@@ -44,15 +44,84 @@ class Speaker extends AudioPeer {
44
44
  this.buttonsContainer = params?.buttonsContainer ?? document.createElement('div');
45
45
 
46
46
  /* Set up callbacks that handle any events related to our peer object. */
47
+ }
48
+
49
+ uri = '';
50
+ qrImage;
51
+ shortURL;
52
+
53
+
54
+ initPeer = async () => {
55
+ const id = await this.generateTimeBasedPeerID();
56
+ this.peer = new Peer(id, {
57
+ secure: true,
58
+ host: 'easyeyes-peer-server.herokuapp.com',
59
+ port: 443,
60
+ config: {
61
+ iceServers: [
62
+ {
63
+ urls: "stun:stun.relay.metered.ca:80",
64
+ },
65
+ {
66
+ urls: "turn:global.relay.metered.ca:80",
67
+ username: "de884cfc34189cdf1a5dd616",
68
+ credential: "IcOpouU9/TYBmpHU",
69
+ },
70
+ {
71
+ urls: "turn:global.relay.metered.ca:80?transport=tcp",
72
+ username: "de884cfc34189cdf1a5dd616",
73
+ credential: "IcOpouU9/TYBmpHU",
74
+ },
75
+ {
76
+ urls: "turn:global.relay.metered.ca:443",
77
+ username: "de884cfc34189cdf1a5dd616",
78
+ credential: "IcOpouU9/TYBmpHU",
79
+ },
80
+ {
81
+ urls: "turns:global.relay.metered.ca:443?transport=tcp",
82
+ username: "de884cfc34189cdf1a5dd616",
83
+ credential: "IcOpouU9/TYBmpHU",
84
+ },
85
+ ],
86
+ },
87
+ });
47
88
  this.peer.on('open', this.#onPeerOpen);
48
89
  this.peer.on('connection', this.#onPeerConnection);
49
90
  this.peer.on('close', this.onPeerClose);
50
91
  this.peer.on('disconnected', this.#onPeerDisconnected);
51
92
  this.peer.on('error', this.#onPeerError);
52
93
  }
94
+ generateTimeBasedPeerID = async () => {
95
+ const now = new Date().getTime();
96
+ const randomBuffer = new Uint8Array(10);
97
+ crypto.getRandomValues(randomBuffer);
98
+ const randomPart = Array.from(randomBuffer)
99
+ .map((b) => b.toString(36))
100
+ .join("");
101
+ const toHash = `${now}-${randomPart}`;
102
+ const encoder = new TextEncoder();
103
+ const data = encoder.encode(toHash);
104
+ const hash = await crypto.subtle.digest("SHA-256", data);
105
+ const hashArray = Array.from(new Uint8Array(hash)); // Convert buffer to byte array
106
+ const hashString = hashArray
107
+ .map((b) => b.toString(16).padStart(2, "0"))
108
+ .join("");
109
+ const shortHash = hashString.substring(0, 12); // Use more of the hash for a longer ID
110
+ // return shortHash; // Consider converting this to Base62
111
+ return this.encodeBase62(parseInt(shortHash, 16));
112
+ };
113
+
114
+ encodeBase62 = (num) => {
115
+ const base = 36;
116
+ const characters = "0123456789abcdefghijklmnopqrstuvwxyz";
117
+ let result = "";
118
+ while (num > 0) {
119
+ result = characters[num % base] + result;
120
+ num = Math.floor(num / base);
121
+ }
122
+ return result;
123
+ };
53
124
 
54
- uri = '';
55
- qrImage;
56
125
  /**
57
126
  * Async factory method that creates the Speaker object, and returns a promise that resolves to the result of the calibration.
58
127
  *
@@ -66,7 +135,7 @@ class Speaker extends AudioPeer {
66
135
  static startCalibration = async (params, CalibratorInstance, timeOut = 180000) => {
67
136
  window.speaker = new Speaker(params, CalibratorInstance);
68
137
  const {speaker} = window;
69
-
138
+ await speaker.initPeer();
70
139
  // wrap the calibration process in a promise so we can await it
71
140
  return new Promise((resolve, reject) => {
72
141
  // when a call is received
@@ -153,7 +222,7 @@ class Speaker extends AudioPeer {
153
222
  static testIIR = async (params, CalibratorInstance, IIR, timeOut = 180000) => {
154
223
  window.speaker = new Speaker(params, CalibratorInstance);
155
224
  const {speaker} = window;
156
-
225
+ speaker.initPeer();
157
226
  // wrap the calibration process in a promise so we can await it
158
227
  return new Promise((resolve, reject) => {
159
228
  // when a call is received
@@ -205,9 +274,9 @@ class Speaker extends AudioPeer {
205
274
  // Get query string, the URL parameters to specify a Listener
206
275
  const queryStringParameters = {
207
276
  speakerPeerId: this.peer.id,
208
- isSmartPhone: this.isSmartPhone,
209
- calibrateSoundHz: this.calibrateSoundHz,
210
- calibrateSoundSamplingDesiredBits: this.calibrateSoundSamplingDesiredBits,
277
+ sp: this.isSmartPhone,
278
+ hz: this.calibrateSoundHz,
279
+ bits: this.calibrateSoundSamplingDesiredBits,
211
280
  lang: this.language,
212
281
  };
213
282
  const queryString = this.queryStringFromObject(queryStringParameters);
@@ -222,13 +291,42 @@ class Speaker extends AudioPeer {
222
291
  const explanation = document.createElement("p");
223
292
  explanation.id = "skipQRExplanation";
224
293
  explanation.style = `
225
- overflow-wrap: break-word; /* Ensure long URLs or text break properly */
226
294
  user-select: text;
227
295
  `;
228
296
 
229
- explanation.innerHTML = phrases.RC_skipQR_ExplanationWithoutPreferNot[this.language]
230
- .replace("xxx", `<b style="user-select: text">${this.uri}</b>`)
231
- .replace("XXX", `<b style="user-select: text">${this.uri}</b>`);
297
+ // Define the URL and options for the request
298
+ const url = 'https://api.short.io/links';
299
+ const options = {
300
+ method: 'POST',
301
+ headers: {
302
+ 'Accept': 'application/json',
303
+ 'Content-Type': 'application/json',
304
+ 'Authorization': 'sk_7Y58e7P68nyqOQ0J'
305
+ },
306
+ body: JSON.stringify({
307
+ domain: 'listeners.link', // Ensure this domain is valid for your account
308
+ originalURL: this.uri
309
+ })
310
+ };
311
+
312
+ // Make the request using fetch
313
+ fetch(url, options)
314
+ .then(response => {
315
+ if (!response.ok) {
316
+ throw new Error(`HTTP error! Status: ${response.status}`);
317
+ }
318
+ return response.json(); // Parse the JSON response
319
+ })
320
+ .then(data => {
321
+ explanation.innerHTML = phrases.RC_skipQR_ExplanationWithoutPreferNot[this.language]
322
+ .replace("xxx", `<b style="user-select: text">${data.shortURL}</b>`)
323
+ .replace("XXX", `<b style="user-select: text">${data.shortURL}</b>`);
324
+ })
325
+ .catch(error => {
326
+ console.error('Error:', error.message); // Handle errors
327
+ });
328
+
329
+
232
330
 
233
331
  const qrImage = new Image(400, 400);
234
332
  qrImage.setAttribute('id', 'compatibilityCheckQRImage');