有没有办法使用 Web Audio API 来比实时更快地采样音频?

IT技术 javascript html webkit web-audio-api
2021-03-20 00:17:06

我正在使用 Web Audio API 并试图找到一种导入 mp3 的方法(因此这仅在 Chrome 中),并在画布上生成它的波形。我可以实时执行此操作,但我的目标是比实时执行此操作更快。

我能找到的所有示例都涉及从分析器对象中读取频率数据,在一个附加到 onaudioprocess 事件的函数中:

processor = context.createJavascriptNode(2048,1,1);
processor.onaudioprocess = processAudio;
...
function processAudio{
    var freqByteData = new Uint8Array(analyser.frequencyBinCount);
    analyser.getByteFrequencyData(freqByteData);
    //calculate magnitude & render to canvas
}

但看起来,analyser.frequencyBinCount只有在播放声音时才会填充(关于缓冲区被填充的东西)。

我想要的是能够尽快手动/以编程方式单步执行文件,以生成画布图像。

我到目前为止是这样的:

$("#files").on('change',function(e){
    var FileList = e.target.files,
        Reader = new FileReader();

    var File = FileList[0];

    Reader.onload = (function(theFile){
        return function(e){
            context.decodeAudioData(e.target.result,function(buffer){
                source.buffer = buffer;
                source.connect(analyser);
                analyser.connect(jsNode);

                var freqData = new Uint8Array(buffer.getChannelData(0));

                console.dir(analyser);
                console.dir(jsNode);

                jsNode.connect(context.destination);
                //source.noteOn(0);
            });
        };
    })(File);

    Reader.readAsArrayBuffer(File);
});

但是 getChannelData() 总是返回一个空的类型化数组。

任何见解都值得赞赏 - 即使事实证明它无法完成。我想我是唯一一个互联网想要做的东西,在实时。

谢谢。

2个回答

Web Audio API有一种非常棒的“离线”模式,它允许您通过音频上下文对整个文件进行预处理,然后对结果进行处理:

var context = new webkitOfflineAudioContext();

var source = context.createBufferSource();
source.buffer = buffer;
source.connect(context.destination);
source.noteOn(0);

context.oncomplete = function(e) {
  var audioBuffer = e.renderedBuffer;
};

context.startRendering();

因此,设置看起来与实时处理模式完全相同,只是您设置了oncomplete回调和对 的调用startRendering()你得到的e.redneredBuffer是一个AudioBuffer.

在您添加本地AudioContextoncomplete 回调:var context = new webkitAudioContext();本质上,我认为您需要创建一个用于播放的上下文和一个用于处理的上下文。
2021-04-25 00:17:06
现在真的很晚了,我正在做其他事情,但是这个答案听起来很有希望(主要是因为它是我唯一得到的)。我一定会让你知道这是怎么回事。
2021-04-26 00:17:06
好吧,我已经玩了一个小时的大部分时间,我迷路了。你的代码如何适合我的?当你set source.buffer = bufferbuffer从哪里来?我能看到正在创建的 AudioBuffer 的唯一地方是作为 context.decodeAudioData() 的成功函数的参数。这是我所拥有的 jsfiddle。这完全是一团糟,但它就是我所在的地方——几乎是在黑暗中刺伤。 jsfiddle.net/NW7E3 确保控制台处于活动状态
2021-05-04 00:17:06
@Pickle 你有没有用过这个?
2021-05-05 00:17:06
对。bufferAudioBuffer你从decodeAudioData
2021-05-13 00:17:06

我使用以下代码使用 OfflineAudioContext 使其工作。此处的完整示例显示了如何使用它来计算线性啁啾的 FFT 幅度。一旦你有了将节点连接在一起的概念,你就可以离线使用它做任何事情。

function fsin(freq, phase, t) {
  return Math.sin(2 * Math.PI * freq * t + phase)
}

function linearChirp(startFreq, endFreq, duration, sampleRate) {
  if (duration === undefined) {
    duration = 1; // seconds
  }
  if (sampleRate === undefined) {
    sampleRate = 44100; // per second
  }
  var numSamples = Math.floor(duration * sampleRate);
  var chirp = new Array(numSamples);
  var df = (endFreq - startFreq) / numSamples;
  for (var i = 0; i < numSamples; i++) {
    chirp[i] = fsin(startFreq + df * i, 0, i / sampleRate);
  }
  return chirp;
}

function AnalyzeWithFFT() {
  var numChannels = 1; // mono
  var duration = 1; // seconds
  var sampleRate = 44100; // Any value in [22050, 96000] is allowed
  var chirp = linearChirp(10000, 20000, duration, sampleRate);
  var numSamples = chirp.length;

  // Now we create the offline context to render this with.
  var ctx = new OfflineAudioContext(numChannels, numSamples, sampleRate);
  
  // Our example wires up an analyzer node in between source and destination.
  // You may or may not want to do that, but if you can follow how things are
  // connected, it will at least give you an idea of what is possible.
  //
  // This is what computes the spectrum (FFT) information for us.
  var analyser = ctx.createAnalyser();

  // There are abundant examples of how to get audio from a URL or the
  // microphone. This one shows you how to create it programmatically (we'll
  // use the chirp array above).
  var source = ctx.createBufferSource();
  var chirpBuffer = ctx.createBuffer(numChannels, numSamples, sampleRate);
  var data = chirpBuffer.getChannelData(0); // first and only channel
  for (var i = 0; i < numSamples; i++) {
    data[i] = 128 + Math.floor(chirp[i] * 127); // quantize to [0,256)
  }
  source.buffer = chirpBuffer;

  // Now we wire things up: source (data) -> analyser -> offline destination.
  source.connect(analyser);
  analyser.connect(ctx.destination);

  // When the audio buffer has been processed, this will be called.
  ctx.oncomplete = function(event) {
    console.log("audio processed");
    // To get the spectrum data (e.g., if you want to plot it), you use this.
    var frequencyBins = new Uint8Array(analyser.frequencyBinCount);
    analyser.getByteFrequencyData(frequencyBins);
    console.log(frequencyBins);
    // You can also get the result of any filtering or any other stage here:
    console.log(event.renderedBuffer);
  };

  // Everything is now wired up - start the source so that it produces a
  // signal, and tell the context to start rendering.
  //
  // oncomplete above will be called when it is done.
  source.start();
  ctx.startRendering();
}