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

149 lines
4.9 KiB
JavaScript
Raw Normal View History

2023-12-02 20:11:06 +01:00
import { getRequestHeaders } from '../../../script.js';
2023-12-02 19:04:51 +01:00
import { saveTtsProviderSettings } from './index.js';
2023-11-12 01:28:03 +01:00
2023-12-02 20:11:06 +01:00
export { OpenAITtsProvider };
2023-11-12 01:28:03 +01:00
class OpenAITtsProvider {
static voices = [
{ name: 'Alloy', voice_id: 'alloy', lang: 'en-US', preview_url: 'https://cdn.openai.com/API/docs/audio/alloy.wav' },
{ name: 'Echo', voice_id: 'echo', lang: 'en-US', preview_url: 'https://cdn.openai.com/API/docs/audio/echo.wav' },
{ name: 'Fable', voice_id: 'fable', lang: 'en-US', preview_url: 'https://cdn.openai.com/API/docs/audio/fable.wav' },
{ name: 'Onyx', voice_id: 'onyx', lang: 'en-US', preview_url: 'https://cdn.openai.com/API/docs/audio/onyx.wav' },
{ name: 'Nova', voice_id: 'nova', lang: 'en-US', preview_url: 'https://cdn.openai.com/API/docs/audio/nova.wav' },
{ name: 'Shimmer', voice_id: 'shimmer', lang: 'en-US', preview_url: 'https://cdn.openai.com/API/docs/audio/shimmer.wav' },
];
2023-12-02 20:11:06 +01:00
settings;
voices = [];
separator = ' . ';
audioElement = document.createElement('audio');
2023-11-12 01:28:03 +01:00
defaultSettings = {
voiceMap: {},
customVoices: [],
model: 'tts-1',
speed: 1,
2023-12-02 20:11:06 +01:00
};
2023-11-12 01:28:03 +01:00
get settingsHtml() {
let html = `
<div>Use OpenAI's TTS engine.</div>
<small>Hint: Save an API key in the OpenAI API settings to use it here.</small>
<div>
<label for="openai-tts-model">Model:</label>
<select id="openai-tts-model">
<optgroup label="Latest">
<option value="tts-1">tts-1</option>
<option value="tts-1-hd">tts-1-hd</option>
</optgroup>
<optgroup label="Snapshots">
<option value="tts-1-1106">tts-1-1106</option>
<option value="tts-1-hd-1106">tts-1-hd-1106</option>
</optgroup>
<select>
</div>
<div>
<label for="openai-tts-speed">Speed: <span id="openai-tts-speed-output"></span></label>
2023-12-16 00:58:52 +01:00
<input type="range" id="openai-tts-speed" value="1" min="0.25" max="4" step="0.05">
2023-11-12 01:28:03 +01:00
</div>`;
return html;
}
async loadSettings(settings) {
// 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-11-12 01:28:03 +01:00
}
// 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}`;
}
}
$('#openai-tts-model').val(this.settings.model);
$('#openai-tts-model').on('change', () => {
this.onSettingsChange();
});
$('#openai-tts-speed').val(this.settings.speed);
$('#openai-tts-speed').on('input', () => {
this.onSettingsChange();
});
$('#openai-tts-speed-output').text(this.settings.speed);
await this.checkReady();
2023-12-02 19:04:51 +01:00
console.debug('OpenAI TTS: Settings loaded');
2023-11-12 01:28:03 +01:00
}
onSettingsChange() {
// Update dynamically
this.settings.model = String($('#openai-tts-model').find(':selected').val());
this.settings.speed = Number($('#openai-tts-speed').val());
$('#openai-tts-speed-output').text(this.settings.speed);
saveTtsProviderSettings();
}
async checkReady() {
await this.fetchTtsVoiceObjects();
}
async onRefreshClick() {
return;
}
async getVoice(voiceName) {
if (!voiceName) {
2023-12-02 20:11:06 +01:00
throw 'TTS Voice name not provided';
2023-11-12 01:28:03 +01:00
}
const voice = OpenAITtsProvider.voices.find(voice => voice.voice_id === voiceName || voice.name === voiceName);
if (!voice) {
2023-12-02 20:11:06 +01:00
throw `TTS Voice not found: ${voiceName}`;
2023-11-12 01:28:03 +01:00
}
return voice;
}
async generateTts(text, voiceId) {
2023-12-02 20:11:06 +01:00
const response = await this.fetchTtsGeneration(text, voiceId);
return response;
2023-11-12 01:28:03 +01:00
}
async fetchTtsVoiceObjects() {
return OpenAITtsProvider.voices;
}
async previewTtsVoice(_) {
return;
}
async fetchTtsGeneration(inputText, voiceId) {
2023-12-02 20:11:06 +01:00
console.info(`Generating new TTS for voice_id ${voiceId}`);
2023-12-02 19:04:51 +01:00
const response = await fetch('/api/openai/generate-voice', {
2023-11-12 01:28:03 +01:00
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify({
2023-12-02 19:04:51 +01:00
'text': inputText,
'voice': voiceId,
'model': this.settings.model,
'speed': this.settings.speed,
2023-11-12 01:28:03 +01:00
}),
});
if (!response.ok) {
toastr.error(response.statusText, 'TTS Generation Failed');
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
return response;
}
}