2015-05-05 25 views
2

我正在使用網絡音頻隨機振盪器,並且遇到了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

在恩尼斯·克塞諾基斯GENDY隨機合成程序這是所有基於。

謝謝!

+0

它從代碼似乎'M'應該改變頻率吧? – notthetup

+0

@notthetup'm'是等式'y = m(x)+ b'中的斜率,用於在每個點之間進行線性插值。我正在嘗試將'scriptNode.bufferSize'與波形寫入緩衝區的頻率分開。我一直在研究這個例子,使一個scriptProcessorNode振盪器的音調與緩衝區無關,但我還沒有破解它。 http://sriku.org/blog/2013/01/30/taming-the-scriptprocessornode/#replacing-oscillator-with-scriptprocessornode。任何指導將不勝感激! –

回答

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(); 
     } 
    } 
}