TTS code clean-up

This commit is contained in:
Cohee 2024-06-17 21:21:41 +03:00
parent feb7675d2f
commit b09e86fb53
1 changed files with 36 additions and 24 deletions

View File

@ -1,4 +1,4 @@
import { callPopup, cancelTtsPlay, eventSource, event_types, name2, saveSettingsDebounced, substituteParams } from '../../../script.js'; import { callPopup, cancelTtsPlay, eventSource, event_types, isStreamingEnabled, name2, saveSettingsDebounced, substituteParams } from '../../../script.js';
import { ModuleWorkerWrapper, doExtrasFetch, extension_settings, getApiUrl, getContext, modules } from '../../extensions.js'; import { ModuleWorkerWrapper, doExtrasFetch, extension_settings, getApiUrl, getContext, modules } from '../../extensions.js';
import { delay, escapeRegex, getBase64Async, getStringHash, onlyUnique } from '../../utils.js'; import { delay, escapeRegex, getBase64Async, getStringHash, onlyUnique } from '../../utils.js';
import { EdgeTtsProvider } from './edge.js'; import { EdgeTtsProvider } from './edge.js';
@ -18,6 +18,7 @@ import { AzureTtsProvider } from './azure.js';
import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js'; import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js';
import { SlashCommand } from '../../slash-commands/SlashCommand.js'; import { SlashCommand } from '../../slash-commands/SlashCommand.js';
import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from '../../slash-commands/SlashCommandArgument.js'; import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from '../../slash-commands/SlashCommandArgument.js';
import { debounce_timeout } from '../../constants.js';
export { talkingAnimation }; export { talkingAnimation };
const UPDATE_INTERVAL = 1000; const UPDATE_INTERVAL = 1000;
@ -28,6 +29,8 @@ let talkingHeadState = false;
let lastChatId = null; let lastChatId = null;
let lastMessage = null; let lastMessage = null;
let lastMessageHash = null; let lastMessageHash = null;
let periodicMessageGenerationTimer = null;
let lastPositionOfParagraphEnd = -1;
const DEFAULT_VOICE_MARKER = '[Default Voice]'; const DEFAULT_VOICE_MARKER = '[Default Voice]';
const DISABLED_VOICE_MARKER = 'disabled'; const DISABLED_VOICE_MARKER = 'disabled';
@ -109,7 +112,7 @@ async function onNarrateOneMessage() {
async function onNarrateText(args, text) { async function onNarrateText(args, text) {
if (!text) { if (!text) {
return; return '';
} }
audioElement.src = '/sounds/silence.mp3'; audioElement.src = '/sounds/silence.mp3';
@ -135,6 +138,7 @@ async function onNarrateText(args, text) {
// Return back to the chat voices // Return back to the chat voices
await initVoiceMap(false); await initVoiceMap(false);
return '';
} }
async function moduleWorker() { async function moduleWorker() {
@ -686,9 +690,10 @@ export function saveTtsProviderSettings() {
//###################// //###################//
async function onChatChanged() { async function onChatChanged() {
await resetTtsPlayback(); await onGenerationEnded();
resetTtsPlayback();
const voiceMapInit = initVoiceMap(); const voiceMapInit = initVoiceMap();
await Promise.race([voiceMapInit, delay(1000)]); await Promise.race([voiceMapInit, delay(debounce_timeout.relaxed)]);
lastMessage = null; lastMessage = null;
} }
@ -793,8 +798,12 @@ async function onMessageDeleted() {
resetTtsPlayback(); resetTtsPlayback();
} }
async function onGenerationStarted(userMessageType) { async function onGenerationStarted(generationType, _args, isDryRun) {
if (userMessageType === undefined) { // If dry running or quiet mode, do nothing
if (isDryRun || ['quiet', 'impersonate'].includes(generationType)) {
return;
}
// If TTS is disabled, do nothing // If TTS is disabled, do nothing
if (!extension_settings.tts.enabled) { if (!extension_settings.tts.enabled) {
return; return;
@ -810,23 +819,25 @@ async function onGenerationStarted(userMessageType) {
return; return;
} }
// start the timer // If the reply is not being streamed
if (periodicMessageGenerationTimer === undefined) { if (!isStreamingEnabled()) {
periodicMessageGenerationTimer = setInterval(onPeriodicMessageGenerationTick, 1000); return;
} }
// start the timer
if (!periodicMessageGenerationTimer) {
periodicMessageGenerationTimer = setInterval(onPeriodicMessageGenerationTick, UPDATE_INTERVAL);
} }
} }
async function onGenerationEnded() { async function onGenerationEnded() {
if (periodicMessageGenerationTimer !== undefined) { if (periodicMessageGenerationTimer) {
clearInterval(periodicMessageGenerationTimer); clearInterval(periodicMessageGenerationTimer);
periodicMessageGenerationTimer = undefined; periodicMessageGenerationTimer = null;
} }
lastPositionOfParagraphEnd = -1; lastPositionOfParagraphEnd = -1;
} }
var periodicMessageGenerationTimer;
var lastPositionOfParagraphEnd = -1;
async function onPeriodicMessageGenerationTick() { async function onPeriodicMessageGenerationTick() {
const context = getContext(); const context = getContext();
@ -846,7 +857,7 @@ async function onPeriodicMessageGenerationTick() {
const lastMessageText = lastMessage?.mes ?? ''; const lastMessageText = lastMessage?.mes ?? '';
// look for double ending lines which should indicate the end of a paragraph // look for double ending lines which should indicate the end of a paragraph
var newLastPositionOfParagraphEnd = lastMessageText let newLastPositionOfParagraphEnd = lastMessageText
.indexOf('\n\n', lastPositionOfParagraphEnd + 1); .indexOf('\n\n', lastPositionOfParagraphEnd + 1);
// if not found, look for a single ending line which should indicate the end of a paragraph // if not found, look for a single ending line which should indicate the end of a paragraph
if (newLastPositionOfParagraphEnd === -1) { if (newLastPositionOfParagraphEnd === -1) {
@ -858,7 +869,7 @@ async function onPeriodicMessageGenerationTick() {
if (newLastPositionOfParagraphEnd > -1) { if (newLastPositionOfParagraphEnd > -1) {
onMessageEvent(lastMessageId, newLastPositionOfParagraphEnd); onMessageEvent(lastMessageId, newLastPositionOfParagraphEnd);
if (periodicMessageGenerationTimer !== undefined) { if (periodicMessageGenerationTimer) {
lastPositionOfParagraphEnd = newLastPositionOfParagraphEnd; lastPositionOfParagraphEnd = newLastPositionOfParagraphEnd;
} }
} }
@ -1093,9 +1104,9 @@ $(document).ready(function () {
<input type="checkbox" id="tts_auto_generation"> <input type="checkbox" id="tts_auto_generation">
<small>Auto Generation</small> <small>Auto Generation</small>
</label> </label>
<label class="checkbox_label" for="tts_periodic_auto_generation"> <label class="checkbox_label" for="tts_periodic_auto_generation" title="Requires auto generation to be enabled.">
<input type="checkbox" id="tts_periodic_auto_generation"> <input type="checkbox" id="tts_periodic_auto_generation">
<small>Narrate by paragraphs (streaming)</small> <small>Narrate by paragraphs (when streaming)</small>
</label> </label>
<label class="checkbox_label" for="tts_narrate_quoted"> <label class="checkbox_label" for="tts_narrate_quoted">
<input type="checkbox" id="tts_narrate_quoted"> <input type="checkbox" id="tts_narrate_quoted">
@ -1191,7 +1202,8 @@ $(document).ready(function () {
eventSource.on(event_types.GENERATION_ENDED, onGenerationEnded); eventSource.on(event_types.GENERATION_ENDED, onGenerationEnded);
eventSource.makeLast(event_types.CHARACTER_MESSAGE_RENDERED, onMessageEvent); eventSource.makeLast(event_types.CHARACTER_MESSAGE_RENDERED, onMessageEvent);
eventSource.makeLast(event_types.USER_MESSAGE_RENDERED, onMessageEvent); eventSource.makeLast(event_types.USER_MESSAGE_RENDERED, onMessageEvent);
SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'speak', SlashCommandParser.addCommandObject(SlashCommand.fromProps({
name: 'speak',
callback: onNarrateText, callback: onNarrateText,
aliases: ['narrate', 'tts'], aliases: ['narrate', 'tts'],
namedArgumentList: [ namedArgumentList: [