【问题标题】:How do I perform simple linear resampling using a ScriptProcessorNode?如何使用 ScriptProcessorNode 执行简单的线性重采样?
【发布时间】:2020-04-01 23:07:32
【问题描述】:

我目前正在尝试使用 ScriptProcessorNode 动态降低播放速度。这是迄今为止我一起破解的代码(仅处理左声道):

let processor = audioContext.createScriptProcessor(2**14);
let stored = [];
let currIndex = 0;
let playbackRate = 0.666;

processor.onaudioprocess = (e) => {
    let leftChannel = e.inputBuffer.getChannelData(0);
    for (let i = 0; i < leftChannel.length; i++) stored.push(leftChannel[i]);
    let outputLeft = e.outputBuffer.getChannelData(0);

    for (let i = 0; i < outputLeft.length; i++) {
        let otherIndex = currIndex + i * playbackRate;
        let completion = otherIndex % 1;
        let otherSampleLow = stored[Math.floor(otherIndex)];
        let otherSampleHigh = stored[Math.ceil(otherIndex)];

        let val = (completion-1)*otherSampleLow + completion*otherSampleHigh;
        outputLeft[i] = val;
    }

    currIndex += Math.floor(leftChannel.length * playbackRate);
};

let osc = audioContext.createOscillator();
osc.frequency.value = 440;
osc.connect(processor);
osc.start();

然而,对于任何小于 1 的播放速率来说,这听起来都是垃圾。这是为什么呢?我是否太天真地认为我可以通过在信号之间进行线性插值来减慢音频信号的速度?

这是一个小提琴:https://jsfiddle.net/6Le7aq42/

【问题讨论】:

    标签: javascript web-audio-api resampling


    【解决方案1】:

    知道了,错误是写(completion - 1) 而不是(1 - completion)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 2019-04-15
      • 2018-11-16
      • 1970-01-01
      • 1970-01-01
      • 2021-05-21
      相关资源
      最近更新 更多