我如何使用ScriptProcessorNode执行简单的线性重采样?

问题描述 投票:0回答:1

我目前正在尝试使用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个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.