Merge pull request #3371 from fearmear/release Decrease TTS generation delay by splitting a message on a new line

6dde068e71383c55cb70f76960ed4c1a34aab134

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
2 files changed, +49 -3Showing whitespace changes
public/scripts/extensions/tts/index.js+45 -3
@@ -120,7 +120,7 @@ async function onNarrateOneMessage() {
120120 }
121121
122122 resetTtsPlayback();
123123 ttsJobQueue.pushprocessAndQueueTtsMessage(message);
124124 moduleWorker();
125125}
126126
@@ -147,7 +147,7 @@ async function onNarrateText(args, text) {
147147 }
148148
149149 resetTtsPlayback();
150150 ttsJobQueue.pushprocessAndQueueTtsMessage({ mes: text, name: name });
151151 await moduleWorker();
152152
153153 // Return back to the chat voices
@@ -220,6 +220,36 @@ function isTtsProcessing() {
220220 return processing;
221221}
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+ */
230+function processAndQueueTtsMessage(message) {
231+ if (!extension_settings.tts.narrate_by_paragraphs) {
232+ ttsJobQueue.push(message);
233+ return;
234+ }
235+
236+ const lines = message.mes.split('\n');
237+
238+ for (let i = 0; i < lines.length; i++) {
239+ const line = lines[i];
240+
241+ if (line.length === 0) {
242+ continue;
243+ }
244+
245+ ttsJobQueue.push(
246+ Object.assign({}, message, {
247+ mes: line,
248+ }),
249+ );
250+ }
251+}
252+
223253function debugTtsPlayback() {
224254 console.log(JSON.stringify(
225255 {
@@ -350,7 +380,7 @@ function onAudioControlClicked() {
350380 talkingAnimation(false);
351381 } else {
352382 // Default play behavior if not processing or playing is to play the last message.
353383 ttsJobQueue.pushprocessAndQueueTtsMessage(context.chat[context.chat.length - 1]);
354384 }
355385 updateUiAudioPlayState();
356386}
@@ -569,6 +599,7 @@ function loadSettings() {
569599 $('#tts_narrate_quoted').prop('checked', extension_settings.tts.narrate_quoted_only);
570600 $('#tts_auto_generation').prop('checked', extension_settings.tts.auto_generation);
571601 $('#tts_periodic_auto_generation').prop('checked', extension_settings.tts.periodic_auto_generation);
602+ $('#tts_narrate_by_paragraphs').prop('checked', extension_settings.tts.narrate_by_paragraphs);
572603 $('#tts_narrate_translated_only').prop('checked', extension_settings.tts.narrate_translated_only);
573604 $('#tts_narrate_user').prop('checked', extension_settings.tts.narrate_user);
574605 $('#tts_pass_asterisks').prop('checked', extension_settings.tts.pass_asterisks);
@@ -638,6 +669,11 @@ function onPeriodicAutoGenerationClick() {
638669 saveSettingsDebounced();
639670}
640671
672+function onNarrateByParagraphsClick() {
673+ extension_settings.tts.narrate_by_paragraphs = !!$('#tts_narrate_by_paragraphs').prop('checked');
674+ saveSettingsDebounced();
675+}
676+
641677
642678function onNarrateDialoguesClick() {
643679 extension_settings.tts.narrate_dialogues_only = !!$('#tts_narrate_dialogues').prop('checked');
@@ -816,7 +852,12 @@ async function onMessageEvent(messageId, lastCharIndex) {
816852 lastChatId = context.chatId;
817853
818854 console.debug(`Adding message from ${message.name} for TTS processing: "${message.mes}"`);
855+
856+ if (extension_settings.tts.periodic_auto_generation) {
819857 ttsJobQueue.push(message);
858+ } else {
859+ processAndQueueTtsMessage(message);
860+ }
820861}
821862
822863async function onMessageDeleted() {
@@ -1156,6 +1197,7 @@ jQuery(async function () {
11561197 $('#tts_pass_asterisks').on('click', onPassAsterisksClick);
11571198 $('#tts_auto_generation').on('click', onAutoGenerationClick);
11581199 $('#tts_periodic_auto_generation').on('click', onPeriodicAutoGenerationClick);
1200+ $('#tts_narrate_by_paragraphs').on('click', onNarrateByParagraphsClick);
11591201 $('#tts_narrate_user').on('click', onNarrateUserClick);
11601202
11611203 $('#playback_rate').on('input', function () {
public/scripts/extensions/tts/settings.html+4 -0
@@ -30,6 +30,10 @@
3030 <input type="checkbox" id="tts_periodic_auto_generation">
3131 <small data-i18n="Narrate by paragraphs (when streaming)">Narrate by paragraphs (when streaming)</small>
3232 </label>
33+ <label class="checkbox_label" for="tts_narrate_by_paragraphs">
34+ <input type="checkbox" id="tts_narrate_by_paragraphs">
35+ <small data-i18n="Narrate by paragraphs (when not streaming)">Narrate by paragraphs (when not streaming)</small>
36+ </label>
3337 <label class="checkbox_label" for="tts_narrate_quoted">
3438 <input type="checkbox" id="tts_narrate_quoted">
3539 <small data-i18n="Only narrate quotes">Only narrate "quotes"</small>