2023-12-02 20:11:06 +01:00
|
|
|
import { getRequestHeaders, callPopup } from '../../../script.js';
|
2024-01-01 20:31:08 +01:00
|
|
|
import { splitRecursive } from '../../utils.js';
|
2023-12-02 20:11:06 +01:00
|
|
|
import { getPreviewString, saveTtsProviderSettings } from './index.js';
|
|
|
|
import { initVoiceMap } from './index.js';
|
2023-07-20 19:32:15 +02:00
|
|
|
|
2023-12-02 20:11:06 +01:00
|
|
|
export { NovelTtsProvider };
|
2023-07-20 19:32:15 +02:00
|
|
|
|
|
|
|
class NovelTtsProvider {
|
|
|
|
//########//
|
|
|
|
// Config //
|
|
|
|
//########//
|
|
|
|
|
2023-12-02 20:11:06 +01:00
|
|
|
settings;
|
|
|
|
voices = [];
|
|
|
|
separator = ' . ';
|
|
|
|
audioElement = document.createElement('audio');
|
2023-07-20 19:32:15 +02:00
|
|
|
|
|
|
|
defaultSettings = {
|
2023-08-22 15:30:17 +02:00
|
|
|
voiceMap: {},
|
2023-12-02 21:06:57 +01:00
|
|
|
customVoices: [],
|
2023-12-02 20:11:06 +01:00
|
|
|
};
|
2023-07-20 19:32:15 +02:00
|
|
|
|
2023-11-27 12:25:49 +01:00
|
|
|
/**
|
|
|
|
* Perform any text processing before passing to TTS engine.
|
|
|
|
* @param {string} text Input text
|
|
|
|
* @returns {string} Processed text
|
|
|
|
*/
|
|
|
|
processText(text) {
|
|
|
|
// Novel reads tilde as a word. Replace with full stop
|
|
|
|
text = text.replace(/~/g, '.');
|
2024-03-17 23:31:28 +01:00
|
|
|
// Novel reads asterisk as a word. Remove it
|
|
|
|
text = text.replace(/\*/g, '');
|
2023-11-27 12:25:49 +01:00
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2023-07-20 19:32:15 +02:00
|
|
|
get settingsHtml() {
|
2023-08-22 15:30:17 +02:00
|
|
|
let html = `
|
2023-08-28 20:46:41 +02:00
|
|
|
<div class="novel_tts_hints">
|
|
|
|
<div>Use NovelAI's TTS engine.</div>
|
|
|
|
<div>
|
|
|
|
The default Voice IDs are only examples. Add custom voices and Novel will create a new random voice for it.
|
|
|
|
Feel free to try different options!
|
|
|
|
</div>
|
|
|
|
<i>Hint: Save an API key in the NovelAI API settings to use it here.</i>
|
|
|
|
</div>
|
2023-08-22 15:30:17 +02:00
|
|
|
<label for="tts-novel-custom-voices-add">Custom Voices</label>
|
2023-08-28 20:46:41 +02:00
|
|
|
<div class="tts_custom_voices">
|
2023-08-22 15:30:17 +02:00
|
|
|
<select id="tts-novel-custom-voices-select"><select>
|
2023-08-28 20:46:41 +02:00
|
|
|
<i id="tts-novel-custom-voices-add" class="tts-button fa-solid fa-plus fa-xl success" title="Add"></i>
|
|
|
|
<i id="tts-novel-custom-voices-delete" class="tts-button fa-solid fa-xmark fa-xl failure" title="Delete"></i>
|
2023-08-22 15:30:17 +02:00
|
|
|
</div>
|
|
|
|
`;
|
2023-07-20 19:32:15 +02:00
|
|
|
return html;
|
|
|
|
}
|
|
|
|
|
2023-08-22 15:30:17 +02:00
|
|
|
|
|
|
|
// Add a new Novel custom voice to provider
|
2024-01-01 20:31:08 +01:00
|
|
|
async addCustomVoice() {
|
2023-12-02 20:11:06 +01:00
|
|
|
const voiceName = await callPopup('<h3>Custom Voice name:</h3>', 'input');
|
|
|
|
this.settings.customVoices.push(voiceName);
|
|
|
|
this.populateCustomVoices();
|
|
|
|
initVoiceMap(); // Update TTS extension voiceMap
|
|
|
|
saveTtsProviderSettings();
|
2023-08-22 15:30:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete selected custom voice from provider
|
|
|
|
deleteCustomVoice() {
|
2023-12-02 19:04:51 +01:00
|
|
|
const selected = $('#tts-novel-custom-voices-select').find(':selected').val();
|
2023-08-22 15:30:17 +02:00
|
|
|
const voiceIndex = this.settings.customVoices.indexOf(selected);
|
2023-08-28 20:46:41 +02:00
|
|
|
|
2023-08-22 15:30:17 +02:00
|
|
|
if (voiceIndex !== -1) {
|
|
|
|
this.settings.customVoices.splice(voiceIndex, 1);
|
|
|
|
}
|
2023-12-02 20:11:06 +01:00
|
|
|
this.populateCustomVoices();
|
|
|
|
initVoiceMap(); // Update TTS extension voiceMap
|
|
|
|
saveTtsProviderSettings();
|
2023-08-22 15:30:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the UI dropdown list of voices in provider
|
2024-01-01 20:31:08 +01:00
|
|
|
populateCustomVoices() {
|
2023-12-02 20:11:06 +01:00
|
|
|
let voiceSelect = $('#tts-novel-custom-voices-select');
|
|
|
|
voiceSelect.empty();
|
2023-08-22 15:30:17 +02:00
|
|
|
this.settings.customVoices.forEach(voice => {
|
2023-12-02 20:11:06 +01:00
|
|
|
voiceSelect.append(`<option>${voice}</option>`);
|
|
|
|
});
|
2023-07-20 19:32:15 +02:00
|
|
|
}
|
|
|
|
|
2023-08-28 20:58:46 +02:00
|
|
|
async loadSettings(settings) {
|
2023-07-20 19:32:15 +02:00
|
|
|
// Populate Provider UI given input settings
|
|
|
|
if (Object.keys(settings).length == 0) {
|
2023-12-02 20:11:06 +01:00
|
|
|
console.info('Using default TTS Provider settings');
|
2023-07-20 19:32:15 +02:00
|
|
|
}
|
2023-12-02 20:11:06 +01:00
|
|
|
$('#tts-novel-custom-voices-add').on('click', () => (this.addCustomVoice()));
|
2024-01-01 20:31:08 +01:00
|
|
|
$('#tts-novel-custom-voices-delete').on('click', () => (this.deleteCustomVoice()));
|
2023-07-20 19:32:15 +02:00
|
|
|
|
|
|
|
// Only accept keys defined in defaultSettings
|
2023-12-02 20:11:06 +01:00
|
|
|
this.settings = this.defaultSettings;
|
2023-07-20 19:32:15 +02:00
|
|
|
|
|
|
|
for (const key in settings) {
|
|
|
|
if (key in this.settings) {
|
2023-12-02 20:11:06 +01:00
|
|
|
this.settings[key] = settings[key];
|
2023-07-20 19:32:15 +02:00
|
|
|
} else {
|
2023-12-02 20:11:06 +01:00
|
|
|
throw `Invalid setting passed to TTS Provider: ${key}`;
|
2023-07-20 19:32:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-02 20:11:06 +01:00
|
|
|
this.populateCustomVoices();
|
|
|
|
await this.checkReady();
|
|
|
|
console.debug('NovelTTS: Settings loaded');
|
2023-07-20 19:32:15 +02:00
|
|
|
}
|
|
|
|
|
2023-08-22 15:30:17 +02:00
|
|
|
// Perform a simple readiness check by trying to fetch voiceIds
|
|
|
|
// Doesnt really do much for Novel, not seeing a good way to test this at the moment.
|
2024-01-01 20:31:08 +01:00
|
|
|
async checkReady() {
|
2023-12-02 20:11:06 +01:00
|
|
|
await this.fetchTtsVoiceObjects();
|
2023-08-22 15:30:17 +02:00
|
|
|
}
|
2023-07-20 19:32:15 +02:00
|
|
|
|
2023-08-26 05:52:26 +02:00
|
|
|
async onRefreshClick() {
|
2023-12-02 20:11:06 +01:00
|
|
|
return;
|
2023-07-20 19:32:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//#################//
|
|
|
|
// TTS Interfaces //
|
|
|
|
//#################//
|
|
|
|
|
|
|
|
async getVoice(voiceName) {
|
|
|
|
if (!voiceName) {
|
2023-12-02 20:11:06 +01:00
|
|
|
throw 'TTS Voice name not provided';
|
2023-07-20 19:32:15 +02:00
|
|
|
}
|
|
|
|
|
2023-12-04 18:32:41 +01:00
|
|
|
return { name: voiceName, voice_id: voiceName, lang: 'en-US', preview_url: false };
|
2023-07-20 19:32:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async generateTts(text, voiceId) {
|
2023-12-02 20:11:06 +01:00
|
|
|
const response = await this.fetchTtsGeneration(text, voiceId);
|
|
|
|
return response;
|
2023-07-20 19:32:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//###########//
|
|
|
|
// API CALLS //
|
|
|
|
//###########//
|
2023-08-26 05:52:26 +02:00
|
|
|
async fetchTtsVoiceObjects() {
|
2023-08-22 15:30:17 +02:00
|
|
|
let voices = [
|
2023-07-20 19:32:15 +02:00
|
|
|
{ name: 'Ligeia', voice_id: 'Ligeia', lang: 'en-US', preview_url: false },
|
|
|
|
{ name: 'Aini', voice_id: 'Aini', lang: 'en-US', preview_url: false },
|
|
|
|
{ name: 'Orea', voice_id: 'Orea', lang: 'en-US', preview_url: false },
|
|
|
|
{ name: 'Claea', voice_id: 'Claea', lang: 'en-US', preview_url: false },
|
|
|
|
{ name: 'Lim', voice_id: 'Lim', lang: 'en-US', preview_url: false },
|
|
|
|
{ name: 'Aurae', voice_id: 'Aurae', lang: 'en-US', preview_url: false },
|
|
|
|
{ name: 'Naia', voice_id: 'Naia', lang: 'en-US', preview_url: false },
|
|
|
|
{ name: 'Aulon', voice_id: 'Aulon', lang: 'en-US', preview_url: false },
|
|
|
|
{ name: 'Elei', voice_id: 'Elei', lang: 'en-US', preview_url: false },
|
|
|
|
{ name: 'Ogma', voice_id: 'Ogma', lang: 'en-US', preview_url: false },
|
|
|
|
{ name: 'Raid', voice_id: 'Raid', lang: 'en-US', preview_url: false },
|
|
|
|
{ name: 'Pega', voice_id: 'Pega', lang: 'en-US', preview_url: false },
|
|
|
|
{ name: 'Lam', voice_id: 'Lam', lang: 'en-US', preview_url: false },
|
|
|
|
];
|
|
|
|
|
2023-08-22 15:30:17 +02:00
|
|
|
// Add in custom voices to the map
|
2023-08-28 20:46:41 +02:00
|
|
|
let addVoices = this.settings.customVoices.map(voice =>
|
2023-12-02 21:06:57 +01:00
|
|
|
({ name: voice, voice_id: voice, lang: 'en-US', preview_url: false }),
|
2023-12-02 20:11:06 +01:00
|
|
|
);
|
|
|
|
voices = voices.concat(addVoices);
|
2023-08-22 15:30:17 +02:00
|
|
|
|
2023-07-20 19:32:15 +02:00
|
|
|
return voices;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async previewTtsVoice(id) {
|
|
|
|
this.audioElement.pause();
|
|
|
|
this.audioElement.currentTime = 0;
|
|
|
|
|
2023-12-02 20:11:06 +01:00
|
|
|
const text = getPreviewString('en-US');
|
|
|
|
const response = await this.fetchTtsGeneration(text, id);
|
2023-07-20 19:32:15 +02:00
|
|
|
if (!response.ok) {
|
2023-12-02 20:11:06 +01:00
|
|
|
throw new Error(`HTTP ${response.status}`);
|
2023-07-20 19:32:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const audio = await response.blob();
|
|
|
|
const url = URL.createObjectURL(audio);
|
|
|
|
this.audioElement.src = url;
|
|
|
|
this.audioElement.play();
|
|
|
|
}
|
|
|
|
|
2024-01-01 20:31:08 +01:00
|
|
|
async* fetchTtsGeneration(inputText, voiceId) {
|
|
|
|
const MAX_LENGTH = 1000;
|
2023-12-02 20:11:06 +01:00
|
|
|
console.info(`Generating new TTS for voice_id ${voiceId}`);
|
2024-01-01 20:31:08 +01:00
|
|
|
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': chunk,
|
|
|
|
'voice': voiceId,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
if (!response.ok) {
|
|
|
|
toastr.error(response.statusText, 'TTS Generation Failed');
|
|
|
|
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
|
|
|
|
}
|
|
|
|
yield response;
|
2023-07-20 19:32:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|