@micdrop/client 2.2.6 → 2.2.8
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/dist/index.d.mts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +78 -17
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +78 -17
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -8
- package/LICENSE +0 -9
package/dist/index.d.mts
CHANGED
|
@@ -312,6 +312,7 @@ interface MicdropEvents {
|
|
|
312
312
|
interface MicdropReconnectOptions {
|
|
313
313
|
maxAttempts?: number;
|
|
314
314
|
delayMs?: number;
|
|
315
|
+
connectionTimeout?: number;
|
|
315
316
|
}
|
|
316
317
|
interface MicdropOptions {
|
|
317
318
|
url?: string;
|
|
@@ -357,6 +358,7 @@ declare class MicdropClient extends EventEmitter<MicdropEvents> implements Micdr
|
|
|
357
358
|
private _isReconnecting;
|
|
358
359
|
private reconnectAttempt;
|
|
359
360
|
private reconnectTimer?;
|
|
361
|
+
private connectionTimer?;
|
|
360
362
|
constructor(options?: MicdropOptions);
|
|
361
363
|
get vad(): VAD | undefined;
|
|
362
364
|
get isStarted(): boolean;
|
package/dist/index.d.ts
CHANGED
|
@@ -312,6 +312,7 @@ interface MicdropEvents {
|
|
|
312
312
|
interface MicdropReconnectOptions {
|
|
313
313
|
maxAttempts?: number;
|
|
314
314
|
delayMs?: number;
|
|
315
|
+
connectionTimeout?: number;
|
|
315
316
|
}
|
|
316
317
|
interface MicdropOptions {
|
|
317
318
|
url?: string;
|
|
@@ -357,6 +358,7 @@ declare class MicdropClient extends EventEmitter<MicdropEvents> implements Micdr
|
|
|
357
358
|
private _isReconnecting;
|
|
358
359
|
private reconnectAttempt;
|
|
359
360
|
private reconnectTimer?;
|
|
361
|
+
private connectionTimer?;
|
|
360
362
|
constructor(options?: MicdropOptions);
|
|
361
363
|
get vad(): VAD | undefined;
|
|
362
364
|
get isStarted(): boolean;
|
package/dist/index.js
CHANGED
|
@@ -221,26 +221,39 @@ var AudioStream = class extends import_eventemitter32.EventEmitter {
|
|
|
221
221
|
// src/audio/stream/Pcm16AudioStream.ts
|
|
222
222
|
var SAMPLE_RATE = 16e3;
|
|
223
223
|
var CHANNELS = 1;
|
|
224
|
+
var PREBUFFER_DURATION = 100;
|
|
225
|
+
var QUIET_FLUSH_DELAY = 600;
|
|
224
226
|
var Pcm16AudioStream = class extends AudioStream {
|
|
225
227
|
constructor() {
|
|
226
228
|
super();
|
|
227
|
-
this.
|
|
229
|
+
this.blobQueue = [];
|
|
230
|
+
this.processing = false;
|
|
228
231
|
this.sourceNodes = [];
|
|
229
232
|
this.nextStartTime = 0;
|
|
233
|
+
this.prebuffering = true;
|
|
234
|
+
this.prebuffer = [];
|
|
235
|
+
this.prebufferDuration = 0;
|
|
230
236
|
}
|
|
231
237
|
start() {
|
|
232
238
|
return this.outputNode;
|
|
233
239
|
}
|
|
234
240
|
async playAudio(blob) {
|
|
241
|
+
this.blobQueue.push(blob);
|
|
242
|
+
if (this.processing) return;
|
|
243
|
+
this.processing = true;
|
|
235
244
|
try {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
245
|
+
while (this.blobQueue.length > 0) {
|
|
246
|
+
const next = this.blobQueue.shift();
|
|
247
|
+
try {
|
|
248
|
+
const arrayBuffer = await next.arrayBuffer();
|
|
249
|
+
const audioBuffer = this.createAudioBuffer(arrayBuffer);
|
|
250
|
+
if (audioBuffer) this.handleBuffer(audioBuffer);
|
|
251
|
+
} catch (error) {
|
|
252
|
+
console.error("Failed to play audio chunk:", error);
|
|
253
|
+
}
|
|
241
254
|
}
|
|
242
|
-
}
|
|
243
|
-
|
|
255
|
+
} finally {
|
|
256
|
+
this.processing = false;
|
|
244
257
|
}
|
|
245
258
|
}
|
|
246
259
|
stopAudio() {
|
|
@@ -252,10 +265,47 @@ var Pcm16AudioStream = class extends AudioStream {
|
|
|
252
265
|
}
|
|
253
266
|
}
|
|
254
267
|
this.sourceNodes = [];
|
|
255
|
-
this.
|
|
268
|
+
this.blobQueue = [];
|
|
256
269
|
this.nextStartTime = 0;
|
|
270
|
+
this.resetPrebuffer();
|
|
257
271
|
this.setIsPlaying(false);
|
|
258
272
|
}
|
|
273
|
+
// Accumulate audio until enough is buffered, then start playing. Once
|
|
274
|
+
// playing, schedule incoming buffers directly.
|
|
275
|
+
handleBuffer(audioBuffer) {
|
|
276
|
+
if (!this.prebuffering) {
|
|
277
|
+
this.scheduleBuffer(audioBuffer);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
this.prebuffer.push(audioBuffer);
|
|
281
|
+
this.prebufferDuration += audioBuffer.duration * 1e3;
|
|
282
|
+
if (this.prebufferDuration >= PREBUFFER_DURATION) {
|
|
283
|
+
this.flushPrebuffer();
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
if (this.quietTimer !== void 0) clearTimeout(this.quietTimer);
|
|
287
|
+
this.quietTimer = setTimeout(() => this.flushPrebuffer(), QUIET_FLUSH_DELAY);
|
|
288
|
+
}
|
|
289
|
+
flushPrebuffer() {
|
|
290
|
+
if (this.quietTimer !== void 0) {
|
|
291
|
+
clearTimeout(this.quietTimer);
|
|
292
|
+
this.quietTimer = void 0;
|
|
293
|
+
}
|
|
294
|
+
this.prebuffering = false;
|
|
295
|
+
const buffers = this.prebuffer;
|
|
296
|
+
this.prebuffer = [];
|
|
297
|
+
this.prebufferDuration = 0;
|
|
298
|
+
for (const audioBuffer of buffers) this.scheduleBuffer(audioBuffer);
|
|
299
|
+
}
|
|
300
|
+
resetPrebuffer() {
|
|
301
|
+
if (this.quietTimer !== void 0) {
|
|
302
|
+
clearTimeout(this.quietTimer);
|
|
303
|
+
this.quietTimer = void 0;
|
|
304
|
+
}
|
|
305
|
+
this.prebuffering = true;
|
|
306
|
+
this.prebuffer = [];
|
|
307
|
+
this.prebufferDuration = 0;
|
|
308
|
+
}
|
|
259
309
|
createAudioBuffer(arrayBuffer) {
|
|
260
310
|
try {
|
|
261
311
|
const int16Data = new Int16Array(arrayBuffer);
|
|
@@ -278,12 +328,6 @@ var Pcm16AudioStream = class extends AudioStream {
|
|
|
278
328
|
return null;
|
|
279
329
|
}
|
|
280
330
|
}
|
|
281
|
-
processQueue() {
|
|
282
|
-
while (this.audioQueue.length > 0) {
|
|
283
|
-
const audioBuffer = this.audioQueue.shift();
|
|
284
|
-
this.scheduleBuffer(audioBuffer);
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
331
|
scheduleBuffer(audioBuffer) {
|
|
288
332
|
const sourceNode = audioContext.createBufferSource();
|
|
289
333
|
sourceNode.buffer = audioBuffer;
|
|
@@ -294,8 +338,9 @@ var Pcm16AudioStream = class extends AudioStream {
|
|
|
294
338
|
sourceNode.onended = () => {
|
|
295
339
|
const index = this.sourceNodes.indexOf(sourceNode);
|
|
296
340
|
if (index !== -1) this.sourceNodes.splice(index, 1);
|
|
297
|
-
if (this.sourceNodes.length === 0 && this.
|
|
341
|
+
if (this.sourceNodes.length === 0 && this.blobQueue.length === 0) {
|
|
298
342
|
this.nextStartTime = 0;
|
|
343
|
+
this.resetPrebuffer();
|
|
299
344
|
this.setIsPlaying(false);
|
|
300
345
|
}
|
|
301
346
|
};
|
|
@@ -1028,7 +1073,8 @@ function isRecoverableError(error) {
|
|
|
1028
1073
|
// src/client/MicdropClient.ts
|
|
1029
1074
|
var DEFAULT_RECONNECT_OPTIONS = {
|
|
1030
1075
|
maxAttempts: Infinity,
|
|
1031
|
-
delayMs: 1e3
|
|
1076
|
+
delayMs: 1e3,
|
|
1077
|
+
connectionTimeout: 5e3
|
|
1032
1078
|
};
|
|
1033
1079
|
var MicdropClient = class extends import_eventemitter36.EventEmitter {
|
|
1034
1080
|
constructor(options = {}) {
|
|
@@ -1201,6 +1247,10 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
|
|
|
1201
1247
|
};
|
|
1202
1248
|
this.onWSOpen = () => {
|
|
1203
1249
|
this.log("WebSocket opened");
|
|
1250
|
+
if (this.connectionTimer) {
|
|
1251
|
+
window.clearTimeout(this.connectionTimer);
|
|
1252
|
+
this.connectionTimer = void 0;
|
|
1253
|
+
}
|
|
1204
1254
|
this._isReconnecting = false;
|
|
1205
1255
|
this.reconnectAttempt = 0;
|
|
1206
1256
|
this.error = void 0;
|
|
@@ -1403,6 +1453,13 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
|
|
|
1403
1453
|
this.ws.onmessage = this.onWSMessage;
|
|
1404
1454
|
this.ws.onclose = this.onWSClose;
|
|
1405
1455
|
this.ws.onerror = this.onWSError;
|
|
1456
|
+
this.connectionTimer = window.setTimeout(() => {
|
|
1457
|
+
this.connectionTimer = void 0;
|
|
1458
|
+
if (this.ws?.readyState === WebSocket.CONNECTING) {
|
|
1459
|
+
this.log("WebSocket connection timeout");
|
|
1460
|
+
this.ws.close();
|
|
1461
|
+
}
|
|
1462
|
+
}, this.options.reconnect?.connectionTimeout ?? DEFAULT_RECONNECT_OPTIONS.connectionTimeout);
|
|
1406
1463
|
} catch (error) {
|
|
1407
1464
|
this.setError(
|
|
1408
1465
|
new MicdropClientError(
|
|
@@ -1415,6 +1472,10 @@ var MicdropClient = class extends import_eventemitter36.EventEmitter {
|
|
|
1415
1472
|
}
|
|
1416
1473
|
}
|
|
1417
1474
|
stopWS() {
|
|
1475
|
+
if (this.connectionTimer) {
|
|
1476
|
+
window.clearTimeout(this.connectionTimer);
|
|
1477
|
+
this.connectionTimer = void 0;
|
|
1478
|
+
}
|
|
1418
1479
|
if (!this.ws) return;
|
|
1419
1480
|
if (this.ws.readyState === WebSocket.OPEN || this.ws.readyState === WebSocket.CONNECTING) {
|
|
1420
1481
|
this.ws.close();
|