Spaces:
Running
Running
Delete SpeechChunks.js
Browse files- SpeechChunks.js +0 -126
SpeechChunks.js
DELETED
@@ -1,126 +0,0 @@
|
|
1 |
-
import MicrophoneAudio from './MicrophoneAudio.js';
|
2 |
-
import { VadDetector } from './VoiceActivityDetector.js';
|
3 |
-
|
4 |
-
export class SpeechChunks {
|
5 |
-
static SAMPLE_RATE = 16000;
|
6 |
-
static START_THRESHOLD = 0.6;
|
7 |
-
static END_THRESHOLD = 0.45;
|
8 |
-
static MIN_SILENCE_DURATION_MS = 600;
|
9 |
-
static SPEECH_PAD_MS = 500;
|
10 |
-
static WINDOW_SIZE_SAMPLES = 512;
|
11 |
-
|
12 |
-
constructor(onSpeechStart, onSpeechEnd) {
|
13 |
-
this.chunks = [];
|
14 |
-
this.isSpeechActive = false;
|
15 |
-
this.onSpeechStart = onSpeechStart;
|
16 |
-
this.onSpeechEnd = onSpeechEnd;
|
17 |
-
console.log('SpeechChunks initialized');
|
18 |
-
}
|
19 |
-
|
20 |
-
async initialize() {
|
21 |
-
this.microphoneAudio = new MicrophoneAudio({
|
22 |
-
sampleRate: SpeechChunks.SAMPLE_RATE,
|
23 |
-
windowSizeSamples: SpeechChunks.WINDOW_SIZE_SAMPLES,
|
24 |
-
onAudioData: this.processAudioData.bind(this)
|
25 |
-
});
|
26 |
-
|
27 |
-
this.vadDetector = new VadDetector(
|
28 |
-
SpeechChunks.START_THRESHOLD,
|
29 |
-
SpeechChunks.END_THRESHOLD,
|
30 |
-
SpeechChunks.SAMPLE_RATE,
|
31 |
-
SpeechChunks.MIN_SILENCE_DURATION_MS,
|
32 |
-
SpeechChunks.SPEECH_PAD_MS
|
33 |
-
);
|
34 |
-
}
|
35 |
-
|
36 |
-
async processAudioData(audioData) {
|
37 |
-
console.log(`Processing audio data of length ${audioData.length}`);
|
38 |
-
try {
|
39 |
-
const result = await this.vadDetector.apply(audioData, false);
|
40 |
-
if (result.start !== undefined) {
|
41 |
-
this.isSpeechActive = true;
|
42 |
-
console.log('Speech start detected');
|
43 |
-
this.onSpeechStart();
|
44 |
-
} else if (result.end !== undefined) {
|
45 |
-
this.isSpeechActive = false;
|
46 |
-
console.log('Speech end detected');
|
47 |
-
this.onSpeechEnd(this.getBlob());
|
48 |
-
}
|
49 |
-
if (this.isSpeechActive) {
|
50 |
-
console.log('Adding chunk to speech');
|
51 |
-
this.chunks.push(Array.from(audioData));
|
52 |
-
}
|
53 |
-
} catch (error) {
|
54 |
-
console.error('Error processing audio data', error);
|
55 |
-
}
|
56 |
-
}
|
57 |
-
|
58 |
-
async start() {
|
59 |
-
console.log('Starting SpeechChunks');
|
60 |
-
await this.initialize();
|
61 |
-
await this.microphoneAudio.start();
|
62 |
-
}
|
63 |
-
|
64 |
-
stop() {
|
65 |
-
console.log('Stopping SpeechChunks');
|
66 |
-
this.microphoneAudio.stop();
|
67 |
-
this.vadDetector.reset();
|
68 |
-
this.isSpeechActive = false;
|
69 |
-
}
|
70 |
-
|
71 |
-
getSpeechChunks() {
|
72 |
-
console.log(`Returning ${this.chunks.length} speech chunks`);
|
73 |
-
const speechChunks = this.chunks;
|
74 |
-
this.chunks = [];
|
75 |
-
return speechChunks;
|
76 |
-
}
|
77 |
-
|
78 |
-
getBlob() {
|
79 |
-
console.log('Creating audio blob from speech chunks');
|
80 |
-
const combinedChunks = this.chunks.flat();
|
81 |
-
const combinedAudio = new Float32Array(combinedChunks);
|
82 |
-
|
83 |
-
const intData = new Int16Array(combinedAudio.length);
|
84 |
-
for (let i = 0; i < combinedAudio.length; i++) {
|
85 |
-
const s = Math.max(-1, Math.min(1, combinedAudio[i]));
|
86 |
-
intData[i] = s < 0 ? s * 0x8000 : s * 0x7FFF;
|
87 |
-
}
|
88 |
-
|
89 |
-
const buffer = new ArrayBuffer(44 + intData.length * 2);
|
90 |
-
const view = new DataView(buffer);
|
91 |
-
|
92 |
-
this.writeString(view, 0, 'RIFF');
|
93 |
-
view.setUint32(4, 36 + intData.length * 2, true);
|
94 |
-
this.writeString(view, 8, 'WAVE');
|
95 |
-
this.writeString(view, 12, 'fmt ');
|
96 |
-
view.setUint32(16, 16, true);
|
97 |
-
view.setUint16(20, 1, true);
|
98 |
-
view.setUint16(22, 1, true);
|
99 |
-
view.setUint32(24, SpeechChunks.SAMPLE_RATE, true);
|
100 |
-
view.setUint32(28, SpeechChunks.SAMPLE_RATE * 2, true);
|
101 |
-
view.setUint16(32, 2, true);
|
102 |
-
view.setUint16(34, 16, true);
|
103 |
-
this.writeString(view, 36, 'data');
|
104 |
-
view.setUint32(40, intData.length * 2, true);
|
105 |
-
|
106 |
-
for (let i = 0; i < intData.length; i++) {
|
107 |
-
view.setInt16(44 + i * 2, intData[i], true);
|
108 |
-
}
|
109 |
-
|
110 |
-
const blob = new Blob([buffer], { type: 'audio/wav' });
|
111 |
-
console.log(`Created blob of size ${blob.size} bytes`);
|
112 |
-
return blob;
|
113 |
-
}
|
114 |
-
|
115 |
-
writeString(view, offset, string) {
|
116 |
-
for (let i = 0; i < string.length; i++) {
|
117 |
-
view.setUint8(offset + i, string.charCodeAt(i));
|
118 |
-
}
|
119 |
-
}
|
120 |
-
|
121 |
-
async close() {
|
122 |
-
console.log('Closing SpeechChunks');
|
123 |
-
this.stop();
|
124 |
-
await this.vadDetector.close();
|
125 |
-
}
|
126 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|