WebAudio:setTargetAtTime中的timeConstant如何工作?

mrf*_*lix 8 html5 web-audio-api

我想快速淡出一个振荡器,以便消除我从简单地停止它的流行音乐.Chris Wilson提出了在增益上设置setTargetAtTime技术.

现在我还没有完全掌握它的最后一个参数'timeConstant':

它的单位是什么?秒?我需要在那里投入1ms来达到目标​​值?

cwi*_*lso 10

那个克里斯威尔逊的家伙,这么麻烦.:)

setTargetAtTime是指数衰减.参数是时间常数:

"timeConstant是指一阶输入响应(从0到1的转换),一阶线性连续时不变系统达到值1 - 1/e(约63.2%)所需的时间."

因此,对于每个"时间常数"的时间长度,电平将下降超过2/3rds(假设增益为1,并且您将目标设置为0.在某些时候,衰减变得如此接近于零,它低于噪声阈值,你不必担心这个问题.它不会"达到目标值" - 它会相继逼近它,尽管当然在某些时候差异会低于你能达到的精度代表浮动.

我建议尝试一下,但这是一个快速的猜测,让你开始:

// only setting this up as a var to multiply it later - you can hardcode.
// initial value is 1 millisecond - experiment with this value if it's not fading
// quickly enough.
var timeConstant = 0.001; 

gain = ctx.createGain();
// connect up the node in place here
gain.gain.setTargetAtTime(0, ctx.currentTime, timeConstant);

// by my quick math, 8x TC should take you to around 2.5% of the original level 
// - more than enough to smooth the envelope off.
myBufferSourceNode.stop( ctx.currentTime + (8 * timeConstant) );
Run Code Online (Sandbox Code Playgroud)

  • timeConstant对我来说非常不直观,来自css-transitions,您可以在其中控制转换的持续时间。我知道零将永远不会达到。一位朋友建议使用一种实用的方法,将指数函数与一个非常缓慢减小的线性函数(例如y = 1-(x / 10000))相乘,以最终得到零交叉。 (2认同)