Send SSML items in the synthesis API

This commit is contained in:
Hongbo Wu 2022-09-01 17:09:48 +08:00
parent 7bb3d711c5
commit 3953a9098e
4 changed files with 37 additions and 2 deletions

View file

@ -19,6 +19,7 @@
"@google-cloud/storage": "^5.18.1",
"@google-cloud/tasks": "^2.3.0",
"@omnivore/readability": "1.0.0",
"@omnivore/text-to-speech-handler": "1.0.0",
"@opentelemetry/api": "^1.0.1",
"@opentelemetry/core": "^1.3.1",
"@opentelemetry/exporter-jaeger": "^1.0.1",

View file

@ -21,6 +21,7 @@ import { getPageById, updatePage } from '../elastic/pages'
import { generateDownloadSignedUrl } from '../utils/uploads'
import { enqueueTextToSpeech } from '../utils/createTask'
import { createPubSubClient } from '../datalayer/pubsub'
import { htmlToSsml } from '@omnivore/text-to-speech-handler'
const logger = buildLogger('app.dispatch')
@ -81,7 +82,7 @@ export function articleRouter() {
const priority = req.params.priority
if (
!articleId ||
!['mp3', 'speech-marks'].includes(outputFormat) ||
!['mp3', 'speech-marks', 'ssml'].includes(outputFormat) ||
!['low', 'high'].includes(priority)
) {
return res.status(400).send('Invalid data')
@ -91,7 +92,6 @@ export function articleRouter() {
return res.status(401).send({ errorCode: 'UNAUTHORIZED' })
}
const { uid } = jwt.decode(token) as Claims
logger.info(`Get article speech in ${outputFormat} format`, {
params: req.params,
labels: {
@ -100,6 +100,21 @@ export function articleRouter() {
},
})
if (outputFormat === 'ssml') {
const page = await getPageById(articleId)
if (!page) {
return res.status(404).send('Page not found')
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
const ssmlItems = htmlToSsml(page.content, {
primaryVoice: voice,
secondaryVoice: 'en-US-GuyNeural',
rate: '1',
language: page.language || 'en-US',
})
return res.send({ ssmlItems })
}
const existingSpeech = await getRepository(Speech).findOne({
where: {
elasticPageId: articleId,

18
packages/api/src/textToSpeech.d.ts vendored Normal file
View file

@ -0,0 +1,18 @@
declare module '@omnivore/text-to-speech-handler' {
function htmlToSsml(html: string, options: SSMLOptions): SSMLItem[]
interface SSMLOptions {
primaryVoice: string
secondaryVoice: string
rate: string
language: string
}
interface SSMLItem {
open: string
close: string
textItems: string[]
}
export { htmlToSsml }
}

View file

@ -0,0 +1 @@
/test/