| | 1 | import { saveTtsProviderSettings } from './index.js'; |
| | 2 | import { getCharacters, getPreviewString } from './index.js'; |
| | 3 | |
| | 4 | export { GptSoVITSAdapterProvider }; |
| | 5 | |
| | 6 | /* |
| | 7 | This file is adapted from gpt-sovits-v2.js. It was created because the original file is no longer maintained. |
| | 8 | Some logic has been optimized and more functionality has been added. |
| | 9 | */ |
| | 10 | |
| | 11 | class GptSoVITSAdapterProvider { |
| | 12 | settings; |
| | 13 | ready = false; |
| | 14 | voices = []; |
| | 15 | separator = '. '; |
| | 16 | audioElement = document.createElement('audio'); |
| | 17 | /* |
| | 18 | do not modify the text, adapter will handle it |
| | 19 | */ |
| | 20 | processText(text) { |
| | 21 | return text; |
| | 22 | } |
| | 23 | |
| | 24 | audioFormats = ['wav', 'ogg', 'silk', 'mp3', 'flac']; |
| | 25 | |
| | 26 | langKey2LangCode = { |
| | 27 | 'zh': 'zh-CN', |
| | 28 | 'en': 'en-US', |
| | 29 | 'ja': 'ja-JP', |
| | 30 | 'ko': 'ko-KR', |
| | 31 | }; |
| | 32 | |
| | 33 | defaultSettings = { |
| | 34 | provider_endpoint: 'http://localhost:9881', |
| | 35 | format: 'wav', |
| | 36 | lang: 'auto', |
| | 37 | streaming: false, |
| | 38 | text_lang: 'zh', |
| | 39 | media_type: 'auto', |
| | 40 | }; |
| | 41 | |
| | 42 | textLangOptions = [ |
| | 43 | { value: 'zh', label: 'Chinese' }, |
| | 44 | { value: 'en', label: 'English' }, |
| | 45 | { value: 'ja', label: 'Japanese' }, |
| | 46 | { value: 'ko', label: 'Korean' }, |
| | 47 | ]; |
| | 48 | |
| | 49 | mediaTypeOptions = [ |
| | 50 | { value: 'auto', label: 'Auto' }, |
| | 51 | { value: 'wav', label: 'WAV' }, |
| | 52 | { value: 'mp3', label: 'MP3' }, |
| | 53 | { value: 'ogg', label: 'OGG' }, |
| | 54 | { value: 'silk', label: 'SILK' }, |
| | 55 | { value: 'flac', label: 'FLAC' }, |
| | 56 | ]; |
| | 57 | |
| | 58 | _generateOptions(options, currentSetting) { |
| | 59 | return options.map(opt => { |
| | 60 | const isSelected = opt.value === currentSetting ? 'selected' : ''; |
| | 61 | return `<option value="${opt.value}" ${isSelected}>${opt.label}</option>`; |
| | 62 | }).join(''); |
| | 63 | } |
| | 64 | get settingsHtml() { |
| | 65 | const currentSettings = this.settings || this.defaultSettings; |
| | 66 | |
| | 67 | let html = ` |
| | 68 | <label for="gpt_sovits_adapter_tts_endpoint">Provider Endpoint:</label> |
| | 69 | <div class="flex1"> |
| | 70 | <input id="gpt_sovits_adapter_tts_endpoint" type="text" class="text_pole" maxlength="250" height="300" value="${this.defaultSettings.provider_endpoint}"/> |
| | 71 | </div> |
| | 72 | <span>Use <a target="_blank" href="https://github.com/guoql666/GPT-SoVITS_sillytavern_adapter">GPT-SoVITS-adapter</a>.</span><br/> |
| | 73 | <label for="text_lang">Text Lang(Inference text language):</label> |
| | 74 | <select id="text_lang" class="text_pole"> |
| | 75 | ${this._generateOptions(this.textLangOptions, currentSettings.text_lang)} |
| | 76 | </select> |
| | 77 | <label for="media_type">Media Type:</label> |
| | 78 | <select id="media_type" class="text_pole"> |
| | 79 | ${this._generateOptions(this.mediaTypeOptions, currentSettings.media_type)} |
| | 80 | </select> |
| | 81 | <br/> |
| | 82 | `; |
| | 83 | |
| | 84 | return html; |
| | 85 | } |
| | 86 | |
| | 87 | onSettingsChange() { |
| | 88 | // Used when provider settings are updated from UI |
| | 89 | this.settings.provider_endpoint = $('#gpt_sovits_adapter_tts_endpoint').val(); |
| | 90 | this.settings.text_lang = $('#text_lang').val(); |
| | 91 | this.settings.media_type = $('#media_type').val(); |
| | 92 | |
| | 93 | saveTtsProviderSettings(); |
| | 94 | this.changeTTSSettings(); |
| | 95 | } |
| | 96 | |
| | 97 | async loadSettings(settings) { |
| | 98 | // Populate Provider UI given input settings |
| | 99 | if (Object.keys(settings).length == 0) { |
| | 100 | console.info('Using default TTS Provider settings'); |
| | 101 | } |
| | 102 | |
| | 103 | // Only accept keys defined in defaultSettings |
| | 104 | this.settings = this.defaultSettings; |
| | 105 | |
| | 106 | for (const key in settings) { |
| | 107 | if (key in this.settings) { |
| | 108 | this.settings[key] = settings[key]; |
| | 109 | } else { |
| | 110 | console.debug(`Ignoring non-user-configurable setting: ${key}`); |
| | 111 | } |
| | 112 | } |
| | 113 | |
| | 114 | // Set initial values from the settings |
| | 115 | $('#tts_endpoint').val(this.settings.provider_endpoint).on('change', this.onSettingsChange.bind(this)); |
| | 116 | $('#text_lang').val(this.settings.text_lang).on('change', this.onSettingsChange.bind(this)); |
| | 117 | $('#media_type').val(this.settings.media_type).on('change', this.onSettingsChange.bind(this)); |
| | 118 | await this.checkReady(); |
| | 119 | console.info('ITS: Settings loaded'); |
| | 120 | } |
| | 121 | |
| | 122 | // Perform a simple readiness check by trying to fetch voiceIds |
| | 123 | async checkReady() { |
| | 124 | await Promise.allSettled([this.fetchTtsVoiceObjects(), this.changeTTSSettings()]); |
| | 125 | } |
| | 126 | |
| | 127 | async onRefreshClick() { |
| | 128 | return await this.checkReady(); |
| | 129 | } |
| | 130 | |
| | 131 | //#################// |
| | 132 | // TTS Interfaces // |
| | 133 | //#################// |
| | 134 | |
| | 135 | async getVoice(voiceName) { |
| | 136 | if (this.voices.length == 0) { |
| | 137 | this.voices = await this.fetchTtsVoiceObjects(); |
| | 138 | } |
| | 139 | |
| | 140 | const match = this.voices.filter( |
| | 141 | v => v.name == voiceName, |
| | 142 | )[0]; |
| | 143 | if (!match) { |
| | 144 | throw `TTS Voice name ${voiceName} not found`; |
| | 145 | } |
| | 146 | return match; |
| | 147 | } |
| | 148 | |
| | 149 | async generateTts(text, voiceId) { |
| | 150 | const response = await this.fetchTtsGeneration(text, voiceId); |
| | 151 | return response; |
| | 152 | } |
| | 153 | |
| | 154 | //###########// |
| | 155 | // API CALLS // |
| | 156 | //###########// |
| | 157 | async fetchTtsVoiceObjects() { |
| | 158 | const response = await fetch(`${this.settings.provider_endpoint}/speakers`); |
| | 159 | console.info(response); |
| | 160 | |
| | 161 | if (!response.ok) { |
| | 162 | throw new Error(`HTTP ${response.status}: ${await response.json()}`); |
| | 163 | } |
| | 164 | const responseJson = await response.json(); |
| | 165 | this.voices = responseJson; |
| | 166 | return responseJson; |
| | 167 | } |
| | 168 | |
| | 169 | // Each time a parameter is changed, we change the configuration |
| | 170 | async changeTTSSettings() { |
| | 171 | } |
| | 172 | |
| | 173 | /** |
| | 174 | * Preview TTS voice by generating a short sample. |
| | 175 | * @param {string} voiceId Voice ID to preview (model_type&speaker_id)) |
| | 176 | */ |
| | 177 | async previewTtsVoice(voiceId) { |
| | 178 | const langCode = this.langKey2LangCode[this.settings.text_lang] || 'zh-CN'; |
| | 179 | const previewText = getPreviewString(langCode); |
| | 180 | const response = await this.fetchTtsGeneration(previewText, voiceId); |
| | 181 | |
| | 182 | const audio = await response.blob(); |
| | 183 | const url = URL.createObjectURL(audio); |
| | 184 | this.audioElement.src = url; |
| | 185 | this.audioElement.play(); |
| | 186 | this.audioElement.onended = () => URL.revokeObjectURL(url); |
| | 187 | } |
| | 188 | |
| | 189 | /** |
| | 190 | * Fetch TTS generation from the API. |
| | 191 | * @param {string} inputText Text to generate TTS for |
| | 192 | * @param {string} voiceId Voice ID to use (model_type&speaker_id)) |
| | 193 | * @returns {Promise<Response>} Fetch response |
| | 194 | */ |
| | 195 | async fetchTtsGeneration(inputText, voiceId, lang = null, forceNoStreaming = false) { |
| | 196 | console.info(`Generating new TTS for voice_id ${voiceId}`); |
| | 197 | |
| | 198 | const params = { |
| | 199 | text: inputText, |
| | 200 | card_name: getCharacters(false), |
| | 201 | use_st_adapter: true, |
| | 202 | target_voice: voiceId, |
| | 203 | text_lang: this.settings.text_lang, |
| | 204 | text_split_method: 'cut5', |
| | 205 | batch_size: 1, |
| | 206 | media_type: this.settings.media_type, |
| | 207 | streaming_mode: 'true', |
| | 208 | }; |
| | 209 | |
| | 210 | const url = `${this.settings.provider_endpoint}/`; |
| | 211 | |
| | 212 | const response = await fetch( |
| | 213 | url, |
| | 214 | { |
| | 215 | method: 'POST', |
| | 216 | headers: { |
| | 217 | 'Content-Type': 'application/json', |
| | 218 | }, |
| | 219 | body: JSON.stringify(params), |
| | 220 | }, |
| | 221 | ); |
| | 222 | if (!response.ok) { |
| | 223 | toastr.error(response.statusText, 'TTS Generation Failed'); |
| | 224 | throw new Error(`HTTP ${response.status}: ${await response.text()}`); |
| | 225 | } |
| | 226 | return response; |
| | 227 | } |
| | 228 | |
| | 229 | // Interface not used |
| | 230 | async fetchTtsFromHistory(history_item_id) { |
| | 231 | return Promise.resolve(history_item_id); |
| | 232 | } |
| | 233 | } |