Decrease TTS generation delay by splitting a message on a new line
| @@ -120,7 +120,7 @@ async function onNarrateOneMessage() { | ||
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | resetTtsPlayback(); |
| 123 | 123 | ttsJobQueue.pushsplitMessageAndAddToTtsJobQueue(message); |
| 124 | 124 | moduleWorker(); |
| 125 | 125 | } |
| 126 | 126 | |
| @@ -147,7 +147,7 @@ async function onNarrateText(args, text) { | ||
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | resetTtsPlayback(); |
| 150 | 150 | ttsJobQueue.pushsplitMessageAndAddToTtsJobQueue({ mes: text, name: name }); |
| 151 | 151 | await moduleWorker(); |
| 152 | 152 | |
| 153 | 153 | // Return back to the chat voices |
| @@ -220,6 +220,31 @@ function isTtsProcessing() { | ||
| 220 | 220 | return processing; |
| 221 | 221 | } |
| 222 | 222 | |
| 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 | + */ | |
| 230 | +function 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 | + | |
| 223 | 248 | function debugTtsPlayback() { |
| 224 | 249 | console.log(JSON.stringify( |
| 225 | 250 | { |
| @@ -350,7 +375,7 @@ function onAudioControlClicked() { | ||
| 350 | 375 | talkingAnimation(false); |
| 351 | 376 | } else { |
| 352 | 377 | // Default play behavior if not processing or playing is to play the last message. |
| 353 | 378 | ttsJobQueue.pushsplitMessageAndAddToTtsJobQueue(context.chat[context.chat.length - 1]); |
| 354 | 379 | } |
| 355 | 380 | updateUiAudioPlayState(); |
| 356 | 381 | } |
| @@ -816,7 +841,7 @@ async function onMessageEvent(messageId, lastCharIndex) { | ||
| 816 | 841 | lastChatId = context.chatId; |
| 817 | 842 | |
| 818 | 843 | console.debug(`Adding message from ${message.name} for TTS processing: "${message.mes}"`); |
| 819 | 844 | ttsJobQueue.pushsplitMessageAndAddToTtsJobQueue(message); |
| 820 | 845 | } |
| 821 | 846 | |
| 822 | 847 | async function onMessageDeleted() { |