From 83c1930378900908f62af485de6b656b3a96405e Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Wed, 17 Aug 2022 12:25:23 +0800 Subject: [PATCH] Split text into chunks of 5000 characters in paragraph --- packages/api/src/routers/article_router.ts | 2 +- packages/api/src/utils/textToSpeech.ts | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/api/src/routers/article_router.ts b/packages/api/src/routers/article_router.ts index 85ccf9b0a..e9f851286 100644 --- a/packages/api/src/routers/article_router.ts +++ b/packages/api/src/routers/article_router.ts @@ -120,7 +120,7 @@ export function articleRouter() { return res.status(200).send('Page not found') } - const text = parseHTML(page.content).document.documentElement.textContent + const text = parseHTML(page.content).document.documentElement.innerText if (!text) { return res.status(200).send('Page has no text') } diff --git a/packages/api/src/utils/textToSpeech.ts b/packages/api/src/utils/textToSpeech.ts index 7f1a1d45c..b8249c14e 100644 --- a/packages/api/src/utils/textToSpeech.ts +++ b/packages/api/src/utils/textToSpeech.ts @@ -129,13 +129,19 @@ export const synthesizeTextToSpeech = async ( ) }) } - // slice the text into chunks of 1,000 characters - const textChunks = input.text.match(/(.|[\r\n]){1,1000}/g) || [] + // slice the text into chunks of 5,000 characters + let currentTextChunk = '' + const textChunks = input.text.split('\n') for (const textChunk of textChunks) { - logger.debug(`synthesizing ${textChunk}`) - const result = await speakTextAsyncPromise(textChunk) + currentTextChunk += textChunk + '\n' + if (currentTextChunk.length < 5000) { + continue + } + logger.debug(`synthesizing ${currentTextChunk}`) + const result = await speakTextAsyncPromise(currentTextChunk) timeOffset = timeOffset + result.audioDuration - characterOffset = characterOffset + textChunk.length + characterOffset = characterOffset + currentTextChunk.length + currentTextChunk = '' } writeStream.end() synthesizer.close()