scriptProcessorNode振荡器频率

时间:2015-05-05 05:00:03

标签: javascript web-audio synthesizer scriptprocessor

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

当断点在x轴上移动时,我认为振荡波形的频率会发生变化,但只有一个滤波效应,而频率似乎只是由scriptProcessorNode缓冲区大小决定,它必须是2在256和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随机综合计划。

谢谢!

1 个答案:

答案 0 :(得分: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(); 
        }  
    }
}