@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.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");
@@ -353,6 +343,9 @@ var speaker_exports = {};
353
343
  __export(speaker_exports, {
354
344
  canChangeSpeakerDevice: () => canChangeSpeakerDevice,
355
345
  changeSpeakerDevice: () => changeSpeakerDevice,
346
+ disableStreaming: () => disableStreaming,
347
+ enableStreaming: () => enableStreaming,
348
+ isStreamingEnabled: () => isStreamingEnabled,
356
349
  pauseAudio: () => pauseAudio,
357
350
  playAudio: () => playAudio,
358
351
  resumeAudio: () => resumeAudio,
@@ -360,14 +353,269 @@ __export(speaker_exports, {
360
353
  stopAudio: () => stopAudio
361
354
  });
362
355
 
363
- // src/audio/unlock.ts
364
- function unlock(audioContext3) {
356
+ // src/audio/player/SpeakerPlayer.ts
357
+ var SpeakerPlayer = class {
358
+ constructor(outputNode) {
359
+ this.outputNode = outputNode;
360
+ const ctx = outputNode.context;
361
+ if (!(ctx instanceof AudioContext)) {
362
+ throw new Error("OutputNode must be connected to an AudioContext");
363
+ }
364
+ this.audioContext = ctx;
365
+ }
366
+ };
367
+
368
+ // src/audio/player/SpeakerConcatPlayer.ts
369
+ var SpeakerConcatPlayer = class extends SpeakerPlayer {
370
+ constructor() {
371
+ super(...arguments);
372
+ this.pendingBlobs = [];
373
+ this.prevSourceDuration = 0;
374
+ this.isConvertingBlob = false;
375
+ }
376
+ async setup() {
377
+ }
378
+ replaceSource() {
379
+ if (!this.nextSource) return;
380
+ const time = performance.now() / 1e3;
381
+ const source = this.nextSource;
382
+ source.connect(this.outputNode);
383
+ source.onended = () => {
384
+ if (source !== this.currentSource) return;
385
+ this.prevSourceDuration = source.buffer?.duration ?? 0;
386
+ this.sourceStartTime = void 0;
387
+ this.currentSource = void 0;
388
+ source.disconnect();
389
+ this.replaceSource();
390
+ };
391
+ const offset = this.prevSourceDuration + (this.sourceStartTime ? time - this.sourceStartTime : 0);
392
+ source.start(0, offset);
393
+ if (!this.sourceStartTime) {
394
+ this.sourceStartTime = time;
395
+ }
396
+ this.currentSource?.stop();
397
+ this.currentSource?.disconnect();
398
+ this.currentSource = source;
399
+ this.nextSource = void 0;
400
+ }
401
+ async convertNextBlob() {
402
+ if (this.pendingBlobs.length === 0 || this.isConvertingBlob) return;
403
+ this.isConvertingBlob = true;
404
+ const blob = this.pendingBlobs.shift();
405
+ try {
406
+ const arrayBuffer = await blob.arrayBuffer();
407
+ this.currentBuffer = this.currentBuffer ? this.concatArrayBuffers(this.currentBuffer, arrayBuffer) : arrayBuffer;
408
+ const audioBuffer = await this.audioContext.decodeAudioData(
409
+ // Copy the buffer to avoid mutating the original
410
+ this.copyArrayBuffer(this.currentBuffer)
411
+ );
412
+ const source = this.audioContext.createBufferSource();
413
+ source.buffer = audioBuffer;
414
+ this.nextSource = source;
415
+ this.replaceSource();
416
+ } catch (error) {
417
+ console.error("Error converting blob", error);
418
+ } finally {
419
+ this.isConvertingBlob = false;
420
+ }
421
+ this.convertNextBlob();
422
+ }
423
+ concatArrayBuffers(buffer1, buffer2) {
424
+ const tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
425
+ tmp.set(new Uint8Array(buffer1), 0);
426
+ tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
427
+ return tmp.buffer;
428
+ }
429
+ copyArrayBuffer(buffer) {
430
+ const destination = new ArrayBuffer(buffer.byteLength);
431
+ new Uint8Array(destination).set(new Uint8Array(buffer));
432
+ return destination;
433
+ }
434
+ addBlob(blob) {
435
+ this.pendingBlobs.push(blob);
436
+ this.convertNextBlob();
437
+ }
438
+ stop() {
439
+ this.pendingBlobs.length = 0;
440
+ this.sourceStartTime = void 0;
441
+ this.prevSourceDuration = 0;
442
+ this.currentSource?.stop();
443
+ this.currentSource?.disconnect();
444
+ this.currentSource = void 0;
445
+ this.nextSource = void 0;
446
+ this.currentBuffer = void 0;
447
+ this.isConvertingBlob = false;
448
+ }
449
+ destroy() {
450
+ this.stop();
451
+ }
452
+ };
453
+
454
+ // src/audio/player/SpeakerMediaSourcePlayer.ts
455
+ var SpeakerMediaSourcePlayer = class extends SpeakerPlayer {
456
+ constructor() {
457
+ super(...arguments);
458
+ this.isProcessingQueue = false;
459
+ this.pendingBlobs = [];
460
+ }
461
+ async setup() {
462
+ if (this.audioElement || this.mediaSource) return;
463
+ this.audioElement = new Audio();
464
+ this.audioElement.autoplay = true;
465
+ this.audioElement.muted = true;
466
+ this.mediaSource = new MediaSource();
467
+ if (!this.mediaSource) {
468
+ throw new Error("Failed to create MediaSource");
469
+ }
470
+ this.audioElement.src = URL.createObjectURL(this.mediaSource);
471
+ await new Promise((resolve) => {
472
+ this.mediaSource?.addEventListener("sourceopen", () => {
473
+ const sourceBuffer = this.mediaSource?.addSourceBuffer("audio/mpeg");
474
+ if (!sourceBuffer) throw new Error("Failed to create SourceBuffer");
475
+ this.sourceBuffer = sourceBuffer;
476
+ sourceBuffer.addEventListener("updateend", () => this.processQueue());
477
+ this.processQueue();
478
+ resolve();
479
+ });
480
+ });
481
+ await new Promise((resolve) => {
482
+ const onTimeUpdate = () => {
483
+ this.audioElement?.removeEventListener("timeupdate", onTimeUpdate);
484
+ if (!this.audioElement) {
485
+ throw new Error("Audio element or audio context not found");
486
+ }
487
+ const mediaStream = this.audioElement.captureStream();
488
+ if (mediaStream.getAudioTracks().length === 0) return;
489
+ this.mediaStream = mediaStream;
490
+ this.sourceNode = this.audioContext.createMediaStreamSource(mediaStream);
491
+ this.sourceNode.connect(this.outputNode);
492
+ this.audioElement.currentTime = 0;
493
+ resolve();
494
+ };
495
+ this.audioElement?.addEventListener("timeupdate", onTimeUpdate);
496
+ });
497
+ }
498
+ async processQueue() {
499
+ if (!this.sourceBuffer || !this.mediaSource || this.pendingBlobs.length === 0 || this.isProcessingQueue || this.sourceBuffer.updating) {
500
+ return;
501
+ }
502
+ this.isProcessingQueue = true;
503
+ const blob = this.pendingBlobs.shift();
504
+ try {
505
+ const arrayBuffer = await blob.arrayBuffer();
506
+ if (this.isProcessingQueue) {
507
+ this.sourceBuffer.appendBuffer(arrayBuffer);
508
+ }
509
+ } catch (error) {
510
+ console.error("Error appending buffer", error);
511
+ } finally {
512
+ this.isProcessingQueue = false;
513
+ }
514
+ }
515
+ addBlob(blob) {
516
+ this.pendingBlobs.push(blob);
517
+ if (this.sourceBuffer && !this.sourceBuffer.updating && !this.isProcessingQueue) {
518
+ this.processQueue();
519
+ }
520
+ }
521
+ stop() {
522
+ this.destroy();
523
+ this.setup();
524
+ }
525
+ destroy() {
526
+ if (this.sourceNode) {
527
+ this.sourceNode.disconnect();
528
+ this.sourceNode = void 0;
529
+ }
530
+ this.pendingBlobs.length = 0;
531
+ if (this.mediaSource) {
532
+ try {
533
+ if (this.mediaSource.readyState === "open") {
534
+ this.mediaSource.endOfStream();
535
+ }
536
+ } catch (error) {
537
+ console.error("Error cleaning up media source", error);
538
+ }
539
+ }
540
+ if (this.audioElement) {
541
+ this.audioElement.pause();
542
+ this.audioElement.src = "";
543
+ this.audioElement = void 0;
544
+ }
545
+ if (this.mediaStream) {
546
+ this.mediaStream.getTracks().forEach((track) => track.stop());
547
+ this.mediaStream = void 0;
548
+ }
549
+ this.mediaSource = void 0;
550
+ this.sourceBuffer = void 0;
551
+ }
552
+ };
553
+ SpeakerMediaSourcePlayer.isCompatible = typeof MediaSource !== "undefined" && MediaSource.isTypeSupported("audio/mpeg");
554
+
555
+ // src/audio/player/SpeakerSequencePlayer.ts
556
+ var SpeakerSequencePlayer = class extends SpeakerPlayer {
557
+ constructor() {
558
+ super(...arguments);
559
+ this.pendingBlobs = [];
560
+ this.pendingSources = [];
561
+ this.isConvertingBlob = false;
562
+ }
563
+ async setup() {
564
+ }
565
+ processQueue() {
566
+ if (this.pendingSources.length === 0 || this.currentSource) return;
567
+ const source = this.pendingSources.shift();
568
+ this.currentSource = source;
569
+ source.connect(this.outputNode);
570
+ source.onended = () => {
571
+ source.disconnect();
572
+ this.currentSource = void 0;
573
+ this.processQueue();
574
+ };
575
+ source.start(0);
576
+ }
577
+ async convertNextBlob() {
578
+ if (this.pendingBlobs.length === 0 || this.isConvertingBlob) return;
579
+ this.isConvertingBlob = true;
580
+ const blob = this.pendingBlobs.shift();
581
+ try {
582
+ const arrayBuffer = await blob.arrayBuffer();
583
+ const audioBuffer = await this.audioContext.decodeAudioData(arrayBuffer);
584
+ const source = this.audioContext.createBufferSource();
585
+ source.buffer = audioBuffer;
586
+ this.pendingSources.push(source);
587
+ this.processQueue();
588
+ } catch (error) {
589
+ console.error("Error converting blob", error);
590
+ } finally {
591
+ this.isConvertingBlob = false;
592
+ }
593
+ this.convertNextBlob();
594
+ }
595
+ addBlob(blob) {
596
+ this.pendingBlobs.push(blob);
597
+ this.convertNextBlob();
598
+ }
599
+ stop() {
600
+ this.pendingBlobs.length = 0;
601
+ this.pendingSources.length = 0;
602
+ this.currentSource?.stop();
603
+ this.currentSource?.disconnect();
604
+ this.currentSource = void 0;
605
+ }
606
+ destroy() {
607
+ this.stop();
608
+ }
609
+ };
610
+
611
+ // src/audio/utils/unlock.ts
612
+ function unlock(audioContext2) {
365
613
  const events = ["touchstart", "touchend", "mousedown", "keydown"];
366
614
  events.forEach((e) => document.body.addEventListener(e, unlock2, false));
367
615
  async function unlock2() {
368
- unlockSafari(audioContext3);
369
- if (audioContext3.state === "suspended") {
370
- await audioContext3.resume();
616
+ unlockSafari(audioContext2);
617
+ if (audioContext2.state === "suspended") {
618
+ await audioContext2.resume();
371
619
  }
372
620
  clean();
373
621
  }
@@ -375,10 +623,10 @@ function unlock(audioContext3) {
375
623
  events.forEach((e) => document.body.removeEventListener(e, unlock2));
376
624
  }
377
625
  }
378
- function unlockSafari(audioContext3) {
379
- const source = audioContext3.createBufferSource();
380
- source.buffer = audioContext3.createBuffer(1, 1, 22050);
381
- source.connect(audioContext3.destination);
626
+ function unlockSafari(audioContext2) {
627
+ const source = audioContext2.createBufferSource();
628
+ source.buffer = audioContext2.createBuffer(1, 1, 22050);
629
+ source.connect(audioContext2.destination);
382
630
  const onEnded = () => {
383
631
  source.disconnect();
384
632
  source.buffer = null;
@@ -390,107 +638,49 @@ function unlockSafari(audioContext3) {
390
638
 
391
639
  // src/audio/speaker.ts
392
640
  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
- }
641
+ var player;
642
+ var streamingEnabled = false;
643
+ var speakerAnalyser = new AudioAnalyser(audioContext);
644
+ async function init() {
645
+ unlock(audioContext);
646
+ audioElement = new Audio();
647
+ audioElement.autoplay = true;
648
+ const speakerId = localStorage.getItem("micdrop.speakerDevice" /* SpeakerDevice */);
444
649
  if (speakerId) {
445
650
  try {
446
651
  await setSinkId(speakerId);
447
652
  } catch (error) {
448
- console.error(`Error setting Audio Output to ${speakerId}`, error);
449
- }
450
- }
451
- return audioElement;
452
- }
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();
653
+ console.error("Failed to set sink ID:", error);
463
654
  }
464
655
  }
656
+ await setupSpeakerPlayer();
657
+ const destinationNode = audioContext.createMediaStreamDestination();
658
+ speakerAnalyser.node.connect(destinationNode);
659
+ audioElement.srcObject = destinationNode.stream;
465
660
  }
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();
661
+ init();
662
+ async function setupSpeakerPlayer() {
476
663
  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
- }
664
+ player?.destroy();
665
+ player = streamingEnabled ? SpeakerMediaSourcePlayer.isCompatible ? new SpeakerMediaSourcePlayer(speakerAnalyser.node) : new SpeakerConcatPlayer(speakerAnalyser.node) : new SpeakerSequencePlayer(speakerAnalyser.node);
666
+ await player?.setup();
485
667
  } 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
- }
668
+ console.error("Error setting up speaker player", error);
492
669
  }
493
670
  }
