@micdrop/client 1.3.1 → 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 +326 -167
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +326 -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");
|
|
@@ -312,6 +302,9 @@ var speaker_exports = {};
|
|
|
312
302
|
__export(speaker_exports, {
|
|
313
303
|
canChangeSpeakerDevice: () => canChangeSpeakerDevice,
|
|
314
304
|
changeSpeakerDevice: () => changeSpeakerDevice,
|
|
305
|
+
disableStreaming: () => disableStreaming,
|
|
306
|
+
enableStreaming: () => enableStreaming,
|
|
307
|
+
isStreamingEnabled: () => isStreamingEnabled,
|
|
315
308
|
pauseAudio: () => pauseAudio,
|
|
316
309
|
playAudio: () => playAudio,
|
|
317
310
|
resumeAudio: () => resumeAudio,
|
|
@@ -319,14 +312,269 @@ __export(speaker_exports, {
|
|
|
319
312
|
stopAudio: () => stopAudio
|
|
320
313
|
});
|
|
321
314
|
|
|
322
|
-
// src/audio/
|
|
323
|
-
|
|
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) {
|
|
324
572
|
const events = ["touchstart", "touchend", "mousedown", "keydown"];
|
|
325
573
|
events.forEach((e) => document.body.addEventListener(e, unlock2, false));
|
|
326
574
|
async function unlock2() {
|
|
327
|
-
unlockSafari(
|
|
328
|
-
if (
|
|
329
|
-
await
|
|
575
|
+
unlockSafari(audioContext2);
|
|
576
|
+
if (audioContext2.state === "suspended") {
|
|
577
|
+
await audioContext2.resume();
|
|
330
578
|
}
|
|
331
579
|
clean();
|
|
332
580
|
}
|
|
@@ -334,10 +582,10 @@ function unlock(audioContext3) {
|
|
|
334
582
|
events.forEach((e) => document.body.removeEventListener(e, unlock2));
|
|
335
583
|
}
|
|
336
584
|
}
|
|
337
|
-
function unlockSafari(
|
|
338
|
-
const source =
|
|
339
|
-
source.buffer =
|
|
340
|
-
source.connect(
|
|
585
|
+
function unlockSafari(audioContext2) {
|
|
586
|
+
const source = audioContext2.createBufferSource();
|
|
587
|
+
source.buffer = audioContext2.createBuffer(1, 1, 22050);
|
|
588
|
+
source.connect(audioContext2.destination);
|
|
341
589
|
const onEnded = () => {
|
|
342
590
|
source.disconnect();
|
|
343
591
|
source.buffer = null;
|
|
@@ -349,107 +597,49 @@ function unlockSafari(audioContext3) {
|
|
|
349
597
|
|
|
350
598
|
// src/audio/speaker.ts
|
|
351
599
|
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
|
-
}
|
|
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 */);
|
|
403
608
|
if (speakerId) {
|
|
404
609
|
try {
|
|
405
610
|
await setSinkId(speakerId);
|
|
406
611
|
} catch (error) {
|
|
407
|
-
console.error(
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
return audioElement;
|
|
411
|
-
}
|
|
412
|
-
async function processQueue() {
|
|
413
|
-
if (!sourceBuffer || !mediaSource || pendingBlobs.length === 0) return;
|
|
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();
|
|
612
|
+
console.error("Failed to set sink ID:", error);
|
|
422
613
|
}
|
|
423
614
|
}
|
|
615
|
+
await setupSpeakerPlayer();
|
|
616
|
+
const destinationNode = audioContext.createMediaStreamDestination();
|
|
617
|
+
speakerAnalyser.node.connect(destinationNode);
|
|
618
|
+
audioElement.srcObject = destinationNode.stream;
|
|
424
619
|
}
|
|
425
|
-
|
|
426
|
-
|
|
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();
|
|
620
|
+
init();
|
|
621
|
+
async function setupSpeakerPlayer() {
|
|
435
622
|
try {
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
} else {
|
|
440
|
-
console.warn("SourceBuffer is no longer valid, resetting audio pipeline");
|
|
441
|
-
stopAudio();
|
|
442
|
-
await setupAudioElement();
|
|
443
|
-
}
|
|
623
|
+
player?.destroy();
|
|
624
|
+
player = streamingEnabled ? SpeakerMediaSourcePlayer.isCompatible ? new SpeakerMediaSourcePlayer(speakerAnalyser.node) : new SpeakerConcatPlayer(speakerAnalyser.node) : new SpeakerSequencePlayer(speakerAnalyser.node);
|
|
625
|
+
await player?.setup();
|
|
444
626
|
} 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
|
-
}
|
|
627
|
+
console.error("Error setting up speaker player", error);
|
|
451
628
|
}
|
|
452
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
|
+
}
|
|
453
643
|
function canChangeSpeakerDevice() {
|
|
454
644
|
return window.HTMLMediaElement && "sinkId" in window.HTMLMediaElement.prototype;
|
|
455
645
|
}
|
|
@@ -461,7 +651,7 @@ async function setSinkId(speakerId) {
|
|
|
461
651
|
async function changeSpeakerDevice(speakerId) {
|
|
462
652
|
if (!canChangeSpeakerDevice()) return;
|
|
463
653
|
try {
|
|
464
|
-
await
|
|
654
|
+
await setSinkId(speakerId);
|
|
465
655
|
localStorage.setItem("micdrop.speakerDevice" /* SpeakerDevice */, speakerId);
|
|
466
656
|
} catch (error) {
|
|
467
657
|
console.error(`Error setting Audio Output to ${speakerId}`, error);
|
|
@@ -478,48 +668,19 @@ function resumeAudio() {
|
|
|
478
668
|
}
|
|
479
669
|
}
|
|
480
670
|
async function playAudio(blob) {
|
|
481
|
-
if (!
|
|
482
|
-
pendingBlobs.push(blob);
|
|
483
|
-
if (!audioElement) {
|
|
484
|
-
await setupAudioElement();
|
|
485
|
-
}
|
|
486
|
-
if (!audioElement) return;
|
|
671
|
+
if (!audioElement || !player) return;
|
|
487
672
|
try {
|
|
673
|
+
player.addBlob(blob);
|
|
488
674
|
if (audioElement.paused) {
|
|
489
675
|
audioElement.play();
|
|
490
676
|
}
|
|
491
|
-
if (sourceBuffer && !sourceBuffer.updating && !isProcessingQueue) {
|
|
492
|
-
processQueue();
|
|
493
|
-
}
|
|
494
677
|
} catch (error) {
|
|
495
678
|
console.error("Error playing audio blob", error, blob);
|
|
496
679
|
}
|
|
497
680
|
}
|
|
498
681
|
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
|
-
}
|
|
682
|
+
audioElement?.pause();
|
|
683
|
+
player?.stop();
|
|
523
684
|
}
|
|
524
685
|
|
|
525
686
|
// src/audio/vad/SileroVAD.ts
|
|
@@ -614,9 +775,8 @@ var VolumeVAD = class extends VAD {
|
|
|
614
775
|
if (threshold !== void 0) {
|
|
615
776
|
this.threshold = threshold;
|
|
616
777
|
}
|
|
617
|
-
this.
|
|
618
|
-
this.
|
|
619
|
-
this.sourceNode = this.audioContext.createMediaStreamSource(stream);
|
|
778
|
+
this.analyser = audioContext.createAnalyser();
|
|
779
|
+
this.sourceNode = audioContext.createMediaStreamSource(stream);
|
|
620
780
|
this.analyser.fftSize = 512;
|
|
621
781
|
this.analyser.smoothingTimeConstant = 0.1;
|
|
622
782
|
this.fftBins = new Float32Array(this.analyser.frequencyBinCount);
|
|
@@ -674,10 +834,6 @@ var VolumeVAD = class extends VAD {
|
|
|
674
834
|
this.analyser.disconnect();
|
|
675
835
|
this.analyser = void 0;
|
|
676
836
|
}
|
|
677
|
-
if (this.audioContext) {
|
|
678
|
-
await this.audioContext.close();
|
|
679
|
-
this.audioContext = void 0;
|
|
680
|
-
}
|
|
681
837
|
this.fftBins = void 0;
|
|
682
838
|
}
|
|
683
839
|
async setThreshold(threshold) {
|
|
@@ -862,6 +1018,8 @@ var CallHandler = class _CallHandler extends EventEmitter4 {
|
|
|
862
1018
|
this.conversation = this.conversation.slice(0, -1);
|
|
863
1019
|
this.notifyStateChange();
|
|
864
1020
|
}
|
|
1021
|
+
} else if (event.data === "enableSpeakerStreaming" /* EnableSpeakerStreaming */) {
|
|
1022
|
+
enableStreaming();
|
|
865
1023
|
}
|
|
866
1024
|
};
|
|
867
1025
|
this.ws.onclose = (event) => {
|
|
@@ -912,6 +1070,7 @@ var CallClientCommands = /* @__PURE__ */ ((CallClientCommands2) => {
|
|
|
912
1070
|
var CallServerCommands = /* @__PURE__ */ ((CallServerCommands2) => {
|
|
913
1071
|
CallServerCommands2["Message"] = "message";
|
|
914
1072
|
CallServerCommands2["CancelLastAssistantMessage"] = "cancelLastAssistantMessage";
|
|
1073
|
+
CallServerCommands2["EnableSpeakerStreaming"] = "enableSpeakerStreaming";
|
|
915
1074
|
CallServerCommands2["EndInterview"] = "endInterview";
|
|
916
1075
|
return CallServerCommands2;
|
|
917
1076
|
})(CallServerCommands || {});
|