AudioParam.setValueCurveAtTime() - Web APIs 编辑
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.
Syntax
var paramRef = param.setValueCurveAtTime(values, startTime, duration);
Parameters
values
- An array of floating-point numbers representing the value curve the
AudioParam
will change through along the specifiedduration
. Every value in the array must be a finite number; if any value isNaN
,Infinity
, or-Infinity
, aTypeError
exception is thrown. startTime
- A double representing the time (in seconds) after the
AudioContext
was first created that the change in value will happen. If this value is lower thanAudioContext.currentTime
, it is clamped tocurrentTime
. duration
- A double representing the total time (in seconds) over which the parameter's
value
will 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
- The specified array of
values
has fewer than 2 items in it. RangeError
- The specified
startTime
is either negative or a non-finite value, orduration
is not a finite, strictly positive number. TypeError
- One or more of the values in the
values
array 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:
// 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 valueCurve = document.querySelector('.value-curve');
// 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 button to do something onclick
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);
}
Specifications
Specification | Status | Comment |
---|---|---|
Web Audio API The definition of 'setValueCurveAtTime' in that specification. | Working Draft |
Browser compatibility
BCD tables only load in the browser
Versions before Chrome 46 use nearest neighbor instead of linear interpolation.
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论