671
+ async function isStreamingEnabled() {
672
+ return streamingEnabled;
673
+ }
674
+ async function enableStreaming() {
675
+ if (streamingEnabled) return;
676
+ streamingEnabled = true;
677
+ await setupSpeakerPlayer();
678
+ }
679
+ async function disableStreaming() {
680
+ if (!streamingEnabled) return;
681
+ streamingEnabled = false;
682
+ await setupSpeakerPlayer();
683
+ }
494
684
  function canChangeSpeakerDevice() {
495
685
  return window.HTMLMediaElement && "sinkId" in window.HTMLMediaElement.prototype;
496
686
  }
@@ -502,7 +692,7 @@ async function setSinkId(speakerId) {
502
692
  async function changeSpeakerDevice(speakerId) {
503
693
  if (!canChangeSpeakerDevice()) return;
504
694
  try {
505
- await setupAudioElement(speakerId);
695
+ await setSinkId(speakerId);
506
696
  localStorage.setItem("micdrop.speakerDevice" /* SpeakerDevice */, speakerId);
507
697
  } catch (error) {
508
698
  console.error(`Error setting Audio Output to ${speakerId}`, error);
@@ -519,48 +709,19 @@ function resumeAudio() {
519
709
  }
520
710
  }
521
711
  async function playAudio(blob) {
522
- if (!audioContext2 || !speakerAnalyser) return;
523
- pendingBlobs.push(blob);
524
- if (!audioElement) {
525
- await setupAudioElement();
526
- }
527
- if (!audioElement) return;
712
+ if (!audioElement || !player) return;
528
713
  try {
714
+ player.addBlob(blob);
529
715
  if (audioElement.paused) {
530
716
  audioElement.play();
531
717
  }
532
- if (sourceBuffer && !sourceBuffer.updating && !isProcessingQueue) {
533
- processQueue();
534
- }
535
718
  } catch (error) {
536
719
  console.error("Error playing audio blob", error, blob);
537
720
  }
538
721
  }
539
722
  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
- }
723
+ audioElement?.pause();
724
+ player?.stop();
564
725
  }
