BaseAudioContext: createScriptProcessor() 方法
已弃用:此特性不再推荐。虽然某些浏览器可能仍然支持它,但它可能已经从相关的网络标准中删除,可能正在删除过程中,或者可能仅为兼容性目的而保留。请避免使用它,如果可能,请更新现有代码;请参阅本页底部的兼容性表格以指导您的决策。请注意,此特性可能随时停止工作。
BaseAudioContext 接口的 createScriptProcessor() 方法创建一个 ScriptProcessorNode,用于直接音频处理。
注意:此功能已被 AudioWorklets 和 AudioWorkletNode 接口取代。
语法
createScriptProcessor(bufferSize, numberOfInputChannels, numberOfOutputChannels)
参数
bufferSize-
以采样帧为单位的缓冲区大小。如果指定,bufferSize 必须是以下值之一:256、512、1024、2048、4096、8192、16384。如果未传入或值为 0,则实现将为给定环境选择最佳缓冲区大小,该大小在节点的生命周期内将是 2 的恒定幂。
此值控制
audioprocess事件的触发频率以及每次调用需要处理的采样帧数。较低的bufferSize值将导致较低(更好)的延迟。为了避免音频中断和失真,需要更高的值。建议作者不要指定此缓冲区大小,而是允许实现选择一个好的缓冲区大小来平衡延迟和音频质量。 numberOfInputChannels-
指定此节点输入通道数的整数,默认为 2。最多支持 32 个值。
numberOfOutputChannels-
指定此节点输出通道数的整数,默认为 2。最多支持 32 个值。
警告: WebKit 目前(31 版本)要求在调用此方法时传入一个有效的 bufferSize。
注意: numberOfInputChannels 和 numberOfOutputChannels 都为零是无效的。
返回值
示例
使用脚本处理器添加白噪声
以下示例展示了如何使用 ScriptProcessorNode 来处理通过 AudioContext.decodeAudioData() 加载的音轨,为其每个音频样本添加少量白噪声,然后通过 AudioDestinationNode 播放。
对于每个通道和每个采样帧,脚本节点的 audioprocess 事件处理程序使用关联的 audioProcessingEvent 来遍历输入缓冲区的每个通道,以及每个通道中的每个样本,添加少量白噪声,然后在每种情况下将结果设置为输出样本。
const myScript = document.querySelector("script");
const myPre = document.querySelector("pre");
const playButton = document.querySelector("button");
// Create AudioContext and buffer source
let audioCtx;
async function init() {
audioCtx = new AudioContext();
const source = audioCtx.createBufferSource();
// Create a ScriptProcessorNode with a bufferSize of 4096 and
// a single input and output channel
const scriptNode = audioCtx.createScriptProcessor(4096, 1, 1);
// Load in an audio track using fetch() and decodeAudioData()
try {
const response = await fetch("viper.ogg");
const arrayBuffer = await response.arrayBuffer();
source.buffer = await audioCtx.decodeAudioData(arrayBuffer);
} catch (err) {
console.error(
`Unable to fetch the audio file: ${name} Error: ${err.message}`,
);
}
// Give the node a function to process audio events
scriptNode.addEventListener("audioprocess", (audioProcessingEvent) => {
// The input buffer is the song we loaded earlier
let inputBuffer = audioProcessingEvent.inputBuffer;
// The output buffer contains the samples that will be modified and played
let outputBuffer = audioProcessingEvent.outputBuffer;
// Loop through the output channels (in this case there is only one)
for (let channel = 0; channel < outputBuffer.numberOfChannels; channel++) {
let inputData = inputBuffer.getChannelData(channel);
let 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.1;
}
}
});
source.connect(scriptNode);
scriptNode.connect(audioCtx.destination);
source.start();
// When the buffer source stops playing, disconnect everything
source.addEventListener("ended", () => {
source.disconnect(scriptNode);
scriptNode.disconnect(audioCtx.destination);
});
}
// wire up play button
playButton.addEventListener("click", () => {
if (!audioCtx) {
init();
}
});
规范
| 规范 |
|---|
| Web Audio API # dom-baseaudiocontext-createscriptprocessor |
浏览器兼容性
加载中…