-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudioManager.ts
More file actions
277 lines (233 loc) · 7.63 KB
/
Copy pathaudioManager.ts
File metadata and controls
277 lines (233 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// --- AUDIO WORKLET FOR VAD ---
// This code runs in the AudioWorkletGlobalScope
// --- ENHANCED AUDIO WORKLET ---
// Includes error handling, overflow protection, and adaptive buffering
export const AUDIO_WORKLET_CODE = `
class ZenAudioProcessor extends AudioWorkletProcessor {
constructor() {
super();
this.bufferSize = 2048;
this.buffer = new Float32Array(this.bufferSize);
this.bufferIndex = 0;
this.overflowCount = 0;
this.maxOverflow = 10;
this.lastProcessTime = 0;
}
process(inputs, outputs, parameters) {
const input = inputs[0];
const channel0 = input?.[0];
if (!channel0 || channel0.length === 0) {
return true;
}
// Overflow protection
const currentTime = currentTime || 0;
if (currentTime - this.lastProcessTime > 100) {
this.overflowCount++;
if (this.overflowCount > this.maxOverflow) {
this.port.postMessage({ type: 'error', message: 'Audio overflow detected' });
this.reset();
}
}
this.lastProcessTime = currentTime;
// Process audio samples
for (let i = 0; i < channel0.length; i++) {
this.buffer[this.bufferIndex++] = channel0[i];
// Buffer full handling
if (this.bufferIndex >= this.bufferSize) {
try {
this.port.postMessage({
type: 'input_data',
buffer: this.buffer.slice()
});
} catch (error) {
this.port.postMessage({ type: 'error', message: 'Buffer send failed' });
}
this.bufferIndex = 0;
this.overflowCount = 0;
}
}
return true;
}
reset() {
this.bufferIndex = 0;
this.overflowCount = 0;
this.buffer.fill(0);
}
}
registerProcessor('zen-audio-processor', ZenAudioProcessor);
`;
// --- RESAMPLER ---
/**
* Resamples audio buffer using Windowed Sinc (Lanczos) Interpolation.
* SOTA 2026 Standard for high-fidelity audio resampling (prevents aliasing).
*/
export const resampleAudio = (audioBuffer: Float32Array, fromSampleRate: number, toSampleRate: number): Float32Array => {
if (fromSampleRate === toSampleRate) return audioBuffer;
const ratio = fromSampleRate / toSampleRate;
const newLength = Math.round(audioBuffer.length / ratio);
const result = new Float32Array(newLength);
const width = 3; // Lanczos window size (a=3 for high quality)
const sinc = (x: number) => {
if (x === 0) return 1;
const piX = Math.PI * x;
return Math.sin(piX) / piX;
};
const lanczos = (x: number) => {
if (Math.abs(x) >= width) return 0;
return sinc(x) * sinc(x / width);
};
for (let i = 0; i < newLength; i++) {
const center = i * ratio;
const start = Math.ceil(center - width);
const end = Math.floor(center + width);
let sum = 0;
let weightSum = 0;
for (let j = start; j <= end; j++) {
if (j >= 0 && j < audioBuffer.length) {
const weight = lanczos(center - j);
sum += audioBuffer[j] * weight;
weightSum += weight; // Optional normalization
}
}
// Normalization prevents amplitude loss
result[i] = weightSum !== 0 ? sum / weightSum : sum;
}
return result;
};
// --- AUDIO HELPERS ---
/**
* Utility to convert Float32 to 16-bit PCM for Gemini
*/
export const floatTo16BitPCM = (float32Array: Float32Array): ArrayBuffer => {
const buffer = new ArrayBuffer(float32Array.length * 2);
const view = new DataView(buffer);
for (let i = 0; i < float32Array.length; i++) {
let s = Math.max(-1, Math.min(1, float32Array[i]));
view.setInt16(i * 2, s < 0 ? s * 0x8000 : s * 0x7FFF, true);
}
return buffer;
};
export const base64EncodeAudio = (float32Array: Float32Array): string => {
const pcm = floatTo16BitPCM(float32Array);
let binary = '';
const bytes = new Uint8Array(pcm);
const len = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary);
};
// --- ROBUST VOICE ACTIVITY DETECTION ---
// Energy-based VAD with adaptive thresholding and noise floor estimation
export class RobustVoiceDetector {
private sampleRate: number;
private energyThreshold: number;
private noiseFloor: number;
private adaptationRate: number;
private voiceFrames: number = 0;
private silenceFrames: number = 0;
private readonly VOICE_THRESHOLD_FRAMES = 3;
private readonly SILENCE_THRESHOLD_FRAMES = 10;
private readonly MIN_ENERGY_THRESHOLD = 0.01;
private readonly MAX_ENERGY_THRESHOLD = 0.5;
// DSP Filter State (High-pass at 300Hz to kill rumble noise)
private a1 = 0;
private x1 = 0;
private y1 = 0;
constructor(sampleRate: number) {
this.sampleRate = sampleRate;
this.energyThreshold = 0.05; // Initial threshold
this.noiseFloor = 0.001;
this.adaptationRate = 0.1;
// Calculate high-pass filter coefficients for 300Hz cutoff
// This eliminates motorcycle/AC rumble common in VN environments
const rc = 1.0 / (300 * 2 * Math.PI);
const dt = 1.0 / sampleRate;
this.a1 = rc / (rc + dt);
}
/**
* Process audio frame and detect voice activity
* Uses energy-based detection with adaptive threshold
*/
process(audioData: Float32Array): boolean {
if (!audioData || audioData.length === 0) return false;
// Calculate RMS energy
const energy = this.calculateRMS(audioData);
// Update noise floor estimation (slow adaptation)
if (energy < this.energyThreshold) {
this.noiseFloor = this.noiseFloor * 0.99 + energy * 0.01;
}
// Adaptive threshold adjustment
this.adaptThreshold(energy);
// Voice activity detection with hysteresis
const isVoiceActive = energy > this.energyThreshold;
if (isVoiceActive) {
this.voiceFrames++;
this.silenceFrames = 0;
} else {
this.silenceFrames++;
if (this.silenceFrames > this.SILENCE_THRESHOLD_FRAMES) {
this.voiceFrames = 0;
}
}
// Require consecutive voice frames to trigger detection
return this.voiceFrames >= this.VOICE_THRESHOLD_FRAMES;
}
private calculateRMS(audioData: Float32Array): number {
let sum = 0;
// Apply high-pass filter to each sample before RMS calculation
for (let i = 0; i < audioData.length; i++) {
const filtered = this.applyHighPass(audioData[i]);
sum += filtered * filtered;
}
return Math.sqrt(sum / audioData.length);
}
/**
* High-pass filter at 300Hz to eliminate rumble noise
* Filter equation: y[i] = α * (y[i-1] + x[i] - x[i-1])
*/
private applyHighPass(sample: number): number {
const y = this.a1 * (this.y1 + sample - this.x1);
this.x1 = sample;
this.y1 = y;
return y;
}
private adaptThreshold(currentEnergy: number): void {
// Slowly adapt threshold based on recent audio levels
if (currentEnergy > this.energyThreshold) {
// Voice detected - slowly increase threshold
this.energyThreshold += this.adaptationRate * 0.01;
} else {
// Silence detected - slowly decrease threshold
this.energyThreshold -= this.adaptationRate * 0.005;
}
// Keep threshold in reasonable bounds
this.energyThreshold = Math.max(
this.MIN_ENERGY_THRESHOLD,
Math.min(this.MAX_ENERGY_THRESHOLD, this.energyThreshold)
);
// Ensure threshold is above noise floor
this.energyThreshold = Math.max(this.energyThreshold, this.noiseFloor * 3);
}
/**
* Reset detector state
*/
reset(): void {
this.voiceFrames = 0;
this.silenceFrames = 0;
this.energyThreshold = 0.05;
this.noiseFloor = 0.001;
}
/**
* Get current threshold for debugging
*/
getThreshold(): number {
return this.energyThreshold;
}
/**
* Get noise floor estimation
*/
getNoiseFloor(): number {
return this.noiseFloor;
}
}