javascript - scriptProcessorNode 振荡器频率

标签 javascript web-audio-api 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 随机合成程序。

谢谢!

最佳答案

我通过在 scriptNode.onaudioprocess 函数之外使用 index 变量将波形写入 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(); 
        }  
    }
}

关于javascript - scriptProcessorNode 振荡器频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30044594/

相关文章:

python - 在python中以任意音高合成声音

javascript - 可拖动的 CSS 切换开关

javascript - Angular 2-datatable 中的 Angular 2 搜索过滤器查询 - 类型 'first_name' 上不存在属性 '{}'

javascript - 如何以编程方式 ng-bind svg 元素上的属性?

javascript - Angular 2+路由redirectTo作为一个函数

c - 合成器 - 记录按下的键盘

javascript - Webaudio scriptProcessorNode功能替换

javascript - 如何将 Web Audio API 连接到 Tone.js?

api - 如何使用 Web Audio Api 选择目标输出设备

multithreading - 多线程波表合成器单击。这是由于阅读中断造成的吗?