tx3bas commited on
Commit
4a67086
·
verified ·
1 Parent(s): b668356

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +97 -65
index.html CHANGED
@@ -1,12 +1,13 @@
1
  <!DOCTYPE html>
2
- <html lang="en">
3
  <head>
4
  <meta charset="UTF-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
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 rows="9" cols="60" id="lines">Enter text here...</textarea><br>
22
 
23
- <button id="speak">Speak</button>
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
- let streamDestination;
 
 
 
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
45
- tts.rate = 1; // Velocidad predeterminada
46
- tts.pitch = 1; // Tono predeterminado
47
  } else {
48
  alert("La voz 'Google español' no está disponible en este navegador.");
49
  }
50
  };
51
- // Función para dividir el texto en párrafos
52
- function splitIntoParagraphs(text) {
53
- return text.split(/(?<=[.?!])\s+/); // Divide el texto en párrafos por '.' '!' '?'
 
54
  }
55
- // Inicializamos el MediaRecorder para capturar el audio
56
- async function initMediaRecorder() {
57
- const audioContext = new AudioContext();
58
- streamDestination = audioContext.createMediaStreamDestination();
59
- // Conectamos el SpeechSynthesisUtterance al MediaStreamDestination
60
- const utteranceSource = audioContext.createMediaStreamSource(streamDestination.stream);
61
- mediaRecorder = new MediaRecorder(streamDestination.stream);
62
- // Empezamos a grabar cuando empieza el habla
63
- tts.onstart = () => {
64
- mediaRecorder.start();
65
- audioChunks = [];
66
- };
67
- // Guardamos los datos de audio en chunks
68
- mediaRecorder.ondataavailable = (event) => {
69
- audioChunks.push(event.data);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  };
71
- // Al finalizar la grabación, generamos el archivo .wav
72
  mediaRecorder.onstop = () => {
73
- audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
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.wav';
 
79
  };
 
 
 
 
 
 
 
 
 
 
80
  }
81
- // Función para hablar un párrafo
82
- function speakParagraph() {
83
- if (currentParagraph < paragraphs.length) {
84
- tts.text = paragraphs[currentParagraph];
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
- // Función para empezar a hablar el texto ingresado
96
- document.getElementById("speak").addEventListener("click", async () => {
97
- await initMediaRecorder(); // Inicializamos la grabación
98
- let text = document.getElementById("lines").value;
99
- paragraphs = splitIntoParagraphs(text);
100
- currentParagraph = 0;
101
- speakParagraph();
102
  });
103
- // Pausar el discurso
104
  document.getElementById("pause").addEventListener("click", () => {
105
  window.speechSynthesis.pause();
106
  });
107
- // Reanudar el discurso pausado
108
  document.getElementById("resume").addEventListener("click", () => {
109
  window.speechSynthesis.resume();
110
  });
111
- // Cancelar el discurso
112
  document.getElementById("cancel").addEventListener("click", () => {
113
  window.speechSynthesis.cancel();
114
- mediaRecorder.stop();
 
 
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>