kon*_*rad 4 javascript web-audio-api
I am pretty new in js, so I was wondering if anybody could he help with this (for some) quite simple problem I am dealing with. I am trying to make an oscillatorNode connect to a convolverNode, an achieve a hall reverb. I still haven't made it work even though it's probably really simple. Now, there are plenty of examples on how to do it when you load sounds with the XMLHttpRequest, but I am not interested in doing it with loaded sound. I guess I am just longing for a good and super short example/fiddle of how that would be done and how to make the buffer work with oscillatorNodes. Here's the basics of how to make a convolverNode, now how to make the rest?
// Make a source node for the sample.
var source = context.createBufferSource();
source.buffer = this.buffer;
// Make a convolver node for the impulse response.
var convolver = context.createConvolver();
convolver.buffer = this.impulseResponseBuffer;
// Connect the graph.
source.connect(convolver);
convolver.connect(context.destination);
Run Code Online (Sandbox Code Playgroud)
Thank you so so much!
我想你问是否可以将振荡器连接到ConvolverNode,但这很容易做到:
var osc = context.createOscillator();
osc.connect(convolver);
osc.start(0);
Run Code Online (Sandbox Code Playgroud)
所以也许正如凯文所猜测的那样,你试图产生(而不是下载)一个脉冲响应.你真的不想使用振荡器 - 脉冲响应的长度必须是有限的.您可以在缓冲区中生成有限持续时间的正弦波,但这会产生与混响不同的事情.
假设你试图通过算法生成一个基本的混响脉冲响应以避免下载,它实际上相对容易做到 - 你可以在对数衰减上使用噪声.这是我刚才写的一个函数:
function impulseResponse( duration, decay, reverse ) {
var sampleRate = audioContext.sampleRate;
var length = sampleRate * duration;
var impulse = audioContext.createBuffer(2, length, sampleRate);
var impulseL = impulse.getChannelData(0);
var impulseR = impulse.getChannelData(1);
if (!decay)
decay = 2.0;
for (var i = 0; i < length; i++){
var n = reverse ? length - i : i;
impulseL[i] = (Math.random() * 2 - 1) * Math.pow(1 - n / length, decay);
impulseR[i] = (Math.random() * 2 - 1) * Math.pow(1 - n / length, decay);
}
return impulse;
}
Run Code Online (Sandbox Code Playgroud)
尝试开始
var impulseBuffer = impulseResponse(4,4,false);
Run Code Online (Sandbox Code Playgroud)
并将你的convolverNode.buffer设置为.这将是一个不错的开始.根据需要玩持续时间和衰变参数.这可能不像记录的脉冲响应那样平滑或有趣,但它可以用于大多数基本目的.
现在,有很多关于如何在使用 XMLHttpRequest 加载声音时执行此操作的示例,但我对使用加载的声音执行此操作不感兴趣。
卷积器需要AudioBuffer包含脉冲响应的 。如果不尝试以编程方式创建一个(这可能非常困难),您将需要通过 XHR 加载一个。
如果你谷歌一下,你可以找到很多免费的脉冲响应 WAV 或 MP3 文件。
| 归档时间: |
|
| 查看次数: |
872 次 |
| 最近记录: |