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

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +59 -52
index.html CHANGED
@@ -1,13 +1,12 @@
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,49 +18,57 @@
19
  <body>
20
  <h3>Text to Speech - Google Español</h3>
21
 
22
- <textarea id="lines" placeholder="Ingresa el texto aquí...">Hola, este es un ejemplo de texto a voz que se puede descargar.</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
- <button id="download" style="display:none;">Descargar Audio</button>
29
 
30
  <script>
31
  let tts = new SpeechSynthesisUtterance();
32
  let voices = [];
 
 
33
  let audioChunks = [];
34
  let mediaRecorder;
35
  let audioBlob;
36
-
37
  // Cargar voces y seleccionar "Google español" si está disponible
38
  window.speechSynthesis.onvoiceschanged = () => {
39
  voices = window.speechSynthesis.getVoices();
40
- let selectedVoice = voices.find(voice => voice.lang === "es-ES" && voice.name.includes("Google"));
41
  if (selectedVoice) {
42
  tts.voice = selectedVoice;
43
- tts.volume = 1; // Volumen predeterminado
44
- tts.rate = 1; // Velocidad predeterminada
45
- tts.pitch = 1; // Tono predeterminado
46
  } else {
47
  alert("La voz 'Google español' no está disponible en este navegador.");
48
  }
49
  };
50
-
51
- // Inicializa MediaRecorder usando un MediaStream del audio de la síntesis
 
 
 
52
  async function initMediaRecorder() {
53
- const audioContext = new (window.AudioContext || window.webkitAudioContext)();
54
- const dest = audioContext.createMediaStreamDestination();
55
- const synth = audioContext.createMediaStreamSource(dest.stream);
56
-
57
- mediaRecorder = new MediaRecorder(dest.stream);
58
-
 
 
 
 
 
59
  mediaRecorder.ondataavailable = (event) => {
60
- if (event.data.size > 0) {
61
- audioChunks.push(event.data);
62
- }
63
  };
64
-
65
  mediaRecorder.onstop = () => {
66
  audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
67
  const audioUrl = URL.createObjectURL(audioBlob);
@@ -71,41 +78,41 @@
71
  downloadButton.download = 'tts_audio.wav';
72
  };
73
  }
74
-
75
- // Función para reproducir el habla y grabar
76
- async function speakAndRecord(text) {
77
- await initMediaRecorder();
78
- audioChunks = [];
79
- mediaRecorder.start();
80
-
81
- tts.text = text;
82
- window.speechSynthesis.speak(tts);
83
-
84
- tts.onend = () => {
85
- mediaRecorder.stop();
86
- };
87
  }
88
-
89
- // Manejo de eventos de los botones
90
- document.getElementById("speak").addEventListener("click", () => {
91
- const text = document.getElementById("lines").value;
92
- speakAndRecord(text);
 
 
 
 
 
 
93
  });
94
-
95
  document.getElementById("pause").addEventListener("click", () => {
96
  window.speechSynthesis.pause();
97
  });
98
-
99
  document.getElementById("resume").addEventListener("click", () => {
100
  window.speechSynthesis.resume();
101
  });
102
-
103
  document.getElementById("cancel").addEventListener("click", () => {
104
  window.speechSynthesis.cancel();
105
- if (mediaRecorder && mediaRecorder.state !== "inactive") {
106
- mediaRecorder.stop();
107
- }
108
  });
109
  </script>
110
  </body>
111
- </html>
 
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
  <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);
 
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>