@micdrop/client 2.0.14 → 2.1.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 +3 -9
- package/dist/index.d.ts +3 -9
- package/dist/index.js +42 -92
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +42 -92
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -1
package/dist/index.d.mts
CHANGED
|
@@ -121,14 +121,12 @@ interface MicRecorderEvents {
|
|
|
121
121
|
declare class MicRecorder extends EventEmitter<MicRecorderEvents> {
|
|
122
122
|
state: MicRecorderState;
|
|
123
123
|
vad: VAD;
|
|
124
|
-
private audioContext;
|
|
125
124
|
private source;
|
|
126
|
-
private
|
|
125
|
+
private workletNode;
|
|
127
126
|
private delayedStream;
|
|
127
|
+
private workletUrl;
|
|
128
128
|
private speakingConfirmed;
|
|
129
129
|
private queuedChunks;
|
|
130
|
-
private audioBuffer;
|
|
131
|
-
private chunkTimer;
|
|
132
130
|
constructor(vadConfig?: VADConfig);
|
|
133
131
|
start: (stream: MediaStream) => Promise<void>;
|
|
134
132
|
mute: () => void;
|
|
@@ -138,11 +136,7 @@ declare class MicRecorder extends EventEmitter<MicRecorderEvents> {
|
|
|
138
136
|
private onConfirmSpeaking;
|
|
139
137
|
private onCancelSpeaking;
|
|
140
138
|
private onStopSpeaking;
|
|
141
|
-
private
|
|
142
|
-
private startChunking;
|
|
143
|
-
private stopChunking;
|
|
144
|
-
private processAudioChunk;
|
|
145
|
-
private floatToPCM16;
|
|
139
|
+
private onWorkletMessage;
|
|
146
140
|
private changeState;
|
|
147
141
|
}
|
|
148
142
|
|
package/dist/index.d.ts
CHANGED
|
@@ -121,14 +121,12 @@ interface MicRecorderEvents {
|
|
|
121
121
|
declare class MicRecorder extends EventEmitter<MicRecorderEvents> {
|
|
122
122
|
state: MicRecorderState;
|
|
123
123
|
vad: VAD;
|
|
124
|
-
private audioContext;
|
|
125
124
|
private source;
|
|
126
|
-
private
|
|
125
|
+
private workletNode;
|
|
127
126
|
private delayedStream;
|
|
127
|
+
private workletUrl;
|
|
128
128
|
private speakingConfirmed;
|
|
129
129
|
private queuedChunks;
|
|
130
|
-
private audioBuffer;
|
|
131
|
-
private chunkTimer;
|
|
132
130
|
constructor(vadConfig?: VADConfig);
|
|
133
131
|
start: (stream: MediaStream) => Promise<void>;
|
|
134
132
|
mute: () => void;
|
|
@@ -138,11 +136,7 @@ declare class MicRecorder extends EventEmitter<MicRecorderEvents> {
|
|
|
138
136
|
private onConfirmSpeaking;
|
|
139
137
|
private onCancelSpeaking;
|
|
140
138
|
private onStopSpeaking;
|
|
141
|
-
private
|
|
142
|
-
private startChunking;
|
|
143
|
-
private stopChunking;
|
|
144
|
-
private processAudioChunk;
|
|
145
|
-
private floatToPCM16;
|
|
139
|
+
private onWorkletMessage;
|
|
146
140
|
private changeState;
|
|
147
141
|
}
|
|
148
142
|
|
package/dist/index.js
CHANGED
|
@@ -387,6 +387,9 @@ var Speaker = class extends import_eventemitter33.EventEmitter {
|
|
|
387
387
|
// src/audio/MicRecorder.ts
|
|
388
388
|
var import_eventemitter35 = require("eventemitter3");
|
|
389
389
|
|
|
390
|
+
// raw:./pcm-processor-worklet
|
|
391
|
+
var pcm_processor_worklet_default = "const INTERVAL_MS = 100\n\nclass PCMProcessorWorklet extends AudioWorkletProcessor {\n constructor() {\n super()\n this.audioBuffer = []\n this.isRecording = false\n this.sourceSampleRate = 16000\n this.targetSampleRate = 16000\n this.resamplingRatio = 1\n this.CHUNK_INTERVAL_SAMPLES = Math.round(\n (this.sourceSampleRate * INTERVAL_MS) / 1000\n )\n\n // Listen for messages\n this.port.onmessage = this.handleMessage.bind(this)\n }\n\n handleMessage(event) {\n if (event.data.type === 'configure') {\n this.sourceSampleRate = event.data.sourceSampleRate\n this.targetSampleRate = event.data.targetSampleRate\n this.resamplingRatio = this.sourceSampleRate / this.targetSampleRate\n // Recalculate chunk interval for the source sample rate\n this.CHUNK_INTERVAL_SAMPLES = Math.round(\n (this.sourceSampleRate * INTERVAL_MS) / 1000\n )\n } else if (event.data.type === 'start') {\n this.isRecording = true\n } else if (event.data.type === 'stop') {\n this.isRecording = false\n // Clear any remaining audio buffer when stopping\n this.audioBuffer.length = 0\n }\n }\n\n process(inputs) {\n const input = inputs[0]\n\n if (input && input.length > 0 && this.isRecording) {\n const inputChannel = input[0]\n\n // Store audio data in buffer\n this.audioBuffer.push(new Float32Array(inputChannel))\n\n // Check if we have enough data for a chunk\n const totalSamples = this.audioBuffer.reduce(\n (sum, buffer) => sum + buffer.length,\n 0\n )\n\n if (totalSamples >= this.CHUNK_INTERVAL_SAMPLES) {\n this.processChunk()\n }\n }\n\n return true\n }\n\n processChunk() {\n if (this.audioBuffer.length === 0) return\n\n // Calculate total samples\n const totalSamples = this.audioBuffer.reduce(\n (sum, buffer) => sum + buffer.length,\n 0\n )\n if (totalSamples === 0) return\n\n // Merge all buffers\n const mergedBuffer = new Float32Array(totalSamples)\n let offset = 0\n for (const buffer of this.audioBuffer) {\n mergedBuffer.set(buffer, offset)\n offset += buffer.length\n }\n\n // Resample to target sample rate if necessary\n const resampledBuffer = this.resample(mergedBuffer)\n\n // Convert float32 to int16 PCM\n const pcmData = this.floatToPCM16(resampledBuffer)\n\n // Clear the buffer\n this.audioBuffer.length = 0\n\n // Send PCM chunk to main thread\n this.port.postMessage({\n type: 'chunk',\n data: pcmData,\n })\n }\n\n resample(inputBuffer) {\n // If no resampling needed, return original buffer\n if (this.resamplingRatio === 1) {\n return inputBuffer\n }\n\n // Calculate output length\n const outputLength = Math.round(inputBuffer.length / this.resamplingRatio)\n const outputBuffer = new Float32Array(outputLength)\n\n // Simple linear interpolation resampling\n for (let i = 0; i < outputLength; i++) {\n const srcIndex = i * this.resamplingRatio\n const srcIndexFloor = Math.floor(srcIndex)\n const srcIndexCeil = Math.min(srcIndexFloor + 1, inputBuffer.length - 1)\n const fraction = srcIndex - srcIndexFloor\n\n // Linear interpolation\n outputBuffer[i] =\n inputBuffer[srcIndexFloor] * (1 - fraction) +\n inputBuffer[srcIndexCeil] * fraction\n }\n\n return outputBuffer\n }\n\n floatToPCM16(floatData) {\n const pcmData = new Int16Array(floatData.length)\n for (let i = 0; i < floatData.length; i++) {\n const sample = Math.max(-1, Math.min(1, floatData[i]))\n pcmData[i] = sample < 0 ? sample * 0x8000 : sample * 0x7fff\n }\n return pcmData\n }\n}\n\nregisterProcessor('pcm-processor', PCMProcessorWorklet)\n";
|
|
392
|
+
|
|
390
393
|
// src/audio/utils/delayedStream.ts
|
|
391
394
|
function createDelayedStream(stream, delay) {
|
|
392
395
|
const audioSource = audioContext.createMediaStreamSource(stream);
|
|
@@ -705,8 +708,6 @@ function getVAD(vad) {
|
|
|
705
708
|
|
|
706
709
|
// src/audio/MicRecorder.ts
|
|
707
710
|
var SAMPLE_RATE2 = 16e3;
|
|
708
|
-
var BUFFER_SIZE = 4096;
|
|
709
|
-
var CHUNK_INTERVAL_MS = 100;
|
|
710
711
|
var defaultMicRecorderState = {
|
|
711
712
|
isStarting: false,
|
|
712
713
|
isStarted: false,
|
|
@@ -718,7 +719,6 @@ var MicRecorder = class extends import_eventemitter35.EventEmitter {
|
|
|
718
719
|
super();
|
|
719
720
|
this.speakingConfirmed = false;
|
|
720
721
|
this.queuedChunks = [];
|
|
721
|
-
this.audioBuffer = [];
|
|
722
722
|
this.start = async (stream) => {
|
|
723
723
|
if (this.state.isStarted) {
|
|
724
724
|
throw new Error("MicRecorder is already started");
|
|
@@ -729,21 +729,22 @@ var MicRecorder = class extends import_eventemitter35.EventEmitter {
|
|
|
729
729
|
isMuted: false,
|
|
730
730
|
isSpeaking: false
|
|
731
731
|
});
|
|
732
|
+
this.speakingConfirmed = false;
|
|
732
733
|
const delayedStream = createDelayedStream(
|
|
733
734
|
stream,
|
|
734
735
|
this.vad.delay / 1e3 + 0.05
|
|
735
736
|
);
|
|
736
737
|
this.delayedStream = delayedStream;
|
|
737
|
-
this.
|
|
738
|
-
this.source =
|
|
739
|
-
this.
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
738
|
+
await audioContext.audioWorklet.addModule(this.workletUrl);
|
|
739
|
+
this.source = audioContext.createMediaStreamSource(delayedStream);
|
|
740
|
+
this.workletNode = new AudioWorkletNode(audioContext, "pcm-processor");
|
|
741
|
+
this.workletNode.port.onmessage = this.onWorkletMessage;
|
|
742
|
+
this.source.connect(this.workletNode);
|
|
743
|
+
this.workletNode.port.postMessage({
|
|
744
|
+
type: "configure",
|
|
745
|
+
sourceSampleRate: audioContext.sampleRate,
|
|
746
|
+
targetSampleRate: SAMPLE_RATE2
|
|
747
|
+
});
|
|
747
748
|
await this.vad.start(stream);
|
|
748
749
|
this.vad.on("StartSpeaking", this.onStartSpeaking);
|
|
749
750
|
this.vad.on("ConfirmSpeaking", this.onConfirmSpeaking);
|
|
@@ -788,22 +789,14 @@ var MicRecorder = class extends import_eventemitter35.EventEmitter {
|
|
|
788
789
|
this.vad.off("ConfirmSpeaking", this.onConfirmSpeaking);
|
|
789
790
|
this.vad.off("CancelSpeaking", this.onCancelSpeaking);
|
|
790
791
|
this.vad.off("StopSpeaking", this.onStopSpeaking);
|
|
791
|
-
if (this.
|
|
792
|
-
|
|
793
|
-
this.
|
|
794
|
-
}
|
|
795
|
-
if (this.processor) {
|
|
796
|
-
this.processor.disconnect();
|
|
797
|
-
this.processor = void 0;
|
|
792
|
+
if (this.workletNode) {
|
|
793
|
+
this.workletNode.disconnect();
|
|
794
|
+
this.workletNode = void 0;
|
|
798
795
|
}
|
|
799
796
|
if (this.source) {
|
|
800
797
|
this.source.disconnect();
|
|
801
798
|
this.source = void 0;
|
|
802
799
|
}
|
|
803
|
-
if (this.audioContext) {
|
|
804
|
-
this.audioContext.close();
|
|
805
|
-
this.audioContext = void 0;
|
|
806
|
-
}
|
|
807
800
|
if (this.delayedStream) {
|
|
808
801
|
stopStream(this.delayedStream);
|
|
809
802
|
this.delayedStream = void 0;
|
|
@@ -814,92 +807,49 @@ var MicRecorder = class extends import_eventemitter35.EventEmitter {
|
|
|
814
807
|
};
|
|
815
808
|
this.onStartSpeaking = async () => {
|
|
816
809
|
if (this.state.isMuted) return;
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
this.speakingConfirmed = false;
|
|
821
|
-
} catch (error) {
|
|
822
|
-
console.error(error);
|
|
823
|
-
}
|
|
810
|
+
this.speakingConfirmed = false;
|
|
811
|
+
this.queuedChunks.length = 0;
|
|
812
|
+
this.workletNode?.port.postMessage({ type: "start" });
|
|
824
813
|
};
|
|
825
814
|
this.onConfirmSpeaking = async () => {
|
|
826
815
|
if (this.state.isMuted) return;
|
|
827
|
-
this.emit("StartSpeaking");
|
|
828
|
-
this.changeState({ isSpeaking: true });
|
|
829
816
|
this.speakingConfirmed = true;
|
|
817
|
+
this.changeState({ isSpeaking: true });
|
|
818
|
+
this.emit("StartSpeaking");
|
|
830
819
|
};
|
|
831
820
|
this.onCancelSpeaking = async () => {
|
|
832
821
|
if (this.state.isMuted) return;
|
|
833
|
-
try {
|
|
834
|
-
this.stopChunking();
|
|
835
|
-
} catch (error) {
|
|
836
|
-
console.error(error);
|
|
837
|
-
}
|
|
838
822
|
this.queuedChunks.length = 0;
|
|
823
|
+
this.workletNode?.port.postMessage({ type: "stop" });
|
|
839
824
|
};
|
|
840
825
|
this.onStopSpeaking = async () => {
|
|
841
826
|
if (this.state.isMuted) return;
|
|
842
|
-
|
|
843
|
-
this.stopChunking();
|
|
844
|
-
} catch (error) {
|
|
845
|
-
console.error(error);
|
|
846
|
-
}
|
|
827
|
+
this.speakingConfirmed = false;
|
|
847
828
|
this.changeState({ isSpeaking: false });
|
|
848
829
|
this.emit("StopSpeaking");
|
|
849
|
-
this.
|
|
830
|
+
this.workletNode?.port.postMessage({ type: "stop" });
|
|
850
831
|
};
|
|
851
|
-
this.
|
|
832
|
+
this.onWorkletMessage = (event) => {
|
|
852
833
|
if (this.state.isMuted) return;
|
|
853
|
-
const
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
this.
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
clearInterval(this.chunkTimer);
|
|
866
|
-
this.chunkTimer = void 0;
|
|
867
|
-
}
|
|
868
|
-
};
|
|
869
|
-
this.processAudioChunk = () => {
|
|
870
|
-
if (this.audioBuffer.length === 0) return;
|
|
871
|
-
const totalSamples = this.audioBuffer.reduce(
|
|
872
|
-
(sum, buffer) => sum + buffer.length,
|
|
873
|
-
0
|
|
874
|
-
);
|
|
875
|
-
if (totalSamples === 0) return;
|
|
876
|
-
const mergedBuffer = new Float32Array(totalSamples);
|
|
877
|
-
let offset = 0;
|
|
878
|
-
for (const buffer of this.audioBuffer) {
|
|
879
|
-
mergedBuffer.set(buffer, offset);
|
|
880
|
-
offset += buffer.length;
|
|
881
|
-
}
|
|
882
|
-
const pcmData = this.floatToPCM16(mergedBuffer);
|
|
883
|
-
this.audioBuffer.length = 0;
|
|
884
|
-
if (!this.speakingConfirmed) {
|
|
885
|
-
this.queuedChunks.push(pcmData);
|
|
886
|
-
return;
|
|
887
|
-
}
|
|
888
|
-
for (const chunk of this.queuedChunks) {
|
|
889
|
-
this.emit("Chunk", chunk);
|
|
890
|
-
}
|
|
891
|
-
this.queuedChunks.length = 0;
|
|
892
|
-
this.emit("Chunk", pcmData);
|
|
893
|
-
};
|
|
894
|
-
this.floatToPCM16 = (floatData) => {
|
|
895
|
-
const pcmData = new Int16Array(floatData.length);
|
|
896
|
-
for (let i = 0; i < floatData.length; i++) {
|
|
897
|
-
const sample = Math.max(-1, Math.min(1, floatData[i]));
|
|
898
|
-
pcmData[i] = sample < 0 ? sample * 32768 : sample * 32767;
|
|
834
|
+
const { type, data } = event.data;
|
|
835
|
+
if (type === "chunk") {
|
|
836
|
+
const pcmData = data;
|
|
837
|
+
if (!this.speakingConfirmed) {
|
|
838
|
+
this.queuedChunks.push(pcmData);
|
|
839
|
+
return;
|
|
840
|
+
}
|
|
841
|
+
for (const chunk of this.queuedChunks) {
|
|
842
|
+
this.emit("Chunk", chunk);
|
|
843
|
+
}
|
|
844
|
+
this.queuedChunks.length = 0;
|
|
845
|
+
this.emit("Chunk", pcmData);
|
|
899
846
|
}
|
|
900
|
-
return pcmData;
|
|
901
847
|
};
|
|
902
848
|
this.state = defaultMicRecorderState;
|
|
849
|
+
const workletBlob = new Blob([pcm_processor_worklet_default], {
|
|
850
|
+
type: "application/javascript"
|
|
851
|
+
});
|
|
852
|
+
this.workletUrl = URL.createObjectURL(workletBlob);
|
|
903
853
|
this.vad = getVAD(vadConfig);
|
|
904
854
|
}
|
|
905
855
|
changeState(state) {
|