Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用web音频api分析麦克风输入(将MediaStreamSource转换为BufferSource)_Javascript_Audio_Web_Web Audio Api - Fatal编程技术网

Javascript 使用web音频api分析麦克风输入(将MediaStreamSource转换为BufferSource)

Javascript 使用web音频api分析麦克风输入(将MediaStreamSource转换为BufferSource),javascript,audio,web,web-audio-api,Javascript,Audio,Web,Web Audio Api,我正在尝试使用Web音频Api获取每分钟的节拍(BPM),就像在以下链接(或)中一样,但是从音频流(麦克风)获取。 不幸的是,我没有让它运行。 有人知道我如何将麦克风MediaStreamSource转换为BufferSource并像第一个链接的网站一样继续吗? 以下是我到目前为止的代码: navigator.mediaDevices.getUserMedia({ audio: true, video: false }) .then(function(stream) { /* use t

我正在尝试使用Web音频Api获取每分钟的节拍(BPM),就像在以下链接(或)中一样,但是从音频流(麦克风)获取。 不幸的是,我没有让它运行。 有人知道我如何将麦克风MediaStreamSource转换为BufferSource并像第一个链接的网站一样继续吗? 以下是我到目前为止的代码:

navigator.mediaDevices.getUserMedia({ audio: true, video: false })
.then(function(stream) {
    /* use the stream */

    var OfflineContext = window.OfflineAudioContext || window.webkitOfflineAudioContext;
    var source = OfflineContext.createMediaStreamSource(stream);
    source.connect(OfflineContext);
    var offlineContext = new OfflineContext(2, 30 * 44100, 44100);

    offlineContext.decodeAudioData(stream, function(buffer) {
      // Create buffer source
      var source = offlineContext.createBufferSource();
      source.buffer = buffer;
      // Beats, or kicks, generally occur around the 100 to 150 hz range.
      // Below this is often the bassline.  So let's focus just on that.
      // First a lowpass to remove most of the song.
      var lowpass = offlineContext.createBiquadFilter();
      lowpass.type = "lowpass";
      lowpass.frequency.value = 150;
      lowpass.Q.value = 1;
      // Run the output of the source through the low pass.
      source.connect(lowpass);
      // Now a highpass to remove the bassline.
      var highpass = offlineContext.createBiquadFilter();
      highpass.type = "highpass";
      highpass.frequency.value = 100;
      highpass.Q.value = 1;
      // Run the output of the lowpass through the highpass.
      lowpass.connect(highpass);
      // Run the output of the highpass through our offline context.
      highpass.connect(offlineContext.destination);
      // Start the source, and render the output into the offline conext.
      source.start(0);
      offlineContext.startRendering();
    });
})
.catch(function(err) {
    /* handle the error */
    alert("Error");
});

谢谢大家!

那些文章很棒。您当前的方法存在一些问题:

  • 您不需要对流进行解码—您需要使用MediaStreamAudioSourceNode将其连接到web音频上下文,然后使用ScriptProcessor(已弃用)或AudioWorker(尚未在任何地方实现)获取位并进行检测。decodeAudioData采用编码缓冲区(即MP3文件的内容),而不是流对象
  • 请记住,这是一个流,而不是一个单一的文件-你不能真的只是把整个歌曲音频文件交给节拍检测器。好吧,你可以,但是如果你是流媒体,那么你需要等到整个文件进来,这将是糟糕的。您必须分块工作,bpm可能会在歌曲播放过程中发生变化。因此,一次收集一个片段-可能一次收集一秒钟或更多的音频-传递给节拍检测代码
  • 虽然对数据进行低通滤波可能是个好主意,但对其进行高通滤波可能不值得。请记住,滤波器不是砖墙滤波器——它们不会将高于或低于其频率的所有内容切掉,它们只是专注于它
    此外,您不能在脱机上下文中创建MediaStreamAudioSourceNode。它必须是一个音频上下文。哦,是的。我知道有一个我忘记了