File size: 9,421 Bytes
c120a1c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
import { getRequestHeaders, substituteParams } from '../../../script.js';
import { saveTtsProviderSettings, sanitizeId } from './index.js';
export { OpenAITtsProvider };
class OpenAITtsProvider {
static voices = [
{ name: 'Alloy', voice_id: 'alloy', lang: 'en-US', preview_url: 'https://cdn.openai.com/API/docs/audio/alloy.wav' },
{ name: 'Ash', voice_id: 'ash', lang: 'en-US', preview_url: 'https://cdn.openai.com/API/docs/audio/ash.wav' },
{ name: 'Coral', voice_id: 'coral', lang: 'en-US', preview_url: 'https://cdn.openai.com/API/docs/audio/coral.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: 'Sage', voice_id: 'sage', lang: 'en-US', preview_url: 'https://cdn.openai.com/API/docs/audio/sage.wav' },
{ name: 'Shimmer', voice_id: 'shimmer', lang: 'en-US', preview_url: 'https://cdn.openai.com/API/docs/audio/shimmer.wav' },
];
settings;
voices = [];
separator = ' . ';
audioElement = document.createElement('audio');
defaultSettings = {
voiceMap: {},
customVoices: [],
model: 'tts-1',
speed: 1,
characterInstructions: {},
};
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>
<option value="gpt-4o-mini-tts">gpt-4o-mini-tts</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>
<input type="range" id="openai-tts-speed" value="1" min="0.25" max="4" step="0.05">
</div>`;
return html;
}
async loadSettings(settings) {
// Populate 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}`;
}
}
$('#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();
// Initialize UI state based on current model (gpt-4o-mini-tts or other)
this.updateInstructionsUI();
// Look for voice map changes
this.setupVoiceMapObserver();
console.debug('OpenAI TTS: Settings loaded');
}
setupVoiceMapObserver() {
if (this.voiceMapObserver) {
this.voiceMapObserver.disconnect();
this.voiceMapObserver = null;
}
const targetNode = document.getElementById('tts_voicemap_block');
if (!targetNode) return;
const observer = new MutationObserver(() => {
if (this.settings.model === 'gpt-4o-mini-tts') {
this.populateCharacterInstructions();
}
});
observer.observe(targetNode, { childList: true, subtree: true });
this.voiceMapObserver = observer;
}
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);
this.updateInstructionsUI();
saveTtsProviderSettings();
}
updateInstructionsUI() {
if (this.settings.model === 'gpt-4o-mini-tts') {
this.createInstructionsContainer();
$('#openai-instructions-container').show();
this.populateCharacterInstructions();
} else {
$('#openai-instructions-container').hide();
this.voiceMapObserver?.disconnect();
this.voiceMapObserver = null;
}
}
createInstructionsContainer() {
if ($('#openai-instructions-container').length === 0) {
const containerHtml = `
<div id="openai-instructions-container" style="display: none;">
<span>Voice Instructions (GPT-4o Mini TTS)</span><br>
<small>Customize how each character speaks</small>
<div id="openai-character-instructions"></div>
</div>
`;
$('#openai-tts-speed').parent().after(containerHtml);
}
}
populateCharacterInstructions() {
const currentCharacters = $('.tts_voicemap_block_char span').map((i, el) => $(el).text()).get();
$('#openai-character-instructions').empty();
for (const char of currentCharacters) {
if (char === 'SillyTavern System' || char === '[Default Voice]') continue;
const sanitizedName = sanitizeId(char);
const savedInstructions = this.settings.characterInstructions?.[char] || '';
const instructionBlock = document.createElement('div');
const label = document.createElement('label');
const textArea = document.createElement('textarea');
instructionBlock.appendChild(label);
instructionBlock.appendChild(textArea);
instructionBlock.className = 'character-instructions';
label.setAttribute('for', `openai_char_${sanitizedName}`);
label.innerText = `${char}:`;
textArea.id = `openai_char_${sanitizedName}`;
textArea.placeholder = 'e.g., "Speak cheerfully and energetically"';
textArea.className = 'textarea_compact autoSetHeight';
textArea.value = savedInstructions;
textArea.addEventListener('input', () => {
this.saveCharacterInstructions(char, textArea.value);
});
$('#openai-character-instructions').append(instructionBlock);
}
}
saveCharacterInstructions(characterName, instructions) {
if (!this.settings.characterInstructions) {
this.settings.characterInstructions = {};
}
this.settings.characterInstructions[characterName] = instructions;
saveTtsProviderSettings();
}
async checkReady() {
await this.fetchTtsVoiceObjects();
}
async onRefreshClick() {
return;
}
async getVoice(voiceName) {
if (!voiceName) {
throw 'TTS Voice name not provided';
}
const voice = OpenAITtsProvider.voices.find(voice => voice.voice_id === voiceName || voice.name === voiceName);
if (!voice) {
throw `TTS Voice not found: ${voiceName}`;
}
return voice;
}
async generateTts(text, voiceId, characterName = null) {
const response = await this.fetchTtsGeneration(text, voiceId, characterName);
return response;
}
async fetchTtsVoiceObjects() {
return OpenAITtsProvider.voices;
}
async previewTtsVoice(_) {
return;
}
async fetchTtsGeneration(inputText, voiceId, characterName = null) {
console.info(`Generating new TTS for voice_id ${voiceId}`);
const requestBody = {
'text': inputText,
'voice': voiceId,
'model': this.settings.model,
'speed': this.settings.speed,
};
if (this.settings.model === 'gpt-4o-mini-tts' && characterName) {
const instructions = this.settings.characterInstructions?.[characterName];
if (instructions && instructions.trim()) {
requestBody.instructions = substituteParams(instructions);
}
}
const response = await fetch('/api/openai/generate-voice', {
method: 'POST',
headers: getRequestHeaders(),
body: JSON.stringify(requestBody),
});
if (!response.ok) {
toastr.error(response.statusText, 'TTS Generation Failed');
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
return response;
}
}
|