scriptProcessorNode 振荡器频率
scriptProcessorNode oscillator frequency
我正在研究网络音频随机振荡器,但在使用 scriptProcessorNode 时遇到了问题。我的算法使用随机游走来确定波形中的动态断点,然后在它们之间进行插值。
随着断点在x轴上的移动,我以为振荡波形的频率会发生变化,但只是过滤效果,而且频率似乎只是由scriptProcessorNode缓冲区大小决定的,必须是256 和 16384 之间的 2 的幂。
如何更改 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;
}
}
这是一个 link 的工作示例: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();
}
}
}
我正在研究网络音频随机振荡器,但在使用 scriptProcessorNode 时遇到了问题。我的算法使用随机游走来确定波形中的动态断点,然后在它们之间进行插值。
随着断点在x轴上的移动,我以为振荡波形的频率会发生变化,但只是过滤效果,而且频率似乎只是由scriptProcessorNode缓冲区大小决定的,必须是256 和 16384 之间的 2 的幂。
如何更改 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;
}
}
这是一个 link 的工作示例: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();
}
}
}