2

我正在研究一个网络音频随机振荡器,并且在使用 scriptProcessorNode 时遇到了问题。我的算法使用随机游走来确定波形中的动态断点,然后在它们之间进行插值。

随着断点在 x 轴上移动,我认为振荡波形的频率会发生变化,但只是有一个过滤效果,而且频率似乎只是由 scriptProcessorNode 缓冲区大小决定的,它必须是 256 之间的 2 的幂和 16384。

如何更改 scriptProcessorNode 振荡器的频率?

这是我的综合代码:

scriptNode.onaudioprocess = function(audioProcessingEvent) {

    walk(); //use random walk to generate new x/y position for each breakpoint

    var outputBuffer = audioProcessingEvent.outputBuffer;
    var lastPoint = 0;
    var index = 0;

    // linearly interpolate between the new breakpoint positions
    for(var i = 0; i < breakpoint.length-1; i++) {
        var y = breakpoint[lastPoint].y; 
        for(var channel = 0; channel <= 0;channel++) {
            var outputData = outputBuffer.getChannelData(channel);
            if(i != 0){
                if(y >= breakpoint[i].y) {
                    while(y >= breakpoint[i].y) {
                        y = (breakpoint[i].m*index)+breakpoint[i].b;// y = m(x)+b
                        outputData[index] = y; 
                        index++; 
                    }
                } else if(y <= breakpoint[i].y) {
                    while(y <= breakpoint[i].y) {
                        y = (breakpoint[i].m*index)+breakpoint[i].b;
                        outputData[index] = y; 
                        index++;  
                    }
                }
            } 
        }
        lastPoint = i;
    }
}

这是一个工作示例的链接:http: //andrewbernste.in/bernie/gendy011.html

这一切都基于 Iannis Xenakis 的 GENDY 随机综合程序。

谢谢!

4

1 回答 1

1

我通过使用函数index外部的变量scriptNode.onaudioprocess将波形写入 scriptNode 缓冲区来解决问题。这样,将波形写入缓冲区的频率与缓冲区的大小无关。

这是最终代码:

var index = 0;
var freq = 0.8;

scriptNode.onaudioprocess = function(audioProcessingEvent){

    var outputBuffer = audioProcessingEvent.outputBuffer;
    var outputData = outputBuffer.getChannelData(0);
    for(var j = 0; j < outputData.length;j++){
        // linearly interpolate between the new breakpoint positions
        // get the interp point by comparing index to the x distance
        var lerp = (index - breakpoint[point].x) / (breakpoint[point+1].x - breakpoint[point].x)

        y = nx.interp(lerp,breakpoint[point].y,breakpoint[point+1].y);
        if(point < breakpoint.length && index >= breakpoint[point+1].x) {
            point++;
        }

        outputData[j] = y;
        index+=freq; 
        if(index >= breakpoint[breakpoint.length-1].x){
            index = 0;
            point = 0;
            walk(); 
        }  
    }
}
于 2015-05-25T19:11:39.153 回答