detto.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // detto - cesco@ventuordici.org - 2020
  2. /**
  3. *
  4. * @license magnet:?xt=urn:btih:90dc5c0be029de84e523b9b3922520e79e0e6f08&dn=cc0.txt CC0
  5. */
  6. /* THE CHINOTTO-WARE LICENSE" (Revision 42):
  7. * cesco@ventuordici.org wrote this code.
  8. * As long as you retain this notice you can do whatever you want with this stuff.
  9. * I reserve the right to do the absolute minimum provided by law, up to and including absolutely nothing.
  10. * If we meet some day, and you think this stuff is worth it, you can buy me a chinotto in return.
  11. */
  12. var audioCtx;
  13. const canvas = document.querySelector('.visualizer');
  14. canvasCtx = canvas.getContext("2d");
  15. document.addEventListener('DOMContentLoaded', function() {
  16. if(window.location.hash) {
  17. document.getElementById('visualizer').style.display = 'none';
  18. document.getElementById('record_audio').style.display = 'none';
  19. document.getElementById('url').style.display = 'none';
  20. document.getElementById('stop_record').style.display = 'none';
  21. var downloadbutton = document.getElementById('download');
  22. if (downloadbutton) { downloadbutton.addEventListener('click', content_download, false); }
  23. } else {
  24. document.getElementById('player').style.display = 'none';
  25. document.getElementById('download').style.display = 'none';
  26. var recordbutton = document.getElementById('record_audio');
  27. if (recordbutton) {
  28. recordbutton.addEventListener('click', processa_audio, false);
  29. }
  30. var stopbutton = document.getElementById('stop_record');
  31. if (stopbutton) { stopbutton.addEventListener('click', stop_recorder, false); }
  32. }
  33. }, false);
  34. function processa_audio(){
  35. filename = "";
  36. fileext = ".webm";
  37. var constraints = { audio: true };
  38. var options = {
  39. audioBitsPerSecond : 64000,
  40. mimeType : 'audio/webm;codecs=opus'
  41. }
  42. audioCtx = new AudioContext();
  43. navigator.mediaDevices.getUserMedia(constraints).then(stream => {
  44. $('body').trigger('recording-start-ok')
  45. window.localStream = stream;
  46. const recorder = new MediaRecorder(stream, options);
  47. window.localRecorder = recorder;
  48. console.log("creato recorder " + recorder );
  49. visualize(stream);
  50. const chunks = [];
  51. recorder.ondataavailable = function(e) {
  52. // console.log("aggiungo chunk");
  53. chunks.push(e.data);
  54. }
  55. recorder.onstop = function(e) {
  56. console.log("stoppato recorder");
  57. const blob = new Blob(chunks, { type: 'audio/webm' });
  58. var reader = new FileReader();
  59. reader.onload = function(event) {
  60. console.log("reader loaded");
  61. var content = event.target.result;
  62. $('body').trigger('recording-stop-ready', content)
  63. }
  64. reader.readAsArrayBuffer(blob);
  65. }
  66. recorder.start(1000);
  67. }).catch(function onMicDenied(err) {
  68. console.log('ouch!', err)
  69. $('body').trigger('recording-start-fail', err)
  70. });
  71. }
  72. function stop_recorder(stream){
  73. console.log("stopping");
  74. localRecorder.stop();
  75. localStream.getAudioTracks()[0].stop();
  76. localStream.getTracks().forEach(track => track.stop());
  77. }
  78. function visualize(stream) {
  79. console.log("start visualize");
  80. if(!audioCtx) {
  81. audioCtx = new AudioContext();
  82. console.log("creating audioctx" + audioCtx);
  83. }
  84. const source = audioCtx.createMediaStreamSource(stream);
  85. const analyser = audioCtx.createAnalyser();
  86. console.log("created analyser" + analyser);
  87. analyser.fftSize = 2048;
  88. const bufferLength = analyser.frequencyBinCount;
  89. const dataArray = new Uint8Array(bufferLength);
  90. source.connect(analyser);
  91. draw()
  92. function draw() {
  93. // console.log("start draw");
  94. WIDTH = canvas.width
  95. HEIGHT = canvas.height;
  96. requestAnimationFrame(draw);
  97. analyser.getByteTimeDomainData(dataArray);
  98. canvasCtx.fillStyle = 'rgb(255, 255, 255)';
  99. canvasCtx.fillRect(0, 0, WIDTH, HEIGHT);
  100. canvasCtx.lineWidth = 2;
  101. canvasCtx.strokeStyle = 'rgb(150, 0, 0)';
  102. canvasCtx.beginPath();
  103. let sliceWidth = WIDTH * 1.0 / bufferLength;
  104. let x = 0;
  105. for(let i = 0; i < bufferLength; i++) {
  106. let v = dataArray[i] / 128.0;
  107. let y = v * HEIGHT/2;
  108. if(i === 0) {
  109. canvasCtx.moveTo(x, y);
  110. } else {
  111. canvasCtx.lineTo(x, y);
  112. }
  113. x += sliceWidth;
  114. }
  115. canvasCtx.lineTo(canvas.width, canvas.height/2);
  116. canvasCtx.stroke();
  117. }
  118. }