Create static/tts.js
Browse files- static/tts.js +61 -0
static/tts.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
async function setEngine() {
|
| 2 |
+
var engine = document.getElementById("engine").value;
|
| 3 |
+
await fetch('/set_engine?engine_name=' + engine);
|
| 4 |
+
}
|
| 5 |
+
|
| 6 |
+
async function speak() {
|
| 7 |
+
var text = document.getElementById("text").value;
|
| 8 |
+
try {
|
| 9 |
+
var url = '/tts?text=' + encodeURIComponent(text);
|
| 10 |
+
var audio = document.getElementById("audio");
|
| 11 |
+
audio.src = url;
|
| 12 |
+
audio.play();
|
| 13 |
+
} catch (error) {
|
| 14 |
+
console.error('Error during fetch or audio playback:', error);
|
| 15 |
+
}
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
async function fetchVoices() {
|
| 19 |
+
try {
|
| 20 |
+
var response = await fetch('/voices');
|
| 21 |
+
if (!response.ok) {
|
| 22 |
+
throw new Error('Network response was not ok: ' + response.statusText);
|
| 23 |
+
}
|
| 24 |
+
var data = await response.json();
|
| 25 |
+
var voicesDropdown = document.getElementById("voice");
|
| 26 |
+
voicesDropdown.innerHTML = ''; // Clear previous options
|
| 27 |
+
data.forEach(function(voice) {
|
| 28 |
+
var option = document.createElement("option");
|
| 29 |
+
option.text = voice;
|
| 30 |
+
option.value = voice;
|
| 31 |
+
voicesDropdown.add(option);
|
| 32 |
+
});
|
| 33 |
+
} catch (error) {
|
| 34 |
+
console.error('Error fetching voices:', error);
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
async function setVoice() {
|
| 39 |
+
var voice = document.getElementById("voice").value;
|
| 40 |
+
try {
|
| 41 |
+
var response = await fetch('/setvoice?voice_name=' + encodeURIComponent(voice));
|
| 42 |
+
if (!response.ok) {
|
| 43 |
+
throw new Error('Network response was not ok: ' + response.statusText);
|
| 44 |
+
}
|
| 45 |
+
console.log('Voice set successfully:', voice);
|
| 46 |
+
} catch (error) {
|
| 47 |
+
console.error('Error setting voice:', error);
|
| 48 |
+
}
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
document.addEventListener('DOMContentLoaded', (event) => {
|
| 52 |
+
document.getElementById("text").value = "This is a text to speech demo text";
|
| 53 |
+
document.getElementById("speakButton").addEventListener("click", speak);
|
| 54 |
+
document.getElementById("engine").addEventListener("change", async function() {
|
| 55 |
+
await setEngine();
|
| 56 |
+
await fetchVoices();
|
| 57 |
+
});
|
| 58 |
+
document.getElementById("voice").addEventListener("change", setVoice);
|
| 59 |
+
|
| 60 |
+
fetchVoices();
|
| 61 |
+
});
|