@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.js CHANGED
@@ -45,37 +45,28 @@ __export(index_exports, {
45
45
  });
46
46
  module.exports = __toCommonJS(index_exports);
47
47
 
48
- // src/audio/localStorage.ts
49
- var LocalStorageKeys = /* @__PURE__ */ ((LocalStorageKeys2) => {
50
- LocalStorageKeys2["MicDevice"] = "micdrop.micDevice";
51
- LocalStorageKeys2["SpeakerDevice"] = "micdrop.speakerDevice";
52
- LocalStorageKeys2["MicThreshold"] = "micdrop.micThreshold";
53
- return LocalStorageKeys2;
54
- })(LocalStorageKeys || {});
55
-
56
48
  // src/audio/MicRecorder.ts
57
49
  var import_eventemitter32 = __toESM(require("eventemitter3"));
58
50
 
59
51
  // src/audio/microphone.ts
60
52
  var microphone_exports = {};
61
53
  __export(microphone_exports, {
62
- audioContext: () => audioContext,
63
54
  defaultMicThreshold: () => defaultMicThreshold,
64
55
  micAnalyser: () => micAnalyser,
65
56
  startMicrophone: () => startMicrophone,
66
57
  stopMicrophone: () => stopMicrophone
67
58
  });
68
59
 
69
- // src/audio/AudioAnalyser.ts
60
+ // src/audio/utils/AudioAnalyser.ts
70
61
  var import_eventemitter3 = __toESM(require("eventemitter3"));
