setValueCurveAtTime() 方法在 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.

句法

var paramRef = param.setValueCurveAtTime(values, startTime, duration);
					

参数

An array of floating-point numbers representing the value curve the AudioParam will change through along the specified duration . Every value in the array must be a finite number; if any value is NaN , Infinity ,或 -Infinity TypeError 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 than AudioContext.currentTime , it is clamped to currentTime .
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.

返回值

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

异常

InvalidStateError
The specified array of has fewer than 2 items in it.
RangeError
指定 startTime is either negative or a non-finite value, or duration is not a finite, strictly positive number.
TypeError
One or more of the values in the array is non-finite. Non-finite values are NaN , Infinity ,和 -Infinity .

用法注意事项

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 参数。

注意: 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:

// 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);
}
					

规范

规范 状态 注释
Web 音频 API
The definition of 'setValueCurveAtTime' in that specification.
工作草案

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
setValueCurveAtTime Chrome 14 Edge 12 Firefox 25 IE No Opera 15 Safari 6 WebView Android Yes Chrome Android 18 Firefox Android 26 Opera Android 14 Safari iOS Yes Samsung Internet Android 1.0

图例

完整支持

完整支持

不支持

不支持

Versions before Chrome 46 use nearest neighbor instead of linear interpolation.

另请参阅

元数据

  • 最后修改: