只读 参数 特性为 AudioWorkletNode interface returns the associated AudioParamMap — that is, a 地图 -like collection of AudioParam objects. They are instantiated during creation of the underlying AudioWorkletProcessor according to its parameterDescriptors static getter.

句法

audioWorkletNodeInstance.parameters
					

AudioParamMap object containing AudioParam instances. They can be automated in the same way as with default AudioNode s, and their calculated values can be used in the process method of your AudioWorkletProcessor .

范例

To demonstrate creation and usage of custom AudioParam s, we'll expand the example from AudioWorkletNode page. There we've created a simple node which outputs white noise. Here, additionally, we'll create a custom gain parameter, so we can directly change volume of the output (although you could use GainNode to achieve this as well).

First, we need to define a custom AudioWorkletProcessor , and register it. Note that this should be done in a separate file.

We expand the processor by adding a static parameterDescriptors getter. It will be used internally by the AudioWorkletNode constructor to populate its 参数 with instantiated AudioParam 对象。

// white-noise-processor.js
class WhiteNoiseProcessor extends AudioWorkletProcessor {
  static get parameterDescriptors () {
    return [{
      name: 'customGain',
      defaultValue: 1,
      minValue: 0,
      maxValue: 1,
      automationRate: 'a-rate'
    }]
  }
  process (inputs, outputs, parameters) {
    const output = outputs[0]
    output.forEach(channel => {
      for (let i = 0; i < channel.length; i++) {
        channel[i] = (Math.random() * 2 - 1) *
          (parameters['customGain'].length > 1 ? parameters['customGain'][i] : parameters['customGain'][0])
        // note: a parameter contains an array of 128 values (one value for each of 128 samples),
        // however it may contain a single value which is to be used for all 128 samples
        // if no automation is scheduled for the moment.
      }
    })
    return true
  }
}
registerProcessor('white-noise-processor', WhiteNoiseProcessor)
					

Next, in our main scripts file we'll load the processor, create an instance of AudioWorkletNode passing it the name of the processor, and connect the node to an audio graph.

const audioContext = new AudioContext()
await audioContext.audioWorklet.addModule('white-noise-processor.js')
const whiteNoiseNode = new AudioWorkletNode(audioContext, 'white-noise-processor')
whiteNoiseNode.connect(audioContext.destination)
					

Now we can change the gain on the node like this:

const gainParam = whiteNoiseNode.parameters.get('customGain')
gainParam.setValueAtTime(0, audioContext.currentTime)
gainParam.linearRampToValueAtTime(0.5, audioContext.currentTime + 0.5)
					

规范

规范 状态 注释
Web 音频 API
The definition of 'parameters' 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
参数 Chrome 66 Edge 79 Firefox 76 IE 不支持 No Opera Yes Safari 不支持 No WebView Android 66 Chrome Android 66 Firefox Android 不支持 No Opera Android Yes Safari iOS 不支持 No Samsung Internet Android 9.0

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: