@micdrop/client 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.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");
@@ -213,6 +203,7 @@ var MicRecorder = class extends import_eventemitter32.default {
213
203
  this.queuedChunks.length = 0;
214
204
  }).bind(this);
215
205
  this.onConfirmSpeaking = (async () => {
206
+ if (this.state.isMuted) return;
216
207
  this.emit("StartSpeaking");
217
208
  this.changeState({ isSpeaking: true });
218
209
  this.speakingConfirmed = true;
@@ -352,6 +343,9 @@ var speaker_exports = {};
352
343
  __export(speaker_exports, {
353
344
  canChangeSpeakerDevice: () => canChangeSpeakerDevice,
354
345
  changeSpeakerDevice: () => changeSpeakerDevice,
346
+ disableStreaming: () => disableStreaming,
347
+ enableStreaming: () => enableStreaming,
348
+ isStreamingEnabled: () => isStreamingEnabled,
355
349
  pauseAudio: () => pauseAudio,
356
350
  playAudio: () => playAudio,
357
351
  resumeAudio: () => resumeAudio,
@@ -359,14 +353,269 @@ __export(speaker_exports, {
359
353
  stopAudio: () => stopAudio
360
354
  });
361
355
 
362
- // src/audio/unlock.ts
363
- 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) {
364
613
  const events = ["touchstart", "touchend", "mousedown", "keydown"];
365
614
  events.forEach((e) => document.body.addEventListener(e, unlock2, false));
366
615
  async function unlock2() {
367
- unlockSafari(audioContext3);
368
- if (audioContext3.state === "suspended") {
369
- await audioContext3.resume();
616
+ unlockSafari(audioContext2);
617
+ if (audioContext2.state === "suspended") {
618
+ await audioContext2.resume();
370
619
  }
371
620
  clean();
372
621
  }
@@ -374,10 +623,10 @@ function unlock(audioContext3) {
374
623
  events.forEach((e) => document.body.removeEventListener(e, unlock2));
375
624
  }
376
625
  }
377
- function unlockSafari(audioContext3) {
378
- const source = audioContext3.createBufferSource();
379
- source.buffer = audioContext3.createBuffer(1, 1, 22050);
380
- 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);
381
630
  const onEnded = () => {
382
631
  source.disconnect();
383
632
  source.buffer = null;
@@ -389,107 +638,49 @@ function unlockSafari(audioContext3) {
389
638
 
390
639
  // src/audio/speaker.ts
391
640
  var audioElement;
392
- var audioContext2;
393
- var mediaSource;
394
- var sourceBuffer;
395
- var currentMediaStream;
396
- var isProcessingQueue = false;
397
- var pendingBlobs = [];
398
- var speakerAnalyser;
399
- function init2() {
400
- if (audioContext2) return;
401
- audioContext2 = new (window.AudioContext || window.webkitAudioContext)();
402
- speakerAnalyser = new AudioAnalyser(audioContext2);
403
- unlock(audioContext2);
404
- }
405
- init2();
406
- async function setupAudioElement(speakerId) {
407
- if (!audioContext2 || !speakerAnalyser) return;
408
- if (!speakerId) {
409
- speakerId = localStorage.getItem("micdrop.speakerDevice" /* SpeakerDevice */) || void 0;
410
- }
411
- if (!audioElement) {
412
- audioElement = new Audio();
413
- audioElement.autoplay = true;
414
- mediaSource = new MediaSource();
415
- if (!mediaSource) {
416
- throw new Error("Failed to create MediaSource");
417
- }
418
- audioElement.src = URL.createObjectURL(mediaSource);
419
- const ms = mediaSource;
420
- ms.addEventListener("sourceopen", () => {
421
- sourceBuffer = ms.addSourceBuffer("audio/mpeg");
422
- sourceBuffer.addEventListener("updateend", () => {
423
- processQueue();
424
- });
425
- if (pendingBlobs.length > 0) {
426
- processQueue();
427
- }
428
- });
429
- audioElement.addEventListener("timeupdate", function onTimeUpdate() {
430
- if (!audioContext2 || !speakerAnalyser || !audioElement) return;
431
- try {
432
- const mediaStream = audioElement.captureStream();
433
- if (mediaStream.getAudioTracks().length === 0) return;
434
- currentMediaStream = mediaStream;
435
- const sourceNode2 = audioContext2.createMediaStreamSource(mediaStream);
436
- sourceNode2.connect(speakerAnalyser.node);
437
- audioElement.removeEventListener("timeupdate", onTimeUpdate);
438
- } catch (error) {
439
- console.error("Error creating media stream source:", error);
440
- }
441
- });
442
- }
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 */);
443
649
  if (speakerId) {
444
650
  try {
445
651
  await setSinkId(speakerId);
446
652
  } catch (error) {
447
- console.error(`Error setting Audio Output to ${speakerId}`, error);
448
- }
449
- }
450
- return audioElement;
451
- }
452
- async function processQueue() {
453
- if (!sourceBuffer || !mediaSource || pendingBlobs.length === 0) return;
454
- if (isProcessingQueue || sourceBuffer.updating) return;
455
- isProcessingQueue = true;
456
- try {
457
- await playNextBlob();
458
- } finally {
459
- isProcessingQueue = false;
460
- if (pendingBlobs.length > 0 && !sourceBuffer.updating) {
461
- processQueue();
653
+ console.error("Failed to set sink ID:", error);
462
654
  }
463
655
  }
656
+ await setupSpeakerPlayer();
657
+ const destinationNode = audioContext.createMediaStreamDestination();
658
+ speakerAnalyser.node.connect(destinationNode);
659
+ audioElement.srcObject = destinationNode.stream;
464
660
  }
465
- async function playNextBlob() {
466
- if (!sourceBuffer || !mediaSource || pendingBlobs.length === 0) return;
467
- if (sourceBuffer.updating) return;
468
- if (mediaSource.readyState !== "open") {
469
- console.warn("MediaSource is no longer open, resetting audio pipeline");
470
- stopAudio();
471
- await setupAudioElement();
472
- return;
473
- }
474
- const blob = pendingBlobs.shift();
661
+ init();
662
+ async function setupSpeakerPlayer() {
475
663
  try {
476
- if (sourceBuffer && Array.from(mediaSource.sourceBuffers).includes(sourceBuffer)) {
477
- const arrayBuffer = await blob.arrayBuffer();
478
- sourceBuffer.appendBuffer(arrayBuffer);
479
- } else {
480
- console.warn("SourceBuffer is no longer valid, resetting audio pipeline");
481
- stopAudio();
482
- await setupAudioElement();
483
- }
664
+ player?.destroy();
665
+ player = streamingEnabled ? SpeakerMediaSourcePlayer.isCompatible ? new SpeakerMediaSourcePlayer(speakerAnalyser.node) : new SpeakerConcatPlayer(speakerAnalyser.node) : new SpeakerSequencePlayer(speakerAnalyser.node);
666
+ await player?.setup();
484
667
  } catch (error) {
485
- console.error("Error appending buffer", error);
486
- if (error instanceof Error && error.name === "InvalidStateError") {
487
- console.warn("Resetting audio pipeline due to invalid state");
488
- stopAudio();
489
- await setupAudioElement();
490
- }
668
+ console.error("Error setting up speaker player", error);
491
669
  }
492
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
+ }
493
684
  function canChangeSpeakerDevice() {
494
685
  return window.HTMLMediaElement && "sinkId" in window.HTMLMediaElement.prototype;
495
686
  }
@@ -501,7 +692,7 @@ async function setSinkId(speakerId) {
501
692
  async function changeSpeakerDevice(speakerId) {
502
693
  if (!canChangeSpeakerDevice()) return;
503
694
  try {
504
- await setupAudioElement(speakerId);
695
+ await setSinkId(speakerId);
505
696
  localStorage.setItem("micdrop.speakerDevice" /* SpeakerDevice */, speakerId);
506
697
  } catch (error) {
507
698
  console.error(`Error setting Audio Output to ${speakerId}`, error);
@@ -518,48 +709,19 @@ function resumeAudio() {
518
709
  }
519
710
  }
520
711
  async function playAudio(blob) {
521
- if (!audioContext2 || !speakerAnalyser) return;
522
- pendingBlobs.push(blob);
523
- if (!audioElement) {
524
- await setupAudioElement();
525
- }
526
- if (!audioElement) return;
712
+ if (!audioElement || !player) return;
527
713
  try {
714
+ player.addBlob(blob);
528
715
  if (audioElement.paused) {
529
716
  audioElement.play();
530
717
  }
531
- if (sourceBuffer && !sourceBuffer.updating && !isProcessingQueue) {
532
- processQueue();
533
- }
534
718
  } catch (error) {
535
719
  console.error("Error playing audio blob", error, blob);
536
720
  }
537
721
  }
538
722
  function stopAudio() {
539
- pendingBlobs.length = 0;
540
- if (mediaSource) {
541
- try {
542
- if (mediaSource.readyState === "open") {
543
- mediaSource.endOfStream();
544
- }
545
- if (sourceBuffer && mediaSource.readyState !== "closed") {
546
- mediaSource.removeSourceBuffer(sourceBuffer);
547
- }
548
- } catch (error) {
549
- console.error("Error cleaning up media source", error);
550
- }
551
- mediaSource = void 0;
552
- sourceBuffer = void 0;
553
- }
554
- if (audioElement) {
555
- audioElement.pause();
556
- audioElement.src = "";
557
- audioElement = void 0;
558
- }
559
- if (currentMediaStream) {
560
- currentMediaStream.getTracks().forEach((track) => track.stop());
561
- currentMediaStream = void 0;
562
- }
723
+ audioElement?.pause();
724
+ player?.stop();
563
725
  }
564
726
 
565
727
  // src/audio/vad/SileroVAD.ts
@@ -654,9 +816,8 @@ var VolumeVAD = class extends VAD {
654
816
  if (threshold !== void 0) {
655
817
  this.threshold = threshold;
656
818
  }
657
- this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
658
- this.analyser = this.audioContext.createAnalyser();
659
- this.sourceNode = this.audioContext.createMediaStreamSource(stream);
819
+ this.analyser = audioContext.createAnalyser();
820
+ this.sourceNode = audioContext.createMediaStreamSource(stream);
660
821
  this.analyser.fftSize = 512;
661
822
  this.analyser.smoothingTimeConstant = 0.1;
662
823
  this.fftBins = new Float32Array(this.analyser.frequencyBinCount);
@@ -714,10 +875,6 @@ var VolumeVAD = class extends VAD {
714
875
  this.analyser.disconnect();
715
876
  this.analyser = void 0;
716
877
  }
717
- if (this.audioContext) {
718
- await this.audioContext.close();
719
- this.audioContext = void 0;
720
- }
721
878
  this.fftBins = void 0;
722
879
  }
723
880
  async setThreshold(threshold) {
@@ -762,6 +919,8 @@ var CallHandler = class _CallHandler extends import_eventemitter34.default {
762
919
  this.micRecorder.on("StartSpeaking", () => {
763
920
  this.log("[Mic] Start speaking");
764
921
  this.ws?.send("startSpeaking" /* StartSpeaking */);
922
+ this._isProcessing = false;
923
+ this.notifyStateChange();
765
924
  stopAudio();
766
925
  });
767
926
  this.micRecorder.on("StopSpeaking", () => {
@@ -900,6 +1059,8 @@ var CallHandler = class _CallHandler extends import_eventemitter34.default {
900
1059
  this.conversation = this.conversation.slice(0, -1);
901
1060
  this.notifyStateChange();
902
1061
  }
1062
+ } else if (event.data === "enableSpeakerStreaming" /* EnableSpeakerStreaming */) {
1063
+ enableStreaming();
903
1064
  }
904
1065
  };
905
1066
  this.ws.onclose = (event) => {
@@ -950,6 +1111,7 @@ var CallClientCommands = /* @__PURE__ */ ((CallClientCommands2) => {
950
1111
  var CallServerCommands = /* @__PURE__ */ ((CallServerCommands2) => {
951
1112
  CallServerCommands2["Message"] = "message";
952
1113
  CallServerCommands2["CancelLastAssistantMessage"] = "cancelLastAssistantMessage";
1114
+ CallServerCommands2["EnableSpeakerStreaming"] = "enableSpeakerStreaming";
953
1115
  CallServerCommands2["EndInterview"] = "endInterview";
954
1116
  return CallServerCommands2;
955
1117
  })(CallServerCommands || {});