javascript - 使用网络音频 api 分析来自麦克风的输入(将 MediaStreamSource 转换为 BufferSource)

标签 javascript audio web web-audio-api

我正在尝试使用 Web Audio Api 获取每分钟节拍数 (BPM),就像在以下链接( http://joesul.li/van/beat-detection-using-web-audio/https://github.com/JMPerez/beats-audio-api/blob/gh-pages/script.js )中完成的那样,但来自音频流(麦克风)。 不幸的是,我无法运行它。 有人知道如何将麦克风 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");
});

谢谢!

最佳答案

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

  1. 您不需要解码流 - 您需要使用 MediaStreamAudioSourceNode 将其连接到网络音频上下文,然后使用 ScriptProcessor(已弃用)或 AudioWorker(尚未在所有地方实现)来获取位并执行检测。 decodeAudioData 采用编码缓冲区 - 即 MP3 文件的内容 - 而不是流对象。
  2. 请记住,这是一个流,而不是单个文件 - 您实际上不能将整个歌曲音频文件交给节拍检测器。好吧,你可以 - 但如果你正在流式传输,那么你需要等到整个文件进入,这会很糟糕。你必须分块工作,并且节奏可能会在歌曲播放过程中发生变化。因此,一次收集一 block (可能一次一秒或更多音频)以传递给节拍检测代码。
  3. 虽然对数据进行低通滤波可能是个好主意,但对其进行高通滤波可能不值得。请记住,滤波器不是砖墙滤波器 - 它们不会将高于或低于其频率的所有内容切掉,它们只是将其衰减。

关于javascript - 使用网络音频 api 分析来自麦克风的输入(将 MediaStreamSource 转换为 BufferSource),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51879587/

相关文章:

amazon-web-services - 登录页面 Cognito 的自定义

java - 元素类型 "servlet"的内容必须匹配“(icon?,servlet-name,display-name?,

java - 有谁知道我在哪里可以找到 "Orto"Javascript JVM?

python - 在python中将音频文件与视频文件组合

c# - 应用程序启动Windows Phone时如何播放声音

audio - 关于音频编解码器术语的定义

visual-studio-2005 - Visual Studio 将升级后的 Web 应用程序作为网站打开

javascript - 我需要有关如何通过python/numpy将创建的矩阵转换为javascript的指南

javascript - 如何使用 Selenium 滚动到元素内部的底部?

javascript - 纯Javascript数据发布