From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/audioworkletnode/index.html | 103 ++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 files/zh-cn/web/api/audioworkletnode/index.html (limited to 'files/zh-cn/web/api/audioworkletnode') diff --git a/files/zh-cn/web/api/audioworkletnode/index.html b/files/zh-cn/web/api/audioworkletnode/index.html new file mode 100644 index 0000000000..19a14ef2f1 --- /dev/null +++ b/files/zh-cn/web/api/audioworkletnode/index.html @@ -0,0 +1,103 @@ +--- +title: AudioWorkletNode +slug: Web/API/AudioWorkletNode +translation_of: Web/API/AudioWorkletNode +--- +

{{APIRef("Web Audio API")}} {{SeeCompatTable}}

+ +
虽然这个接口可以在 secure contexts 之外调用, 但是 {{domxref("BaseAudioContext.audioWorklet")}} 属性不行, 从而 {{domxref("AudioWorkletProcessor")}}s 不能在外部定义.
+ +

Web Audio API 中的  AudioWorkletNode  接口代表了用户定义的{{domxref("AudioNode")}}的基类, 该基类可以与其他节点一起连接到音频路由图. 其具有关联的{{domxref("AudioWorkletProcessor")}}, 它在 Web Audio 执行实际的音频处理.

+ +

构造函数

+ +
+
{{domxref("AudioWorkletNode.AudioWorkletNode", "AudioWorkletNode()")}}
+
为 AudioWorkletNode 创建一个新的实例对象.
+
+ +

属性

+ +

也继承父类的属性, {{domxref("AudioNode")}}.

+ +
+
{{domxref("AudioWorkletNode.port")}} {{readonlyinline}}
+
返回一个 {{domxref("MessagePort")}} 用于节点与其关联的 {{domxref("AudioWorkletProcessor")}} 间的双向通讯. 另一端在处理器属性{{domxref("AudioWorkletProcessor.port", "port")}} 下可用.
+
{{domxref("AudioWorkletNode.parameters")}} {{readonlyinline}}
+
返回一个 {{domxref("AudioParamMap")}} —  {{domxref("AudioParam")}} 对象的集合. 它们在创建 AudioWorkletProcessor的过程中被实例化. 如果 AudioWorkletProcessor 有一个静态的 {{domxref("AudioWorkletProcessor.parameterDescriptors", "parameterDescriptors")}} getter, 从其返回的 {{domxref("AudioParamDescriptor")}} 数组用于在 AudioWorkletNode 创建 AudioParam 对象. 通过这种机制,使得 AudioParam 对象可以从 AudioWorkletNode 中访问. 你可以在与其关联的 AudioWorkletProcessor 中使用它的值.
+
+ +

Event handlers

+ +
+
{{domxref("AudioWorkletNode.onprocessorerror")}}
+
在关联的 {{domxref("AudioWorkletProcessor")}} 对象发生异常时触发. 一旦触发,处理器及其节点将在其整个生命周期内处于输出静默状态.
+
+ +

方法

+ +

同样继承了其父类的方法, {{domxref("AudioNode")}}.

+ +

AudioWorkletNode 接口未定义其自己的任何方法.

+ +

示例

+ +

在本示例中我们创建了 AudioWorkletNode 对象, 它会输出白噪声.

+ +

首先, 我们需要定义一个自定义的 {{domxref("AudioWorkletProcessor")}}, 它将输出白噪声并进行注册. 注意, 这需要在一个单独的文件中完成.

+ +
// white-noise-processor.js
+class WhiteNoiseProcessor extends AudioWorkletProcessor {
+  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
+      }
+    })
+    return true
+  }
+}
+
+registerProcessor('white-noise-processor', WhiteNoiseProcessor)
+
+ +

接下来, 在脚本主文件中一个 AudioWorkletNode 实例, 并传递处理器的名称, 然后将该实例连接到一个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)
+
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Web Audio API', '#audioworkletnode', 'AudioWorkletNode')}}{{Spec2('Web Audio API')}}
+ +

浏览器兼容性

+ +
+ + +

{{Compat("api.AudioWorkletNode")}}

+
+ +

See also

+ + -- cgit v1.2.3-54-g00ecf