File size: 9,423 Bytes
69b8b1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
$(document).ready(function() {
    // --- DOM Element References ---
    const amharicTextInput = $('#amharicText');
    const ttsModelSelect = $('#ttsModel');
    const speedInput = $('#speed');
    const speedValueDisplay = $('#speed-value');
    const generateBtn = $('#generateBtn');
    const statusMessage = $('#statusMessage');
    const audioPlayer = $('#audioPlayer');
    const downloadBtn = $('#downloadBtn');
    
    // --- Other References ---
    const API_URL = '/api/tts';
    let lastGeneratedAudioUrl = null; // Variable to store the blob URL

    // --- Event Listeners ---
    ttsModelSelect.on('change', updateConfigVisibility);
    
    // THIS IS THE CORRECTED LINE
    speedInput.on('input', function() { 
        speedValueDisplay.text($(this).val() + 'x'); 
    });

    generateBtn.on('click', function() {
        const text = amharicTextInput.val();
        if (!text.trim()) {
            alert('Please enter some Amharic text.');
            return;
        }

        const requestData = buildRequestData();
        console.log("Sending data to backend:", JSON.stringify(requestData, null, 2));

        // --- 1. Set UI to loading state ---
        setLoadingState(true);

        fetch(API_URL, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(requestData),
        })
        .then(handleResponse)
        .then(data => {
            if (data instanceof Blob) {
                console.log("Received audio blob from backend!");
                if (lastGeneratedAudioUrl) {
                    URL.revokeObjectURL(lastGeneratedAudioUrl);
                }
                lastGeneratedAudioUrl = URL.createObjectURL(data);
                audioPlayer.attr('src', lastGeneratedAudioUrl);
                setControlsEnabled(true);
                showStatusMessage("Audio generated successfully!", "success");
            }
        })
        .catch(error => {
            console.error('Error during fetch operation:', error);
            showStatusMessage(`Error: ${error.message}`, "danger");
            setControlsEnabled(false);
        })
        .finally(() => {
            setLoadingState(false);
        });
    });

    downloadBtn.on('click', function() {
        if (!lastGeneratedAudioUrl) return;
        const link = document.createElement('a');
        link.href = lastGeneratedAudioUrl;
        // Choose file extension based on model
        const model = ttsModelSelect.val();
        const ext = (model === 'mms') ? 'wav' : 'mp3';
        link.download = `amharic_speech.${ext}`;
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
    });

    // --- Helper Functions ---
    function buildRequestData() {
        const model = ttsModelSelect.val();
        let modelSettings = {};
        switch (model) {
            case 'openai': modelSettings.voice = $('#openai-voice').val(); break;
            case 'azure': modelSettings.voice = $('#azure-voice').val(); modelSettings.style = $('#azure-style').val(); break;
            case 'mms': modelSettings.voice = $('#mms-voice').val(); break;
        }
        return {
            text: amharicTextInput.val(),
            model: model,
            speed: speedInput.val(),
            settings: modelSettings
        };
    }

    function setLoadingState(isLoading) {
        if (isLoading) {
            generateBtn.prop('disabled', true).html('<span class="glyphicon glyphicon-refresh spinning"></span> Generating...');
            statusMessage.html('');
            setControlsEnabled(false);
        } else {
            generateBtn.prop('disabled', false).html('<span class="glyphicon glyphicon-bullhorn"></span> ድምፅ ፍጠር (Generate)');
        }
    }

    function setControlsEnabled(isEnabled) {
        if (isEnabled) {
            audioPlayer.removeAttr('disabled');
            downloadBtn.removeAttr('disabled');
        } else {
            audioPlayer.attr('disabled', 'disabled');
            downloadBtn.attr('disabled', 'disabled');
        }
    }

    function showStatusMessage(message, type) {
        statusMessage.html(`<div class="alert alert-${type}">${message}</div>`);
    }

    async function handleResponse(response) {
        if (!response.ok) {
            let errJson = {};
            try { errJson = await response.json(); } catch (e) { /* ignore */ }
            const msg = errJson.details ? `${errJson.error || 'Server returned an error'} - ${errJson.details}` : (errJson.error || 'Server returned an error');
            throw new Error(msg);
        }
        if (response.headers.get("Content-Type").includes("audio")) {
            return response.blob();
        }
        return response.json();
    }
    
    function updateConfigVisibility() {
        $('.model-config').hide();
        const selectedModel = ttsModelSelect.val();
        switch (selectedModel) {
            case 'openai': $('#openai-config').show(); $('#speed-config').show(); break;
            case 'azure': $('#azure-config').show(); $('#speed-config').show(); break;
            case 'mms': $('#mms-config').show(); $('#speed-config').show(); break;
            case 'gtts': $('#speed-config').show(); break;
        }
    }

    // --- Initial page setup ---
    const spinningGlyph = `<style>.spinning{animation:spin 1s infinite linear}@keyframes spin{from{transform:rotate(0deg)}to{transform:rotate(360deg)}}</style>`;
    $('head').append(spinningGlyph);
    updateConfigVisibility();
});

