We are trying to create a text-to-speech audio file and upload it to an external bucket storage (GCS in our case).
Our problem is to convert the binary data that Azure returns into a Buffer, it seems like the binary data is corrupted because the response gives us "RIFF�~\u0004\u0000WAVEfmt...(continues)" with "������������������\u0000\u0000\u0000\u0000" in the middle, is it a sign of corruption?
That causes the audio file we created to be empty (I guess).
We use NodeJS with JavaScript as a reference. The default API call is made with Axios:
const azureLongAudio = axios.create({
baseURL: DEFAULTPATH + apiVersion,
headers: {
'Ocp-Apim-Subscription-Key': API_KEY,
'content-type': 'application/json',
'X-Microsoft-OutputFormat': 'riff-24khz-16bit-mono-pcm',
'Content-Type': 'application/ssml+xml'
}
});
Then we use the next function to call Azure, save a buffer from the returned binary data, create a WAV file, and upload it to an external bucket:
const createAudioFromText = (text) => {
const version = '1.0';
const language = supportedLanguages[0];
const voiceGender = 'Male';
const voiceName = 'es-ES-EliasNeural';
const textContent = `
<speak version='${version}' xml:lang='${language}'>
<voice xml:lang='${language}' xml:gender='${voiceGender}' name='${voiceName}'>
${text}
</voice>
</speak>`;
return new Promise(async (resolve, reject) => {
const bucket = gcs.getBucket();
const filename = 'test-01.wav';
const file = bucket.file(`recordings/${filename}`);
const { data: audioData } = await azureLongAudio.post('/', textContent).catch((err) => {
reject(err.response);
});
const audioBuffer = Buffer.from(audioData);
const writer = new wav.FileWriter(filename, {
sampleRate: 24000,
channels: 1,
bitDepth: 16,
audioFormat: 1
});
writer.write(audioBuffer);
writer.end();
writer.on('finish', async () => {
const writeStream = file.createWriteStream({
resumable: true,
contentType: 'audio/wav'
});
writer.pipe(writeStream);
writeStream.on('error', (err) => {
console.error('Failed to save into GCS:', err);
reject(err);
});
writeStream.on('finish', () => {
console.log(`Saved in GCS: ${filename}`);
resolve(audioData);
});
});
});
};
I'd appreciate any response.