Spaces:
Sleeping
Sleeping
File size: 11,946 Bytes
0ceb99b 774783d 0ceb99b 774783d 0ceb99b a50d0af 0ceb99b dcd0100 0ceb99b 774783d 0ceb99b 774783d 0ceb99b 774783d 0ceb99b 774783d 0ceb99b 774783d 0ceb99b 774783d 0ceb99b 774783d 0ceb99b 774783d 0ceb99b 774783d 3cacbe8 957ec1f 0ceb99b 774783d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
//webkitURL is deprecated but nevertheless
URL = window.URL || window.webkitURL;
var gumStream; //stream from getUserMedia()
var rec; //Recorder.js object
var input; //MediaStreamAudioSourceNode we'll be recording
// shim for AudioContext when it's not avb.
var AudioContext = window.AudioContext || window.webkitAudioContext;
var audioContext //audio context to help us record
var recordButton = document.getElementById("recordButton");
var stopButton = document.getElementById("stopButton");
//var pauseButton = document.getElementById("pauseButton");
//add events to those 2 buttons
recordButton.addEventListener("click", startRecording);
stopButton.addEventListener("click", stopRecording);
//pauseButton.addEventListener("click", pauseRecording);
function startRecording() {
console.log("recordButton clicked");
/*
Simple constraints object, for more advanced audio features see
https://addpipe.com/blog/audio-constraints-getusermedia/
*/
var constraints = { audio: true, video: false }
/*
Disable the record button until we get a success or fail from getUserMedia()
*/
recordButton.disabled = true;
stopButton.disabled = false;
//pauseButton.disabled = false
/*
We're using the standard promise based getUserMedia()
https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
*/
navigator.mediaDevices.getUserMedia(constraints).then(function (stream) {
console.log("getUserMedia() success, stream created, initializing Recorder.js ...");
/*
create an audio context after getUserMedia is called
sampleRate might change after getUserMedia is called, like it does on macOS when recording through AirPods
the sampleRate defaults to the one set in your OS for your playback device
*/
audioContext = new AudioContext();
//update the format
document.getElementById("formats").innerHTML = "Format: 1 channel pcm @ " + audioContext.sampleRate / 1000 + "kHz"
/* assign to gumStream for later use */
gumStream = stream;
/* use the stream */
input = audioContext.createMediaStreamSource(stream);
/*
Create the Recorder object and configure to record mono sound (1 channel)
Recording 2 channels will double the file size
*/
rec = new Recorder(input, { numChannels: 1 })
//start the recording process
rec.record()
console.log("Recording started");
}).catch(function (err) {
//enable the record button if getUserMedia() fails
recordButton.disabled = false;
stopButton.disabled = true;
//pauseButton.disabled = true
});
}
//function pauseRecording() {
// console.log("pauseButton clicked rec.recording=", rec.recording);
// if (rec.recording) {
// //pause
// rec.stop();
// pauseButton.innerHTML = "Resume";
// } else {
// //resume
// rec.record()
// pauseButton.innerHTML = "Pause";
// }
//}
function stopRecording() {
console.log("stopButton clicked");
//disable the stop button, enable the record too allow for new recordings
stopButton.disabled = true;
recordButton.disabled = false;
//pauseButton.disabled = true;
//reset button just in case the recording is stopped while paused
//pauseButton.innerHTML = "Pause";
//tell the recorder to stop the recording
rec.stop();
//stop microphone access
gumStream.getAudioTracks()[0].stop();
//create the wav blob and pass it on to createDownloadLink
//rec.exportWAV(createDownloadLink);
// Exportar los datos de audio como un Blob una vez que la grabaci n haya finalizado
rec.exportWAV(function (blob) {
// La funci n de devoluci n de llamada se llama con el Blob que contiene los datos de audio en formato WAV
// Puedes utilizar este Blob como desees, por ejemplo, crear una URL para descargarlo
var url = URL.createObjectURL(blob);
// Puedes utilizar audioUrl para reproducir o descargar el archivo de audio
/////////////////////////////////////////////////////////////////////////
//var url = URL.createObjectURL(blob);
var au = document.createElement('audio');
var li = document.createElement('li');
var link = document.createElement('a');
//name of .wav file to use during upload and download (without extendion)
var filename = new Date().toISOString();
//add controls to the <audio> element
au.controls = true;
au.src = url;
//save to disk link
link.href = url;
link.download = filename + ".wav"; //download forces the browser to donwload the file using the filename
link.innerHTML = "Save to disk";
//add the new audio element to li
li.appendChild(au);
//add the filename to the li
li.appendChild(document.createTextNode(filename + ".wav "))
//add the save to disk link to li
li.appendChild(link);
//upload link
var upload = document.createElement('a');
upload.href = "#";
upload.innerHTML = "Upload";
//upload.addEventListener("click", function (event) {
var xhr = new XMLHttpRequest();
xhr.onload = function (e) {
if (this.readyState === 4) {
console.log("Server returned: ", e.target.responseText);
}
};
// Supongamos que "data" es una cadena de bytes en formato WAV
var formData = new FormData();
// Supongamos que "audioBlob" es un objeto Blob que contiene el audio WAV
formData.append("audio_data", blob, "archivo.wav");
xhr.open("POST", "/escuchar_trauma", true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
// Manejar la respuesta del servidor
console.log("Respuesta del servidor:", xhr.responseText);
////////////////////////////////////////////////////////
// Muestra el resultado del reconocimiento en el cuadro de texto
//document.getElementById("responseTextBox").value = xhr.responseText;
// Buscar el contenido dentro de las etiquetas <p></p>
//var parser = new DOMParser();
//var responseHTML = parser.parseFromString(xhr.responseText, 'text/html');
/*var paragraphContent = responseHTML.querySelector('p').textContent;*/
// Muestra el resultado del reconocimiento en el cuadro de texto
//document.getElementById("responseTextBox").value = paragraphContent;
// Muestra el resultado del reconocimiento como texto plano
//var textElement = document.getElementById("textElement"); // Reemplaza "textElement" con el ID adecuado
//textElement.textContent = paragraphContent;
//////////////////////////////////////////////////////////
}
};
xhr.send(formData);
//////////////////////////////////////////
// 4. This will be called after the response is received
xhr.onload = function () {
if (xhr.status != 200) {
// analyze HTTP status of the response
alert(`Error ${xhr.status}: ${xhr.statusText}`);
// e.g. 404: Not Found
} else { // show the result
$('body').html(xhr.response)
}
};
///////////////////////////////////////////
//});
//li.appendChild(document.createTextNode(" "))//add a space in between
//li.appendChild(upload)//add the upload link to li
//add the li element to the ol
recordingsList.appendChild(li);
});
}
//function createDownloadLink(blob) {
// var url = URL.createObjectURL(blob);
// var au = document.createElement('audio');
// var li = document.createElement('li');
// var link = document.createElement('a');
// //name of .wav file to use during upload and download (without extendion)
// var filename = new Date().toISOString();
// //add controls to the <audio> element
// au.controls = true;
// au.src = url;
// //save to disk link
// link.href = url;
// link.download = filename + ".wav"; //download forces the browser to donwload the file using the filename
// link.innerHTML = "Save to disk";
// //add the new audio element to li
// li.appendChild(au);
// //add the filename to the li
// li.appendChild(document.createTextNode(filename + ".wav "))
// //add the save to disk link to li
// li.appendChild(link);
// //upload link
// var upload = document.createElement('a');
// upload.href = "#";
// upload.innerHTML = "Upload";
// upload.addEventListener("click", function (event) {
// var xhr = new XMLHttpRequest();
// xhr.onload = function (e) {
// if (this.readyState === 4) {
// console.log("Server returned: ", e.target.responseText);
// }
// };
// // Supongamos que "data" es una cadena de bytes en formato WAV
// var formData = new FormData();
// // Supongamos que "audioBlob" es un objeto Blob que contiene el audio WAV
// formData.append("audio_data", blob, "archivo.wav");
// xhr.open("POST", "/escuchar_trauma", true);
// xhr.onreadystatechange = function () {
// if (xhr.readyState === 4 && xhr.status === 200) {
// // Manejar la respuesta del servidor
// console.log("Respuesta del servidor:", xhr.responseText);
// ////////////////////////////////////////////////////////
// // Muestra el resultado del reconocimiento en el cuadro de texto
// //document.getElementById("responseTextBox").value = xhr.responseText;
// // Buscar el contenido dentro de las etiquetas <p></p>
// //var parser = new DOMParser();
// //var responseHTML = parser.parseFromString(xhr.responseText, 'text/html');
// /*var paragraphContent = responseHTML.querySelector('p').textContent;*/
// // Muestra el resultado del reconocimiento en el cuadro de texto
// //document.getElementById("responseTextBox").value = paragraphContent;
// // Muestra el resultado del reconocimiento como texto plano
// //var textElement = document.getElementById("textElement"); // Reemplaza "textElement" con el ID adecuado
// //textElement.textContent = paragraphContent;
// //////////////////////////////////////////////////////////
// }
// };
// xhr.send(formData);
// //////////////////////////////////////////
// // 4. This will be called after the response is received
// xhr.onload = function () {
// if (xhr.status != 200) {
// // analyze HTTP status of the response
// alert(`Error ${xhr.status}: ${xhr.statusText}`);
// // e.g. 404: Not Found
// } else { // show the result
// $('body').html(xhr.response)
// }
// };
// ///////////////////////////////////////////
// })
// li.appendChild(document.createTextNode(" "))//add a space in between
// li.appendChild(upload)//add the upload link to li
// //add the li element to the ol
// recordingsList.appendChild(li);
//} |