ScriptProcessorNode: audioprocess 事件
已弃用:此特性不再推荐。虽然某些浏览器可能仍然支持它,但它可能已经从相关的网络标准中删除,可能正在删除过程中,或者可能仅为兼容性目的而保留。请避免使用它,如果可能,请更新现有代码;请参阅本页底部的兼容性表格以指导您的决策。请注意,此特性可能随时停止工作。
ScriptProcessorNode
接口的 audioprocess
事件在脚本处理器的输入缓冲区准备好进行处理时触发。
注意:此功能已被 AudioWorklets 和 AudioWorkletNode
接口取代。
此事件不可取消,也不会冒泡。
语法
在诸如 addEventListener()
之类的方法中使用事件名称,或设置事件处理程序属性。
js
addEventListener("audioprocess", (event) => { })
onaudioprocess = (event) => { }
事件类型
一个 AudioProcessingEvent
。继承自 Event
。
事件属性
还实现了其父接口 Event
的继承属性。
playbackTime
只读-
一个双精度浮点数,表示音频将要播放的时间,定义为
AudioContext.currentTime
的时间。 inputBuffer
只读-
一个
AudioBuffer
,它是一个包含待处理输入音频数据的缓冲区。通道数量由工厂方法AudioContext.createScriptProcessor()
的参数numberOfInputChannels
定义。请注意,返回的AudioBuffer
仅在事件处理器的作用域内有效。 outputBuffer
只读-
一个
AudioBuffer
,它是一个应该写入输出音频数据的缓冲区。通道数量由工厂方法AudioContext.createScriptProcessor()
的参数numberOfOutputChannels
定义。请注意,返回的AudioBuffer
仅在事件处理器的作用域内有效。
示例
js
scriptNode.addEventListener("audioprocess", (audioProcessingEvent) => {
// The input buffer is a song we loaded earlier
const inputBuffer = audioProcessingEvent.inputBuffer;
// The output buffer contains the samples that will be modified and played
const outputBuffer = audioProcessingEvent.outputBuffer;
// Loop through the output channels (in this case there is only one)
for (let channel = 0; channel < outputBuffer.numberOfChannels; channel++) {
const inputData = inputBuffer.getChannelData(channel);
const outputData = outputBuffer.getChannelData(channel);
// Loop through the 4096 samples
for (let sample = 0; sample < inputBuffer.length; sample++) {
// make output equal to the same as the input
outputData[sample] = inputData[sample];
// add noise to each output sample
outputData[sample] += (Math.random() * 2 - 1) * 0.2;
}
}
});
您也可以通过 onaudioprocess
属性设置事件处理程序。
js
scriptNode.onaudioprocess = (audioProcessingEvent) => {
// …
};
规范
此特性似乎未在任何规范中定义。浏览器兼容性
加载中…