File size: 8,743 Bytes
b82d373
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
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';

export { EdgeTtsProvider };

const EDGE_TTS_PROVIDER = {
    extras: 'extras',
    plugin: 'plugin',
};

class EdgeTtsProvider {
    //########//
    // Config //
    //########//

    settings;
    voices = [];
    separator = ' . ';
    audioElement = document.createElement('audio');

    defaultSettings = {
        voiceMap: {},
        rate: 0,
        provider: EDGE_TTS_PROVIDER.extras,
    };

    get settingsHtml() {
        let html = `Microsoft Edge TTS<br>
        <label for="edge_tts_provider">Provider</label>
        <select id="edge_tts_provider">
            <option value="${EDGE_TTS_PROVIDER.extras}">Extras</option>
            <option value="${EDGE_TTS_PROVIDER.plugin}">Plugin</option>
        </select>
        <label for="edge_tts_rate">Rate: <span id="edge_tts_rate_output"></span></label>
        <input id="edge_tts_rate" type="range" value="${this.defaultSettings.rate}" min="-100" max="100" step="1" />
        `;
        return html;
    }

    onSettingsChange() {
        this.settings.rate = Number($('#edge_tts_rate').val());
        $('#edge_tts_rate_output').text(this.settings.rate);
        this.settings.provider = String($('#edge_tts_provider').val());
        saveTtsProviderSettings();
    }

    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}`;
            }
        }

        $('#edge_tts_rate').val(this.settings.rate || 0);
        $('#edge_tts_rate_output').text(this.settings.rate || 0);
        $('#edge_tts_rate').on('input', () => { this.onSettingsChange(); });
        $('#edge_tts_provider').val(this.settings.provider || EDGE_TTS_PROVIDER.extras);
        $('#edge_tts_provider').on('change', () => { this.onSettingsChange(); });
        await this.checkReady();

        console.debug('EdgeTTS: Settings loaded');
    }

    /**
    * Perform a simple readiness check by trying to fetch voiceIds
    */
    async checkReady() {
        await this.throwIfModuleMissing();
        await this.fetchTtsVoiceObjects();
    }

    async onRefreshClick() {
        return;
    }

    //#################//
    //  TTS Interfaces //
    //#################//

    /**
     * Get a voice from the TTS provider.
     * @param {string} voiceName Voice name to get
     * @returns {Promise<Object>} Voice object
     */
    async getVoice(voiceName) {
        if (this.voices.length == 0) {
            this.voices = await this.fetchTtsVoiceObjects();
        }
        const match = this.voices.filter(
            voice => voice.name == voiceName,
        )[0];
        if (!match) {
            throw `TTS Voice name ${voiceName} not found`;
        }
        return match;
    }

    /**
     * Generate TTS for a given text.
     * @param {string} text Text to generate TTS for
     * @param {string} voiceId Voice ID to use
     * @returns {Promise<Response>} Fetch response
     */
    async generateTts(text, voiceId) {
        const response = await this.fetchTtsGeneration(text, voiceId);
        return response;
    }

    //###########//
    // API CALLS //
    //###########//
    async fetchTtsVoiceObjects() {
        await this.throwIfModuleMissing();

        const url = this.getVoicesUrl();
        const response = await this.doFetch(url);
        if (!response.ok) {
            throw new Error(`HTTP ${response.status}: ${await response.text()}`);
        }
        let responseJson = await response.json();
        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 }));
        return responseJson;
    }

    /**
     * Preview TTS for a given voice ID.
     * @param {string} id Voice ID
     */
    async previewTtsVoice(id) {
        this.audioElement.pause();
        this.audioElement.currentTime = 0;
        const voice = await this.getVoice(id);
        const text = getPreviewString(voice.lang);
        const response = await this.fetchTtsGeneration(text, id);
        if (!response.ok) {
            throw new Error(`HTTP ${response.status}: ${await response.text()}`);
        }

        const audio = await response.blob();
        const url = URL.createObjectURL(audio);
        this.audioElement.src = url;
        this.audioElement.play();
        this.audioElement.onended = () => URL.revokeObjectURL(url);
    }

    /**
     * Fetch TTS generation from the API.
     * @param {string} inputText Text to generate TTS for
     * @param {string} voiceId Voice ID to use
     * @returns {Promise<Response>} Fetch response
     */
    async fetchTtsGeneration(inputText, voiceId) {
        await this.throwIfModuleMissing();

        console.info(`Generating new TTS for voice_id ${voiceId}`);
        const url = this.getGenerateUrl();
        const response = await this.doFetch(url,
            {
                method: 'POST',
                headers: getRequestHeaders(),
                body: JSON.stringify({
                    'text': inputText,
                    'voice': voiceId,
                    'rate': Number(this.settings.rate),
                }),
            },
        );
        if (!response.ok) {
            toastr.error(response.statusText, 'TTS Generation Failed');
            throw new Error(`HTTP ${response.status}: ${await response.text()}`);
        }
        return response;
    }

    /**
     * Perform a fetch request using the configured provider.
     * @param {string} url URL string
     * @param {any} options Request options
     * @returns {Promise<Response>} Fetch response
     */
    doFetch(url, options) {
        if (this.settings.provider === EDGE_TTS_PROVIDER.extras) {
            return doExtrasFetch(url, options);
        }

        if (this.settings.provider === EDGE_TTS_PROVIDER.plugin) {
            return fetch(url, options);
        }

        throw new Error('Invalid TTS Provider');
    }

    /**
     * Get the URL for the TTS generation endpoint.
     * @returns {string} URL string
     */
    getGenerateUrl() {
        if (this.settings.provider === EDGE_TTS_PROVIDER.extras) {
            const url = new URL(getApiUrl());
            url.pathname = '/api/edge-tts/generate';
            return url.toString();
        }

        if (this.settings.provider === EDGE_TTS_PROVIDER.plugin) {
            return '/api/plugins/edge-tts/generate';
        }

        throw new Error('Invalid TTS Provider');
    }

    /**
     * Get the URL for the TTS voices endpoint.
     * @returns {string} URL object or string
     */
    getVoicesUrl() {
        if (this.settings.provider === EDGE_TTS_PROVIDER.extras) {
            const url = new URL(getApiUrl());
            url.pathname = '/api/edge-tts/list';
            return url.toString();
        }

        if (this.settings.provider === EDGE_TTS_PROVIDER.plugin) {
            return '/api/plugins/edge-tts/list';
        }

        throw new Error('Invalid TTS Provider');
    }

    async throwIfModuleMissing() {
        if (this.settings.provider === EDGE_TTS_PROVIDER.extras && !modules.includes('edge-tts')) {
            const message = 'Edge TTS module not loaded. Add edge-tts to enable-modules and restart the Extras API.';
            // toastr.error(message)
            throw new Error(message);
        }

        if (this.settings.provider === EDGE_TTS_PROVIDER.plugin && !this.isPluginAvailable()) {
            const message = 'Edge TTS Server plugin not loaded. Install it from https://github.com/SillyTavern/SillyTavern-EdgeTTS-Plugin and restart the SillyTavern server.';
            // toastr.error(message)
            throw new Error(message);
        }
    }

    async isPluginAvailable() {
        try {
            const result = await fetch('/api/plugins/edge-tts/probe', {
                method: 'POST',
                headers: getRequestHeaders(),
            });
            return result.ok;
        } catch (e) {
            return false;
        }
    }
}