Spaces:
Sleeping
Sleeping
File size: 14,252 Bytes
16a84ba |
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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 |
import {
LitElement,
html,
} from 'https://cdn.jsdelivr.net/gh/lit/dist@3/core/lit-core.min.js';
// TODO: Update to use Audio Worklet instead createScriptProcessor which is deprecated.
class AudioRecorder extends LitElement {
static properties = {
dataEvent: {type: String},
stateChangeEvent: {type: String},
state: {type: String},
isRecording: {type: Boolean},
debugBuffer: {state: true},
debug: {type: Boolean},
voiceDetectionEnabled: {type: Boolean},
voiceThreshold: {type: Number},
voiceHoldTime: {type: Number},
};
constructor() {
super();
this.debug = false;
this.mediaStream = null;
this.audioContext = null;
this.processor = null;
this.isStreaming = false;
this.isRecording = false;
this.isInitializing = false;
this.sequenceNumber = 0;
this.debugBuffer = [];
this.debugBufferSize = 50;
this.targetSampleRate = 16000;
// Voice detection parameters
//
// The Gemini Live API has voice activity detection (VAD), but this will waste quota
// to the API, so it's more efficient implement VAD on the client as well.
this.voiceDetectionEnabled = true; // Enable by default
this.voiceThreshold = 0.01; // RMS threshold for voice detection
this.voiceHoldTime = 500; // Time to hold voice detection state in ms
this.lastVoiceDetectedTime = 0; // Last time voice was detected
this.isVoiceDetected = false; // Current voice detection state
this.consecutiveSilentFrames = 0; // Counter for silent frames
this.silenceThreshold = 10; // Number of silent frames before cutting off
this.onGeminiLiveStarted = (e) => {
if (this.isRecording) {
this.startStreaming();
}
};
this.onGeminiLiveStopped = (e) => {
this.stop();
};
}
connectedCallback() {
super.connectedCallback();
window.addEventListener(
'gemini-live-api-started',
this.onGeminiLiveStarted,
);
window.addEventListener(
'gemini-live-api-stopped',
this.onGeminiLiveStopped,
);
}
disconnectedCallback() {
super.disconnectedCallback();
this.stop();
window.removeEventListener(
'gemini-live-api-started',
this.onAudioInputReceived,
);
window.removeEventListener(
'gemini-live-api-stopped',
this.onGeminiLiveStopped,
);
}
firstUpdated() {
if (this.state !== 'disabled') {
this.startStreaming();
}
}
isVoiceFrame(audioData) {
// Calculate RMS of the audio frame
let sumSquares = 0;
for (let i = 0; i < audioData.length; i++) {
sumSquares += audioData[i] * audioData[i];
}
const rms = Math.sqrt(sumSquares / audioData.length);
const now = Date.now();
// Check if we detect voice in this frame
if (rms > this.voiceThreshold) {
this.lastVoiceDetectedTime = now;
this.consecutiveSilentFrames = 0;
this.isVoiceDetected = true;
return true;
}
// Check if we're still within the hold time
if (now - this.lastVoiceDetectedTime < this.voiceHoldTime) {
return true;
}
// Increment silent frames counter
this.consecutiveSilentFrames++;
// If we've seen enough silent frames, mark as silent
if (this.consecutiveSilentFrames > this.silenceThreshold) {
this.isVoiceDetected = false;
}
return this.isVoiceDetected;
}
async startStreaming() {
if (this.state === 'disabled') {
this.dispatchEvent(new MesopEvent(this.stateChangeEvent, 'initializing'));
}
this.isInitializing = true;
const initialized = await this.initialize();
this.isInitializing = false;
if (initialized) {
this.isRecording = true;
this.dispatchEvent(new MesopEvent(this.stateChangeEvent, 'recording'));
this.start();
}
}
async initialize() {
try {
// First check what sample rates are supported with echo cancellation
const testStream = await navigator.mediaDevices.getUserMedia({
audio: {
echoCancellation: true,
noiseSuppression: true,
autoGainControl: true,
},
video: false,
});
// Get the actual sample rate from the system
const systemTrack = testStream.getAudioTracks()[0];
const settings = systemTrack.getSettings();
this.log('System audio settings:', settings);
// Clean up the test stream
testStream.getTracks().forEach((track) => track.stop());
// Now create the real stream using the system's capabilities
this.mediaStream = await navigator.mediaDevices.getUserMedia({
audio: {
channelCount: 1,
sampleRate: settings.sampleRate,
echoCancellation: true,
noiseSuppression: true,
autoGainControl: true,
echoCancellationType: 'system',
latency: 0,
},
video: false,
});
// Log the actual constraints that were applied
const audioTrack = this.mediaStream.getAudioTracks()[0];
const actualConstraints = audioTrack.getSettings();
this.log('Applied audio constraints:', actualConstraints);
// Set up audio context matching the system rate
this.audioContext = new AudioContext({
sampleRate: settings.sampleRate,
});
this.log(
'AudioContext created with sample rate:',
this.audioContext.sampleRate,
);
const micSource = this.audioContext.createMediaStreamSource(
this.mediaStream,
);
this.processor = this.audioContext.createScriptProcessor(4096, 1, 1);
// Connect the audio nodes
micSource.connect(this.processor);
this.processor.connect(this.audioContext.destination);
return true;
} catch (error) {
this.error('Error initializing audio streamer:', error);
return false;
}
}
downsampleBuffer(buffer, originalSampleRate) {
if (originalSampleRate === this.targetSampleRate) {
return buffer;
}
const ratio = originalSampleRate / this.targetSampleRate;
const newLength = Math.floor(buffer.length / ratio);
const result = new Float32Array(newLength);
for (let i = 0; i < newLength; i++) {
const startIndex = Math.floor(i * ratio);
const endIndex = Math.floor((i + 1) * ratio);
let sum = 0;
let count = 0;
for (let j = startIndex; j < endIndex && j < buffer.length; j++) {
sum += buffer[j];
count++;
}
result[i] = count > 0 ? sum / count : 0;
}
this.log('Downsampling details:', {
originalRate: originalSampleRate,
targetRate: this.targetSampleRate,
originalLength: buffer.length,
newLength: result.length,
actualRatio: buffer.length / result.length,
});
return result;
}
addAudioDebugger(sourceNode, label) {
if (!this.debug) return;
const analyser = this.audioContext.createAnalyser();
analyser.fftSize = 2048;
sourceNode.connect(analyser);
const bufferLength = analyser.frequencyBinCount;
const dataArray = new Float32Array(bufferLength);
this.debugInterval = setInterval(() => {
if (!this.isStreaming) return;
analyser.getFloatTimeDomainData(dataArray);
let rms = 0;
for (let i = 0; i < bufferLength; i++) {
rms += dataArray[i] * dataArray[i];
}
rms = Math.sqrt(rms / bufferLength);
this.log(`${label} RMS Level: ${rms.toFixed(6)}`);
}, 1000);
}
start() {
this.isStreaming = true;
this.debugBuffer = [];
this.lastVoiceDetectedTime = 0;
this.isVoiceDetected = false;
this.consecutiveSilentFrames = 0;
this.processor.onaudioprocess = (event) => {
if (!this.isStreaming) return;
const inputData = event.inputBuffer.getChannelData(0);
const originalSampleRate = event.inputBuffer.sampleRate;
// Log initial processing details if needed
if (this.sequenceNumber === 0) {
this.log('Audio Processing Details:', {
bufferSize: this.processor.bufferSize,
inputChannels: this.processor.numberOfInputs,
outputChannels: this.processor.numberOfOutputs,
originalSampleRate: originalSampleRate,
targetSampleRate: this.targetSampleRate,
length: inputData.length,
timestamp: event.timeStamp,
});
}
// Check for voice activity if enabled
if (this.voiceDetectionEnabled && !this.isVoiceFrame(inputData)) {
// Skip this frame if no voice is detected
this.sequenceNumber++; // Still increment to maintain sequence
return;
}
const downsampledData = this.downsampleBuffer(
inputData,
originalSampleRate,
);
const processedData = new Float32Array(downsampledData.length);
const gain = 5.0;
for (let i = 0; i < downsampledData.length; i++) {
processedData[i] = downsampledData[i] * gain;
}
// Debug logging
if (this.sequenceNumber % 50 === 0 && this.debug) {
const stats = {
originalLength: inputData.length,
downsampledLength: downsampledData.length,
maxValue: Math.max(...processedData),
minValue: Math.min(...processedData),
originalSampleRate,
targetSampleRate: this.targetSampleRate,
isVoiceDetected: this.isVoiceDetected,
};
this.log('Audio buffer stats:', stats);
}
// Store in debug buffer
this.debugBuffer.push(processedData);
if (this.debugBuffer.length > this.debugBufferSize) {
this.debugBuffer.shift();
}
// Audio level monitoring
let rms = 0;
for (let i = 0; i < processedData.length; i++) {
rms += processedData[i] * processedData[i];
}
rms = Math.sqrt(rms / processedData.length);
if (this.sequenceNumber % 10 === 0 && this.debug) {
this.log(
`Audio Level (RMS): ${rms.toFixed(4)}, Voice Detected: ${
this.isVoiceDetected
}`,
);
if (rms < 0.0001) {
this.warn(
'Warning: Very low audio level detected. Check if microphone is working.',
);
}
}
// Convert to Int16Array for transmission
const intData = new Int16Array(processedData.length);
for (let i = 0; i < processedData.length; i++) {
intData[i] = Math.max(
-32768,
Math.min(32767, processedData[i] * 32768),
);
if (this.sequenceNumber % 100 === 0 && i < 10 && this.debug) {
this.log(
`Sample ${i}: Float=${processedData[i].toFixed(4)}, Int16=${
intData[i]
}`,
);
}
}
// Convert to base64 and dispatch
const bytes = new Uint8Array(intData.buffer);
const base64Data = btoa(
Array.from(bytes)
.map((byte) => String.fromCharCode(byte))
.join(''),
);
this.dispatchEvent(
new MesopEvent(this.dataEvent, {
sequence: this.sequenceNumber++,
sampleRate: this.targetSampleRate,
data: base64Data,
isVoice: this.isVoiceDetected,
}),
);
this.dispatchEvent(
new CustomEvent('audio-input-received', {
detail: {data: base64Data},
// Allow event to cross shadow DOM boundaries (both need to be true)
bubbles: true,
composed: true,
}),
);
};
return true;
}
stop() {
this.isStreaming = false;
this.isRecording = false;
this.dispatchEvent(new MesopEvent(this.stateChangeEvent, 'disabled'));
if (this.debugInterval) {
clearInterval(this.debugInterval);
}
if (this.processor) {
this.processor.onaudioprocess = null;
}
if (this.mediaStream) {
this.mediaStream.getTracks().forEach((track) => track.stop());
}
if (this.audioContext) {
this.audioContext.close();
}
}
async playbackDebug() {
if (!this.debugBuffer.length) {
this.log('No audio data available for playback');
return;
}
const playbackContext = new AudioContext();
const systemSampleRate = playbackContext.sampleRate;
const totalSamples16k =
this.debugBuffer.length * this.debugBuffer[0].length;
const upsampledLength = Math.round(
totalSamples16k * (systemSampleRate / this.targetSampleRate),
);
const audioBuffer = playbackContext.createBuffer(
1,
upsampledLength,
systemSampleRate,
);
const channelData = audioBuffer.getChannelData(0);
const combined16kBuffer = new Float32Array(totalSamples16k);
let offset = 0;
for (let i = 0; i < this.debugBuffer.length; i++) {
combined16kBuffer.set(this.debugBuffer[i], offset);
offset += this.debugBuffer[i].length;
}
const ratio = this.targetSampleRate / systemSampleRate;
for (let i = 0; i < upsampledLength; i++) {
const position = i * ratio;
const index = Math.floor(position);
const decimal = position - index;
const sample1 = combined16kBuffer[index] || 0;
const sample2 = combined16kBuffer[index + 1] || sample1;
channelData[i] = sample1 + decimal * (sample2 - sample1);
}
const source = playbackContext.createBufferSource();
source.buffer = audioBuffer;
source.connect(playbackContext.destination);
source.start();
this.log('Playing debug audio at system rate...', {
systemSampleRate,
originalLength: totalSamples16k,
upsampledLength,
});
source.onended = () => {
this.log('Debug playback finished');
playbackContext.close();
};
}
render() {
if (this.isInitializing) {
return html`<span><slot></slot></span>`;
}
if (this.isRecording) {
return html`<span @click="${this.stop}"><slot></slot></span> `;
}
return html`<span @click="${this.startStreaming}"><slot></slot></span>`;
}
log(...args) {
if (this.debug) {
console.log(...args);
}
}
warn(...args) {
if (this.debug) {
console.warn(...args);
}
}
error(...args) {
if (this.debug) {
console.error(...args);
}
}
}
customElements.define('audio-recorder', AudioRecorder);
|