Update index.html
Browse files- index.html +97 -65
index.html
CHANGED
|
@@ -1,12 +1,13 @@
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
-
<html lang="
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
-
<meta name="viewport" content="width=device-width
|
| 6 |
-
<title>Text to Speech</title>
|
| 7 |
<style>
|
| 8 |
textarea {
|
| 9 |
width: 100%;
|
|
|
|
| 10 |
}
|
| 11 |
button {
|
| 12 |
padding: 10px;
|
|
@@ -18,101 +19,132 @@
|
|
| 18 |
<body>
|
| 19 |
<h3>Text to Speech - Google Español</h3>
|
| 20 |
|
| 21 |
-
<textarea
|
| 22 |
|
| 23 |
-
<button id="speak">
|
| 24 |
-
<button id="pause">
|
| 25 |
-
<button id="resume">
|
| 26 |
-
<button id="cancel">
|
| 27 |
-
<
|
| 28 |
|
| 29 |
<script>
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
let voices = [];
|
| 32 |
-
let paragraphs = [];
|
| 33 |
-
let currentParagraph = 0;
|
| 34 |
-
let audioChunks = [];
|
| 35 |
let mediaRecorder;
|
| 36 |
-
let
|
| 37 |
-
let
|
|
|
|
|
|
|
|
|
|
| 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.
|
| 42 |
if (selectedVoice) {
|
| 43 |
tts.voice = selectedVoice;
|
| 44 |
-
tts.volume =
|
| 45 |
-
tts.rate = 1;
|
| 46 |
-
tts.pitch = 1;
|
| 47 |
} else {
|
| 48 |
alert("La voz 'Google español' no está disponible en este navegador.");
|
| 49 |
}
|
| 50 |
};
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
| 54 |
}
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
mediaRecorder.
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
};
|
| 71 |
-
|
| 72 |
mediaRecorder.onstop = () => {
|
| 73 |
-
audioBlob = new Blob(
|
| 74 |
const audioUrl = URL.createObjectURL(audioBlob);
|
| 75 |
const downloadButton = document.getElementById("download");
|
| 76 |
downloadButton.style.display = "inline";
|
| 77 |
downloadButton.href = audioUrl;
|
| 78 |
-
downloadButton.download = 'tts_audio.
|
|
|
|
| 79 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
}
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
window.speechSynthesis.speak(tts);
|
| 86 |
-
currentParagraph++;
|
| 87 |
-
} else {
|
| 88 |
-
mediaRecorder.stop(); // Detenemos la grabación cuando termina el discurso
|
| 89 |
-
}
|
| 90 |
-
}
|
| 91 |
-
// Cuando termina de hablar un párrafo, habla el siguiente
|
| 92 |
-
tts.onend = function () {
|
| 93 |
-
speakParagraph();
|
| 94 |
};
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
currentParagraph = 0;
|
| 101 |
-
speakParagraph();
|
| 102 |
});
|
| 103 |
-
|
| 104 |
document.getElementById("pause").addEventListener("click", () => {
|
| 105 |
window.speechSynthesis.pause();
|
| 106 |
});
|
| 107 |
-
|
| 108 |
document.getElementById("resume").addEventListener("click", () => {
|
| 109 |
window.speechSynthesis.resume();
|
| 110 |
});
|
| 111 |
-
|
| 112 |
document.getElementById("cancel").addEventListener("click", () => {
|
| 113 |
window.speechSynthesis.cancel();
|
| 114 |
-
mediaRecorder.
|
|
|
|
|
|
|
| 115 |
});
|
| 116 |
</script>
|
| 117 |
</body>
|
| 118 |
-
</html>
|
|
|
|
| 1 |
<!DOCTYPE html>
|
| 2 |
+
<html lang="es">
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width">
|
| 6 |
+
<title>Text to Speech Descargar Audio</title>
|
| 7 |
<style>
|
| 8 |
textarea {
|
| 9 |
width: 100%;
|
| 10 |
+
height: 150px;
|
| 11 |
}
|
| 12 |
button {
|
| 13 |
padding: 10px;
|
|
|
|
| 19 |
<body>
|
| 20 |
<h3>Text to Speech - Google Español</h3>
|
| 21 |
|
| 22 |
+
<textarea id="lines" placeholder="Ingresa el texto aquí...">Hola, soy Juan. ¿Tú quién eres? No te conozco.</textarea><br>
|
| 23 |
|
| 24 |
+
<button id="speak">Hablar y Grabar</button>
|
| 25 |
+
<button id="pause">Pausar</button>
|
| 26 |
+
<button id="resume">Reanudar</button>
|
| 27 |
+
<button id="cancel">Cancelar</button>
|
| 28 |
+
<a id="download" style="display:none;">Descargar Audio</a>
|
| 29 |
|
| 30 |
<script>
|
| 31 |
+
// Verifica si el navegador soporta las APIs necesarias
|
| 32 |
+
if (!window.speechSynthesis || !window.MediaRecorder || !navigator.mediaDevices.getDisplayMedia) {
|
| 33 |
+
alert("Tu navegador no soporta las APIs necesarias para esta función.");
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
let voices = [];
|
|
|
|
|
|
|
|
|
|
| 37 |
let mediaRecorder;
|
| 38 |
+
let chunks = [];
|
| 39 |
+
let tts = new SpeechSynthesisUtterance();
|
| 40 |
+
let fragments = [];
|
| 41 |
+
let currentFragment = 0;
|
| 42 |
+
|
| 43 |
// Cargar voces y seleccionar "Google español" si está disponible
|
| 44 |
window.speechSynthesis.onvoiceschanged = () => {
|
| 45 |
voices = window.speechSynthesis.getVoices();
|
| 46 |
+
let selectedVoice = voices.find(voice => voice.lang === "es-ES" && voice.name.includes("Google"));
|
| 47 |
if (selectedVoice) {
|
| 48 |
tts.voice = selectedVoice;
|
| 49 |
+
tts.volume = .8;
|
| 50 |
+
tts.rate = 1.1;
|
| 51 |
+
tts.pitch = 1;
|
| 52 |
} else {
|
| 53 |
alert("La voz 'Google español' no está disponible en este navegador.");
|
| 54 |
}
|
| 55 |
};
|
| 56 |
+
|
| 57 |
+
// Función para dividir el texto en fragmentos (párrafos)
|
| 58 |
+
function dividirTexto(texto) {
|
| 59 |
+
return texto.match(/[^.!?]+[.!?]+/g) || [texto];
|
| 60 |
}
|
| 61 |
+
|
| 62 |
+
// Función para reproducir el siguiente fragmento
|
| 63 |
+
function reproducirSiguienteFragmento() {
|
| 64 |
+
if (currentFragment < fragments.length) {
|
| 65 |
+
tts.text = fragments[currentFragment].trim();
|
| 66 |
+
window.speechSynthesis.speak(tts);
|
| 67 |
+
currentFragment++;
|
| 68 |
+
} else {
|
| 69 |
+
// Cuando termine de reproducir todos los fragmentos, detener la grabación
|
| 70 |
+
mediaRecorder.stop();
|
| 71 |
+
}
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
async function speakAndRecord(text) {
|
| 75 |
+
// Solicitar permisos para capturar el audio
|
| 76 |
+
let stream;
|
| 77 |
+
try {
|
| 78 |
+
stream = await navigator.mediaDevices.getDisplayMedia({ audio: true});
|
| 79 |
+
} catch (err) {
|
| 80 |
+
console.error("Error al capturar el audio:", err);
|
| 81 |
+
alert("No se pudo capturar el audio. Asegúrate de conceder los permisos necesarios.");
|
| 82 |
+
return;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
// Eliminar las pistas de video
|
| 86 |
+
const videoTracks = stream.getVideoTracks();
|
| 87 |
+
videoTracks.forEach(track => {
|
| 88 |
+
track.stop();
|
| 89 |
+
stream.removeTrack(track);
|
| 90 |
+
});
|
| 91 |
+
|
| 92 |
+
// Crear MediaRecorder para capturar el audio
|
| 93 |
+
mediaRecorder = new MediaRecorder(stream, { mimeType: 'audio/webm' });
|
| 94 |
+
chunks = [];
|
| 95 |
+
|
| 96 |
+
mediaRecorder.ondataavailable = (e) => {
|
| 97 |
+
if (e.data.size > 0) {
|
| 98 |
+
chunks.push(e.data);
|
| 99 |
+
}
|
| 100 |
};
|
| 101 |
+
|
| 102 |
mediaRecorder.onstop = () => {
|
| 103 |
+
const audioBlob = new Blob(chunks, { type: 'audio/webm' });
|
| 104 |
const audioUrl = URL.createObjectURL(audioBlob);
|
| 105 |
const downloadButton = document.getElementById("download");
|
| 106 |
downloadButton.style.display = "inline";
|
| 107 |
downloadButton.href = audioUrl;
|
| 108 |
+
downloadButton.download = 'tts_audio.webm';
|
| 109 |
+
downloadButton.textContent = 'Descargar Audio';
|
| 110 |
};
|
| 111 |
+
|
| 112 |
+
// Iniciar la grabación
|
| 113 |
+
mediaRecorder.start();
|
| 114 |
+
|
| 115 |
+
// Dividir el texto en fragmentos
|
| 116 |
+
fragments = dividirTexto(text);
|
| 117 |
+
currentFragment = 0;
|
| 118 |
+
|
| 119 |
+
// Reproducir el primer fragmento
|
| 120 |
+
reproducirSiguienteFragmento();
|
| 121 |
}
|
| 122 |
+
|
| 123 |
+
// Cuando termine de hablar un fragmento, pasar al siguiente
|
| 124 |
+
tts.onend = () => {
|
| 125 |
+
reproducirSiguienteFragmento();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
};
|
| 127 |
+
|
| 128 |
+
// Manejo de eventos de los botones
|
| 129 |
+
document.getElementById("speak").addEventListener("click", () => {
|
| 130 |
+
const text = document.getElementById("lines").value;
|
| 131 |
+
speakAndRecord(text);
|
|
|
|
|
|
|
| 132 |
});
|
| 133 |
+
|
| 134 |
document.getElementById("pause").addEventListener("click", () => {
|
| 135 |
window.speechSynthesis.pause();
|
| 136 |
});
|
| 137 |
+
|
| 138 |
document.getElementById("resume").addEventListener("click", () => {
|
| 139 |
window.speechSynthesis.resume();
|
| 140 |
});
|
| 141 |
+
|
| 142 |
document.getElementById("cancel").addEventListener("click", () => {
|
| 143 |
window.speechSynthesis.cancel();
|
| 144 |
+
if (mediaRecorder && mediaRecorder.state !== "inactive") {
|
| 145 |
+
mediaRecorder.stop();
|
| 146 |
+
}
|
| 147 |
});
|
| 148 |
</script>
|
| 149 |
</body>
|
| 150 |
+
</html>
|