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

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +60 -120
index.html CHANGED
@@ -8,163 +8,103 @@
8
  textarea {
9
  width: 100%;
10
  height: 150px;
11
- padding: 10px;
12
- font-size: 16px;
13
- box-sizing: border-box;
14
  }
15
  button {
16
- padding: 10px 20px;
17
- margin: 10px 5px 0 0;
18
- font-size: 16px;
19
  cursor: pointer;
20
  }
21
- #download {
22
- display: none;
23
- padding: 10px 20px;
24
- background-color: #4CAF50;
25
- color: white;
26
- text-decoration: none;
27
- border-radius: 5px;
28
- margin-top: 10px;
29
- display: inline-block;
30
- }
31
- #status {
32
- margin-top: 10px;
33
- font-weight: bold;
34
- }
35
  </style>
36
- <!-- Incluir ResponsiveVoice -->
37
- <script src="https://code.responsivevoice.org/responsivevoice.js?key=PCQ3lKHt"></script>
38
  </head>
39
  <body>
40
  <h3>Text to Speech - Google Español</h3>
41
 
42
  <textarea id="lines" placeholder="Ingresa el texto aquí...">Hola, este es un ejemplo de texto a voz que se puede descargar.</textarea><br>
43
 
44
- <button id="speak">Hablar</button>
45
  <button id="pause">Pausar</button>
46
  <button id="resume">Reanudar</button>
47
  <button id="cancel">Cancelar</button>
48
- <a id="download" href="#" download="tts_audio.webm">Descargar Audio</a>
49
- <div id="status"></div>
50
 
51
  <script>
52
- // Verifica si el navegador soporta las APIs necesarias
53
- if (!window.speechSynthesis || !window.MediaRecorder) {
54
- alert("Tu navegador no soporta las APIs necesarias para esta función.");
55
- }
56
-
57
- let chunks = [];
58
  let mediaRecorder;
59
- let stream;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
- // Referencias a elementos del DOM
62
- const speakBtn = document.getElementById("speak");
63
- const pauseBtn = document.getElementById("pause");
64
- const resumeBtn = document.getElementById("resume");
65
- const cancelBtn = document.getElementById("cancel");
66
- const downloadLink = document.getElementById("download");
67
- const statusDiv = document.getElementById("status");
68
 
69
- // Función para iniciar la captura de audio
70
- async function startCapture() {
71
- try {
72
- statusDiv.textContent = "Solicitando permisos para capturar audio...";
73
- // Solicitar captura de audio de la pestaña actual
74
- stream = await navigator.mediaDevices.getDisplayMedia({
75
- audio: true,
76
- video: false // Intentar capturar solo audio
77
- });
78
 
79
- // Verificar si se capturó audio
80
- if (stream.getAudioTracks().length === 0) {
81
- throw new Error("No se capturó ningún audio.");
82
  }
83
-
84
- // Crear MediaRecorder para capturar el audio
85
- mediaRecorder = new MediaRecorder(stream, { mimeType: "audio/webm" });
86
-
87
- mediaRecorder.ondataavailable = function(e) {
88
- if (e.data.size > 0) {
89
- chunks.push(e.data);
90
- }
91
- };
92
-
93
- mediaRecorder.onstop = function() {
94
- const blob = new Blob(chunks, { type: "audio/webm" });
95
- const url = URL.createObjectURL(blob);
96
- downloadLink.href = url;
97
- downloadLink.style.display = "inline-block";
98
- statusDiv.textContent = "Grabación completada. Puedes descargar el audio.";
99
- // Limpiar chunks para futuras grabaciones
100
- chunks = [];
101
- // Detener todas las pistas de audio
102
- stream.getAudioTracks().forEach(track => track.stop());
103
- };
104
-
105
- // Iniciar la grabación
106
- mediaRecorder.start();
107
- statusDiv.textContent = "Grabación iniciada. Generando audio...";
108
-
109
- } catch (err) {
110
- console.error("Error al capturar el audio:", err);
111
- alert("No se pudo capturar el audio. Asegúrate de conceder los permisos necesarios.");
112
- statusDiv.textContent = "Error al capturar el audio.";
113
- }
114
  }
115
 
116
- // Función para detener la captura de audio
117
- function stopCapture() {
118
- if (mediaRecorder && mediaRecorder.state !== "inactive") {
119
- mediaRecorder.stop();
120
- statusDiv.textContent = "Deteniendo la grabación...";
121
- }
122
- }
123
 
124
- // Función para hablar y grabar
125
- function hablarYGrabar() {
126
- const text = document.getElementById("lines").value.trim();
127
- if (text === "") {
128
- alert("Por favor, ingresa algún texto para convertir a voz.");
129
- return;
130
- }
131
 
132
- // Iniciar la captura de audio
133
- startCapture().then(() => {
134
- // Iniciar la síntesis de voz
135
- statusDiv.textContent = "Reproduciendo el audio...";
136
- responsiveVoice.speak(text, "Spanish Latin American Female", {
137
- onend: function() {
138
- // Detener la grabación al finalizar la síntesis
139
- stopCapture();
140
- }
141
- });
142
- });
143
  }
144
 
145
  // Manejo de eventos de los botones
146
- speakBtn.addEventListener("click", () => {
147
- // Limpiar estados anteriores
148
- downloadLink.style.display = "none";
149
- downloadLink.href = "#";
150
- statusDiv.textContent = "";
151
- hablarYGrabar();
152
  });
153
 
154
- pauseBtn.addEventListener("click", () => {
155
  window.speechSynthesis.pause();
156
- statusDiv.textContent = "Grabación y reproducción pausadas.";
157
  });
158
 
159
- resumeBtn.addEventListener("click", () => {
160
  window.speechSynthesis.resume();
161
- statusDiv.textContent = "Grabación y reproducción reanudadas.";
162
  });
163
 
164
- cancelBtn.addEventListener("click", () => {
165
  window.speechSynthesis.cancel();
166
- stopCapture();
167
- statusDiv.textContent = "Grabación y reproducción canceladas.";
 
168
  });
169
  </script>
170
  </body>
 
8
  textarea {
9
  width: 100%;
10
  height: 150px;
 
 
 
11
  }
12
  button {
13
+ padding: 10px;
14
+ margin: 10px 5px;
 
15
  cursor: pointer;
16
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  </style>
 
 
18
  </head>
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);
68
+ const downloadButton = document.getElementById("download");
69
+ downloadButton.style.display = "inline";
70
+ downloadButton.href = audioUrl;
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>