SillyTavern/public/scripts/extensions/tts/edge.js

163 lines
5.0 KiB
JavaScript
Raw Normal View History

2023-12-02 20:11:06 +01:00
import { getRequestHeaders } from '../../../script.js';
import { getApiUrl } from '../../extensions.js';
import { doExtrasFetch, modules } from '../../extensions.js';
import { getPreviewString } from './index.js';
import { saveTtsProviderSettings } from './index.js';
2023-07-20 19:32:15 +02:00
2023-12-02 20:11:06 +01:00
export { EdgeTtsProvider };
2023-07-20 19:32:15 +02:00
class EdgeTtsProvider {
//########//
// 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 = {
voiceMap: {},
rate: 0,
2023-12-02 20:11:06 +01:00
};
2023-07-20 19:32:15 +02:00
get settingsHtml() {
let html = `Microsoft Edge TTS Provider<br>
<label for="edge_tts_rate">Rate: <span id="edge_tts_rate_output"></span></label>
2023-12-02 20:11:06 +01:00
<input id="edge_tts_rate" type="range" value="${this.defaultSettings.rate}" min="-100" max="100" step="1" />`;
return html;
2023-07-20 19:32:15 +02:00
}
onSettingsChange() {
this.settings.rate = Number($('#edge_tts_rate').val());
$('#edge_tts_rate_output').text(this.settings.rate);
2023-12-02 20:11:06 +01:00
saveTtsProviderSettings();
2023-07-20 19:32:15 +02:00
}
async loadSettings(settings) {
2023-07-20 19:32:15 +02:00
// Pupulate 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
}
// 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
}
}
$('#edge_tts_rate').val(this.settings.rate || 0);
$('#edge_tts_rate_output').text(this.settings.rate || 0);
2023-12-02 20:11:06 +01:00
$('#edge_tts_rate').on('input', () => {this.onSettingsChange();});
await this.checkReady();
2023-08-22 15:30:33 +02:00
2023-12-02 20:11:06 +01:00
console.debug('EdgeTTS: Settings loaded');
2023-07-20 19:32:15 +02:00
}
2023-08-22 15:30:33 +02:00
// Perform a simple readiness check by trying to fetch voiceIds
async checkReady(){
2023-12-02 20:11:06 +01:00
throwIfModuleMissing();
await this.fetchTtsVoiceObjects();
2023-08-22 15:30:33 +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 (this.voices.length == 0) {
2023-12-02 20:11:06 +01:00
this.voices = await this.fetchTtsVoiceObjects();
2023-07-20 19:32:15 +02:00
}
const match = this.voices.filter(
2023-12-02 21:06:57 +01:00
voice => voice.name == voiceName,
2023-12-02 20:11:06 +01:00
)[0];
2023-07-20 19:32:15 +02:00
if (!match) {
2023-12-02 20:11:06 +01:00
throw `TTS Voice name ${voiceName} not found`;
2023-07-20 19:32:15 +02:00
}
2023-12-02 20:11:06 +01:00
return match;
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-12-02 20:11:06 +01:00
throwIfModuleMissing();
2023-07-20 19:32:15 +02:00
const url = new URL(getApiUrl());
2023-12-02 20:11:06 +01:00
url.pathname = '/api/edge-tts/list';
const response = await doExtrasFetch(url);
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}: ${await response.text()}`);
2023-07-20 19:32:15 +02:00
}
2023-12-02 20:11:06 +01:00
let responseJson = await response.json();
2023-07-20 19:32:15 +02:00
responseJson = responseJson
.sort((a, b) => a.Locale.localeCompare(b.Locale) || a.ShortName.localeCompare(b.ShortName))
.map(x => ({ name: x.ShortName, voice_id: x.ShortName, preview_url: false, lang: x.Locale }));
2023-12-02 20:11:06 +01:00
return responseJson;
2023-07-20 19:32:15 +02:00
}
async previewTtsVoice(id) {
this.audioElement.pause();
this.audioElement.currentTime = 0;
const voice = await this.getVoice(id);
const text = getPreviewString(voice.lang);
2023-12-02 20:11:06 +01:00
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}: ${await response.text()}`);
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();
}
async fetchTtsGeneration(inputText, voiceId) {
2023-12-02 20:11:06 +01:00
throwIfModuleMissing();
2023-07-20 19:32:15 +02:00
2023-12-02 20:11:06 +01:00
console.info(`Generating new TTS for voice_id ${voiceId}`);
2023-07-20 19:32:15 +02:00
const url = new URL(getApiUrl());
2023-12-02 19:04:51 +01:00
url.pathname = '/api/edge-tts/generate';
2023-07-20 19:32:15 +02:00
const response = await doExtrasFetch(url,
{
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({
2023-12-02 19:04:51 +01:00
'text': inputText,
'voice': voiceId,
'rate': Number(this.settings.rate),
2023-12-02 21:06:57 +01:00
}),
},
2023-12-02 20:11:06 +01:00
);
2023-07-20 19:32:15 +02:00
if (!response.ok) {
toastr.error(response.statusText, 'TTS Generation Failed');
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
2023-12-02 20:11:06 +01:00
return response;
2023-07-20 19:32:15 +02:00
}
}
function throwIfModuleMissing() {
if (!modules.includes('edge-tts')) {
2023-12-02 20:11:06 +01:00
const message = 'Edge TTS module not loaded. Add edge-tts to enable-modules and restart the Extras API.';
// toastr.error(message)
2023-12-02 20:11:06 +01:00
throw new Error(message);
2023-07-20 19:32:15 +02:00
}
}