87,995
社区成员




class VoiceDataGet extends AudioWorkletProcessor {
/*
* options由new AudioWorkletNode()时传递
* */
constructor() {
super();
}
/*
* `inputList`和outputList`都是输入或输出的数组
* 比较坑的是只有128个样本???如何设置
* */
process (inputList, outputList, parameters) {
// console.log(inputList)
if(inputList.length>0&&inputList[0].length>0){
this.port.postMessage(inputList[0]);
}
return true;//回来让系统知道我们仍处于活动状态并准备处理音频。
}
}
registerProcessor('voice-data-get', VoiceDataGet)
/*初始化AudioContext、AudioWorkletNode*/
async initData() {
try {
let audioContext = new AudioContext();
/*音频流数据分析节点*/
let audioWorkletNode;
try {
audioWorkletNode = new AudioWorkletNode(audioContext, "voice-data-get");
} catch (e) {
try {
/*---------------加载AudioWorkletProcessor模块并将其添加到当前的Worklet----------------------------*/
await audioContext.audioWorklet.addModule('./js/voice-data-get.js');
/*---------------AudioWorkletNode绑定加载后的AudioWorkletProcessor---------------------------------*/
audioWorkletNode = new AudioWorkletNode(audioContext, "voice-data-get");
} catch (error) {
console.log(error);
}
}
{
/*-------------AudioWorkletNode和AudioWorkletProcessor通信使用MessagePort--------------------------*/
let inputData=[];
const messagePort = audioWorkletNode.port;
messagePort.onmessage = (e) => {
let channelData = e.data[0];
inputData.push(...channelData);
}
}//音频流数据分析
return audioWorkletNode;
} catch (e) {
console.log(e)
}
}