From baaa38547a60be34367e3d228b47cb14027f027a Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Tue, 6 Sep 2022 17:40:48 +0800 Subject: [PATCH] Create buffer for the audio data --- packages/text-to-speech/src/index.ts | 7 +++++-- packages/text-to-speech/src/textToSpeech.ts | 9 ++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/text-to-speech/src/index.ts b/packages/text-to-speech/src/index.ts index 07233a0c8..9a9c93c31 100644 --- a/packages/text-to-speech/src/index.ts +++ b/packages/text-to-speech/src/index.ts @@ -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) { diff --git a/packages/text-to-speech/src/textToSpeech.ts b/packages/text-to-speech/src/textToSpeech.ts index db39e77b8..fa406d3b8 100644 --- a/packages/text-to-speech/src/textToSpeech.ts +++ b/packages/text-to-speech/src/textToSpeech.ts @@ -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, } }