AudioParam.setTargetAtTime()

The setTargetAtTime() method of the AudioParam Interface schedules the start of a change to the value of the AudioParam.

The change starts at the time specified in startTime and exponentially moves towards the value given by the target parameter. The exponential decay rate is defined by the timeConstant parameter. This is the time it takes a first-order linear continuous time-invariant system to reach the value 1 - 1/e (around 63.2%) given a step input response (transition from 0 to 1 value): basically, the larger it is, the slower the transition will be.  This is useful for decay or release portions of envelopes.

Syntax

var AudioParam = AudioParam.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, which will be relative to AudioContext.currentTime.
timeConstant
The time-constant value of first-order filter (exponential) approach to the target value. The larger this value is, the slower the transition will be.

Returns

A reference to this AudioParam object. In some browsers older implementations of this interface return void.

Examples

In this example, we have a media source with two control buttons (see the audio-param 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 (see the AudioParam interface section of the spec for an explanation of the maths behind this.)

// 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

Specification Status Comment
Web Audio API
The definition of 'setTargetAtTime' in that specification.
Working Draft  

Browser compatibility

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 14 webkit (Yes) 23 No support 15 webkit
22 (unprefixed)
6 webkit
Unprefixed (Yes) (Yes)        
Feature Android Chrome Edge Firefox Mobile (Gecko) Firefox OS IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support No support ? (Yes) 25 1.2 No support No support webkit 28 webkit
Unprefixed   (Yes) (Yes)           (Yes)

See also

Document Tags and Contributors

 Contributors to this page: erikadoyle, jpmedley, fscholz, chrisdavidmills
 Last updated by: erikadoyle,