@micdrop/client 1.3.1 → 1.4.1
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 +342 -171
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +342 -171
- 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");
|
|
@@ -168,8 +158,12 @@ var MicRecorder = class extends EventEmitter2 {
|
|
|
168
158
|
this.queuedChunks = [];
|
|
169
159
|
this.onStartSpeaking = (async () => {
|
|
170
160
|
if (!this.recorder || this.state.isMuted) return;
|
|
171
|
-
|
|
172
|
-
|
|
161
|
+
try {
|
|
162
|
+
this.recorder.start(timeSlice);
|
|
163
|
+
this.queuedChunks.length = 0;
|
|
164
|
+
} catch (error) {
|
|
165
|
+
console.error(error);
|
|
166
|
+
}
|
|
173
167
|
}).bind(this);
|
|
174
168
|
this.onConfirmSpeaking = (async () => {
|
|
175
169
|
if (this.state.isMuted) return;
|
|
@@ -179,12 +173,20 @@ var MicRecorder = class extends EventEmitter2 {
|
|
|
179
173
|
}).bind(this);
|
|
180
174
|
this.onCancelSpeaking = (async () => {
|
|
181
175
|
if (!this.recorder) return;
|
|
182
|
-
|
|
176
|
+
try {
|
|
177
|
+
this.recorder.stop();
|
|
178
|
+
} catch (error) {
|
|
179
|
+
console.error(error);
|
|
180
|
+
}
|
|
183
181
|
this.queuedChunks.length = 0;
|
|
184
182
|
}).bind(this);
|
|
185
183
|
this.onStopSpeaking = (async () => {
|
|
186
184
|
if (!this.recorder) return;
|
|
187
|
-
|
|
185
|
+
try {
|
|
186
|
+
this.recorder.stop();
|
|
187
|
+
} catch (error) {
|
|
188
|
+
console.error(error);
|
|
189
|
+
}
|
|
188
190
|
this.changeState({ isSpeaking: false });
|
|
189
191
|
this.emit("StopSpeaking");
|
|
190
192
|
this.speakingConfirmed = false;
|
|
@@ -312,6 +314,9 @@ var speaker_exports = {};
|
|
|
312
314
|
__export(speaker_exports, {
|
|
313
315
|
canChangeSpeakerDevice: () => canChangeSpeakerDevice,
|
|
314
316
|
changeSpeakerDevice: () => changeSpeakerDevice,
|
|
317
|
+
disableStreaming: () => disableStreaming,
|
|
318
|
+
enableStreaming: () => enableStreaming,
|
|
319
|
+
isStreamingEnabled: () => isStreamingEnabled,
|
|
315
320
|
pauseAudio: () => pauseAudio,
|
|
316
321
|
playAudio: () => playAudio,
|
|
317
322
|
resumeAudio: () => resumeAudio,
|
|
@@ -319,14 +324,269 @@ __export(speaker_exports, {
|
|
|
319
324
|
stopAudio: () => stopAudio
|
|
320
325
|
});
|
|
321
326
|
|
|
322
|
-
// src/audio/
|
|
323
|
-
|
|
327
|
+
// src/audio/player/SpeakerPlayer.ts
|
|
328
|
+
var SpeakerPlayer = class {
|
|
329
|
+
constructor(outputNode) {
|
|
330
|
+
this.outputNode = outputNode;
|
|
331
|
+
const ctx = outputNode.context;
|
|
332
|
+
if (!(ctx instanceof AudioContext)) {
|
|
333
|
+
throw new Error("OutputNode must be connected to an AudioContext");
|
|
334
|
+
}
|
|
335
|
+
this.audioContext = ctx;
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
// src/audio/player/SpeakerConcatPlayer.ts
|
|
340
|
+
var SpeakerConcatPlayer = class extends SpeakerPlayer {
|
|
341
|
+
constructor() {
|
|
342
|
+
super(...arguments);
|
|
343
|
+
this.pendingBlobs = [];
|
|
344
|
+
this.prevSourceDuration = 0;
|
|
345
|
+
this.isConvertingBlob = false;
|
|
346
|
+
}
|
|
347
|
+
async setup() {
|
|
348
|
+
}
|
|
349
|
+
replaceSource() {
|
|
350
|
+
if (!this.nextSource) return;
|
|
351
|
+
const time = performance.now() / 1e3;
|
|
352
|
+
const source = this.nextSource;
|
|
353
|
+
source.connect(this.outputNode);
|
|
354
|
+
source.onended = () => {
|
|
355
|
+
if (source !== this.currentSource) return;
|
|
356
|
+
this.prevSourceDuration = source.buffer?.duration ?? 0;
|
|
357
|
+
this.sourceStartTime = void 0;
|
|
358
|
+
this.currentSource = void 0;
|
|
359
|
+
source.disconnect();
|
|
360
|
+
this.replaceSource();
|
|
361
|
+
};
|
|
362
|
+
const offset = this.prevSourceDuration + (this.sourceStartTime ? time - this.sourceStartTime : 0);
|
|
363
|
+
source.start(0, offset);
|
|
364
|
+
if (!this.sourceStartTime) {
|
|
365
|
+
this.sourceStartTime = time;
|
|
366
|
+
}
|
|
367
|
+
this.currentSource?.stop();
|
|
368
|
+
this.currentSource?.disconnect();
|
|
369
|
+
this.currentSource = source;
|
|
370
|
+
this.nextSource = void 0;
|
|
371
|
+
}
|
|
372
|
+
async convertNextBlob() {
|
|
373
|
+
if (this.pendingBlobs.length === 0 || this.isConvertingBlob) return;
|
|
374
|
+
this.isConvertingBlob = true;
|
|
375
|
+
const blob = this.pendingBlobs.shift();
|
|
376
|
+
try {
|
|
377
|
+
const arrayBuffer = await blob.arrayBuffer();
|
|
378
|
+
this.currentBuffer = this.currentBuffer ? this.concatArrayBuffers(this.currentBuffer, arrayBuffer) : arrayBuffer;
|
|
379
|
+
const audioBuffer = await this.audioContext.decodeAudioData(
|
|
380
|
+
// Copy the buffer to avoid mutating the original
|
|
381
|
+
this.copyArrayBuffer(this.currentBuffer)
|
|
382
|
+
);
|
|
383
|
+
const source = this.audioContext.createBufferSource();
|
|
384
|
+
source.buffer = audioBuffer;
|
|
385
|
+
this.nextSource = source;
|
|
386
|
+
this.replaceSource();
|
|
387
|
+
} catch (error) {
|
|
388
|
+
console.error("Error converting blob", error);
|
|
389
|
+
} finally {
|
|
390
|
+
this.isConvertingBlob = false;
|
|
391
|
+
}
|
|
392
|
+
this.convertNextBlob();
|
|
393
|
+
}
|
|
394
|
+
concatArrayBuffers(buffer1, buffer2) {
|
|
395
|
+
const tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
|
|
396
|
+
tmp.set(new Uint8Array(buffer1), 0);
|
|
397
|
+
tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
|
|
398
|
+
return tmp.buffer;
|
|
399
|
+
}
|
|
400
|
+
copyArrayBuffer(buffer) {
|
|
401
|
+
const destination = new ArrayBuffer(buffer.byteLength);
|
|
402
|
+
new Uint8Array(destination).set(new Uint8Array(buffer));
|
|
403
|
+
return destination;
|
|
404
|
+
}
|
|
405
|
+
addBlob(blob) {
|
|
406
|
+
this.pendingBlobs.push(blob);
|
|
407
|
+
this.convertNextBlob();
|
|
408
|
+
}
|
|
409
|
+
stop() {
|
|
410
|
+
this.pendingBlobs.length = 0;
|
|
411
|
+
this.sourceStartTime = void 0;
|
|
412
|
+
this.prevSourceDuration = 0;
|
|
413
|
+
this.currentSource?.stop();
|
|
414
|
+
this.currentSource?.disconnect();
|
|
415
|
+
this.currentSource = void 0;
|
|
416
|
+
this.nextSource = void 0;
|
|
417
|
+
this.currentBuffer = void 0;
|
|
418
|
+
this.isConvertingBlob = false;
|
|
419
|
+
}
|
|
420
|
+
destroy() {
|
|
421
|
+
this.stop();
|
|
422
|
+
}
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
// src/audio/player/SpeakerMediaSourcePlayer.ts
|
|
426
|
+
var SpeakerMediaSourcePlayer = class extends SpeakerPlayer {
|
|
427
|
+
constructor() {
|
|
428
|
+
super(...arguments);
|
|
429
|
+
this.isProcessingQueue = false;
|
|
430
|
+
this.pendingBlobs = [];
|
|
431
|
+
}
|
|
432
|
+
async setup() {
|
|
433
|
+
if (this.audioElement || this.mediaSource) return;
|
|
434
|
+
this.audioElement = new Audio();
|
|
435
|
+
this.audioElement.autoplay = true;
|
|
436
|
+
this.audioElement.muted = true;
|
|
437
|
+
this.mediaSource = new MediaSource();
|
|
438
|
+
if (!this.mediaSource) {
|
|
439
|
+
throw new Error("Failed to create MediaSource");
|
|
440
|
+
}
|
|
441
|
+
this.audioElement.src = URL.createObjectURL(this.mediaSource);
|
|
442
|
+
await new Promise((resolve) => {
|
|
443
|
+
this.mediaSource?.addEventListener("sourceopen", () => {
|
|
444
|
+
const sourceBuffer = this.mediaSource?.addSourceBuffer("audio/mpeg");
|
|
445
|
+
if (!sourceBuffer) throw new Error("Failed to create SourceBuffer");
|
|
446
|
+
this.sourceBuffer = sourceBuffer;
|
|
447
|
+
sourceBuffer.addEventListener("updateend", () => this.processQueue());
|
|
448
|
+
this.processQueue();
|
|
449
|
+
resolve();
|
|
450
|
+
});
|
|
451
|
+
});
|
|
452
|
+
await new Promise((resolve) => {
|
|
453
|
+
const onTimeUpdate = () => {
|
|
454
|
+
this.audioElement?.removeEventListener("timeupdate", onTimeUpdate);
|
|
455
|
+
if (!this.audioElement) {
|
|
456
|
+
throw new Error("Audio element or audio context not found");
|
|
457
|
+
}
|
|
458
|
+
const mediaStream = this.audioElement.captureStream();
|
|
459
|
+
if (mediaStream.getAudioTracks().length === 0) return;
|
|
460
|
+
this.mediaStream = mediaStream;
|
|
461
|
+
this.sourceNode = this.audioContext.createMediaStreamSource(mediaStream);
|
|
462
|
+
this.sourceNode.connect(this.outputNode);
|
|
463
|
+
this.audioElement.currentTime = 0;
|
|
464
|
+
resolve();
|
|
465
|
+
};
|
|
466
|
+
this.audioElement?.addEventListener("timeupdate", onTimeUpdate);
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
async processQueue() {
|
|
470
|
+
if (!this.sourceBuffer || !this.mediaSource || this.pendingBlobs.length === 0 || this.isProcessingQueue || this.sourceBuffer.updating) {
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
this.isProcessingQueue = true;
|
|
474
|
+
const blob = this.pendingBlobs.shift();
|
|
475
|
+
try {
|
|
476
|
+
const arrayBuffer = await blob.arrayBuffer();
|
|
477
|
+
if (this.isProcessingQueue) {
|
|
478
|
+
this.sourceBuffer.appendBuffer(arrayBuffer);
|
|
479
|
+
}
|
|
480
|
+
} catch (error) {
|
|
481
|
+
console.error("Error appending buffer", error);
|
|
482
|
+
} finally {
|
|
483
|
+
this.isProcessingQueue = false;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
addBlob(blob) {
|
|
487
|
+
this.pendingBlobs.push(blob);
|
|
488
|
+
if (this.sourceBuffer && !this.sourceBuffer.updating && !this.isProcessingQueue) {
|
|
489
|
+
this.processQueue();
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
stop() {
|
|
493
|
+
this.destroy();
|
|
494
|
+
this.setup();
|
|
495
|
+
}
|
|
496
|
+
destroy() {
|
|
497
|
+
if (this.sourceNode) {
|
|
498
|
+
this.sourceNode.disconnect();
|
|
499
|
+
this.sourceNode = void 0;
|
|
500
|
+
}
|
|
501
|
+
this.pendingBlobs.length = 0;
|
|
502
|
+
if (this.mediaSource) {
|
|
503
|
+
try {
|
|
504
|
+
if (this.mediaSource.readyState === "open") {
|
|
505
|
+
this.mediaSource.endOfStream();
|
|
506
|
+
}
|
|
507
|
+
} catch (error) {
|
|
508
|
+
console.error("Error cleaning up media source", error);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
if (this.audioElement) {
|
|
512
|
+
this.audioElement.pause();
|
|
513
|
+
this.audioElement.src = "";
|
|
514
|
+
this.audioElement = void 0;
|
|
515
|
+
}
|
|
516
|
+
if (this.mediaStream) {
|
|
517
|
+
this.mediaStream.getTracks().forEach((track) => track.stop());
|
|
518
|
+
this.mediaStream = void 0;
|
|
519
|
+
}
|
|
520
|
+
this.mediaSource = void 0;
|
|
521
|
+
this.sourceBuffer = void 0;
|
|
522
|
+
}
|
|
523
|
+
};
|
|
524
|
+
SpeakerMediaSourcePlayer.isCompatible = typeof MediaSource !== "undefined" && MediaSource.isTypeSupported("audio/mpeg");
|
|
525
|
+
|
|
526
|
+
// src/audio/player/SpeakerSequencePlayer.ts
|
|
527
|
+
var SpeakerSequencePlayer = class extends SpeakerPlayer {
|
|
528
|
+
constructor() {
|
|
529
|
+
super(...arguments);
|
|
530
|
+
this.pendingBlobs = [];
|
|
531
|
+
this.pendingSources = [];
|
|
532
|
+
this.isConvertingBlob = false;
|
|
533
|
+
}
|
|
534
|
+
async setup() {
|
|
535
|
+
}
|
|
536
|
+
processQueue() {
|
|
537
|
+
if (this.pendingSources.length === 0 || this.currentSource) return;
|
|
538
|
+
const source = this.pendingSources.shift();
|
|
539
|
+
this.currentSource = source;
|
|
540
|
+
source.connect(this.outputNode);
|
|
541
|
+
source.onended = () => {
|
|
542
|
+
source.disconnect();
|
|
543
|
+
this.currentSource = void 0;
|
|
544
|
+
this.processQueue();
|
|
545
|
+
};
|
|
546
|
+
source.start(0);
|
|
547
|
+
}
|
|
548
|
+
async convertNextBlob() {
|
|
549
|
+
if (this.pendingBlobs.length === 0 || this.isConvertingBlob) return;
|
|
550
|
+
this.isConvertingBlob = true;
|
|
551
|
+
const blob = this.pendingBlobs.shift();
|
|
552
|
+
try {
|
|
553
|
+
const arrayBuffer = await blob.arrayBuffer();
|
|
554
|
+
const audioBuffer = await this.audioContext.decodeAudioData(arrayBuffer);
|
|
555
|
+
const source = this.audioContext.createBufferSource();
|
|
556
|
+
source.buffer = audioBuffer;
|
|
557
|
+
this.pendingSources.push(source);
|
|
558
|
+
this.processQueue();
|
|
559
|
+
} catch (error) {
|
|
560
|
+
console.error("Error converting blob", error);
|
|
561
|
+
} finally {
|
|
562
|
+
this.isConvertingBlob = false;
|
|
563
|
+
}
|
|
564
|
+
this.convertNextBlob();
|
|
565
|
+
}
|
|
566
|
+
addBlob(blob) {
|
|
567
|
+
this.pendingBlobs.push(blob);
|
|
568
|
+
this.convertNextBlob();
|
|
569
|
+
}
|
|
570
|
+
stop() {
|
|
571
|
+
this.pendingBlobs.length = 0;
|
|
572
|
+
this.pendingSources.length = 0;
|
|
573
|
+
this.currentSource?.stop();
|
|
574
|
+
this.currentSource?.disconnect();
|
|
575
|
+
this.currentSource = void 0;
|
|
576
|
+
}
|
|
577
|
+
destroy() {
|
|
578
|
+
this.stop();
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
|
|
582
|
+
// src/audio/utils/unlock.ts
|
|
583
|
+
function unlock(audioContext2) {
|
|
324
584
|
const events = ["touchstart", "touchend", "mousedown", "keydown"];
|
|
325
585
|
events.forEach((e) => document.body.addEventListener(e, unlock2, false));
|
|
326
586
|
async function unlock2() {
|
|
327
|
-
unlockSafari(
|
|
328
|
-
if (
|
|
329
|
-
await
|
|
587
|
+
unlockSafari(audioContext2);
|
|
588
|
+
if (audioContext2.state === "suspended") {
|
|
589
|
+
await audioContext2.resume();
|
|
330
590
|
}
|
|
331
591
|
clean();
|
|
332
592
|
}
|
|
@@ -334,10 +594,10 @@ function unlock(audioContext3) {
|
|
|
334
594
|
events.forEach((e) => document.body.removeEventListener(e, unlock2));
|
|
335
595
|
}
|
|
336
596
|
}
|
|
337
|
-
function unlockSafari(
|
|
338
|
-
const source =
|
|
339
|
-
source.buffer =
|
|
340
|
-
source.connect(
|
|
597
|
+
function unlockSafari(audioContext2) {
|
|
598
|
+
const source = audioContext2.createBufferSource();
|
|
599
|
+
source.buffer = audioContext2.createBuffer(1, 1, 22050);
|
|
600
|
+
source.connect(audioContext2.destination);
|
|
341
601
|
const onEnded = () => {
|
|
342
602
|
source.disconnect();
|
|
343
603
|
source.buffer = null;
|
|
@@ -349,107 +609,49 @@ function unlockSafari(audioContext3) {
|
|
|
349
609
|
|
|
350
610
|
// src/audio/speaker.ts
|
|
351
611
|
var audioElement;
|
|
352
|
-
var
|
|
353
|
-
var
|
|
354
|
-
var
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
if (audioContext2) return;
|
|
361
|
-
audioContext2 = new (window.AudioContext || window.webkitAudioContext)();
|
|
362
|
-
speakerAnalyser = new AudioAnalyser(audioContext2);
|
|
363
|
-
unlock(audioContext2);
|
|
364
|
-
}
|
|
365
|
-
init2();
|
|
366
|
-
async function setupAudioElement(speakerId) {
|
|
367
|
-
if (!audioContext2 || !speakerAnalyser) return;
|
|
368
|
-
if (!speakerId) {
|
|
369
|
-
speakerId = localStorage.getItem("micdrop.speakerDevice" /* SpeakerDevice */) || void 0;
|
|
370
|
-
}
|
|
371
|
-
if (!audioElement) {
|
|
372
|
-
audioElement = new Audio();
|
|
373
|
-
audioElement.autoplay = true;
|
|
374
|
-
mediaSource = new MediaSource();
|
|
375
|
-
if (!mediaSource) {
|
|
376
|
-
throw new Error("Failed to create MediaSource");
|
|
377
|
-
}
|
|
378
|
-
audioElement.src = URL.createObjectURL(mediaSource);
|
|
379
|
-
const ms = mediaSource;
|
|
380
|
-
ms.addEventListener("sourceopen", () => {
|
|
381
|
-
sourceBuffer = ms.addSourceBuffer("audio/mpeg");
|
|
382
|
-
sourceBuffer.addEventListener("updateend", () => {
|
|
383
|
-
processQueue();
|
|
384
|
-
});
|
|
385
|
-
if (pendingBlobs.length > 0) {
|
|
386
|
-
processQueue();
|
|
387
|
-
}
|
|
388
|
-
});
|
|
389
|
-
audioElement.addEventListener("timeupdate", function onTimeUpdate() {
|
|
390
|
-
if (!audioContext2 || !speakerAnalyser || !audioElement) return;
|
|
391
|
-
try {
|
|
392
|
-
const mediaStream = audioElement.captureStream();
|
|
393
|
-
if (mediaStream.getAudioTracks().length === 0) return;
|
|
394
|
-
currentMediaStream = mediaStream;
|
|
395
|
-
const sourceNode2 = audioContext2.createMediaStreamSource(mediaStream);
|
|
396
|
-
sourceNode2.connect(speakerAnalyser.node);
|
|
397
|
-
audioElement.removeEventListener("timeupdate", onTimeUpdate);
|
|
398
|
-
} catch (error) {
|
|
399
|
-
console.error("Error creating media stream source:", error);
|
|
400
|
-
}
|
|
401
|
-
});
|
|
402
|
-
}
|
|
612
|
+
var player;
|
|
613
|
+
var streamingEnabled = false;
|
|
614
|
+
var speakerAnalyser = new AudioAnalyser(audioContext);
|
|
615
|
+
async function init() {
|
|
616
|
+
unlock(audioContext);
|
|
617
|
+
audioElement = new Audio();
|
|
618
|
+
audioElement.autoplay = true;
|
|
619
|
+
const speakerId = localStorage.getItem("micdrop.speakerDevice" /* SpeakerDevice */);
|
|
403
620
|
if (speakerId) {
|
|
404
621
|
try {
|
|
405
622
|
await setSinkId(speakerId);
|
|
406
623
|
} catch (error) {
|
|
407
|
-
console.error(
|
|
624
|
+
console.error("Failed to set sink ID:", error);
|
|
408
625
|
}
|
|
409
626
|
}
|
|
410
|
-
|
|
627
|
+
await setupSpeakerPlayer();
|
|
628
|
+
const destinationNode = audioContext.createMediaStreamDestination();
|
|
629
|
+
speakerAnalyser.node.connect(destinationNode);
|
|
630
|
+
audioElement.srcObject = destinationNode.stream;
|
|
411
631
|
}
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
if (isProcessingQueue || sourceBuffer.updating) return;
|
|
415
|
-
isProcessingQueue = true;
|
|
416
|
-
try {
|
|
417
|
-
await playNextBlob();
|
|
418
|
-
} finally {
|
|
419
|
-
isProcessingQueue = false;
|
|
420
|
-
if (pendingBlobs.length > 0 && !sourceBuffer.updating) {
|
|
421
|
-
processQueue();
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
async function playNextBlob() {
|
|
426
|
-
if (!sourceBuffer || !mediaSource || pendingBlobs.length === 0) return;
|
|
427
|
-
if (sourceBuffer.updating) return;
|
|
428
|
-
if (mediaSource.readyState !== "open") {
|
|
429
|
-
console.warn("MediaSource is no longer open, resetting audio pipeline");
|
|
430
|
-
stopAudio();
|
|
431
|
-
await setupAudioElement();
|
|
432
|
-
return;
|
|
433
|
-
}
|
|
434
|
-
const blob = pendingBlobs.shift();
|
|
632
|
+
init();
|
|
633
|
+
async function setupSpeakerPlayer() {
|
|
435
634
|
try {
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
} else {
|
|
440
|
-
console.warn("SourceBuffer is no longer valid, resetting audio pipeline");
|
|
441
|
-
stopAudio();
|
|
442
|
-
await setupAudioElement();
|
|
443
|
-
}
|
|
635
|
+
player?.destroy();
|
|
636
|
+
player = streamingEnabled ? SpeakerMediaSourcePlayer.isCompatible ? new SpeakerMediaSourcePlayer(speakerAnalyser.node) : new SpeakerConcatPlayer(speakerAnalyser.node) : new SpeakerSequencePlayer(speakerAnalyser.node);
|
|
637
|
+
await player?.setup();
|
|
444
638
|
} catch (error) {
|
|
445
|
-
console.error("Error
|
|
446
|
-
if (error instanceof Error && error.name === "InvalidStateError") {
|
|
447
|
-
console.warn("Resetting audio pipeline due to invalid state");
|
|
448
|
-
stopAudio();
|
|
449
|
-
await setupAudioElement();
|
|
450
|
-
}
|
|
639
|
+
console.error("Error setting up speaker player", error);
|
|
451
640
|
}
|
|
452
641
|
}
|
|
642
|
+
async function isStreamingEnabled() {
|
|
643
|
+
return streamingEnabled;
|
|
644
|
+
}
|
|
645
|
+
async function enableStreaming() {
|
|
646
|
+
if (streamingEnabled) return;
|
|
647
|
+
streamingEnabled = true;
|
|
648
|
+
await setupSpeakerPlayer();
|
|
649
|
+
}
|
|
650
|
+
async function disableStreaming() {
|
|
651
|
+
if (!streamingEnabled) return;
|
|
652
|
+
streamingEnabled = false;
|
|
653
|
+
await setupSpeakerPlayer();
|
|
654
|
+
}
|
|
453
655
|
function canChangeSpeakerDevice() {
|
|
454
656
|
return window.HTMLMediaElement && "sinkId" in window.HTMLMediaElement.prototype;
|
|
455
657
|
}
|
|
@@ -461,7 +663,7 @@ async function setSinkId(speakerId) {
|
|
|
461
663
|
async function changeSpeakerDevice(speakerId) {
|
|
462
664
|
if (!canChangeSpeakerDevice()) return;
|
|
463
665
|
try {
|
|
464
|
-
await
|
|
666
|
+
await setSinkId(speakerId);
|
|
465
667
|
localStorage.setItem("micdrop.speakerDevice" /* SpeakerDevice */, speakerId);
|
|
466
668
|
} catch (error) {
|
|
467
669
|
console.error(`Error setting Audio Output to ${speakerId}`, error);
|
|
@@ -478,48 +680,19 @@ function resumeAudio() {
|
|
|
478
680
|
}
|
|
479
681
|
}
|
|
480
682
|
async function playAudio(blob) {
|
|
481
|
-
if (!
|
|
482
|
-
pendingBlobs.push(blob);
|
|
483
|
-
if (!audioElement) {
|
|
484
|
-
await setupAudioElement();
|
|
485
|
-
}
|
|
486
|
-
if (!audioElement) return;
|
|
683
|
+
if (!audioElement || !player) return;
|
|
487
684
|
try {
|
|
685
|
+
player.addBlob(blob);
|
|
488
686
|
if (audioElement.paused) {
|
|
489
687
|
audioElement.play();
|
|
490
688
|
}
|
|
491
|
-
if (sourceBuffer && !sourceBuffer.updating && !isProcessingQueue) {
|
|
492
|
-
processQueue();
|
|
493
|
-
}
|
|
494
689
|
} catch (error) {
|
|
495
690
|
console.error("Error playing audio blob", error, blob);
|
|
496
691
|
}
|
|
497
692
|
}
|
|
498
693
|
function stopAudio() {
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
try {
|
|
502
|
-
if (mediaSource.readyState === "open") {
|
|
503
|
-
mediaSource.endOfStream();
|
|
504
|
-
}
|
|
505
|
-
if (sourceBuffer && mediaSource.readyState !== "closed") {
|
|
506
|
-
mediaSource.removeSourceBuffer(sourceBuffer);
|
|
507
|
-
}
|
|
508
|
-
} catch (error) {
|
|
509
|
-
console.error("Error cleaning up media source", error);
|
|
510
|
-
}
|
|
511
|
-
mediaSource = void 0;
|
|
512
|
-
sourceBuffer = void 0;
|
|
513
|
-
}
|
|
514
|
-
if (audioElement) {
|
|
515
|
-
audioElement.pause();
|
|
516
|
-
audioElement.src = "";
|
|
517
|
-
audioElement = void 0;
|
|
518
|
-
}
|
|
519
|
-
if (currentMediaStream) {
|
|
520
|
-
currentMediaStream.getTracks().forEach((track) => track.stop());
|
|
521
|
-
currentMediaStream = void 0;
|
|
522
|
-
}
|
|
694
|
+
audioElement?.pause();
|
|
695
|
+
player?.stop();
|
|
523
696
|
}
|
|
524
697
|
|
|
525
698
|
// src/audio/vad/SileroVAD.ts
|
|
@@ -614,9 +787,8 @@ var VolumeVAD = class extends VAD {
|
|
|
614
787
|
if (threshold !== void 0) {
|
|
615
788
|
this.threshold = threshold;
|
|
616
789
|
}
|
|
617
|
-
this.
|
|
618
|
-
this.
|
|
619
|
-
this.sourceNode = this.audioContext.createMediaStreamSource(stream);
|
|
790
|
+
this.analyser = audioContext.createAnalyser();
|
|
791
|
+
this.sourceNode = audioContext.createMediaStreamSource(stream);
|
|
620
792
|
this.analyser.fftSize = 512;
|
|
621
793
|
this.analyser.smoothingTimeConstant = 0.1;
|
|
622
794
|
this.fftBins = new Float32Array(this.analyser.frequencyBinCount);
|
|
@@ -674,10 +846,6 @@ var VolumeVAD = class extends VAD {
|
|
|
674
846
|
this.analyser.disconnect();
|
|
675
847
|
this.analyser = void 0;
|
|
676
848
|
}
|
|
677
|
-
if (this.audioContext) {
|
|
678
|
-
await this.audioContext.close();
|
|
679
|
-
this.audioContext = void 0;
|
|
680
|
-
}
|
|
681
849
|
this.fftBins = void 0;
|
|
682
850
|
}
|
|
683
851
|
async setThreshold(threshold) {
|
|
@@ -862,6 +1030,8 @@ var CallHandler = class _CallHandler extends EventEmitter4 {
|
|
|
862
1030
|
this.conversation = this.conversation.slice(0, -1);
|
|
863
1031
|
this.notifyStateChange();
|
|
864
1032
|
}
|
|
1033
|
+
} else if (event.data === "enableSpeakerStreaming" /* EnableSpeakerStreaming */) {
|
|
1034
|
+
enableStreaming();
|
|
865
1035
|
}
|
|
866
1036
|
};
|
|
867
1037
|
this.ws.onclose = (event) => {
|
|
@@ -912,6 +1082,7 @@ var CallClientCommands = /* @__PURE__ */ ((CallClientCommands2) => {
|
|
|
912
1082
|
var CallServerCommands = /* @__PURE__ */ ((CallServerCommands2) => {
|
|
913
1083
|
CallServerCommands2["Message"] = "message";
|
|
914
1084
|
CallServerCommands2["CancelLastAssistantMessage"] = "cancelLastAssistantMessage";
|
|
1085
|
+
CallServerCommands2["EnableSpeakerStreaming"] = "enableSpeakerStreaming";
|
|
915
1086
|
CallServerCommands2["EndInterview"] = "endInterview";
|
|
916
1087
|
return CallServerCommands2;
|
|
917
1088
|
})(CallServerCommands || {});
|