Create buffer for the audio data

This commit is contained in:
Hongbo Wu 2022-09-06 17:40:48 +08:00
parent d4dcec75d5
commit baaa38547a
2 changed files with 11 additions and 5 deletions

View file

@ -170,9 +170,12 @@ export const textToSpeechStreamingHandler = Sentry.GCPFunction.wrapHttpFunction(
...utteranceInput,
textType: 'utterance',
}
const { audioStream, speechMarks } = await synthesizeTextToSpeech(input)
const { audioData, speechMarks } = await synthesizeTextToSpeech(input)
if (!audioData) {
return res.status(500).send({ errorCode: 'SYNTHESIZER_ERROR' })
}
res.send({
audioData: audioStream.read().toString('hex'),
audioData: audioData.toString('hex'),
speechMarks,
})
} catch (e) {

View file

@ -21,7 +21,7 @@ export interface TextToSpeechInput {
}
export interface TextToSpeechOutput {
audioStream: NodeJS.ReadWriteStream
audioData?: Buffer
speechMarks: SpeechMark[]
}
@ -146,7 +146,11 @@ export const synthesizeTextToSpeech = async (
} else {
// assemble ssml
const ssml = `${startSsml(null, ssmlOptions)}${input.text}${endSsml()}`
await speakSsmlAsyncPromise(ssml)
const result = await speakSsmlAsyncPromise(ssml)
return {
audioData: Buffer.from(result.audioData),
speechMarks,
}
}
} catch (error) {
console.error('synthesis error', error)
@ -159,7 +163,6 @@ export const synthesizeTextToSpeech = async (
}
return {
audioStream,
speechMarks,
}
}