gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:15:53 26 4
gpt4 key购买 nike

我目前正在尝试使用 ScriptProcessorNode 动态降低播放速度。这是我迄今为止编写的代码(仅处理左 channel ):

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 的播放速率来说,这听起来完全是垃圾。为什么呢?我是否太天真地认为只需在信号之间进行线性插值就可以减慢音频信号的速度?

这是一个 fiddle :https://jsfiddle.net/6Le7aq42/

最佳答案

明白了,错误是写了 (completion - 1) 而不是 (1 - Completion)

关于javascript - 如何使用 ScriptProcessorNode 执行简单的线性重采样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60973973/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com