AudioWorkletProcessor
åºçº¿
广æ³å¯ç¨
èª 2021å¹´4æ èµ·ï¼æ¤ç¹æ§å·²å¨ä¸»æµæµè§å¨ä¸å¾å°æ¯æï¼å¯å¨å¤§å¤æ°è®¾å¤åæµè§å¨çæ¬ä¸æ£å¸¸ä½¿ç¨ã
Web Audio APIç AudioWorkletProcessor æ¥å£ä»£è¡¨äºä¸ä¸ª èªå®ä¹çé³é¢å¤ç代ç AudioWorkletNode. å®èº«å¤äº AudioWorkletGlobalScope å¹¶è¿è¡å¨ Web Audio rendering 线ç¨ä¸ãåæ¶ï¼ä¸ä¸ªå»ºç«å¨å
¶åºç¡ä¸ç AudioWorkletNode è¿è¡å¨ä¸»çº¿ç¨ä¸ã
æé 彿°
夿³¨ï¼AudioWorkletProcessor åå
¶åç±»ä¸è½éè¿ç¨æ·æä¾ç代ç ç´æ¥å®ä¾åãå®ä»¬åªè½éçä¸ä¹ç¸èç³»ç AudioWorkletNode çå建è被å
¶å建åå
é¨ãå
¶åç±»çæé 彿°å°è¢«ä¸ä¸ªå¯é对象è°ç¨ï¼å æ¤ä½ å¯ä»¥æ§è¡èªå®ä¹çåå§åè¿ç¨ââ详ç»ä¿¡æ¯è¯·åè§æé 彿°é¡µé¢ã
AudioWorkletProcessor()-
å建ä¸ä¸ª
AudioWorkletProcessorå¯¹è±¡çæ°å®ä¾ã
屿§
portåªè¯»-
è¿åä¸ä¸ªç¨äºå¨å¤çç¨åºåå ¶æå±ç
AudioWorkletNodeé´ååéä¿¡çMessagePortãå¦ä¸ç«¯ å¯éè¿è¯¥èç¹çport屿§ä½¿ç¨ã
æ¹æ³
AudioWorkletProcessor æ¥å£æ²¡æå®ä¹ä»»ä½èªå·±çæ¹æ³ã使¯ï¼ä½ å¿
é¡»æä¾ä¸ä¸ª process() æ¹æ³ï¼ç¨ä»¥å¤çé³é¢æµã
äºä»¶
AudioWorkletProcessor æ¥å£ä¸ååºä»»ä½äºä»¶ã
使ç¨è¯´æ
>Deriving classes
è¦èªå®ä¹é³é¢å¤ç代ç ï¼ä½ å¿
é¡»ä»AudioWorkletProcessor æ¥å£æ´¾çä¸ä¸ªç±»ãè¿ä¸ªæ´¾çç±»å¿
é¡»å
·æå¨è¯¥æ¥å£ä¸ä¸æ¾å®ä¹çprocess æ¹æ³ãè¯¥æ¹æ³å°è¢«æ¯ä¸ªå«æ 128 æ ·æ¬å¸§çåè°ç¨å¹¶ä¸æ¥åè¾å
¥åè¾åºæ°ç»ä»¥åèªå®ä¹çAudioParams (妿å®ä»¬å被å®ä¹äº) ç计ç®å¼ä½ä¸ºåæ°ãä½ å¯ä»¥ä½¿ç¨è¾å
¥å é³é¢åæ°å¼å»å¡«å
è¾åºæ°ç»ï¼è¿æ¯é»è®¤çç¨äºä½¿è¾åºéé³ã
Optionally, if you want custom AudioParams on your node, you can supply a parameterDescriptors property as a static getter on the processor. The array of AudioParamDescriptor-based objects returned is used internally to create the AudioParams during the instantiation of the AudioWorkletNode.
The resulting AudioParams reside in the parameters property of the node and can be automated using standard methods such as linearRampToValueAtTime. Their calculated values will be passed into the process() method of the processor for you to shape the node output accordingly.
å¤çé³é¢
ä¸ä¸ªå建èªå®ä¹é³é¢å¤çç®æ³çæ¥éª¤çå®ä¾ï¼
-
å建ä¸ä¸ªç¬ç«çæä»¶;
-
å¨è¿ä¸ªæä»¶ä¸ï¼
- Extend the
AudioWorkletProcessorclass (see "Deriving classes" section) and supply your ownprocess()method in it; - Register the processor using
AudioWorkletGlobalScope.registerProcessor()method;
- Extend the
-
Load the file using
addModule()method on your audio context'saudioWorkletproperty; -
Create an
AudioWorkletNodebased on the processor. The processor will be instantiated internally by theAudioWorkletNodeconstructor. -
Connect the node to the other nodes.
ä¾å
In the example below we create a custom AudioWorkletNode that outputs white noise.
First, we need to define a custom AudioWorkletProcessor, which will output white noise, and register it. Note that this should be done in a separate file.
// 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);
Next, in our main script file we'll load the processor, create an instance of AudioWorkletNode, passing it the name of the processor, then 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);
Specifications
| è§è |
|---|
| Web Audio API > # AudioWorkletProcessor > |