Update index.html
Browse files- index.html +35 -2
index.html
CHANGED
|
@@ -24,18 +24,21 @@
|
|
| 24 |
<button id="pause">Pause</button>
|
| 25 |
<button id="resume">Resume</button>
|
| 26 |
<button id="cancel">Cancel</button>
|
|
|
|
| 27 |
|
| 28 |
<script>
|
| 29 |
let tts = new SpeechSynthesisUtterance();
|
| 30 |
let voices = [];
|
| 31 |
let paragraphs = [];
|
| 32 |
let currentParagraph = 0;
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
// Cargar voces y seleccionar "Google espa帽ol" si est谩 disponible
|
| 35 |
window.speechSynthesis.onvoiceschanged = () => {
|
| 36 |
voices = window.speechSynthesis.getVoices();
|
| 37 |
let selectedVoice = voices.find(voice => voice.name === "Google espa帽ol");
|
| 38 |
-
|
| 39 |
if (selectedVoice) {
|
| 40 |
tts.voice = selectedVoice;
|
| 41 |
tts.volume = 0.5; // Volumen predeterminado
|
|
@@ -51,12 +54,40 @@
|
|
| 51 |
return text.split(/(?<=[.?!])\s+/); // Divide el texto en p谩rrafos por '.' '!' '?'
|
| 52 |
}
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
// Funci贸n para hablar un p谩rrafo
|
| 55 |
function speakParagraph() {
|
| 56 |
if (currentParagraph < paragraphs.length) {
|
| 57 |
tts.text = paragraphs[currentParagraph];
|
| 58 |
window.speechSynthesis.speak(tts);
|
| 59 |
currentParagraph++;
|
|
|
|
|
|
|
| 60 |
}
|
| 61 |
}
|
| 62 |
|
|
@@ -66,7 +97,8 @@
|
|
| 66 |
};
|
| 67 |
|
| 68 |
// Funci贸n para empezar a hablar el texto ingresado
|
| 69 |
-
document.getElementById("speak").addEventListener("click", () => {
|
|
|
|
| 70 |
let text = document.getElementById("lines").value;
|
| 71 |
paragraphs = splitIntoParagraphs(text);
|
| 72 |
currentParagraph = 0;
|
|
@@ -86,6 +118,7 @@
|
|
| 86 |
// Cancelar el discurso
|
| 87 |
document.getElementById("cancel").addEventListener("click", () => {
|
| 88 |
window.speechSynthesis.cancel();
|
|
|
|
| 89 |
});
|
| 90 |
</script>
|
| 91 |
</body>
|
|
|
|
| 24 |
<button id="pause">Pause</button>
|
| 25 |
<button id="resume">Resume</button>
|
| 26 |
<button id="cancel">Cancel</button>
|
| 27 |
+
<button id="download" style="display:none;">Download Audio</button>
|
| 28 |
|
| 29 |
<script>
|
| 30 |
let tts = new SpeechSynthesisUtterance();
|
| 31 |
let voices = [];
|
| 32 |
let paragraphs = [];
|
| 33 |
let currentParagraph = 0;
|
| 34 |
+
let audioChunks = [];
|
| 35 |
+
let mediaRecorder;
|
| 36 |
+
let audioBlob;
|
| 37 |
|
| 38 |
// Cargar voces y seleccionar "Google espa帽ol" si est谩 disponible
|
| 39 |
window.speechSynthesis.onvoiceschanged = () => {
|
| 40 |
voices = window.speechSynthesis.getVoices();
|
| 41 |
let selectedVoice = voices.find(voice => voice.name === "Google espa帽ol");
|
|
|
|
| 42 |
if (selectedVoice) {
|
| 43 |
tts.voice = selectedVoice;
|
| 44 |
tts.volume = 0.5; // Volumen predeterminado
|
|
|
|
| 54 |
return text.split(/(?<=[.?!])\s+/); // Divide el texto en p谩rrafos por '.' '!' '?'
|
| 55 |
}
|
| 56 |
|
| 57 |
+
// Inicializamos el MediaRecorder para capturar el audio
|
| 58 |
+
async function initMediaRecorder() {
|
| 59 |
+
const audioContext = new AudioContext();
|
| 60 |
+
const destination = audioContext.createMediaStreamDestination();
|
| 61 |
+
const utteranceStream = audioContext.createMediaStreamSource(destination.stream);
|
| 62 |
+
mediaRecorder = new MediaRecorder(destination.stream);
|
| 63 |
+
|
| 64 |
+
tts.onstart = () => {
|
| 65 |
+
mediaRecorder.start();
|
| 66 |
+
audioChunks = [];
|
| 67 |
+
};
|
| 68 |
+
|
| 69 |
+
mediaRecorder.ondataavailable = (event) => {
|
| 70 |
+
audioChunks.push(event.data);
|
| 71 |
+
};
|
| 72 |
+
|
| 73 |
+
mediaRecorder.onstop = () => {
|
| 74 |
+
audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
|
| 75 |
+
const audioUrl = URL.createObjectURL(audioBlob);
|
| 76 |
+
const downloadButton = document.getElementById("download");
|
| 77 |
+
downloadButton.style.display = "inline";
|
| 78 |
+
downloadButton.href = audioUrl;
|
| 79 |
+
downloadButton.download = 'tts_audio.wav';
|
| 80 |
+
};
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
// Funci贸n para hablar un p谩rrafo
|
| 84 |
function speakParagraph() {
|
| 85 |
if (currentParagraph < paragraphs.length) {
|
| 86 |
tts.text = paragraphs[currentParagraph];
|
| 87 |
window.speechSynthesis.speak(tts);
|
| 88 |
currentParagraph++;
|
| 89 |
+
} else {
|
| 90 |
+
mediaRecorder.stop(); // Detenemos la grabaci贸n cuando termina el discurso
|
| 91 |
}
|
| 92 |
}
|
| 93 |
|
|
|
|
| 97 |
};
|
| 98 |
|
| 99 |
// Funci贸n para empezar a hablar el texto ingresado
|
| 100 |
+
document.getElementById("speak").addEventListener("click", async () => {
|
| 101 |
+
await initMediaRecorder(); // Inicializamos la grabaci贸n
|
| 102 |
let text = document.getElementById("lines").value;
|
| 103 |
paragraphs = splitIntoParagraphs(text);
|
| 104 |
currentParagraph = 0;
|
|
|
|
| 118 |
// Cancelar el discurso
|
| 119 |
document.getElementById("cancel").addEventListener("click", () => {
|
| 120 |
window.speechSynthesis.cancel();
|
| 121 |
+
mediaRecorder.stop();
|
| 122 |
});
|
| 123 |
</script>
|
| 124 |
</body>
|