Not sure why this happens. But I get this weird pause and clicking sometimes (maybe 3% of the time) when I try to convert my text to speech around ellipses, esp. phrases like "...you know..." etc.
Can you give me some feedback on my code and tell me if I'm doing anything wrong?
// Add 'container: none' to prevent header issues that cause distorted/creepy sounds
// This prevents header information from being misinterpreted as audio data
const response = await deepgram.speak.request(
{ text: processedText },
{
model: voiceModel,
container: 'none' // Prevent header misinterpretation that can cause creepy sounds
}
);
const stream = await response.getStream();
const chunks: Uint8Array[] = [];
const reader = stream.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
chunks.push(value);
}
// Combine all chunks into a single Uint8Array
const rawAudioData = chunks.reduce(
(acc, chunk) => {
const newArray = new Uint8Array(acc.length + chunk.length);
newArray.set(acc);
newArray.set(chunk, acc.length);
return newArray;
},
new Uint8Array(0)
);
// Create a proper WAV container for the audio data
// Since we requested container: 'none', we need to manually add WAV headers
// for proper playback in browsers and other audio players
const audioWithHeader = addWavHeader(rawAudioData);
await logToAudit(medplum, taskId, 'Audio generation complete', {
rawAudioSize: rawAudioData.length,
finalAudioSize: audioWithHeader.length,
wasTruncated,
usingContainer: 'WAV (manually added)'
});
return audioWithHeader;
} catch (error) {
await logToAudit(medplum, taskId, 'Error generating audio', {
error: (error as Error).message
});
throw new Error(Failed to generate audio: ${(error as Error).message});
}
}