弃用
This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the
兼容性表格
at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
ScriptProcessorNode
interface allows the generation, processing, or analyzing of audio using JavaScript.
注意
: As of the August 29 2014 Web Audio API spec publication, this feature has been marked as deprecated, and was replaced by
AudioWorklet
(见
AudioWorkletNode
).
ScriptProcessorNode
interface is an
AudioNode
audio-processing module that is linked to two buffers, one containing the input audio data, one containing the processed output audio data. An event, implementing the
AudioProcessingEvent
interface, is sent to the object each time the input buffer contains new data, and the event handler terminates when it has filled the output buffer with data.
The size of the input and output buffer are defined at the creation time, when the
AudioContext.createScriptProcessor()
method is called (both are defined by
AudioContext.createScriptProcessor()
's
bufferSize
parameter). The buffer size must be a power of 2 between
256
and
16384
, that is
256
,
512
,
1024
,
2048
,
4096
,
8192
or
16384
. Small numbers lower the
latency
, but large number may be necessary to avoid audio breakup and glitches.
If the buffer size is not defined, which is recommended, the browser will pick one that its heuristic deems appropriate.
| 输入数 |
1
|
|---|---|
| 输出数 |
1
|
| 通道计数模式 |
"max"
|
| 通道计数 |
2
(不用于默认计数模式)
|
| 通道解释 |
"speakers"
|
继承的特性来自其父级,
AudioNode
.
ScriptProcessorNode.bufferSize
只读
256
–
16384
.
No specific methods; inherits methods from its parent,
AudioNode
.
监听这些事件使用
addEventListener()
或通过把事件监听器赋值给
on
eventname
property of this interface:
audioprocess
ScriptProcessorNode
is ready to be processed.
onaudioprocess
event handler property.
The following example shows basic usage of a
ScriptProcessorNode
to take a track loaded via
AudioContext.decodeAudioData()
, process it, adding a bit of white noise to each audio sample of the input track (buffer) and play it through the
AudioDestinationNode
. For each channel and each sample frame, the
scriptNode.onaudioprocess
function takes the associated
audioProcessingEvent
and uses it to loop through each channel of the input buffer, and each sample in each channel, and add a small amount of white noise, before setting that result to be the output sample in each case.
注意 : For a full working example, see our script-processor-node github repo (also view the 源代码 )。
var myScript = document.querySelector('script');
var myPre = document.querySelector('pre');
var playButton = document.querySelector('button');
// Create AudioContext and buffer source
var audioCtx = new AudioContext();
source = audioCtx.createBufferSource();
// Create a ScriptProcessorNode with a bufferSize of 4096 and a single input and output channel
var scriptNode = audioCtx.createScriptProcessor(4096, 1, 1);
console.log(scriptNode.bufferSize);
// load in an audio track via XHR and decodeAudioData
function getData() {
request = new XMLHttpRequest();
request.open('GET', 'viper.ogg', true);
request.responseType = 'arraybuffer';
request.onload = function() {
var audioData = request.response;
audioCtx.decodeAudioData(audioData, function(buffer) {
myBuffer = buffer;
source.buffer = myBuffer;
},
function(e){"Error with decoding audio data" + e.err});
}
request.send();
}
// Give the node a function to process audio events
scriptNode.onaudioprocess = function(audioProcessingEvent) {
// The input buffer is the song we loaded earlier
var inputBuffer = audioProcessingEvent.inputBuffer;
// The output buffer contains the samples that will be modified and played
var outputBuffer = audioProcessingEvent.outputBuffer;
// Loop through the output channels (in this case there is only one)
for (var channel = 0; channel < outputBuffer.numberOfChannels; channel++) {
var inputData = inputBuffer.getChannelData(channel);
var outputData = outputBuffer.getChannelData(channel);
// Loop through the 4096 samples
for (var 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;
}
}
}
getData();
// wire up play button
playButton.onclick = function() {
source.connect(scriptNode);
scriptNode.connect(audioCtx.destination);
source.start();
}
// When the buffer source stops playing, disconnect everything
source.onended = function() {
source.disconnect(scriptNode);
scriptNode.disconnect(audioCtx.destination);
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
Web 音频 API
The definition of 'ScriptProcessorNode' in that specification. |
工作草案 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
ScriptProcessorNode
弃用
|
Chrome
14
Prefixed
|
Edge ≤18 | Firefox 25 | IE 不支持 No |
Opera
22
|
Safari
6
Prefixed
|
WebView Android Yes | Chrome Android Yes | Firefox Android 25 |
Opera Android
22
|
Safari iOS
6
Prefixed
|
Samsung Internet Android Yes |
audioprocess
event
弃用
|
Chrome
14
Prefixed
|
Edge 12 | Firefox 25 | IE 不支持 No |
Opera
22
|
Safari
6
Prefixed
|
WebView Android Yes | Chrome Android Yes | Firefox Android 25 |
Opera Android
22
|
Safari iOS
6
Prefixed
|
Samsung Internet Android Yes |
bufferSize
弃用
|
Chrome
14
Prefixed
|
Edge 12 | Firefox 25 | IE 不支持 No |
Opera
22
|
Safari
6
Prefixed
|
WebView Android Yes | Chrome Android Yes | Firefox Android 25 |
Opera Android
22
|
Safari iOS
6
Prefixed
|
Samsung Internet Android Yes |
onaudioprocess
弃用
|
Chrome
14
Prefixed
|
Edge 12 | Firefox 25 | IE 不支持 No |
Opera
22
|
Safari
6
Prefixed
|
WebView Android Yes | Chrome Android Yes | Firefox Android 25 |
Opera Android
22
|
Safari iOS
6
Prefixed
|
Samsung Internet Android Yes |
完整支持
不支持
弃用。不要用于新网站。
要求使用供应商前缀或不同名称。
ScriptProcessorNode
AnalyserNode
AudioBuffer
AudioBufferSourceNode
AudioContext
AudioContextOptions
AudioDestinationNode
AudioListener
AudioNode
AudioNodeOptions
AudioParam
AudioProcessingEvent
AudioScheduledSourceNode
AudioWorklet
AudioWorkletGlobalScope
AudioWorkletNode
AudioWorkletProcessor
BaseAudioContext
BiquadFilterNode
ChannelMergerNode
ChannelSplitterNode
ConstantSourceNode
ConvolverNode
DelayNode
DynamicsCompressorNode
GainNode
IIRFilterNode
MediaElementAudioSourceNode
MediaStreamAudioDestinationNode
MediaStreamAudioSourceNode
OfflineAudioCompletionEvent
OfflineAudioContext
OscillatorNode
PannerNode
PeriodicWave
StereoPannerNode
WaveShaperNode