Path: blob/main/files/en-us/web/api/audioparam/setvaluecurveattime/index.md
6581 views
------{{APIRef("Web Audio API")}}
The setValueCurveAtTime() method of the {{domxref("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.
Syntax
Parameters
values: An array of floating-point numbers representing the value curve the {{domxref("AudioParam")}} will change through along the specified
duration. Every value in the array must be a finite number; if any value isNaN,Infinity, or-Infinity, a {{jsxref("TypeError")}} exception is thrown.
startTime: A double representing the time (in seconds) after the {{ domxref("AudioContext") }} was first created that the change in value will happen. If this value is lower than {{domxref("BaseAudioContext/currentTime", "AudioContext.currentTime")}}, it is clamped to
currentTime.
duration: A double representing the total time (in seconds) over which the parameter's
valuewill change following the specified curve. The specified values are spaced equally along this duration.
Return value
A reference to this AudioParam object. Some older browser implementations of this interface return undefined.
Exceptions
InvalidStateError{{domxref("DOMException")}}: Thrown if the specified array of
valueshas fewer than 2 items in it.
{{jsxref("RangeError")}}
: Thrown if the specified
startTimeis either negative or a non-finite value, ordurationis not a finite, strictly positive number.
{{jsxref("TypeError")}}
: Thrown if one or more of the values in the
valuesarray is non-finite. Non-finite values areNaN,Infinity, and-Infinity.
Usage notes
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.
Examples
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:
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
Versions before Chrome 46 use nearest neighbor instead of linear interpolation.