tx3bas commited on
Commit
63ee8e5
·
verified ·
1 Parent(s): b3aa776

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +52 -62
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,64 +19,58 @@
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
 
39
  // Cargar voces y seleccionar "Google español" si está disponible
40
  window.speechSynthesis.onvoiceschanged = () => {
41
  voices = window.speechSynthesis.getVoices();
42
- let selectedVoice = voices.find(voice => voice.name === "Google español");
43
  if (selectedVoice) {
44
  tts.voice = selectedVoice;
45
- tts.volume = 0.5; // Volumen predeterminado
46
- tts.rate = 1; // Velocidad predeterminada
47
- tts.pitch = 1; // Tono predeterminado
48
  } else {
49
  alert("La voz 'Google español' no está disponible en este navegador.");
50
  }
51
  };
52
 
53
- // Función para dividir el texto en párrafos
54
- function splitIntoParagraphs(text) {
55
- return text.split(/(?<=[.?!])\s+/); // Divide el texto en párrafos por '.' '!' '?'
56
- }
57
-
58
- // Inicializamos el MediaRecorder para capturar el audio
59
  async function initMediaRecorder() {
60
- const audioContext = new AudioContext();
61
- streamDestination = audioContext.createMediaStreamDestination();
 
62
 
63
- // Conectamos el SpeechSynthesisUtterance al MediaStreamDestination
64
- const utteranceSource = audioContext.createMediaStreamSource(streamDestination.stream);
65
- mediaRecorder = new MediaRecorder(streamDestination.stream);
 
66
 
67
- // Empezamos a grabar cuando empieza el habla
68
- tts.onstart = () => {
69
- mediaRecorder.start();
70
- audioChunks = [];
71
- };
72
-
73
- // Guardamos los datos de audio en chunks
74
  mediaRecorder.ondataavailable = (event) => {
75
- audioChunks.push(event.data);
 
 
76
  };
77
-
78
- // Al finalizar la grabación, generamos el archivo .wav
79
  mediaRecorder.onstop = () => {
80
  audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
81
  const audioUrl = URL.createObjectURL(audioBlob);
@@ -86,45 +81,40 @@
86
  };
87
  }
88
 
89
- // Función para hablar un párrafo
90
- function speakParagraph() {
91
- if (currentParagraph < paragraphs.length) {
92
- tts.text = paragraphs[currentParagraph];
93
- window.speechSynthesis.speak(tts);
94
- currentParagraph++;
95
- } else {
96
- mediaRecorder.stop(); // Detenemos la grabación cuando termina el discurso
97
- }
98
- }
99
 
100
- // Cuando termina de hablar un párrafo, habla el siguiente
101
- tts.onend = function () {
102
- speakParagraph();
103
- };
104
 
105
- // Función para empezar a hablar el texto ingresado
106
- document.getElementById("speak").addEventListener("click", async () => {
107
- await initMediaRecorder(); // Inicializamos la grabación
108
- let text = document.getElementById("lines").value;
109
- paragraphs = splitIntoParagraphs(text);
110
- currentParagraph = 0;
111
- speakParagraph();
 
 
 
 
112
  });
113
 
114
- // Pausar el discurso
115
  document.getElementById("pause").addEventListener("click", () => {
116
  window.speechSynthesis.pause();
117
  });
118
 
119
- // Reanudar el discurso pausado
120
  document.getElementById("resume").addEventListener("click", () => {
121
  window.speechSynthesis.resume();
122
  });
123
 
124
- // Cancelar el discurso
125
  document.getElementById("cancel").addEventListener("click", () => {
126
  window.speechSynthesis.cancel();
127
- mediaRecorder.stop();
 
 
128
  });
129
  </script>
130
  </body>
 
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, este es un ejemplo de texto a voz que se puede descargar.</textarea><br>
23
 
24
+ <button id="speak">Hablar</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
+ // Verifica si el navegador soporta las APIs necesarias
32
+ if (!window.speechSynthesis || !window.MediaRecorder) {
33
+ alert("Tu navegador no soporta las APIs necesarias para esta función.");
34
+ }
35
+
36
  let tts = new SpeechSynthesisUtterance();
37
  let voices = [];
 
 
38
  let audioChunks = [];
39
  let mediaRecorder;
40
  let audioBlob;
 
41
 
42
  // Cargar voces y seleccionar "Google español" si está disponible
43
  window.speechSynthesis.onvoiceschanged = () => {
44
  voices = window.speechSynthesis.getVoices();
45
+ let selectedVoice = voices.find(voice => voice.lang === "es-ES" && voice.name.includes("Google"));
46
  if (selectedVoice) {
47
  tts.voice = selectedVoice;
48
+ tts.volume = 1; // Volumen predeterminado
49
+ tts.rate = 1; // Velocidad predeterminada
50
+ tts.pitch = 1; // Tono predeterminado
51
  } else {
52
  alert("La voz 'Google español' no está disponible en este navegador.");
53
  }
54
  };
55
 
56
+ // Inicializa MediaRecorder usando un AudioContext
 
 
 
 
 
57
  async function initMediaRecorder() {
58
+ const audioContext = new (window.AudioContext || window.webkitAudioContext)();
59
+ const dest = audioContext.createMediaStreamDestination();
60
+ const source = audioContext.createMediaStreamSource(dest.stream);
61
 
62
+ // Conecta la salida de speechSynthesis al AudioContext
63
+ const synth = window.speechSynthesis;
64
+ const synthNode = audioContext.createMediaStreamSource(dest.stream);
65
+ synth.resume(); // Asegura que speechSynthesis esté activo
66
 
67
+ // Crea MediaRecorder para grabar el audio
68
+ mediaRecorder = new MediaRecorder(dest.stream);
 
 
 
 
 
69
  mediaRecorder.ondataavailable = (event) => {
70
+ if (event.data.size > 0) {
71
+ audioChunks.push(event.data);
72
+ }
73
  };
 
 
74
  mediaRecorder.onstop = () => {
75
  audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
76
  const audioUrl = URL.createObjectURL(audioBlob);
 
81
  };
82
  }
83
 
84
+ // Función para reproducir el habla y grabar
85
+ async function speakAndRecord(text) {
86
+ await initMediaRecorder();
87
+ mediaRecorder.start();
 
 
 
 
 
 
88
 
89
+ tts.text = text;
90
+ window.speechSynthesis.speak(tts);
 
 
91
 
92
+ tts.onend = () => {
93
+ mediaRecorder.stop();
94
+ };
95
+ }
96
+
97
+ // Manejo de eventos de los botones
98
+ document.getElementById("speak").addEventListener("click", () => {
99
+ const text = document.getElementById("lines").value;
100
+ audioChunks = [];
101
+ document.getElementById("download").style.display = "none";
102
+ speakAndRecord(text);
103
  });
104
 
 
105
  document.getElementById("pause").addEventListener("click", () => {
106
  window.speechSynthesis.pause();
107
  });
108
 
 
109
  document.getElementById("resume").addEventListener("click", () => {
110
  window.speechSynthesis.resume();
111
  });
112
 
 
113
  document.getElementById("cancel").addEventListener("click", () => {
114
  window.speechSynthesis.cancel();
115
+ if (mediaRecorder && mediaRecorder.state !== "inactive") {
116
+ mediaRecorder.stop();
117
+ }
118
  });
119
  </script>
120
  </body>