@micdrop/client 1.3.0 → 1.4.0
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 +17 -13
- package/dist/index.d.ts +17 -13
- package/dist/index.js +329 -167
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +329 -167
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -4,37 +4,28 @@ var __export = (target, all) => {
|
|
|
4
4
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
5
|
};
|
|
6
6
|
|
|
7
|
-
// src/audio/localStorage.ts
|
|
8
|
-
var LocalStorageKeys = /* @__PURE__ */ ((LocalStorageKeys2) => {
|
|
9
|
-
LocalStorageKeys2["MicDevice"] = "micdrop.micDevice";
|
|
10
|
-
LocalStorageKeys2["SpeakerDevice"] = "micdrop.speakerDevice";
|
|
11
|
-
LocalStorageKeys2["MicThreshold"] = "micdrop.micThreshold";
|
|
12
|
-
return LocalStorageKeys2;
|
|
13
|
-
})(LocalStorageKeys || {});
|
|
14
|
-
|
|
15
7
|
// src/audio/MicRecorder.ts
|
|
16
8
|
import EventEmitter2 from "eventemitter3";
|
|
17
9
|
|
|
18
10
|
// src/audio/microphone.ts
|
|
19
11
|
var microphone_exports = {};
|
|
20
12
|
__export(microphone_exports, {
|
|
21
|
-
audioContext: () => audioContext,
|
|
22
13
|
defaultMicThreshold: () => defaultMicThreshold,
|
|
23
14
|
micAnalyser: () => micAnalyser,
|
|
24
15
|
startMicrophone: () => startMicrophone,
|
|
25
16
|
stopMicrophone: () => stopMicrophone
|
|
26
17
|
});
|
|
27
18
|
|
|
28
|
-
// src/audio/AudioAnalyser.ts
|
|
19
|
+
// src/audio/utils/AudioAnalyser.ts
|
|
29
20
|
import EventEmitter from "eventemitter3";
|
|
30
21
|
var AudioAnalyser = class extends EventEmitter {
|
|
31
|
-
constructor(
|
|
22
|
+
constructor(audioContext2, volumeInterval = 100) {
|
|
32
23
|
super();
|
|
33
24
|
this.volumeInterval = volumeInterval;
|
|
34
25
|
this.volume = -Infinity;
|
|
35
26
|
this.fftSize = 512;
|
|
36
27
|
this.fftBins = new Float32Array(this.fftSize);
|
|
37
|
-
this.node =
|
|
28
|
+
this.node = audioContext2.createAnalyser();
|
|
38
29
|
this.node.fftSize = this.fftSize;
|
|
39
30
|
this.node.smoothingTimeConstant = 0.1;
|
|
40
31
|
this.interval = window.setInterval(() => {
|
|
@@ -60,7 +51,18 @@ var AudioAnalyser = class extends EventEmitter {
|
|
|
60
51
|
}
|
|
61
52
|
};
|
|
62
53
|
|
|
63
|
-
// src/audio/
|
|
54
|
+
// src/audio/utils/audioContext.ts
|
|
55
|
+
var audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
|
56
|
+
|
|
57
|
+
// src/audio/utils/localStorage.ts
|
|
58
|
+
var LocalStorageKeys = /* @__PURE__ */ ((LocalStorageKeys2) => {
|
|
59
|
+
LocalStorageKeys2["MicDevice"] = "micdrop.micDevice";
|
|
60
|
+
LocalStorageKeys2["SpeakerDevice"] = "micdrop.speakerDevice";
|
|
61
|
+
LocalStorageKeys2["MicThreshold"] = "micdrop.micThreshold";
|
|
62
|
+
return LocalStorageKeys2;
|
|
63
|
+
})(LocalStorageKeys || {});
|
|
64
|
+
|
|
65
|
+
// src/audio/utils/stopStream.ts
|
|
64
66
|
function stopStream(stream) {
|
|
65
67
|
stream?.getTracks().forEach((track) => track.stop());
|
|
66
68
|
}
|
|
@@ -68,21 +70,9 @@ function stopStream(stream) {
|
|
|
68
70
|
// src/audio/microphone.ts
|
|
69
71
|
var currentDeviceId;
|
|
70
72
|
var audioStream;
|
|
71
|
-
var audioContext;
|
|
72
73
|
var sourceNode;
|
|
73
|
-
var micAnalyser;
|
|
74
|
+
var micAnalyser = new AudioAnalyser(audioContext);
|
|
74
75
|
var defaultMicThreshold = -50;
|
|
75
|
-
function init() {
|
|
76
|
-
if (audioContext) return;
|
|
77
|
-
audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
|
78
|
-
micAnalyser = new AudioAnalyser(audioContext);
|
|
79
|
-
}
|
|
80
|
-
init();
|
|
81
|
-
document.addEventListener("click", () => {
|
|
82
|
-
if (audioContext?.state === "suspended") {
|
|
83
|
-
audioContext?.resume();
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
76
|
function getMicConstraints(deviceId) {
|
|
87
77
|
return {
|
|
88
78
|
audio: {
|
|
@@ -137,7 +127,7 @@ function stopMicrophone() {
|
|
|
137
127
|
}
|
|
138
128
|
}
|
|
139
129
|
|
|
140
|
-
// src/audio/createDelayedStream.ts
|
|
130
|
+
// src/audio/utils/createDelayedStream.ts
|
|
141
131
|
function createDelayedStream(stream, delay) {
|
|
142
132
|
if (!audioContext) {
|
|
143
133
|
throw new Error("AudioContext not initialized");
|
|
@@ -172,6 +162,7 @@ var MicRecorder = class extends EventEmitter2 {
|
|
|
172
162
|
this.queuedChunks.length = 0;
|
|
173
163
|
}).bind(this);
|
|
174
164
|
this.onConfirmSpeaking = (async () => {
|
|
165
|
+
if (this.state.isMuted) return;
|
|
175
166
|
this.emit("StartSpeaking");
|
|
176
167
|
this.changeState({ isSpeaking: true });
|
|
177
168
|
this.speakingConfirmed = true;
|
|
@@ -311,6 +302,9 @@ var speaker_exports = {};
|
|
|
311
302
|
__export(speaker_exports, {
|
|
312
303
|
canChangeSpeakerDevice: () => canChangeSpeakerDevice,
|
|
313
304
|
changeSpeakerDevice: () => changeSpeakerDevice,
|
|
305
|
+
disableStreaming: () => disableStreaming,
|
|
306
|
+
enableStreaming: () => enableStreaming,
|
|
307
|
+
isStreamingEnabled: () => isStreamingEnabled,
|
|
314
308
|
pauseAudio: () => pauseAudio,
|
|
315
309
|
playAudio: () => playAudio,
|
|
316
310
|
resumeAudio: () => resumeAudio,
|
|
@@ -318,14 +312,269 @@ __export(speaker_exports, {
|
|
|
318
312
|
stopAudio: () => stopAudio
|
|
319
313
|
});
|
|
320
314
|
|
|
321
|
-
// src/audio/
|
|
322
|
-
|
|
315
|
+
// src/audio/player/SpeakerPlayer.ts
|
|
316
|
+
var SpeakerPlayer = class {
|
|
317
|
+
constructor(outputNode) {
|
|
318
|
+
this.outputNode = outputNode;
|
|
319
|
+
const ctx = outputNode.context;
|
|
320
|
+
if (!(ctx instanceof AudioContext)) {
|
|
321
|
+
throw new Error("OutputNode must be connected to an AudioContext");
|
|
322
|
+
}
|
|
323
|
+
this.audioContext = ctx;
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
// src/audio/player/SpeakerConcatPlayer.ts
|
|
328
|
+
var SpeakerConcatPlayer = class extends SpeakerPlayer {
|
|
329
|
+
constructor() {
|
|
330
|
+
super(...arguments);
|
|
331
|
+
this.pendingBlobs = [];
|
|
332
|
+
this.prevSourceDuration = 0;
|
|
333
|
+
this.isConvertingBlob = false;
|
|
334
|
+
}
|
|
335
|
+
async setup() {
|
|
336
|
+
}
|
|
337
|
+
replaceSource() {
|
|
338
|
+
if (!this.nextSource) return;
|
|
339
|
+
const time = performance.now() / 1e3;
|
|
340
|
+
const source = this.nextSource;
|
|
341
|
+
source.connect(this.outputNode);
|
|
342
|
+
source.onended = () => {
|
|
343
|
+
if (source !== this.currentSource) return;
|
|
344
|
+
this.prevSourceDuration = source.buffer?.duration ?? 0;
|
|
345
|
+
this.sourceStartTime = void 0;
|
|
346
|
+
this.currentSource = void 0;
|
|
347
|
+
source.disconnect();
|
|
348
|
+
this.replaceSource();
|
|
349
|
+
};
|
|
350
|
+
const offset = this.prevSourceDuration + (this.sourceStartTime ? time - this.sourceStartTime : 0);
|
|
351
|
+
source.start(0, offset);
|
|
352
|
+
if (!this.sourceStartTime) {
|
|
353
|
+
this.sourceStartTime = time;
|
|
354
|
+
}
|
|
355
|
+
this.currentSource?.stop();
|
|
356
|
+
this.currentSource?.disconnect();
|
|
357
|
+
this.currentSource = source;
|
|
358
|
+
this.nextSource = void 0;
|
|
359
|
+
}
|
|
360
|
+
async convertNextBlob() {
|
|
361
|
+
if (this.pendingBlobs.length === 0 || this.isConvertingBlob) return;
|
|
362
|
+
this.isConvertingBlob = true;
|
|
363
|
+
const blob = this.pendingBlobs.shift();
|
|
364
|
+
try {
|
|
365
|
+
const arrayBuffer = await blob.arrayBuffer();
|
|
366
|
+
this.currentBuffer = this.currentBuffer ? this.concatArrayBuffers(this.currentBuffer, arrayBuffer) : arrayBuffer;
|
|
367
|
+
const audioBuffer = await this.audioContext.decodeAudioData(
|
|
368
|
+
// Copy the buffer to avoid mutating the original
|
|
369
|
+
this.copyArrayBuffer(this.currentBuffer)
|
|
370
|
+
);
|
|
371
|
+
const source = this.audioContext.createBufferSource();
|
|
372
|
+
source.buffer = audioBuffer;
|
|
373
|
+
this.nextSource = source;
|
|
374
|
+
this.replaceSource();
|
|
375
|
+
} catch (error) {
|
|
376
|
+
console.error("Error converting blob", error);
|
|
377
|
+
} finally {
|
|
378
|
+
this.isConvertingBlob = false;
|
|
379
|
+
}
|
|
380
|
+
this.convertNextBlob();
|
|
381
|
+
}
|
|
382
|
+
concatArrayBuffers(buffer1, buffer2) {
|
|
383
|
+
const tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
|
|
384
|
+
tmp.set(new Uint8Array(buffer1), 0);
|
|
385
|
+
tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
|
|
386
|
+
return tmp.buffer;
|
|
387
|
+
}
|
|
388
|
+
copyArrayBuffer(buffer) {
|
|
389
|
+
const destination = new ArrayBuffer(buffer.byteLength);
|
|
390
|
+
new Uint8Array(destination).set(new Uint8Array(buffer));
|
|
391
|
+
return destination;
|
|
392
|
+
}
|
|
393
|
+
addBlob(blob) {
|
|
394
|
+
this.pendingBlobs.push(blob);
|
|
395
|
+
this.convertNextBlob();
|
|
396
|
+
}
|
|
397
|
+
stop() {
|
|
398
|
+
this.pendingBlobs.length = 0;
|
|
399
|
+
this.sourceStartTime = void 0;
|
|
400
|
+
this.prevSourceDuration = 0;
|
|
401
|
+
this.currentSource?.stop();
|
|
402
|
+
this.currentSource?.disconnect();
|
|
403
|
+
this.currentSource = void 0;
|
|
404
|
+
this.nextSource = void 0;
|
|
405
|
+
this.currentBuffer = void 0;
|
|
406
|
+
this.isConvertingBlob = false;
|
|
407
|
+
}
|
|
408
|
+
destroy() {
|
|
409
|
+
this.stop();
|
|
410
|
+
}
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
// src/audio/player/SpeakerMediaSourcePlayer.ts
|
|
414
|
+
var SpeakerMediaSourcePlayer = class extends SpeakerPlayer {
|
|
415
|
+
constructor() {
|
|
416
|
+
super(...arguments);
|
|
417
|
+
this.isProcessingQueue = false;
|
|
418
|
+
this.pendingBlobs = [];
|
|
419
|
+
}
|
|
420
|
+
async setup() {
|
|
421
|
+
if (this.audioElement || this.mediaSource) return;
|
|
422
|
+
this.audioElement = new Audio();
|
|
423
|
+
this.audioElement.autoplay = true;
|
|
424
|
+
this.audioElement.muted = true;
|
|
425
|
+
this.mediaSource = new MediaSource();
|
|
426
|
+
if (!this.mediaSource) {
|
|
427
|
+
throw new Error("Failed to create MediaSource");
|
|
428
|
+
}
|
|
429
|
+
this.audioElement.src = URL.createObjectURL(this.mediaSource);
|
|
430
|
+
await new Promise((resolve) => {
|
|
431
|
+
this.mediaSource?.addEventListener("sourceopen", () => {
|
|
432
|
+
const sourceBuffer = this.mediaSource?.addSourceBuffer("audio/mpeg");
|
|
433
|
+
if (!sourceBuffer) throw new Error("Failed to create SourceBuffer");
|
|
434
|
+
this.sourceBuffer = sourceBuffer;
|
|
435
|
+
sourceBuffer.addEventListener("updateend", () => this.processQueue());
|
|
436
|
+
this.processQueue();
|
|
437
|
+
resolve();
|
|
438
|
+
});
|
|
439
|
+
});
|
|
440
|
+
await new Promise((resolve) => {
|
|
441
|
+
const onTimeUpdate = () => {
|
|
442
|
+
this.audioElement?.removeEventListener("timeupdate", onTimeUpdate);
|
|
443
|
+
if (!this.audioElement) {
|
|
444
|
+
throw new Error("Audio element or audio context not found");
|
|
445
|
+
}
|
|
446
|
+
const mediaStream = this.audioElement.captureStream();
|
|
447
|
+
if (mediaStream.getAudioTracks().length === 0) return;
|
|
448
|
+
this.mediaStream = mediaStream;
|
|
449
|
+
this.sourceNode = this.audioContext.createMediaStreamSource(mediaStream);
|
|
450
|
+
this.sourceNode.connect(this.outputNode);
|
|
451
|
+
this.audioElement.currentTime = 0;
|
|
452
|
+
resolve();
|
|
453
|
+
};
|
|
454
|
+
this.audioElement?.addEventListener("timeupdate", onTimeUpdate);
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
async processQueue() {
|
|
458
|
+
if (!this.sourceBuffer || !this.mediaSource || this.pendingBlobs.length === 0 || this.isProcessingQueue || this.sourceBuffer.updating) {
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
this.isProcessingQueue = true;
|
|
462
|
+
const blob = this.pendingBlobs.shift();
|
|
463
|
+
try {
|
|
464
|
+
const arrayBuffer = await blob.arrayBuffer();
|
|
465
|
+
if (this.isProcessingQueue) {
|
|
466
|
+
this.sourceBuffer.appendBuffer(arrayBuffer);
|
|
467
|
+
}
|
|
468
|
+
} catch (error) {
|
|
469
|
+
console.error("Error appending buffer", error);
|
|
470
|
+
} finally {
|
|
471
|
+
this.isProcessingQueue = false;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
addBlob(blob) {
|
|
475
|
+
this.pendingBlobs.push(blob);
|
|
476
|
+
if (this.sourceBuffer && !this.sourceBuffer.updating && !this.isProcessingQueue) {
|
|
477
|
+
this.processQueue();
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
stop() {
|
|
481
|
+
this.destroy();
|
|
482
|
+
this.setup();
|
|
483
|
+
}
|
|
484
|
+
destroy() {
|
|
485
|
+
if (this.sourceNode) {
|
|
486
|
+
this.sourceNode.disconnect();
|
|
487
|
+
this.sourceNode = void 0;
|
|
488
|
+
}
|
|
489
|
+
this.pendingBlobs.length = 0;
|
|
490
|
+
if (this.mediaSource) {
|
|
491
|
+
try {
|
|
492
|
+
if (this.mediaSource.readyState === "open") {
|
|
493
|
+
this.mediaSource.endOfStream();
|
|
494
|
+
}
|
|
495
|
+
} catch (error) {
|
|
496
|
+
console.error("Error cleaning up media source", error);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
if (this.audioElement) {
|
|
500
|
+
this.audioElement.pause();
|
|
501
|
+
this.audioElement.src = "";
|
|
502
|
+
this.audioElement = void 0;
|
|
503
|
+
}
|
|
504
|
+
if (this.mediaStream) {
|
|
505
|
+
this.mediaStream.getTracks().forEach((track) => track.stop());
|
|
506
|
+
this.mediaStream = void 0;
|
|
507
|
+
}
|
|
508
|
+
this.mediaSource = void 0;
|
|
509
|
+
this.sourceBuffer = void 0;
|
|
510
|
+
}
|
|
511
|
+
};
|
|
512
|
+
SpeakerMediaSourcePlayer.isCompatible = typeof MediaSource !== "undefined" && MediaSource.isTypeSupported("audio/mpeg");
|
|
513
|
+
|
|
514
|
+
// src/audio/player/SpeakerSequencePlayer.ts
|
|
515
|
+
var SpeakerSequencePlayer = class extends SpeakerPlayer {
|
|
516
|
+
constructor() {
|
|
517
|
+
super(...arguments);
|
|
518
|
+
this.pendingBlobs = [];
|
|
519
|
+
this.pendingSources = [];
|
|
520
|
+
this.isConvertingBlob = false;
|
|
521
|
+
}
|
|
522
|
+
async setup() {
|
|
523
|
+
}
|
|
524
|
+
processQueue() {
|
|
525
|
+
if (this.pendingSources.length === 0 || this.currentSource) return;
|
|
526
|
+
const source = this.pendingSources.shift();
|
|
527
|
+
this.currentSource = source;
|
|
528
|
+
source.connect(this.outputNode);
|
|
529
|
+
source.onended = () => {
|
|
530
|
+
source.disconnect();
|
|
531
|
+
this.currentSource = void 0;
|
|
532
|
+
this.processQueue();
|
|
533
|
+
};
|
|
534
|
+
source.start(0);
|
|
535
|
+
}
|
|
536
|
+
async convertNextBlob() {
|
|
537
|
+
if (this.pendingBlobs.length === 0 || this.isConvertingBlob) return;
|
|
538
|
+
this.isConvertingBlob = true;
|
|
539
|
+
const blob = this.pendingBlobs.shift();
|
|
540
|
+
try {
|
|
541
|
+
const arrayBuffer = await blob.arrayBuffer();
|
|
542
|
+
const audioBuffer = await this.audioContext.decodeAudioData(arrayBuffer);
|
|
543
|
+
const source = this.audioContext.createBufferSource();
|
|
544
|
+
source.buffer = audioBuffer;
|
|
545
|
+
this.pendingSources.push(source);
|
|
546
|
+
this.processQueue();
|
|
547
|
+
} catch (error) {
|
|
548
|
+
console.error("Error converting blob", error);
|
|
549
|
+
} finally {
|
|
550
|
+
this.isConvertingBlob = false;
|
|
551
|
+
}
|
|
552
|
+
this.convertNextBlob();
|
|
553
|
+
}
|
|
554
|
+
addBlob(blob) {
|
|
555
|
+
this.pendingBlobs.push(blob);
|
|
556
|
+
this.convertNextBlob();
|
|
557
|
+
}
|
|
558
|
+
stop() {
|
|
559
|
+
this.pendingBlobs.length = 0;
|
|
560
|
+
this.pendingSources.length = 0;
|
|
561
|
+
this.currentSource?.stop();
|
|
562
|
+
this.currentSource?.disconnect();
|
|
563
|
+
this.currentSource = void 0;
|
|
564
|
+
}
|
|
565
|
+
destroy() {
|
|
566
|
+
this.stop();
|
|
567
|
+
}
|
|
568
|
+
};
|
|
569
|
+
|
|
570
|
+
// src/audio/utils/unlock.ts
|
|
571
|
+
function unlock(audioContext2) {
|
|
323
572
|
const events = ["touchstart", "touchend", "mousedown", "keydown"];
|
|
324
573
|
events.forEach((e) => document.body.addEventListener(e, unlock2, false));
|
|
325
574
|
async function unlock2() {
|
|
326
|
-
unlockSafari(
|
|
327
|
-
if (
|
|
328
|
-
await
|
|
575
|
+
unlockSafari(audioContext2);
|
|
576
|
+
if (audioContext2.state === "suspended") {
|
|
577
|
+
await audioContext2.resume();
|
|
329
578
|
}
|
|
330
579
|
clean();
|
|
331
580
|
}
|
|
@@ -333,10 +582,10 @@ function unlock(audioContext3) {
|
|
|
333
582
|
events.forEach((e) => document.body.removeEventListener(e, unlock2));
|
|
334
583
|
}
|
|
335
584
|
}
|
|
336
|
-
function unlockSafari(
|
|
337
|
-
const source =
|
|
338
|
-
source.buffer =
|
|
339
|
-
source.connect(
|
|
585
|
+
function unlockSafari(audioContext2) {
|
|
586
|
+
const source = audioContext2.createBufferSource();
|
|
587
|
+
source.buffer = audioContext2.createBuffer(1, 1, 22050);
|
|
588
|
+
source.connect(audioContext2.destination);
|
|
340
589
|
const onEnded = () => {
|
|
341
590
|
source.disconnect();
|
|
342
591
|
source.buffer = null;
|
|
@@ -348,107 +597,49 @@ function unlockSafari(audioContext3) {
|
|
|
348
597
|
|
|
349
598
|
// src/audio/speaker.ts
|
|
350
599
|
var audioElement;
|
|
351
|
-
var
|
|
352
|
-
var
|
|
353
|
-
var
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
if (audioContext2) return;
|
|
360
|
-
audioContext2 = new (window.AudioContext || window.webkitAudioContext)();
|
|
361
|
-
speakerAnalyser = new AudioAnalyser(audioContext2);
|
|
362
|
-
unlock(audioContext2);
|
|
363
|
-
}
|
|
364
|
-
init2();
|
|
365
|
-
async function setupAudioElement(speakerId) {
|
|
366
|
-
if (!audioContext2 || !speakerAnalyser) return;
|
|
367
|
-
if (!speakerId) {
|
|
368
|
-
speakerId = localStorage.getItem("micdrop.speakerDevice" /* SpeakerDevice */) || void 0;
|
|
369
|
-
}
|
|
370
|
-
if (!audioElement) {
|
|
371
|
-
audioElement = new Audio();
|
|
372
|
-
audioElement.autoplay = true;
|
|
373
|
-
mediaSource = new MediaSource();
|
|
374
|
-
if (!mediaSource) {
|
|
375
|
-
throw new Error("Failed to create MediaSource");
|
|
376
|
-
}
|
|
377
|
-
audioElement.src = URL.createObjectURL(mediaSource);
|
|
378
|
-
const ms = mediaSource;
|
|
379
|
-
ms.addEventListener("sourceopen", () => {
|
|
380
|
-
sourceBuffer = ms.addSourceBuffer("audio/mpeg");
|
|
381
|
-
sourceBuffer.addEventListener("updateend", () => {
|
|
382
|
-
processQueue();
|
|
383
|
-
});
|
|
384
|
-
if (pendingBlobs.length > 0) {
|
|
385
|
-
processQueue();
|
|
386
|
-
}
|
|
387
|
-
});
|
|
388
|
-
audioElement.addEventListener("timeupdate", function onTimeUpdate() {
|
|
389
|
-
if (!audioContext2 || !speakerAnalyser || !audioElement) return;
|
|
390
|
-
try {
|
|
391
|
-
const mediaStream = audioElement.captureStream();
|
|
392
|
-
if (mediaStream.getAudioTracks().length === 0) return;
|
|
393
|
-
currentMediaStream = mediaStream;
|
|
394
|
-
const sourceNode2 = audioContext2.createMediaStreamSource(mediaStream);
|
|
395
|
-
sourceNode2.connect(speakerAnalyser.node);
|
|
396
|
-
audioElement.removeEventListener("timeupdate", onTimeUpdate);
|
|
397
|
-
} catch (error) {
|
|
398
|
-
console.error("Error creating media stream source:", error);
|
|
399
|
-
}
|
|
400
|
-
});
|
|
401
|
-
}
|
|
600
|
+
var player;
|
|
601
|
+
var streamingEnabled = false;
|
|
602
|
+
var speakerAnalyser = new AudioAnalyser(audioContext);
|
|
603
|
+
async function init() {
|
|
604
|
+
unlock(audioContext);
|
|
605
|
+
audioElement = new Audio();
|
|
606
|
+
audioElement.autoplay = true;
|
|
607
|
+
const speakerId = localStorage.getItem("micdrop.speakerDevice" /* SpeakerDevice */);
|
|
402
608
|
if (speakerId) {
|
|
403
609
|
try {
|
|
404
610
|
await setSinkId(speakerId);
|
|
405
611
|
} catch (error) {
|
|
406
|
-
console.error(
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
return audioElement;
|
|
410
|
-
}
|
|
411
|
-
async function processQueue() {
|
|
412
|
-
if (!sourceBuffer || !mediaSource || pendingBlobs.length === 0) return;
|
|
413
|
-
if (isProcessingQueue || sourceBuffer.updating) return;
|
|
414
|
-
isProcessingQueue = true;
|
|
415
|
-
try {
|
|
416
|
-
await playNextBlob();
|
|
417
|
-
} finally {
|
|
418
|
-
isProcessingQueue = false;
|
|
419
|
-
if (pendingBlobs.length > 0 && !sourceBuffer.updating) {
|
|
420
|
-
processQueue();
|
|
612
|
+
console.error("Failed to set sink ID:", error);
|
|
421
613
|
}
|
|
422
614
|
}
|
|
615
|
+
await setupSpeakerPlayer();
|
|
616
|
+
const destinationNode = audioContext.createMediaStreamDestination();
|
|
617
|
+
speakerAnalyser.node.connect(destinationNode);
|
|
618
|
+
audioElement.srcObject = destinationNode.stream;
|
|
423
619
|
}
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
if (sourceBuffer.updating) return;
|
|
427
|
-
if (mediaSource.readyState !== "open") {
|
|
428
|
-
console.warn("MediaSource is no longer open, resetting audio pipeline");
|
|
429
|
-
stopAudio();
|
|
430
|
-
await setupAudioElement();
|
|
431
|
-
return;
|
|
432
|
-
}
|
|
433
|
-
const blob = pendingBlobs.shift();
|
|
620
|
+
init();
|
|
621
|
+
async function setupSpeakerPlayer() {
|
|
434
622
|
try {
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
} else {
|
|
439
|
-
console.warn("SourceBuffer is no longer valid, resetting audio pipeline");
|
|
440
|
-
stopAudio();
|
|
441
|
-
await setupAudioElement();
|
|
442
|
-
}
|
|
623
|
+
player?.destroy();
|
|
624
|
+
player = streamingEnabled ? SpeakerMediaSourcePlayer.isCompatible ? new SpeakerMediaSourcePlayer(speakerAnalyser.node) : new SpeakerConcatPlayer(speakerAnalyser.node) : new SpeakerSequencePlayer(speakerAnalyser.node);
|
|
625
|
+
await player?.setup();
|
|
443
626
|
} catch (error) {
|
|
444
|
-
console.error("Error
|
|
445
|
-
if (error instanceof Error && error.name === "InvalidStateError") {
|
|
446
|
-
console.warn("Resetting audio pipeline due to invalid state");
|
|
447
|
-
stopAudio();
|
|
448
|
-
await setupAudioElement();
|
|
449
|
-
}
|
|
627
|
+
console.error("Error setting up speaker player", error);
|
|
450
628
|
}
|
|
451
629
|
}
|
|
630
|
+
async function isStreamingEnabled() {
|
|
631
|
+
return streamingEnabled;
|
|
632
|
+
}
|
|
633
|
+
async function enableStreaming() {
|
|
634
|
+
if (streamingEnabled) return;
|
|
635
|
+
streamingEnabled = true;
|
|
636
|
+
await setupSpeakerPlayer();
|
|
637
|
+
}
|
|
638
|
+
async function disableStreaming() {
|
|
639
|
+
if (!streamingEnabled) return;
|
|
640
|
+
streamingEnabled = false;
|
|
641
|
+
await setupSpeakerPlayer();
|
|
642
|
+
}
|
|
452
643
|
function canChangeSpeakerDevice() {
|
|
453
644
|
return window.HTMLMediaElement && "sinkId" in window.HTMLMediaElement.prototype;
|
|
454
645
|
}
|
|
@@ -460,7 +651,7 @@ async function setSinkId(speakerId) {
|
|
|
460
651
|
async function changeSpeakerDevice(speakerId) {
|
|
461
652
|
if (!canChangeSpeakerDevice()) return;
|
|
462
653
|
try {
|
|
463
|
-
await
|
|
654
|
+
await setSinkId(speakerId);
|
|
464
655
|
localStorage.setItem("micdrop.speakerDevice" /* SpeakerDevice */, speakerId);
|
|
465
656
|
} catch (error) {
|
|
466
657
|
console.error(`Error setting Audio Output to ${speakerId}`, error);
|
|
@@ -477,48 +668,19 @@ function resumeAudio() {
|
|
|
477
668
|
}
|
|
478
669
|
}
|
|
479
670
|
async function playAudio(blob) {
|
|
480
|
-
if (!
|
|
481
|
-
pendingBlobs.push(blob);
|
|
482
|
-
if (!audioElement) {
|
|
483
|
-
await setupAudioElement();
|
|
484
|
-
}
|
|
485
|
-
if (!audioElement) return;
|
|
671
|
+
if (!audioElement || !player) return;
|
|
486
672
|
try {
|
|
673
|
+
player.addBlob(blob);
|
|
487
674
|
if (audioElement.paused) {
|
|
488
675
|
audioElement.play();
|
|
489
676
|
}
|
|
490
|
-
if (sourceBuffer && !sourceBuffer.updating && !isProcessingQueue) {
|
|
491
|
-
processQueue();
|
|
492
|
-
}
|
|
493
677
|
} catch (error) {
|
|
494
678
|
console.error("Error playing audio blob", error, blob);
|
|
495
679
|
}
|
|
496
680
|
}
|
|
497
681
|
function stopAudio() {
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
try {
|
|
501
|
-
if (mediaSource.readyState === "open") {
|
|
502
|
-
mediaSource.endOfStream();
|
|
503
|
-
}
|
|
504
|
-
if (sourceBuffer && mediaSource.readyState !== "closed") {
|
|
505
|
-
mediaSource.removeSourceBuffer(sourceBuffer);
|
|
506
|
-
}
|
|
507
|
-
} catch (error) {
|
|
508
|
-
console.error("Error cleaning up media source", error);
|
|
509
|
-
}
|
|
510
|
-
mediaSource = void 0;
|
|
511
|
-
sourceBuffer = void 0;
|
|
512
|
-
}
|
|
513
|
-
if (audioElement) {
|
|
514
|
-
audioElement.pause();
|
|
515
|
-
audioElement.src = "";
|
|
516
|
-
audioElement = void 0;
|
|
517
|
-
}
|
|
518
|
-
if (currentMediaStream) {
|
|
519
|
-
currentMediaStream.getTracks().forEach((track) => track.stop());
|
|
520
|
-
currentMediaStream = void 0;
|
|
521
|
-
}
|
|
682
|
+
audioElement?.pause();
|
|
683
|
+
player?.stop();
|
|
522
684
|
}
|
|
523
685
|
|
|
524
686
|
// src/audio/vad/SileroVAD.ts
|
|
@@ -613,9 +775,8 @@ var VolumeVAD = class extends VAD {
|
|
|
613
775
|
if (threshold !== void 0) {
|
|
614
776
|
this.threshold = threshold;
|
|
615
777
|
}
|
|
616
|
-
this.
|
|
617
|
-
this.
|
|
618
|
-
this.sourceNode = this.audioContext.createMediaStreamSource(stream);
|
|
778
|
+
this.analyser = audioContext.createAnalyser();
|
|
779
|
+
this.sourceNode = audioContext.createMediaStreamSource(stream);
|
|
619
780
|
this.analyser.fftSize = 512;
|
|
620
781
|
this.analyser.smoothingTimeConstant = 0.1;
|
|
621
782
|
this.fftBins = new Float32Array(this.analyser.frequencyBinCount);
|
|
@@ -673,10 +834,6 @@ var VolumeVAD = class extends VAD {
|
|
|
673
834
|
this.analyser.disconnect();
|
|
674
835
|
this.analyser = void 0;
|
|
675
836
|
}
|
|
676
|
-
if (this.audioContext) {
|
|
677
|
-
await this.audioContext.close();
|
|
678
|
-
this.audioContext = void 0;
|
|
679
|
-
}
|
|
680
837
|
this.fftBins = void 0;
|
|
681
838
|
}
|
|
682
839
|
async setThreshold(threshold) {
|
|
@@ -721,6 +878,8 @@ var CallHandler = class _CallHandler extends EventEmitter4 {
|
|
|
721
878
|
this.micRecorder.on("StartSpeaking", () => {
|
|
722
879
|
this.log("[Mic] Start speaking");
|
|
723
880
|
this.ws?.send("startSpeaking" /* StartSpeaking */);
|
|
881
|
+
this._isProcessing = false;
|
|
882
|
+
this.notifyStateChange();
|
|
724
883
|
stopAudio();
|
|
725
884
|
});
|
|
726
885
|
this.micRecorder.on("StopSpeaking", () => {
|
|
@@ -859,6 +1018,8 @@ var CallHandler = class _CallHandler extends EventEmitter4 {
|
|
|
859
1018
|
this.conversation = this.conversation.slice(0, -1);
|
|
860
1019
|
this.notifyStateChange();
|
|
861
1020
|
}
|
|
1021
|
+
} else if (event.data === "enableSpeakerStreaming" /* EnableSpeakerStreaming */) {
|
|
1022
|
+
enableStreaming();
|
|
862
1023
|
}
|
|
863
1024
|
};
|
|
864
1025
|
this.ws.onclose = (event) => {
|
|
@@ -909,6 +1070,7 @@ var CallClientCommands = /* @__PURE__ */ ((CallClientCommands2) => {
|
|
|
909
1070
|
var CallServerCommands = /* @__PURE__ */ ((CallServerCommands2) => {
|
|
910
1071
|
CallServerCommands2["Message"] = "message";
|
|
911
1072
|
CallServerCommands2["CancelLastAssistantMessage"] = "cancelLastAssistantMessage";
|
|
1073
|
+
CallServerCommands2["EnableSpeakerStreaming"] = "enableSpeakerStreaming";
|
|
912
1074
|
CallServerCommands2["EndInterview"] = "endInterview";
|
|
913
1075
|
return CallServerCommands2;
|
|
914
1076
|
})(CallServerCommands || {});
|