565
726
 
566
727
  // src/audio/vad/SileroVAD.ts
@@ -655,9 +816,8 @@ var VolumeVAD = class extends VAD {
655
816
  if (threshold !== void 0) {
656
817
  this.threshold = threshold;
657
818
  }
658
- this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
659
- this.analyser = this.audioContext.createAnalyser();
660
- this.sourceNode = this.audioContext.createMediaStreamSource(stream);
819
+ this.analyser = audioContext.createAnalyser();
820
+ this.sourceNode = audioContext.createMediaStreamSource(stream);
661
821
  this.analyser.fftSize = 512;
662
822
  this.analyser.smoothingTimeConstant = 0.1;
663
823
  this.fftBins = new Float32Array(this.analyser.frequencyBinCount);
@@ -715,10 +875,6 @@ var VolumeVAD = class extends VAD {
715
875
  this.analyser.disconnect();
716
876
  this.analyser = void 0;
717
877
  }
718
- if (this.audioContext) {
719
- await this.audioContext.close();
720
- this.audioContext = void 0;
721
- }
722
878
  this.fftBins = void 0;
723
879
  }
724
880
  async setThreshold(threshold) {
@@ -903,6 +1059,8 @@ var CallHandler = class _CallHandler extends import_eventemitter34.default {
903
1059
  this.conversation = this.conversation.slice(0, -1);
904
1060
  this.notifyStateChange();
905
1061
  }
1062
+ } else if (event.data === "enableSpeakerStreaming" /* EnableSpeakerStreaming */) {
1063
+ enableStreaming();
906
1064
  }
907
1065
  };
908
1066
  this.ws.onclose = (event) => {
@@ -953,6 +1111,7 @@ var CallClientCommands = /* @__PURE__ */ ((CallClientCommands2) => {
953
1111
  var CallServerCommands = /* @__PURE__ */ ((CallServerCommands2) => {
954
1112
  CallServerCommands2["Message"] = "message";
955
1113
  CallServerCommands2["CancelLastAssistantMessage"] = "cancelLastAssistantMessage";
1114
+ CallServerCommands2["EnableSpeakerStreaming"] = "enableSpeakerStreaming";
956
1115
  CallServerCommands2["EndInterview"] = "endInterview";
957
1116
  return CallServerCommands2;
958
1117
  })(CallServerCommands || {});