(function () {
  var $text = $('#amharicText');
  var $char = $('#charCount');
  var $model = $('#ttsModel');
  var $speed = $('#speed');
  var $speedVal = $('#speed-value');
  var $status = $('#statusMessage');
  var $gen = $('#generateBtn');
  var $spinner = $('#genSpinner');
  var $audio = $('#audioPlayer');
  var $download = $('#downloadBtn');

  function updateCharCount() {
    var n = ($text.val() || '').length;
    $char.text(n + ' characters');
    savePrefs();
  }

  function updateSpeedValue() {
    $speedVal.text(parseFloat($speed.val()).toFixed(1) + 'x');
    savePrefs();
  }

  function showModelConfig() {
    var v = $model.val();
    $('.model-config').hide();
    if (v === 'openai') $('#openai-config').show();
    if (v === 'azure') $('#azure-config').show();
    if (v === 'mms') $('#mms-config').show();

    // Hint about output format
    var hint = (v === 'mms') ? 'Output: WAV for MMS.' : 'Output: MP3 for gTTS/OpenAI/Azure.';
    $('#formatHint').text(hint);
    savePrefs();
  }

  function loadPrefs() {
    try {
      var p = JSON.parse(localStorage.getItem('ta_prefs') || '{}');
      if (p.text) $text.val(p.text);
      if (p.model) $model.val(p.model);
      if (p.speed) $speed.val(p.speed);
    } catch (e) {}
  }
  function savePrefs() {
    try {
      localStorage.setItem('ta_prefs', JSON.stringify({
        text: $text.val() || '',
        model: $model.val(),
        speed: $speed.val()
      }));
    } catch (e) {}
  }

  function bindButtons() {
    $('#sampleBtn').on('click', function () {
      var sample = 'ሰላም! ይህ ሳምፕል ጽሑፍ ነው። በቀላሉ የአማርኛ ንግግር ድምፅ ለመፍጠር፤ መጀመሪያ የአማርኛ ጽሁፉን እዚህ ይጻፉ ወይ ኮፒ ፔስት ያድርጉት። ቀጥሎ በስተቀኝ ያሉትን ማስተካከያዎች ያስተካክሉ። ከዛም ድምጽ ፍጠር የሚለውን በተን ይጫኑ። ከትንሽ ቆይታዎች በኋላ የተፈጠረውን ድምፅ  ማጫወት ወም ማውረድ ይችላሉ። መልካም ግዜ። ';
      $text.val(sample).trigger('input');
    });
    $('#clearBtn').on('click', function () {
      $text.val('').trigger('input');
      $text.focus();
    });
  }

  // Optional helper: Call window.uiSetLoading(true/false) around your TTS request
  function setLoading(isLoading, msg) {
    if (isLoading) {
      $gen.prop('disabled', true);
      $spinner.show();
      $status
        .removeClass('alert-danger alert-success')
        .addClass('alert alert-info')
        .text(msg || 'Generating audio...');
    } else {
      $gen.prop('disabled', false);
      $spinner.hide();
    }
  }
  window.uiSetLoading = setLoading;

  // When audio loads, enable controls and show success
  $audio.on('loadeddata', function () {
    $audio.prop('disabled', false);
    $download.prop('disabled', false);
    $status
      .removeClass('alert-info alert-danger')
      .addClass('alert alert-success')
      .text('Ready. Press play or download.');
  });

  $(function () {
    $('[data-toggle="tooltip"]').tooltip();

    loadPrefs();
    updateCharCount();
    updateSpeedValue();
    showModelConfig();
    bindButtons();

    $text.on('input', updateCharCount);
    $model.on('change', showModelConfig);
    $speed.on('input change', updateSpeedValue);
  });
})();

$(function () {
  // Smooth scroll for in-page links
  $('a[href^="#"]').on('click', function (e) {
    var target = this.getAttribute('href');
    if (target.length > 1 && $(target).length) {
      e.preventDefault();
      $('html, body').animate({ scrollTop: $(target).offset().top - 60 }, 400);
    }
  });

  // Back to top visibility
  var $top = $('#backToTop');
  $(window).on('scroll', function () {
    if ($(this).scrollTop() > 200) $top.fadeIn();
    else $top.fadeOut();
  });
});