Decrease TTS generation delay by splitting a message on a new line

283ceb6bbfff486fdbbc8358c02099a4ff92a24d

Dzmitry Kazlouski <d.kazlouski@pxlnd.com>

1 files changed, +29 -4Showing whitespace changes
public/scripts/extensions/tts/index.js+29 -4
@@ -120,7 +120,7 @@ async function onNarrateOneMessage() {
120 }120 }
121121
122 resetTtsPlayback();122 resetTtsPlayback();
123 ttsJobQueue.push(message);123 splitMessageAndAddToTtsJobQueue(message);
124 moduleWorker();124 moduleWorker();
125}125}
126126
@@ -147,7 +147,7 @@ async function onNarrateText(args, text) {
147 }147 }
148148
149 resetTtsPlayback();149 resetTtsPlayback();
150 ttsJobQueue.push({ mes: text, name: name });150 splitMessageAndAddToTtsJobQueue({ mes: text, name: name });
151 await moduleWorker();151 await moduleWorker();
152152
153 // Return back to the chat voices153 // Return back to the chat voices
@@ -220,6 +220,31 @@ function isTtsProcessing() {
220 return processing;220 return processing;
221}221}
222222
223/**
224 * Splits a message into lines and adds each non-empty line to the TTS job queue.
225 * @param {Object} message - The message object to be processed.
226 * @param {string} message.mes - The text of the message to be split into lines.
227 * @param {string} message.name - The name associated with the message.
228 * @returns {void}
229 */
230function splitMessageAndAddToTtsJobQueue(message) {
231 const lines = message.mes.split("\n");
232
233 for (let i = 0; i < lines.length; i++) {
234 const line = lines[i];
235
236 if (line.length === 0) {
237 continue;
238 }
239
240 ttsJobQueue.push(
241 Object.assign({}, message, {
242 mes: line,
243 })
244 );
245 }
246}
247
223function debugTtsPlayback() {248function debugTtsPlayback() {
224 console.log(JSON.stringify(249 console.log(JSON.stringify(
225 {250 {
@@ -350,7 +375,7 @@ function onAudioControlClicked() {
350 talkingAnimation(false);375 talkingAnimation(false);
351 } else {376 } else {
352 // Default play behavior if not processing or playing is to play the last message.377 // Default play behavior if not processing or playing is to play the last message.
353 ttsJobQueue.push(context.chat[context.chat.length - 1]);378 splitMessageAndAddToTtsJobQueue(context.chat[context.chat.length - 1]);
354 }379 }
355 updateUiAudioPlayState();380 updateUiAudioPlayState();
356}381}
@@ -816,7 +841,7 @@ async function onMessageEvent(messageId, lastCharIndex) {
816 lastChatId = context.chatId;841 lastChatId = context.chatId;
817842
818 console.debug(`Adding message from ${message.name} for TTS processing: "${message.mes}"`);843 console.debug(`Adding message from ${message.name} for TTS processing: "${message.mes}"`);
819 ttsJobQueue.push(message);844 splitMessageAndAddToTtsJobQueue(message);
820}845}
821846
822async function onMessageDeleted() {847async function onMessageDeleted() {