Reduce SSML size

This commit is contained in:
Hongbo Wu 2022-09-02 13:19:44 +08:00
parent ba147c0d99
commit 71144d3c84
3 changed files with 10 additions and 7 deletions

View file

@ -91,7 +91,7 @@ function emitTextNode(
if (ssmlElement) {
emit(textItems, `<${ssmlElement}>`)
}
emit(textItems, `${cleanedText}`)
emit(textItems, `${cleanedText.replace(/\s+/g, ' ')}`)
if (ssmlElement) {
emit(textItems, `</${ssmlElement}>`)
}
@ -123,7 +123,7 @@ function emitElement(
) {
const cleanedText = cleanTextNode(child)
if (idx && cleanedText.length > 1) {
// Make sure its more than just a space
// Make sure it's more than just a space
emit(textItems, `<bookmark mark="${idx}" />`)
}
emitTextNode(textItems, cleanedText, child)
@ -158,9 +158,7 @@ const startSsml = (element: Element, options: SSMLOptions): string => {
element.nodeName === 'BLOCKQUOTE'
? options.secondaryVoice
: options.primaryVoice
return `
<speak xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="http://www.w3.org/2001/mstts" xmlns:emo="http://www.w3.org/2009/10/emotionml" version="1.0" xml:lang="${options.language}"><voice name="${voice}"><prosody rate="${options.rate}" pitch="default">
`
return `<speak xmlns="http://www.w3.org/2001/10/synthesis" version="1.0" xml:lang="${options.language}"><voice name="${voice}"><prosody rate="${options.rate}">`
}
const endSsml = (): string => {

View file

@ -10,7 +10,7 @@ import * as dotenv from 'dotenv' // see https://github.com/motdotla/dotenv#how-d
import { synthesizeTextToSpeech, TextToSpeechInput } from './textToSpeech'
import { File, Storage } from '@google-cloud/storage'
import { PassThrough } from 'stream'
import { SSMLItem } from './htmlToSsml'
import { htmlToSsml, SSMLItem } from './htmlToSsml'
dotenv.config()
Sentry.GCPFunction.init({
@ -164,3 +164,9 @@ export const textToSpeechStreamingHandler = Sentry.GCPFunction.wrapHttpFunction(
}
}
)
module.exports = {
htmlToSsml,
textToSpeechStreamingHandler,
textToSpeechHandler,
}

View file

@ -46,7 +46,6 @@ export const synthesizeTextToSpeech = async (
process.env.AZURE_SPEECH_KEY,
process.env.AZURE_SPEECH_REGION
)
const textType = input.textType || 'html'
speechConfig.speechSynthesisOutputFormat =
SpeechSynthesisOutputFormat.Audio16Khz32KBitRateMonoMp3