从浏览器访问麦克风 - Javascript

poi*_*occ 33 javascript audio microphone recording web-audio-api

是否可以使用客户端JavaScript从浏览器访问麦克风(内置或辅助)?

理想情况下,它会将录制的音频存储在浏览器中.谢谢!

Sco*_*and 45

在这里,我们使用getUserMedia()将麦克风音频捕获为Web Audio API事件循环缓冲区 - 打印每个音频事件循环缓冲区的时域和频域片段(可在浏览器控制台中查看,只需按键F12或ctrl + shift + i)

<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>capture microphone audio into buffer</title>

<script type="text/javascript">


  var webaudio_tooling_obj = function () {

    var audioContext = new AudioContext();

    console.log("audio is starting up ...");

    var BUFF_SIZE = 16384;

    var audioInput = null,
        microphone_stream = null,
        gain_node = null,
        script_processor_node = null,
        script_processor_fft_node = null,
        analyserNode = null;

    if (!navigator.getUserMedia)
            navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
                          navigator.mozGetUserMedia || navigator.msGetUserMedia;

    if (navigator.getUserMedia){

        navigator.getUserMedia({audio:true}, 
          function(stream) {
              start_microphone(stream);
          },
          function(e) {
            alert('Error capturing audio.');
          }
        );

    } else { alert('getUserMedia not supported in this browser.'); }

    // ---

    function show_some_data(given_typed_array, num_row_to_display, label) {

        var size_buffer = given_typed_array.length;
        var index = 0;
        var max_index = num_row_to_display;

        console.log("__________ " + label);

        for (; index < max_index && index < size_buffer; index += 1) {

            console.log(given_typed_array[index]);
        }
    }

    function process_microphone_buffer(event) { // invoked by event loop

        var i, N, inp, microphone_output_buffer;

        microphone_output_buffer = event.inputBuffer.getChannelData(0); // just mono - 1 channel for now

        // microphone_output_buffer  <-- this buffer contains current gulp of data size BUFF_SIZE

        show_some_data(microphone_output_buffer, 5, "from getChannelData");
    }

    function start_microphone(stream){

      gain_node = audioContext.createGain();
      gain_node.connect( audioContext.destination );

      microphone_stream = audioContext.createMediaStreamSource(stream);
      microphone_stream.connect(gain_node); 

      script_processor_node = audioContext.createScriptProcessor(BUFF_SIZE, 1, 1);
      script_processor_node.onaudioprocess = process_microphone_buffer;

      microphone_stream.connect(script_processor_node);

      // --- enable volume control for output speakers

      document.getElementById('volume').addEventListener('change', function() {

          var curr_volume = this.value;
          gain_node.gain.value = curr_volume;

          console.log("curr_volume ", curr_volume);
      });

      // --- setup FFT

      script_processor_fft_node = audioContext.createScriptProcessor(2048, 1, 1);
      script_processor_fft_node.connect(gain_node);

      analyserNode = audioContext.createAnalyser();
      analyserNode.smoothingTimeConstant = 0;
      analyserNode.fftSize = 2048;

      microphone_stream.connect(analyserNode);

      analyserNode.connect(script_processor_fft_node);

      script_processor_fft_node.onaudioprocess = function() {

        // get the average for the first channel
        var array = new Uint8Array(analyserNode.frequencyBinCount);
        analyserNode.getByteFrequencyData(array);

        // draw the spectrogram
        if (microphone_stream.playbackState == microphone_stream.PLAYING_STATE) {

            show_some_data(array, 5, "from fft");
        }
      };
    }

  }(); //  webaudio_tooling_obj = function()



</script>

</head>
<body>

    <p>Volume</p>
    <input id="volume" type="range" min="0" max="1" step="0.1" value="0.5"/>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

由于此代码将麦克风数据公开为缓冲区,您可以使用websockets添加流的功能,或者只是将每个事件循环缓冲区聚合到怪物缓冲区中,然后将怪物下载到文件中

注意来电

    var audioContext = new AudioContext();
Run Code Online (Sandbox Code Playgroud)

这表明它使用了所有现代浏览器(包括移动浏览器)中的Web Audio API,以提供一个非常强大的音频平台,其中只有一个小片段... ... 注意 CPU使用量因此而跳跃演示将每个事件循环缓冲区写入浏览器控制台日志,该日志仅用于测试,因此实际使用的资源密集程度要低得多,即使您将此模块流式传输到其他地方

链接到一些Web Audio API文档

  • @binarymax是的,我的代码......随意掠夺...我在编写https://github.com/scottstensland/websockets-streaming-audio时自学了上面的技巧,它使用websockets从服务器传输音频然后使用Web Audio API使用Web Audio API在浏览器中渲染,以保护网络流量...我需要反应堆代码;-)因为它严格来说是一个学习练习 (4认同)
  • 请注意,现在 [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/getUserMedia) 建议使用“较新的 `navigator.mediaDevices.getUserMedia()` 代替” (3认同)

use*_*041 6

是的你可以.

使用getUserMedia()API,您可以捕获麦克风的原始音频输入.

https://nusofthq.com/blog/recording-mp3-using-only-html5-and-javascript-recordmp3-js/

  • 您在答案中给出的链接已断开。如果每个人都避免仅链接答案,那就太好了 (4认同)