{{ draft() }}
{{WebAudioRef}}
The ScriptProcessorNode
interface allows the generation, processing, or analyzing of audio using JavaScript. It is an {{domxref("AudioNode")}} audio-processing module that is linked to two buffers, one containing the current input, one containing the output. An event, implementing the {{domxref("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 {{domxref("AudioContext.createScriptProcessor()")}} factory method is called. 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 size of the buffers isnot defined, which is recommended, the browser will pick one that its heuristic deems appropriate.
- Number of inputs
1
- Number of outputs
1
- Channel count mode
"max"
- Channel count
2
(not used in the default count mode) - Channel interpretation
"speakers"
Properties
Inherits properties from its parent, {{domxref("AudioNode")}}.
- {{domxref("ScriptProcessNode.bufferSize")}} {{readonlyInline}}
-
Returns an integer representing both the input and output buffer size. It is a power of 2 value between
256
and16384
, included.
Event handlers
- {{domxref("ScriptProcessorNode.onaudioprocess")}}
- Represents the {{domxref("EventHandler")}} to be called.
Methods
No specific methods; inherits methods from its parent, {{domxref("AudioNode")}}.
Examples
Example: Generating a sine wave
The following example shows basic usage of a ScriptProcessorNode to generate a sine wave.
var context = new AudioContext(); // Create a ScriptProcessorNode with a bufferSize of 4096 and a single output channel var audioNode = context.createScriptProcessor(4096, 0, 1); var volume = .5; // The frequncy of the sine wave tone var frequency = 440; // Give the node a function to process audio events audioNode.onaudioprocess = function(audioProcessingEvent) { // 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 outData = outputBuffer.getChannelData(channel); // Loop through the 4096 samples for (var sample = 0; sample < outputBuffer.length; sample++) { // The time at which the sample will play var sampleTime = context.currentTime + outputBuffer.duration * sample / outputBuffer.length; // Set the data in the output buffer for each sample outData[sample] = volume * Math.sin(sampleTime * frequency * Math.PI * 2); } } } // Connect the node to the context to start playing the sound audioNode.connect(context.destination); // Connect to speakers
Specifications
Specification | Status | Comment |
---|---|---|
{{SpecName('Web Audio API', '#ScriptProcessorNode-section', 'ScriptProcessorNode')}} | {{Spec2('Web Audio API')}} |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | {{CompatVersionUnknown}}{{property_prefix("webkit")}} | Activated on Nightly only | {{CompatNo}} | {{CompatNo}} | {{CompatUnknown}} |
Feature | Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | {{CompatUnknown}} | Activated on Nightly only | {{CompatNo}} | {{CompatNo}} | {{CompatUnknown}} |
See also
- Using Web Audio