From e6a855db43aaf72b05d48e974a38c56dd2c66765 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Tue, 6 Sep 2022 17:51:13 +0800 Subject: [PATCH] Write to audioStream only when uploading file --- packages/text-to-speech/src/textToSpeech.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/text-to-speech/src/textToSpeech.ts b/packages/text-to-speech/src/textToSpeech.ts index fa406d3b8..3bfe1be86 100644 --- a/packages/text-to-speech/src/textToSpeech.ts +++ b/packages/text-to-speech/src/textToSpeech.ts @@ -8,7 +8,6 @@ import { SpeechSynthesizer, } from 'microsoft-cognitiveservices-speech-sdk' import { endSsml, htmlToSsmlItems, ssmlItemText, startSsml } from './htmlToSsml' -import { PassThrough } from 'stream' export interface TextToSpeechInput { text: string @@ -40,7 +39,7 @@ export const synthesizeTextToSpeech = async ( throw new Error('Azure Speech Key or Region not set') } const textType = input.textType || 'html' - const audioStream = input.audioStream || new PassThrough() + const audioStream = input.audioStream const speechConfig = SpeechConfig.fromSubscription( process.env.AZURE_SPEECH_KEY, process.env.AZURE_SPEECH_REGION @@ -55,7 +54,7 @@ export const synthesizeTextToSpeech = async ( synthesizer.synthesizing = function (s, e) { // convert arrayBuffer to stream and write to stream - audioStream.write(Buffer.from(e.result.audioData)) + audioStream?.write(Buffer.from(e.result.audioData)) } // The event synthesis completed signals that the synthesis is completed. @@ -157,7 +156,7 @@ export const synthesizeTextToSpeech = async ( throw error } finally { console.debug('closing synthesizer') - audioStream.end() + audioStream?.end() synthesizer.close() console.debug('synthesizer closed') }