Demo Fixes 17

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

View File

@@ -720,8 +720,20 @@
} }
function convertToFloat32(arrayBuffer) { function convertToFloat32(arrayBuffer) {
try {
// 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;
// If we had to adjust the length, create a new buffer with even length
let bufferToProcess = arrayBuffer;
if (bytesArray.length !== evenLength) {
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) // Get raw audio data as Int16 (common format for audio)
const int16Array = new Int16Array(arrayBuffer); const int16Array = new Int16Array(bufferToProcess);
// Convert to Float32 (normalize between -1 and 1) // Convert to Float32 (normalize between -1 and 1)
const float32Array = new Float32Array(int16Array.length); const float32Array = new Float32Array(int16Array.length);
@@ -730,6 +742,11 @@
} }
return float32Array; 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);
}
} }
function addMessage(sender, text, textOnly = false) { function addMessage(sender, text, textOnly = false) {