AudioWorkletProcessor.parameterDescriptors (static getter) - Web APIs 编辑
Experimental
This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The read-only parameterDescriptors
property of an AudioWorkletProcessor
-derived class is a static getter, which returns an iterable of AudioParamDescriptor
-based objects.
The property is not a part of the AudioWorkletProcessor
interface, but, if defined, it is called internally by the AudioWorkletProcessor
constructor to create a list of custom AudioParam
objects in the parameters
property of the associated AudioWorkletNode
.
Defining the getter is optional.
Syntax
AudioWorkletProcessorSubclass.parameterDescriptors;
Value
An iterable of AudioParamDescriptor
-based objects. The properties of these objects are as follows:
name
- The
DOMString
which represents the name of theAudioParam
. Under this name theAudioParam
will be available in theparameters
property of the node, and under this name theAudioWorkletProcessor.process
method will acquire the calculated values of thisAudioParam
. automationRate
Optional- Either
"a-rate"
, or"k-rate"
string which represents an automation rate of thisAudioParam
. Defaults to"a-rate"
. minValue
Optional- A
float
which represents minimum value of theAudioParam
. Defaults to-3.4028235e38
. maxValue
Optional- A
float
which represents maximum value of theAudioParam
. Defaults to3.4028235e38
. defaultValue
Optional- A
float
which represents initial value of theAudioParam
. Defaults to0
.
Examples
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 parameters
with instantiated AudioParam
objects.
// 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)
Specifications
Specification | Status | Comment |
---|---|---|
Web Audio API The definition of 'parameterDescriptors' in that specification. | Working Draft |
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论