71
62
  var AudioAnalyser = class extends import_eventemitter3.default {
72
- constructor(audioContext3, volumeInterval = 100) {
63
+ constructor(audioContext2, volumeInterval = 100) {
73
64
  super();
74
65
  this.volumeInterval = volumeInterval;
75
66
  this.volume = -Infinity;
76
67
  this.fftSize = 512;
77
68
  this.fftBins = new Float32Array(this.fftSize);
78
- this.node = audioContext3.createAnalyser();
69
+ this.node = audioContext2.createAnalyser();
79
70
  this.node.fftSize = this.fftSize;
80
71
  this.node.smoothingTimeConstant = 0.1;
81
72
  this.interval = window.setInterval(() => {
@@ -101,7 +92,18 @@ var AudioAnalyser = class extends import_eventemitter3.default {
101
92
  }
102
93
  };
103
94
 
104
- // src/audio/stopStream.ts
95
+ // src/audio/utils/audioContext.ts
96
+ var audioContext = new (window.AudioContext || window.webkitAudioContext)();
97
+
98
+ // src/audio/utils/localStorage.ts
99
+ var LocalStorageKeys = /* @__PURE__ */ ((LocalStorageKeys2) => {
100
+ LocalStorageKeys2["MicDevice"] = "micdrop.micDevice";
101
+ LocalStorageKeys2["SpeakerDevice"] = "micdrop.speakerDevice";
102
+ LocalStorageKeys2["MicThreshold"] = "micdrop.micThreshold";
103
+ return LocalStorageKeys2;
104
+ })(LocalStorageKeys || {});
105
+
106
+ // src/audio/utils/stopStream.ts
105
107
  function stopStream(stream) {
106
108
  stream?.getTracks().forEach((track) => track.stop());
107
109
  }
@@ -109,21 +111,9 @@ function stopStream(stream) {
109
111
  // src/audio/microphone.ts
110
112
  var currentDeviceId;
111
113
  var audioStream;
112
- var audioContext;
113
114
  var sourceNode;
114
- var micAnalyser;
115
+ var micAnalyser = new AudioAnalyser(audioContext);
115
116
  var defaultMicThreshold = -50;
116
- function init() {
117
- if (audioContext) return;
118
- audioContext = new (window.AudioContext || window.webkitAudioContext)();
119
- micAnalyser = new AudioAnalyser(audioContext);
120
- }
121
- init();
122
- document.addEventListener("click", () => {
123
- if (audioContext?.state === "suspended") {
124
- audioContext?.resume();
125
- }
126
- });
127
117
  function getMicConstraints(deviceId) {
128
118
  return {
129
119
  audio: {
@@ -178,7 +168,7 @@ function stopMicrophone() {
178
168
  }
179
169
  }
180
170
 
181
- // src/audio/createDelayedStream.ts
171
+ // src/audio/utils/createDelayedStream.ts
182
172
  function createDelayedStream(stream, delay) {
183
173
  if (!audioContext) {
184
174
  throw new Error("AudioContext not initialized");
@@ -209,8 +199,12 @@ var MicRecorder = class extends import_eventemitter32.default {
209
199
  this.queuedChunks = [];
210
200
  this.onStartSpeaking = (async () => {
211
201
  if (!this.recorder || this.state.isMuted) return;
212
- this.recorder.start(timeSlice);
213
- this.queuedChunks.length = 0;
202
+ try {
203
+ this.recorder.start(timeSlice);
204
+ this.queuedChunks.length = 0;
205
+ } catch (error) {
206
+ console.error(error);
207
+ }
214
208
  }).bind(this);
215
209
  this.onConfirmSpeaking = (async () => {
216
210
  if (this.state.isMuted) return;
@@ -220,12 +214,20 @@ var MicRecorder = class extends import_eventemitter32.default {
220
214
  }).bind(this);
221
215
  this.onCancelSpeaking = (async () => {
222
216
  if (!this.recorder) return;
223
- this.recorder.stop();
217
+ try {
218
+ this.recorder.stop();
219
+ } catch (error) {
220
+ console.error(error);
221
+ }
224
222
  this.queuedChunks.length = 0;
225
223
  }).bind(this);
226
224
  this.onStopSpeaking = (async () => {
227
225
  if (!this.recorder) return;
228
- this.recorder.stop();
226
+ try {
227
+ this.recorder.stop();
228
+ } catch (error) {
229
+ console.error(error);
230
+ }
229
231
  this.changeState({ isSpeaking: false });
230
232
  this.emit("StopSpeaking");
231
233
  this.speakingConfirmed = false;
@@ -353,6 +355,9 @@ var speaker_exports = {};
353
355
  __export(speaker_exports, {
354
356
  canChangeSpeakerDevice: () => canChangeSpeakerDevice,
355
357
  changeSpeakerDevice: () => changeSpeakerDevice,
358
+ disableStreaming: () => disableStreaming,
359
+ enableStreaming: () => enableStreaming,
360
+ isStreamingEnabled: () => isStreamingEnabled,
356
361
  pauseAudio: () => pauseAudio,
357
362
  playAudio: () => playAudio,
358
363
  resumeAudio: () => resumeAudio,
@@ -360,14 +365,269 @@ __export(speaker_exports, {
360
365
  stopAudio: () => stopAudio
361
366
  });
362
367
 
363
- // src/audio/unlock.ts
364
- function unlock(audioContext3) {
368
+ // src/audio/player/SpeakerPlayer.ts
369
+ var SpeakerPlayer = class {
370
+ constructor(outputNode) {
371
+ this.outputNode = outputNode;
372
+ const ctx = outputNode.context;
373
+ if (!(ctx instanceof AudioContext)) {
374
+ throw new Error("OutputNode must be connected to an AudioContext");
375
+ }
376
+ this.audioContext = ctx;
377
+ }
378
+ };
379
+
380
+ // src/audio/player/SpeakerConcatPlayer.ts
381
+ var SpeakerConcatPlayer = class extends SpeakerPlayer {
382
+ constructor() {
383
+ super(...arguments);
384
+ this.pendingBlobs = [];
385
+ this.prevSourceDuration = 0;
386
+ this.isConvertingBlob = false;
387
+ }
388
+ async setup() {
389
+ }
390
+ replaceSource() {
391
+ if (!this.nextSource) return;
392
+ const time = performance.now() / 1e3;
393
+ const source = this.nextSource;
394
+ source.connect(this.outputNode);
395
+ source.onended = () => {
396
+ if (source !== this.currentSource) return;
397
+ this.prevSourceDuration = source.buffer?.duration ?? 0;
398
+ this.sourceStartTime = void 0;
399
+ this.currentSource = void 0;
400
+ source.disconnect();
401
+ this.replaceSource();
402
+ };
403
+ const offset = this.prevSourceDuration + (this.sourceStartTime ? time - this.sourceStartTime : 0);
404
+ source.start(0, offset);
405
+ if (!this.sourceStartTime) {
406
+ this.sourceStartTime = time;
407
+ }
408
+ this.currentSource?.stop();
409
+ this.currentSource?.disconnect();
410
+ this.currentSource = source;
411
+ this.nextSource = void 0;
412
+ }
413
+ async convertNextBlob() {
414
+ if (this.pendingBlobs.length === 0 || this.isConvertingBlob) return;
415
+ this.isConvertingBlob = true;
416
+ const blob = this.pendingBlobs.shift();
417
+ try {
418
+ const arrayBuffer = await blob.arrayBuffer();
419
+ this.currentBuffer = this.currentBuffer ? this.concatArrayBuffers(this.currentBuffer, arrayBuffer) : arrayBuffer;
420
+ const audioBuffer = await this.audioContext.decodeAudioData(
421
+ // Copy the buffer to avoid mutating the original
422
+ this.copyArrayBuffer(this.currentBuffer)
423
+ );
424
+ const source = this.audioContext.createBufferSource();
425
+ source.buffer = audioBuffer;
426
+ this.nextSource = source;
427
+ this.replaceSource();
428
+ } catch (error) {
429
+ console.error("Error converting blob", error);
430
+ } finally {
431
+ this.isConvertingBlob = false;
432
+ }
433
+ this.convertNextBlob();
434
+ }
435
+ concatArrayBuffers(buffer1, buffer2) {
436
+ const tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
437
+ tmp.set(new Uint8Array(buffer1), 0);
438
+ tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
439
+ return tmp.buffer;
440
+ }
441
+ copyArrayBuffer(buffer) {
442
+ const destination = new ArrayBuffer(buffer.byteLength);
443
+ new Uint8Array(destination).set(new Uint8Array(buffer));
444
+ return destination;
445
+ }
446
+ addBlob(blob) {
447
+ this.pendingBlobs.push(blob);
448
+ this.convertNextBlob();
449
+ }
450
+ stop() {
451
+ this.pendingBlobs.length = 0;
452
+ this.sourceStartTime = void 0;
453
+ this.prevSourceDuration = 0;
454
+ this.currentSource?.stop();
455
+ this.currentSource?.disconnect();
456
+ this.currentSource = void 0;
457
+ this.nextSource = void 0;
458
+ this.currentBuffer = void 0;
459
+ this.isConvertingBlob = false;
460
+ }
461
+ destroy() {
462
+ this.stop();
463
+ }
464
+ };
465
+
466
+ // src/audio/player/SpeakerMediaSourcePlayer.ts
467
+ var SpeakerMediaSourcePlayer = class extends SpeakerPlayer {
468
+ constructor() {
469
+ super(...arguments);
470
+ this.isProcessingQueue = false;
471
+ this.pendingBlobs = [];
472
+ }
473
+ async setup() {
474
+ if (this.audioElement || this.mediaSource) return;
475
+ this.audioElement = new Audio();
476
+ this.audioElement.autoplay = true;
477
+ this.audioElement.muted = true;
478
+ this.mediaSource = new MediaSource();
479
+ if (!this.mediaSource) {
480
+ throw new Error("Failed to create MediaSource");
481
+ }
482
+ this.audioElement.src = URL.createObjectURL(this.mediaSource);
483
+ await new Promise((resolve) => {
484
+ this.mediaSource?.addEventListener("sourceopen", () => {
485
+ const sourceBuffer = this.mediaSource?.addSourceBuffer("audio/mpeg");
486
+ if (!sourceBuffer) throw new Error("Failed to create SourceBuffer");
487
+ this.sourceBuffer = sourceBuffer;
488
+ sourceBuffer.addEventListener("updateend", () => this.processQueue());
489
+ this.processQueue();
490
+ resolve();
491
+ });
492
+ });
493
+ await new Promise((resolve) => {
494
+ const onTimeUpdate = () => {
495
+ this.audioElement?.removeEventListener("timeupdate", onTimeUpdate);
496
+ if (!this.audioElement) {
497
+ throw new Error("Audio element or audio context not found");
498
+ }
499
+ const mediaStream = this.audioElement.captureStream();
500
+ if (mediaStream.getAudioTracks().length === 0) return;
501
+ this.mediaStream = mediaStream;
502
+ this.sourceNode = this.audioContext.createMediaStreamSource(mediaStream);
503
+ this.sourceNode.connect(this.outputNode);
504
+ this.audioElement.currentTime = 0;
505
+ resolve();
506
+ };
507
+ this.audioElement?.addEventListener("timeupdate", onTimeUpdate);
508
+ });
509
+ }
510
+ async processQueue() {
511
+ if (!this.sourceBuffer || !this.mediaSource || this.pendingBlobs.length === 0 || this.isProcessingQueue || this.sourceBuffer.updating) {
512
+ return;
513
+ }
514
+ this.isProcessingQueue = true;
515
+ const blob = this.pendingBlobs.shift();
516
+ try {
517
+ const arrayBuffer = await blob.arrayBuffer();
518
+ if (this.isProcessingQueue) {
519
+ this.sourceBuffer.appendBuffer(arrayBuffer);
520
+ }
521
+ } catch (error) {
522
+ console.error("Error appending buffer", error);
523
+ } finally {
524
+ this.isProcessingQueue = false;
525
+ }
526
+ }
527
+ addBlob(blob) {
528
+ this.pendingBlobs.push(blob);
529
+ if (this.sourceBuffer && !this.sourceBuffer.updating && !this.isProcessingQueue) {
530
+ this.processQueue();
531
+ }
532
+ }
533
+ stop() {
534
+ this.destroy();
535
+ this.setup();
536
+ }
537
+ destroy() {
538
+ if (this.sourceNode) {
539
+ this.sourceNode.disconnect();
540
+ this.sourceNode = void 0;
541
+ }
542
+ this.pendingBlobs.length = 0;
543
+ if (this.mediaSource) {
544
+ try {
545
+ if (this.mediaSource.readyState === "open") {
546
+ this.mediaSource.endOfStream();
547
+ }
548
+ } catch (error) {
549
+ console.error("Error cleaning up media source", error);
550
+ }
551
+ }
552
+ if (this.audioElement) {
553
+ this.audioElement.pause();
554
+ this.audioElement.src = "";
555
+ this.audioElement = void 0;
556
+ }
557
+ if (this.mediaStream) {
558
+ this.mediaStream.getTracks().forEach((track) => track.stop());
559
+ this.mediaStream = void 0;
560
+ }
561
+ this.mediaSource = void 0;
562
+ this.sourceBuffer = void 0;
563
+ }
564
+ };
565
+ SpeakerMediaSourcePlayer.isCompatible = typeof MediaSource !== "undefined" && MediaSource.isTypeSupported("audio/mpeg");
566
+
567
+ // src/audio/player/SpeakerSequencePlayer.ts
568
+ var SpeakerSequencePlayer = class extends SpeakerPlayer {
569
+ constructor() {
570
+ super(...arguments);
571
+ this.pendingBlobs = [];
572
+ this.pendingSources = [];
573
+ this.isConvertingBlob = false;
574
+ }
575
+ async setup() {
576
+ }
577
+ processQueue() {
578
+ if (this.pendingSources.length === 0 || this.currentSource) return;
579
+ const source = this.pendingSources.shift();
580
+ this.currentSource = source;
581
+ source.connect(this.outputNode);
582
+ source.onended = () => {
583
+ source.disconnect();
584
+ this.currentSource = void 0;
585
+ this.processQueue();
586
+ };
587
+ source.start(0);
588
+ }
589
+ async convertNextBlob() {
590
+ if (this.pendingBlobs.length === 0 || this.isConvertingBlob) return;
591
+ this.isConvertingBlob = true;
592
+ const blob = this.pendingBlobs.shift();
593
+ try {
594
+ const arrayBuffer = await blob.arrayBuffer();
595
+ const audioBuffer = await this.audioContext.decodeAudioData(arrayBuffer);
596
+ const source = this.audioContext.createBufferSource();
597
+ source.buffer = audioBuffer;
598
+ this.pendingSources.push(source);
599
+ this.processQueue();
600
+ } catch (error) {
601
+ console.error("Error converting blob", error);
602
+ } finally {
603
+ this.isConvertingBlob = false;
604
+ }
605
+ this.convertNextBlob();
606
+ }
607
+ addBlob(blob) {
608
+ this.pendingBlobs.push(blob);
609
+ this.convertNextBlob();
610
+ }
611
+ stop() {
612
+ this.pendingBlobs.length = 0;
613
+ this.pendingSources.length = 0;
614
+ this.currentSource?.stop();
615
+ this.currentSource?.disconnect();
616
+ this.currentSource = void 0;
617
+ }
618
+ destroy() {
619
+ this.stop();
620
+ }
621
+ };
622
+
623
+ // src/audio/utils/unlock.ts
624
+ function unlock(audioContext2) {
365
625
  const events = ["touchstart", "touchend", "mousedown", "keydown"];
366
626
  events.forEach((e) => document.body.addEventListener(e, unlock2, false));
367
627
  async function unlock2() {
368
- unlockSafari(audioContext3);
369
- if (audioContext3.state === "suspended") {
370
- await audioContext3.resume();
628
+ unlockSafari(audioContext2);
629
+ if (audioContext2.state === "suspended") {
630
+ await audioContext2.resume();
371
631
  }
372
632
  clean();
373
633
  }
@@ -375,10 +635,10 @@ function unlock(audioContext3) {
375
635
  events.forEach((e) => document.body.removeEventListener(e, unlock2));
376
636
  }
377
637
  }
378
- function unlockSafari(audioContext3) {
379
- const source = audioContext3.createBufferSource();
380
- source.buffer = audioContext3.createBuffer(1, 1, 22050);
381
- source.connect(audioContext3.destination);
638
+ function unlockSafari(audioContext2) {
639
+ const source = audioContext2.createBufferSource();
640
+ source.buffer = audioContext2.createBuffer(1, 1, 22050);
641
+ source.connect(audioContext2.destination);
382
642
  const onEnded = () => {
383
643
  source.disconnect();
384
644
  source.buffer = null;
@@ -390,107 +650,49 @@ function unlockSafari(audioContext3) {
390
650
 
391
651
  // src/audio/speaker.ts
392
652
  var audioElement;
393
- var audioContext2;
394
- var mediaSource;
395
- var sourceBuffer;
396
- var currentMediaStream;
397
- var isProcessingQueue = false;
398
- var pendingBlobs = [];
399
- var speakerAnalyser;
400
- function init2() {
401
- if (audioContext2) return;
402
- audioContext2 = new (window.AudioContext || window.webkitAudioContext)();
403
- speakerAnalyser = new AudioAnalyser(audioContext2);
404
- unlock(audioContext2);
405
- }
406
- init2();
407
- async function setupAudioElement(speakerId) {
408
- if (!audioContext2 || !speakerAnalyser) return;
409
- if (!speakerId) {
410
- speakerId = localStorage.getItem("micdrop.speakerDevice" /* SpeakerDevice */) || void 0;
411
- }
412
- if (!audioElement) {
413
- audioElement = new Audio();
414
- audioElement.autoplay = true;
415
- mediaSource = new MediaSource();
416
- if (!mediaSource) {
417
- throw new Error("Failed to create MediaSource");
418
- }
419
- audioElement.src = URL.createObjectURL(mediaSource);
420
- const ms = mediaSource;
421
- ms.addEventListener("sourceopen", () => {
422
- sourceBuffer = ms.addSourceBuffer("audio/mpeg");
423
- sourceBuffer.addEventListener("updateend", () => {
424
- processQueue();
425
- });
426
- if (pendingBlobs.length > 0) {
427
- processQueue();
428
- }
429
- });
430
- audioElement.addEventListener("timeupdate", function onTimeUpdate() {
431
- if (!audioContext2 || !speakerAnalyser || !audioElement) return;
432
- try {
433
- const mediaStream = audioElement.captureStream();
434
- if (mediaStream.getAudioTracks().length === 0) return;
435
- currentMediaStream = mediaStream;
436
- const sourceNode2 = audioContext2.createMediaStreamSource(mediaStream);
437
- sourceNode2.connect(speakerAnalyser.node);
438
- audioElement.removeEventListener("timeupdate", onTimeUpdate);
439
- } catch (error) {
440
- console.error("Error creating media stream source:", error);
441
- }
442
- });
443
- }
653
+ var player;
654
+ var streamingEnabled = false;
655
+ var speakerAnalyser = new AudioAnalyser(audioContext);
656
+ async function init() {
657
+ unlock(audioContext);
658
+ audioElement = new Audio();
659
+ audioElement.autoplay = true;
660
+ const speakerId = localStorage.getItem("micdrop.speakerDevice" /* SpeakerDevice */);
444
661
  if (speakerId) {
445
662
  try {
446
663
  await setSinkId(speakerId);
447
664
  } catch (error) {
448
- console.error(`Error setting Audio Output to ${speakerId}`, error);
665
+ console.error("Failed to set sink ID:", error);
449
666
  }
450
667
  }
451
- return audioElement;
668
+ await setupSpeakerPlayer();
669
+ const destinationNode = audioContext.createMediaStreamDestination();
670
+ speakerAnalyser.node.connect(destinationNode);
671
+ audioElement.srcObject = destinationNode.stream;
452
672
  }
453
- async function processQueue() {
454
- if (!sourceBuffer || !mediaSource || pendingBlobs.length === 0) return;
455
- if (isProcessingQueue || sourceBuffer.updating) return;
456
- isProcessingQueue = true;
457
- try {
458
- await playNextBlob();
459
- } finally {
460
- isProcessingQueue = false;
461
- if (pendingBlobs.length > 0 && !sourceBuffer.updating) {
462
- processQueue();
463
- }
464
- }
465
- }
466
- async function playNextBlob() {
467
- if (!sourceBuffer || !mediaSource || pendingBlobs.length === 0) return;
468
- if (sourceBuffer.updating) return;
469
- if (mediaSource.readyState !== "open") {
470
- console.warn("MediaSource is no longer open, resetting audio pipeline");
471
- stopAudio();
472
- await setupAudioElement();
473
- return;
474
- }
475
- const blob = pendingBlobs.shift();
673
+ init();
674
+ async function setupSpeakerPlayer() {
476
675
  try {
477
- if (sourceBuffer && Array.from(mediaSource.sourceBuffers).includes(sourceBuffer)) {
478
- const arrayBuffer = await blob.arrayBuffer();
479
- sourceBuffer.appendBuffer(arrayBuffer);
480
- } else {
481
- console.warn("SourceBuffer is no longer valid, resetting audio pipeline");
482
- stopAudio();
483
- await setupAudioElement();
484
- }
676
+ player?.destroy();
677
+ player = streamingEnabled ? SpeakerMediaSourcePlayer.isCompatible ? new SpeakerMediaSourcePlayer(speakerAnalyser.node) : new SpeakerConcatPlayer(speakerAnalyser.node) : new SpeakerSequencePlayer(speakerAnalyser.node);
678
+ await player?.setup();
485
679
  } catch (error) {
486
- console.error("Error appending buffer", error);
487
- if (error instanceof Error && error.name === "InvalidStateError") {
488
- console.warn("Resetting audio pipeline due to invalid state");
489
- stopAudio();
490
- await setupAudioElement();
491
- }
680
+ console.error("Error setting up speaker player", error);
492
681
  }
493
682
  }
683
+ async function isStreamingEnabled() {
684
+ return streamingEnabled;
685
+ }
686
+ async function enableStreaming() {
687
+ if (streamingEnabled) return;
688
+ streamingEnabled = true;
689
+ await setupSpeakerPlayer();
690
+ }
691
+ async function disableStreaming() {
692
+ if (!streamingEnabled) return;
693
+ streamingEnabled = false;
694
+ await setupSpeakerPlayer();
695
+ }
494
696
  function canChangeSpeakerDevice() {
495
697
  return window.HTMLMediaElement && "sinkId" in window.HTMLMediaElement.prototype;
496
698
  }
@@ -502,7 +704,7 @@ async function setSinkId(speakerId) {
502
704
  async function changeSpeakerDevice(speakerId) {
503
705
  if (!canChangeSpeakerDevice()) return;
504
706
  try {
505
- await setupAudioElement(speakerId);
707
+ await setSinkId(speakerId);
506
708
  localStorage.setItem("micdrop.speakerDevice" /* SpeakerDevice */, speakerId);
507
709
  } catch (error) {
508
710
  console.error(`Error setting Audio Output to ${speakerId}`, error);
@@ -519,48 +721,19 @@ function resumeAudio() {
519
721
  }
520
722
  }
521
723
  async function playAudio(blob) {
522
- if (!audioContext2 || !speakerAnalyser) return;
523
- pendingBlobs.push(blob);
524
- if (!audioElement) {
525
- await setupAudioElement();
526
- }
527
- if (!audioElement) return;
724
+ if (!audioElement || !player) return;
528
725
  try {
726
+ player.addBlob(blob);
529
727
  if (audioElement.paused) {
530
728
  audioElement.play();
531
729
  }
532
- if (sourceBuffer && !sourceBuffer.updating && !isProcessingQueue) {
533
- processQueue();
534
- }
535
730
  } catch (error) {
536
731
  console.error("Error playing audio blob", error, blob);
537
732
  }
538
733
  }
539
734
  function stopAudio() {
540
- pendingBlobs.length = 0;
541
- if (mediaSource) {
542
- try {
543
- if (mediaSource.readyState === "open") {
544
- mediaSource.endOfStream();
545
- }
546
- if (sourceBuffer && mediaSource.readyState !== "closed") {
547
- mediaSource.removeSourceBuffer(sourceBuffer);
548
- }
549
- } catch (error) {
550
- console.error("Error cleaning up media source", error);
551
- }
552
- mediaSource = void 0;
553
- sourceBuffer = void 0;
554
- }
555
- if (audioElement) {
556
- audioElement.pause();
557
- audioElement.src = "";
558
- audioElement = void 0;
559
- }
560
- if (currentMediaStream) {
561
- currentMediaStream.getTracks().forEach((track) => track.stop());
562
- currentMediaStream = void 0;
563
- }
735
+ audioElement?.pause();
736
+ player?.stop();
564
737
  }
565
738
 
566
739
  // src/audio/vad/SileroVAD.ts
@@ -655,9 +828,8 @@ var VolumeVAD = class extends VAD {
655
828
  if (threshold !== void 0) {
656
829
  this.threshold = threshold;
657
830
  }
658
- this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
659
- this.analyser = this.audioContext.createAnalyser();
660
- this.sourceNode = this.audioContext.createMediaStreamSource(stream);
831
+ this.analyser = audioContext.createAnalyser();
832
+ this.sourceNode = audioContext.createMediaStreamSource(stream);
661
833
  this.analyser.fftSize = 512;
662
834
  this.analyser.smoothingTimeConstant = 0.1;
663
835
  this.fftBins = new Float32Array(this.analyser.frequencyBinCount);
@@ -715,10 +887,6 @@ var VolumeVAD = class extends VAD {
715
887
  this.analyser.disconnect();
716
888
  this.analyser = void 0;
717
889
  }
718
- if (this.audioContext) {
719
- await this.audioContext.close();
720
- this.audioContext = void 0;
721
- }
722
890
  this.fftBins = void 0;
723
891
  }
724
892
  async setThreshold(threshold) {
@@ -903,6 +1071,8 @@ var CallHandler = class _CallHandler extends import_eventemitter34.default {
903
1071
  this.conversation = this.conversation.slice(0, -1);
904
1072
  this.notifyStateChange();
905
1073
  }
1074
+ } else if (event.data === "enableSpeakerStreaming" /* EnableSpeakerStreaming */) {
1075
+ enableStreaming();
906
1076
  }
907
1077
  };
908
1078
  this.ws.onclose = (event) => {
@@ -953,6 +1123,7 @@ var CallClientCommands = /* @__PURE__ */ ((CallClientCommands2) => {
953
1123
  var CallServerCommands = /* @__PURE__ */ ((CallServerCommands2) => {
954
1124
  CallServerCommands2["Message"] = "message";
955
1125
  CallServerCommands2["CancelLastAssistantMessage"] = "cancelLastAssistantMessage";
1126
+ CallServerCommands2["EnableSpeakerStreaming"] = "enableSpeakerStreaming";
956
1127
  CallServerCommands2["EndInterview"] = "endInterview";
957
1128
  return CallServerCommands2;
958
1129
  })(CallServerCommands || {});