e01.org

ScriptProcessorNode: audioprocess event

Web Audio API

Web Audio API

The **`audioprocess`** event of the ScriptProcessorNode interface is fired when an input buffer of a script processor is ready to be processed.

> [!NOTE]
> This feature was replaced by [AudioWorklets](/docs/Web/API/AudioWorklet) and the AudioWorkletNode interface.

This event is not cancelable and does not bubble.

## Syntax

Use the event name in addEventListener():

```js-nolint
addEventListener("audioprocess", (event) => { })
```

Alternatively, assign to the [event handler property](/docs/Web/API/Document_Object_Model/Events#using_onevent_properties) of the interface:

```js-nolint
onaudioprocess = (event) => { }
```

## Event type

An AudioProcessingEvent. Inherits from Event.

AudioProcessingEvent

## Examples

```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;
}
}
});
```

You could also set up the event handler using the `onaudioprocess` property (but note that this replaces other event listeners added using `onaudioprocess`, if any):

```js
scriptNode.onaudioprocess = (audioProcessingEvent) => {
// …
};
```

## Specifications

## Browser compatibility

## See also

- [Web Audio API](/docs/Web/API/Web_Audio_API)

source: MDN Web Docs mirror · page

Cite this

"ScriptProcessorNode: audioprocess event". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/API/ScriptProcessorNode/audioprocess_event

JSON record · API collection · citation policy