AudioParam.setTargetAtTime() - Web APIs 编辑

The setTargetAtTime() method of the AudioParam interface schedules the start of a gradual change to the AudioParam value. This is useful for decay or release portions of ADSR envelopes.

Syntax

var paramRef = param.setTargetAtTime(target, startTime, timeConstant);

Parameters

target
The value the parameter will start to transition towards at the given start time.
startTime
The time that the exponential transition will begin, in the same time coordinate system as AudioContext.currentTime. If it is less than or equal to AudioContext.currentTime, the parameter will start changing immediately.
timeConstant
The time-constant value, given in seconds, of an exponential approach to the target value. The larger this value is, the slower the transition will be.

Returns

A reference to this AudioParam object. Some older browser implementations of this interface return void.

Description

The change starts at the time specified in startTime and exponentially moves towards the value given by the target parameter. The decay rate as defined by the timeConstant parameter is exponential; therefore the value will never reach target completely, but after each timestep of length timeConstant, the value will have approached target by another 1-e-163.2%1 - e^{-1} \approx 63.2%. For the complete formula (which uses a first-order linear continuous time-invariant system), check the Web Audio specification.

If you absolutely need to reach the target value by a specific time, you can use AudioParam.exponentialRampToValueAtTime(). However, for mathematical reasons, that method does not work if the current value or the target value is 0.

Choosing a good timeConstant

As mentioned above, the value changes exponentially, with each timeConstant bringing you another 63.2% toward the target value. You don't have to worry about reaching the target value; once you are close enough, any further changes will be imperceptible to a human listener.

Depending on your use case, getting 95% toward the target value may already be enough; in that case, you could set timeConstant to one third of the desired duration.

For more details, check the following table on how the value changes from 0% to 100% as the time progresses.

Time since startTimeValue
0 * timeConstant0%
0.5 * timeConstant39.3%
1 * timeConstant63.2%
2 * timeConstant86.5%
3 * timeConstant95.0%
4 * timeConstant98.2%
5 * timeConstant99.3%
n * timeConstant1-e-n1 - e^{-n}

Examples

In this example, we have a media source with two control buttons (see the webaudio-examples repo for the source code, or view the example live.) When these buttons are pressed, setTargetAtTime() is used to fade the gain value up to 1.0, and down to 0, respectively, with the effect starting after 1 second, and the length of time the effect lasts being controlled by the timeConstant.

// create audio context
var AudioContext = window.AudioContext || window.webkitAudioContext;
var audioCtx = new AudioContext();

// set basic variables for example
var myAudio = document.querySelector('audio');
var pre = document.querySelector('pre');
var myScript = document.querySelector('script');

pre.innerHTML = myScript.innerHTML;

var atTimePlus = document.querySelector('.at-time-plus');
var atTimeMinus = document.querySelector('.at-time-minus');

// Create a MediaElementAudioSourceNode
// Feed the HTMLMediaElement into it
var source = audioCtx.createMediaElementSource(myAudio);

// Create a gain node and set it's gain value to 0.5
var gainNode = audioCtx.createGain();
gainNode.gain.value = 0.5;
var currGain = gainNode.gain.value;

// connect the AudioBufferSourceNode to the gainNode
// and the gainNode to the destination
source.connect(gainNode);
gainNode.connect(audioCtx.destination);

// set buttons to do something onclick
atTimePlus.onclick = function() {
  currGain = 1.0;
  gainNode.gain.setTargetAtTime(1.0, audioCtx.currentTime + 1, 0.5);
}

atTimeMinus.onclick = function() {
  currGain = 0;
  gainNode.gain.setTargetAtTime(0, audioCtx.currentTime + 1, 0.5);
}

Specifications

SpecificationStatusComment
Web Audio API
The definition of 'setTargetAtTime' in that specification.
Working Draft 

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:48 次

字数:7713

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文