The setValueCurveAtTime()
method of the AudioParam
interface schedules the parameter's value to change following a curve defined by a list of values.
The curve is a linear interpolation between the sequence of values defined in an array of floating-point values, which are scaled to fit into the given interval starting at startTime
and a specific duration.
setValueCurveAtTime(values, startTime, duration)
A reference to this AudioParam
object. Some older browser implementations of this interface return undefined
.
When the parameter's value finishes following the curve, its value is guaranteed to match the last value in the set of values specified in the values
parameter.
Note: Some early implementations of the Web Audio API did not ensure this to be the case, causing unexpected results.
In this example, we have a media source with a single button (see the webaudio-examples repo for the source code, or view the example live.) When this button is pressed, setValueCurveAtTime()
is used to change the gain value between the values contained in the waveArray array:
var AudioContext = window.AudioContext || window.webkitAudioContext;
var audioCtx = new AudioContext();
var myAudio = document.querySelector('audio');
var pre = document.querySelector('pre');
var myScript = document.querySelector('script');
pre.innerHTML = myScript.innerHTML;
var valueCurve = document.querySelector('.value-curve');
var source = audioCtx.createMediaElementSource(myAudio);
var gainNode = audioCtx.createGain();
gainNode.gain.value = 0.5;
var currGain = gainNode.gain.value;
source.connect(gainNode);
gainNode.connect(audioCtx.destination);
var waveArray = new Float32Array(9);
waveArray[0] = 0.5;
waveArray[1] = 1;
waveArray[2] = 0.5;
waveArray[3] = 0;
waveArray[4] = 0.5;
waveArray[5] = 1;
waveArray[6] = 0.5;
waveArray[7] = 0;
waveArray[8] = 0.5;
valueCurve.onclick = function() {
gainNode.gain.setValueCurveAtTime(waveArray, audioCtx.currentTime, 2);
}
Versions before Chrome 46 use nearest neighbor instead of linear interpolation.