【问题标题】:Using Web Audio Api AudioWorklet to grab the bits and do BPM detection from audiostream使用 Web Audio Api AudioWorklet 从音频流中抓取比特并进行 BPM 检测
【发布时间】:2019-01-24 20:51:38
【问题描述】:

我正在尝试使用 AudioWorklet 获取块,就像 cwilso 在以下链接中建议的那样:Using web audio api for analyzing input from microphone (convert MediaStreamSource to BufferSource),但不幸的是我没有让它运行。有人知道,我如何使用 AudioWorker 从流中获取块,以便我可以分析它们?这是我的代码:

navigator.mediaDevices.getUserMedia({ audio: true, video: false })
.then(function(stream) {
    /* use the stream */
    var audioCtx = new (window.AudioContext || window.webkitAudioContext)(); // define audio context
    var source = audioCtx.createMediaStreamSource(stream);
    //connect stream to a web audio context with a MediaStreamAudioNode
    source.connect(audioCtx);
    //Use an AudioWorklet to grab the bits and do detection
    //https://developers.google.com/web/updates/2017/12/audio-worklet
    //https://developers.google.com/web/updates/2018/06/audio-worklet-design-pattern
    //https://webaudio.github.io/web-audio-api/#audioworklet
    audioCtx.audioWorklet.addModule('AudioWorklet.js').then(() => {
      let bypassNode = new AudioWorkletNode(audioCtx, 'bypass-processor');
    });

    //collect chunks for beat detection

    //do BPM detection

})
.catch(function(err) {
    /* handle the error */
    alert("Error");
});
        // Script in an extra file like it is explained in the api
class BypassProcessor extends AudioWorkletProcessor {
process (inputs, outputs) {
// Single input, single channel.
let input = inputs[0];
let output = outputs[0];
output[0].set(input[0]);
// Process only while there are inputs.
alert(input);
alert(output);
return false;}}); registerProcessor('bypass-processor', BypassProcessor);

【问题讨论】:

    标签: javascript audio-recording web-audio-api


    【解决方案1】:

    根据您需要进行多少处理,您可以在 AudioWorkletNode 本身内完成。

    如果没有,您需要使用 MessagePort 将数据从 AudioWorkletNode 传递到主线程

    您可能还对MessagePort with AudioWorklet 和AudioWorklet with SharedArrayBuffer and Worker 感兴趣

    【讨论】:

    • 我想处理 30 秒的音频流。我需要在我的代码中进行哪些更改才能实现这一点?
    猜你喜欢
    • 1970-01-01
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多