Web Audio API's AudioParam interface property gets or sets the value of this AudioParam at the current time. Initially, the value is set to AudioParam.defaultValue .

设置 value has the same effect as calling AudioParam.setValueAtTime with the time returned by the AudioContext 's currentTime property..

句法

var curValue = audioParam.value;
audioParam.value = newValue;
					

A floating-point Number indicating the parameter's value as of the current time. This value will be between the values specified by the minValue and maxValue 特性。

用法注意事项

Value precision and variation

The data type used internally to store value is a single-precision (32-bit) floating point number, while JavaScript uses 64-bit double-precision floating point numbers. As a result, the value you read from the value property may not always exactly equal what you set it to.

Consider this example:

const source = new AudioBufferSourceNode(...);
const rate = 5.3;
source.playbackRate.value = rate;
console.log(source.playbackRate.value === rate);
					

The log output will be false , because the playback rate parameter, rate , was converted to the 32-bit floating-point number closest to 5.3, which yields 5.300000190734863. One solution is to use the Math.fround() method, which returns the single-precision value equivalent to the 64-bit JavaScript value specified—when setting value ,像这样:

const source = new AudioBufferSourceNode(...);
const rate = Math.fround(5.3);
source.playbackRate.value = rate;
console.log(source.playbackRate.value === rate);
					

In this case, the log output will be true .

Value of a property which is changing over time

value of an AudioParam can either be fixed or can vary over time. This is reflected by the value getter, which returns the value of the parameter as of the audio rendering engine's most recent render quantum , or moment at which audio buffers are processed and updated. In addition to processing audio buffers, each render quantum updates the value of each AudioParam as needed given the current time and any established time-based parameter value changes.

Upon first creating the parameter, its value is set to its default value, given by AudioParam.defaultValue . This is the parameter's value at a time of 0.0 seconds, and will remain the parameter's value until the first render quantum in which the value is altered.

During each render quantum, the browser does the following things related to managing the value of a parameter:

  • value setter has been used, the parameter's value is changed to the value given.
  • If the current time equals or exceeds the time specified by a previous call to setValueAtTime() value is changed to the value passed into setValueAtTime() .
  • If any gradiated or ramped value changing methods have been called and the current time is within the time range over which the graduated change should occur, the value is updated based on the appropriate algorithm. These ramped or gradiated value-changing methods include linearRampToValueAtTime() , setTargetAtTime() ,和 setValueCurveAtTime() .

Thus, the value of a parameter is maintained to accurately reflect the state of the parameter over time.

范例

This example instantly changes the volume of a GainNode to 40%.

const audioCtx = new AudioContext();
const gainNode = audioCtx.createGain();
gainNode.gain.value = 0.4;
//which is identical to:
gainNode.gain.setValueAtTime(0.4, audioCtx.currentTime);
					

规范

规范 状态 注释
Web 音频 API
The definition of 'value' 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
value Chrome 14 Edge 12 Firefox 25
25
Prior to Firefox 69, value did not take into account scheduled or gradiated changes to the parameter's value; instead, only explicitly set values were returned.
IE No Opera 15 Safari 6 WebView Android Yes Chrome Android 18 Firefox Android 26
26
Firefox for Android does not currently take into account scheduled or gradiated changes to the parameter's value; only the initial value or the most recent explicitly set value is returned.
Opera Android 14 Safari iOS Yes Samsung Internet Android 1.0

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

When changing the gain value of a GainNode , Google Chrome prior to version 64 (January 2018) would perform a smooth interpolation to prevent dezippering. Starting with version 64, the value is changed instantly to bring it in line with the Web Audio spec. See Chrome Platform Status 了解细节。

另请参阅

元数据

  • 最后修改: