detto.js 3.9 KB

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