import { getPreviewString, saveTtsProviderSettings } from './index.js';
import { getBase64Async } from '../../utils.js';
import { getRequestHeaders } from '../../../script.js';
export { SpeechT5TtsProvider };
class SpeechT5TtsProvider {
//########//
// Config //
//########//
settings;
ready = false;
voices = [];
separator = ' .. ';
audioElement = document.createElement('audio');
defaultSettings = {
speakers: [],
speaker: '',
voiceMap: {},
};
get settingsHtml() {
let html = `
Loading model for the first time may take a while!
`;
return html;
}
onSettingsChange() {
// Used when provider settings are updated from UI
this.settings.speaker = $('#speecht5_tts_speaker').val();
saveTtsProviderSettings();
}
async previewTtsVoice(voiceId) {
this.audioElement.pause();
this.audioElement.currentTime = 0;
const text = getPreviewString('en-US');
const response = await this.fetchTtsGeneration(text, voiceId);
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const audio = await response.blob();
const url = URL.createObjectURL(audio);
this.audioElement.src = url;
this.audioElement.play();
}
async loadSettings(settings) {
// Pupulate Provider UI given input settings
if (Object.keys(settings).length == 0) {
console.info('Using default TTS Provider settings');
}
// Only accept keys defined in defaultSettings
this.settings = this.defaultSettings;
for (const key in settings) {
if (key in this.settings) {
this.settings[key] = settings[key];
} else {
throw `Invalid setting passed to TTS Provider: ${key}`;
}
}
for (const speaker of this.settings.speakers) {
$('#speecht5_tts_speaker').append($('