Advertisement
Guest User

Untitled

a guest
Feb 17th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. <script type="text/javascript">
  2.  
  3. var webaudio_tooling_obj = function () {
  4.  
  5. var audioContext = new AudioContext();
  6.  
  7. console.log("audio is starting up ...");
  8.  
  9. var BUFF_SIZE = 16384;
  10.  
  11. var audioInput = null,
  12. microphone_stream = null,
  13. gain_node = null,
  14. script_processor_node = null,
  15. script_processor_fft_node = null,
  16. analyserNode = null;
  17.  
  18. if (!navigator.getUserMedia)
  19. navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
  20. navigator.mozGetUserMedia || navigator.msGetUserMedia;
  21.  
  22. if (navigator.getUserMedia){
  23.  
  24. navigator.getUserMedia({audio:true},
  25. function(stream) {
  26. start_microphone(stream);
  27. },
  28. function(e) {
  29. alert('Error capturing audio.');
  30. }
  31. );
  32.  
  33. } else { alert('getUserMedia not supported in this browser.\nTry using Chrome or Firefox.'); }
  34.  
  35. // ---
  36.  
  37. function show_some_data(given_typed_array, num_row_to_display, label) {
  38.  
  39. var size_buffer = given_typed_array.length;
  40. var index = 0;
  41. var max_index = num_row_to_display;
  42.  
  43. console.log("__________ " + label);
  44.  
  45. for (; index < max_index && index < size_buffer; index += 1) {
  46.  
  47. console.log(given_typed_array[index]);
  48. }
  49. }
  50.  
  51. function process_microphone_buffer(event) {
  52.  
  53. var i, N, inp, microphone_output_buffer;
  54.  
  55. microphone_output_buffer = event.inputBuffer.getChannelData(0); // just mono - 1 channel for now
  56. console.log(microphone_output_buffer);
  57. // microphone_output_buffer <-- this buffer contains current gulp of data size BUFF_SIZE
  58.  
  59. //Probably send it to backend here
  60.  
  61.  
  62. //show_some_data(microphone_output_buffer, 5, "from getChannelData");
  63. }
  64.  
  65. function start_microphone(stream){
  66.  
  67. //gain_node = audioContext.createGain();
  68. //gain_node.connect( audioContext.destination );
  69.  
  70. microphone_stream = audioContext.createMediaStreamSource(stream);
  71. //microphone_stream.connect(gain_node);
  72.  
  73. script_processor_node = audioContext.createScriptProcessor(BUFF_SIZE, 1, 1);
  74. script_processor_node.onaudioprocess = process_microphone_buffer;
  75.  
  76. microphone_stream.connect(script_processor_node);
  77.  
  78. // --- enable volume control for output speakers
  79.  
  80. document.getElementById('volume').addEventListener('change', function() {
  81.  
  82. var curr_volume = 1;//this.value;
  83. //gain_node.gain.value = curr_volume;
  84.  
  85. console.log("curr_volume ", curr_volume);
  86. });
  87.  
  88. // --- setup FFT
  89.  
  90. script_processor_fft_node = audioContext.createScriptProcessor(2048, 1, 1);
  91. script_processor_fft_node.connect(gain_node);
  92.  
  93. analyserNode = audioContext.createAnalyser();
  94. analyserNode.smoothingTimeConstant = 0;
  95. analyserNode.fftSize = 2048;
  96.  
  97. microphone_stream.connect(analyserNode);
  98.  
  99. analyserNode.connect(script_processor_fft_node);
  100.  
  101. script_processor_fft_node.onaudioprocess = function() {
  102.  
  103. // get the average for the first channel
  104. var array = new Uint8Array(analyserNode.frequencyBinCount);
  105. analyserNode.getByteFrequencyData(array);
  106.  
  107. // draw the spectrogram
  108. if (microphone_stream.playbackState == microphone_stream.PLAYING_STATE) {
  109.  
  110. //show_some_data(array, 5, "from fft");
  111. }
  112. };
  113. }
  114.  
  115. }(); // webaudio_tooling_obj = function()
  116.  
  117. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement