Enqueue text to speech tasks

This commit is contained in:
Hongbo Wu
2022-08-29 22:23:07 +08:00
parent 8c8734d153
commit d085c86bb6
2 changed files with 12 additions and 48 deletions

View File

@ -9,9 +9,10 @@ import { getPageById } from '../elastic/pages'
import { Speech, SpeechState } from '../entity/speech'
import { buildLogger } from '../utils/logger'
import { getClaimsByToken } from '../utils/auth'
import { shouldSynthesize, synthesize } from '../services/speech'
import { shouldSynthesize } from '../services/speech'
import { readPushSubscription } from '../datalayer/pubsub'
import { AppDataSource } from '../server'
import { enqueueTextToSpeech } from '../utils/createTask'
const logger = buildLogger('app.dispatch')
@ -62,8 +63,16 @@ export function textToSpeechRouter() {
state: SpeechState.INITIALIZED,
voice: 'en-US-JennyNeural',
})
await synthesize(page, speech)
logger.info('page synthesized')
// enqueue a task to convert text to speech
const taskName = await enqueueTextToSpeech({
userId,
speechId: speech.id,
text: page.content,
voice: speech.voice,
priority: 'low',
})
logger.info('Start Text to speech task', { taskName })
return res.status(202).send('Text to speech task started')
}
res.status(200).send('Page should not synthesize')

View File

@ -1,16 +1,6 @@
import { getRepository } from '../entity/utils'
import { Speech, SpeechState } from '../entity/speech'
import { searchPages } from '../elastic/pages'
import { Page, PageType } from '../elastic/types'
import { SortBy, SortOrder } from '../utils/search'
import { synthesizeTextToSpeech } from '../utils/textToSpeech'
export const setSpeechFailure = async (id: string) => {
// update state
await getRepository(Speech).update(id, {
state: SpeechState.FAILED,
})
}
/*
* We should not synthesize the page when:
@ -54,38 +44,3 @@ export const shouldSynthesize = async (
page.savedAt < recentListenedPage[0].listenedAt
)
}
export const synthesize = async (page: Page, speech: Speech): Promise<void> => {
try {
if (page.pageType === PageType.File || !page.content) {
// we don't synthesize files for now
return
}
console.log('Start synthesizing', { pageId: page.id, speechId: speech.id })
const startTime = Date.now()
const speechOutput = await synthesizeTextToSpeech({
id: speech.id,
text: page.content,
languageCode: page.language,
voice: speech.voice,
textType: 'ssml',
})
console.log('Synthesized article', {
audioFileName: speechOutput.audioFileName,
speechMarksFileName: speechOutput.speechMarksFileName,
duration: Date.now() - startTime,
})
// set state to completed
await getRepository(Speech).update(speech.id, {
audioFileName: speechOutput.audioFileName,
speechMarksFileName: speechOutput.speechMarksFileName,
state: SpeechState.COMPLETED,
})
} catch (error) {
console.log('Error synthesize article', error)
await setSpeechFailure(speech.id)
throw error
}
}