audioWorklet
2024年02月05日
一、认识
通过 audioContext.audioWorklet.addModule('xxxProcessor.js');
向音频渲染线程注入包含自定义音频处理的 audioworkletprocessor
派生类的 JavaScript
代码,以遍后续创建自定义的音频处理节点
二、语法
xxxProcessor.js
class XxxProcessor extends AudioWorkletProcessor {
constructor(...args) {
super(...args);
this.port.onmessage = (e) => {
console.log(e.data);
this.port.postMessage("pong");
};
}
process(inputs, outputs, parameters) {
return true;
}
}
registerProcessor("xxx-processor", XxxProcessor);
const audioContext = new AudioContext();
await audioContext.audioWorklet.addModule('xxxProcessor.js');
const audioWorkletNode = new AudioWorkletNode(audioContext, 'xxx-processor', options);
audioContext.context(audioWorkletNode).context(audioContext.destination);