Chunkify NovelAI TTS

This commit is contained in:
Cohee 2024-01-01 21:31:08 +02:00
parent 58462d96d2
commit b315778e32
3 changed files with 43 additions and 24 deletions

View File

@ -463,13 +463,25 @@ function saveLastValues() {
}
async function tts(text, voiceId, char) {
let response = await ttsProvider.generateTts(text, voiceId);
async function processResponse(response) {
// RVC injection
if (extension_settings.rvc.enabled && typeof window['rvcVoiceConversion'] === 'function')
response = await window['rvcVoiceConversion'](response, char, text);
addAudioJob(response);
await addAudioJob(response);
}
let response = await ttsProvider.generateTts(text, voiceId);
// If async generator, process every chunk as it comes in
if (typeof response[Symbol.asyncIterator] === 'function') {
for await (const chunk of response) {
await processResponse(chunk);
}
} else {
await processResponse(response);
}
completeTtsJob();
}

View File

@ -1,4 +1,5 @@
import { getRequestHeaders, callPopup } from '../../../script.js';
import { splitRecursive } from '../../utils.js';
import { getPreviewString, saveTtsProviderSettings } from './index.js';
import { initVoiceMap } from './index.js';
@ -179,14 +180,17 @@ class NovelTtsProvider {
this.audioElement.play();
}
async fetchTtsGeneration(inputText, voiceId) {
async* fetchTtsGeneration(inputText, voiceId) {
const MAX_LENGTH = 1000;
console.info(`Generating new TTS for voice_id ${voiceId}`);
const chunks = splitRecursive(inputText, MAX_LENGTH);
for (const chunk of chunks) {
const response = await fetch('/api/novelai/generate-voice',
{
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({
'text': inputText,
'text': chunk,
'voice': voiceId,
}),
},
@ -195,6 +199,7 @@ class NovelTtsProvider {
toastr.error(response.statusText, 'TTS Generation Failed');
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
return response;
yield response;
}
}
}

View File

@ -342,7 +342,9 @@ router.post('/generate-voice', jsonParser, async (request, response) => {
});
if (!result.ok) {
return response.sendStatus(result.status);
const errorText = await result.text();
console.log('NovelAI returned an error.', result.statusText, errorText);
return response.sendStatus(500);
}
const chunks = await readAllChunks(result.body);