我想快速淡出一个振荡器,以便删除我从简单地停止它的流行/嘶嘶声。克里斯威尔逊建议technique设置setTargetAtTime获得收益。
现在我还没有完全理解它的最后一个参数'timeConstant':
它的单位是什么?秒? 我需要在那里投入1ms才能达到目标值?
答案 0 :(得分:9)
setTargetAtTime是一个指数衰减。参数是时间常数:
" timeConstant是给定阶跃输入响应(从0到1的值转换),一阶线性连续时不变系统达到值1 - 1 / e(约63.2%)所需的时间"
因此,对于每个" timeconstant"时间长度,水平将下降超过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) );
答案 1 :(得分:1)
虽然我意识到这可能在技术上不正确(考虑到时间常数的指数性质),但我一直在使用这个公式来转换"从秒到"时间常数"
function secondsToTimeConstant( sec ){
return ( sec * 2 ) / 10;
}
......这只是通过反复试验,但它或多或少一直在为我而努力