app.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* global Recorder */
  2. //webkitURL is deprecated but nevertheless
  3. URL = window.URL || window.webkitURL;
  4. var gumStream; //stream from getUserMedia()
  5. var rec;
  6. var input; //MediaStreamAudioSourceNode we'll be recording
  7. // shim for AudioContext when it's not avb.
  8. var AudioContext = window.AudioContext || window.webkitAudioContext;
  9. var audioContext //audio context to help us record
  10. var recordButton = document.getElementById("recordButton");
  11. var stopButton = document.getElementById("stopButton");
  12. var pauseButton = document.getElementById("pauseButton");
  13. //add events to those 2 buttons
  14. recordButton.addEventListener("click", startRecording);
  15. stopButton.addEventListener("click", stopRecording);
  16. pauseButton.addEventListener("click", pauseRecording);
  17. function startRecording() {
  18. console.log("recordButton clicked");
  19. console.log(rec)
  20. /*
  21. Simple constraints object, for more advanced audio features see
  22. https://addpipe.com/blog/audio-constraints-getusermedia/
  23. */
  24. var constraints = { audio: true, video:false }
  25. /*
  26. Disable the record button until we get a success or fail from getUserMedia()
  27. */
  28. recordButton.disabled = true;
  29. stopButton.disabled = false;
  30. pauseButton.disabled = false
  31. /*
  32. We're using the standard promise based getUserMedia()
  33. https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
  34. */
  35. navigator.mediaDevices.getUserMedia(constraints).then(function(stream) {
  36. console.log("getUserMedia() success, stream created, initializing Recorder.js ...");
  37. /*
  38. create an audio context after getUserMedia is called
  39. sampleRate might change after getUserMedia is called, like it does on macOS when recording through AirPods
  40. the sampleRate defaults to the one set in your OS for your playback device
  41. */
  42. audioContext = new AudioContext();
  43. //update the format
  44. document.getElementById("formats").innerHTML="Format: 1 channel pcm @ "+audioContext.sampleRate/1000+"kHz"
  45. /* assign to gumStream for later use */
  46. gumStream = stream;
  47. /* use the stream */
  48. input = audioContext.createMediaStreamSource(stream);
  49. /*
  50. Create the Recorder object and configure to record mono sound (1 channel)
  51. Recording 2 channels will double the file size
  52. */
  53. rec = new Recorder(input,{numChannels:1})
  54. //start the recording process
  55. rec.record()
  56. console.log("Recording started");
  57. }).catch(function(err) {
  58. //enable the record button if getUserMedia() fails
  59. recordButton.disabled = false;
  60. stopButton.disabled = true;
  61. pauseButton.disabled = true
  62. });
  63. }
  64. function pauseRecording(){
  65. console.log("pauseButton clicked rec.recording=",rec.recording );
  66. if (rec.recording){
  67. //pause
  68. rec.stop();
  69. pauseButton.innerHTML="Riprendi";
  70. }else{
  71. //resume
  72. rec.record()
  73. pauseButton.innerHTML="Pausa";
  74. }
  75. }
  76. function stopRecording() {
  77. console.log("stopButton clicked");
  78. //disable the stop button, enable the record too allow for new recordings
  79. stopButton.disabled = true;
  80. recordButton.disabled = false;
  81. pauseButton.disabled = true;
  82. //reset button just in case the recording is stopped while paused
  83. pauseButton.innerHTML="Pausa";
  84. //tell the recorder to stop the recording
  85. rec.stop();
  86. //stop microphone access
  87. gumStream.getAudioTracks()[0].stop();
  88. //create the wav blob and pass it on to createDownloadLink
  89. rec.exportWAV(createDownloadLink)
  90. }
  91. function createDownloadLink(blob) {
  92. console.log(blob)
  93. var url = URL.createObjectURL(blob);
  94. var au = document.createElement('audio');
  95. var li = document.createElement('li');
  96. var link = document.createElement('a');
  97. //name of .wav file to use during upload and download (without extendion)
  98. var filename = new Date().toISOString();
  99. //add controls to the <audio> element
  100. au.controls = true;
  101. au.src = url;
  102. //save to disk link
  103. link.classList.add('button')
  104. link.href = url;
  105. link.download = filename+".wav"; //download forces the browser to donwload the file using the filename
  106. link.innerHTML = "Salva file su disco";
  107. //add the new audio element to li
  108. li.appendChild(au)
  109. //add the filename to the li
  110. li.appendChild(document.createTextNode(filename+".wav"))
  111. //add the save to disk link to li
  112. li.appendChild(link);
  113. //upload link
  114. var upload = document.createElement('a');
  115. upload.classList.add('button')
  116. upload.href="#";
  117. upload.innerHTML = "Upload";
  118. upload.addEventListener("click", function(event){
  119. var xhr =new XMLHttpRequest();
  120. xhr.onload=function(e) {
  121. if(this.readyState === 4) {
  122. console.log("Server returned: ",e.target.responseText);
  123. }
  124. };
  125. var fd=new FormData();
  126. fd.append("audio_data",blob, filename);
  127. xhr.open("POST","upload/" + $('body').data('site'),true);
  128. xhr.send(fd);
  129. })
  130. li.appendChild(document.createTextNode (" "))//add a space in between
  131. li.appendChild(upload)//add the upload link to li
  132. //add the li element to the ol
  133. recordingsList.appendChild(li);
  134. }