Demo Fixes 17

This commit is contained in:
2025-03-30 08:45:55 -04:00
parent 8fe35aa064
commit 4a3a087a88

View File

@@ -720,16 +720,33 @@
} }
function convertToFloat32(arrayBuffer) { function convertToFloat32(arrayBuffer) {
// Get raw audio data as Int16 (common format for audio) try {
const int16Array = new Int16Array(arrayBuffer); // Ensure the buffer length is even (multiple of 2 bytes for Int16)
const bytesArray = new Uint8Array(arrayBuffer);
const evenLength = Math.floor(bytesArray.length / 2) * 2;
// Convert to Float32 (normalize between -1 and 1) // If we had to adjust the length, create a new buffer with even length
const float32Array = new Float32Array(int16Array.length); let bufferToProcess = arrayBuffer;
for (let i = 0; i < int16Array.length; i++) { if (bytesArray.length !== evenLength) {
float32Array[i] = int16Array[i] / 32768.0; console.warn(`Adjusting audio buffer from ${bytesArray.length} to ${evenLength} bytes`);
bufferToProcess = bytesArray.slice(0, evenLength).buffer;
}
// Get raw audio data as Int16 (common format for audio)
const int16Array = new Int16Array(bufferToProcess);
// Convert to Float32 (normalize between -1 and 1)
const float32Array = new Float32Array(int16Array.length);
for (let i = 0; i < int16Array.length; i++) {
float32Array[i] = int16Array[i] / 32768.0;
}
return float32Array;
} catch (e) {
console.error('Error converting audio data:', e);
// Return an empty audio buffer to avoid breaking the app
return new Float32Array(0);
} }
return float32Array;
} }
function addMessage(sender, text, textOnly = false) { function addMessage(sender, text, textOnly = false) {