AudioNode

Estás leyendo la versión en inglés del artículo porque aún no existe una traducción para este idioma. ¡Ayúdanos a traducir este artículo!

La interfaz AudioNode es una interfaz genérica para representar un módulo de procesamiento de audio. Ejemplos:

Note: An AudioNode can be target of events, por lo tanto este implementa  EventTarget interface.

Descripción

The audio routing graph

AudioNodes participating in an AudioContext create a audio routing graph.

Cada AudioNode posee entradas y salidas, y múltiples nodos de audio son conectados para construir un processing graph. Este graph es contenido en AudioContext, y cada nodo de audio solo puede pertecener a un audio context.

Un source node tiene cero entradas pero una o muchas salidas, y puede ser usado para generar sonido. Por otro lado, un destination node no tiene salidas; instead, all its inputs are directly played back on the speakers (or whatever audio output device the audio context uses). In addition, there are processing nodes which have inputs and outputs. The exact processing done varies from one AudioNode to another but, in general, a node reads its inputs, does some audio-related processing, and generates new values for its outputs, or simply lets the audio pass through (for example in the AnalyserNode, where the result of the processing is accessed separately).

The more nodes in a graph, the higher the latency will be. Por ejemplo, si tu graph tiene una latencia de 500ms, Cuando el source node reproduzca un sonido, este va a tomar la mitad de un segundo hasta que el sonido pueda ser escuchado en tus altavoces. (or even longer because of latency in the underlying audio device). Por lo tanto, si tu necesitas tener un audio interactivo, keep the graph as small as possible, and put user-controlled audio nodes at the end of a graph. For example, a volume control (GainNode) should be the last node so that volume changes take immediate effect.

Each input and output has a given amount of channels. For example, mono audio has one channel, while stereo audio has two channels. The Web Audio API will up-mix or down-mix the number of channels as required; check the Web Audio spec for details.

For a list of all audio nodes, see the Web Audio API homepage.

Creating an AudioNode

There are two ways to create an AudioNode: via the constuctor and via the factory method.

// constructor
const analyserNode = new AnalyserNode(audioCtx, {
  fftSize: 2048,
  maxDecibels: -25,
  minDecibels: -60,
  smoothingTimeConstant: 0.5,
});

// factory method
const analyserNode = audioCtx.createAnalyser();
analyserNode.fftSize = 2048;
analyserNode.maxDecibels = -25;
analyserNode.minDecibels = -60;
analyserNode.smoothingTimeConstant = 0.5;

Eres libre de usar cualquiera de los constructors o factory methods, o una mezcla de ambos, sin embargo hay ventajas al usar contructores:

  • All parameters can be set during construction time and don't need to be set individually.
  • You can sub-class an audio node. While the actual processing is done internally by the browser and cannot be altered, you could write a wrapper around an audio node to provide custom properties and methods.
  • Slightly better performance: In both Chrome and Firefox, the factory methods call the constructors internally.

Tener en cuenta que Microsoft Edge does not yet appear to support the constructors; it will throw a "Function expected" error when you use the constructors.

Brief history: The first version of the Web Audio spec only defined the factory methods. After a design review in October 2013, it was decided to add constructors because they have numerous benefits over factory methods. The constructors were added to the spec from August to October 2016. Factory methods continue to be included in the spec and are not deprecated.

Properties

AudioNode.context Read only
Returns the associated BaseAudioContext, that is the object representing the processing graph the node is participating in.
AudioNode.numberOfInputs Read only
Returns the number of inputs feeding the node. Source nodes are defined as nodes having a numberOfInputs property with a value of 0.
AudioNode.numberOfOutputs Read only
Returns the number of outputs coming out of the node. Destination nodes — like AudioDestinationNode — have a value of 0 for this attribute.
AudioNode.channelCount
Represents an integer used to determine how many channels are used when up-mixing and down-mixing connections to any inputs to the node. Its usage and precise definition depend on the value of AudioNode.channelCountMode.
AudioNode.channelCountMode
Represents an enumerated value describing the way channels must be matched between the node's inputs and outputs.
AudioNode.channelInterpretation
Represents an enumerated value describing the meaning of the channels. This interpretation will define how audio up-mixing and down-mixing will happen.
The possible values are "speakers" or "discrete".

Methods

Also implements methods from the interface EventTarget.

AudioNode.connect()
Allows us to connect the output of this node to be input into another node, either as audio data or as the value of an AudioParam.
AudioNode.disconnect()
Allows us to disconnect the current node from another one it is already connected to.

Example

This simple snippet of code shows the creation of some audio nodes, and how the AudioNode properties and methods can be used. You can find examples of such usage on any of the examples linked to on the Web Audio API landing page (for example Violent Theremin.)

const audioCtx = new AudioContext();

const oscillator = new OscillatorNode(audioCtx);
const gainNode = new GainNode(audioCtx);

oscillator.connect(gainNode).connect(audioCtx.destination);

oscillator.context;
oscillator.numberOfInputs;
oscillator.numberOfOutputs;
oscillator.channelCount;

Specifications

Specification Status Comment
Web Audio API
La definición de 'AudioNode' en esta especificación.
Working Draft  

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome para AndroidFirefox para AndroidOpera para AndroidSafari en iOSSamsung Internet
AudioNodeChrome Soporte completo 14Edge Soporte completo SiFirefox Soporte completo 25IE Sin soporte NoOpera Soporte completo 15Safari Soporte completo 6WebView Android Soporte completo SiChrome Android Soporte completo 18Firefox Android Soporte completo 26Opera Android Soporte completo 14Safari iOS Soporte completo SiSamsung Internet Android Soporte completo Si
channelCountChrome Soporte completo 14Edge Soporte completo 12Firefox Soporte completo 25IE Sin soporte NoOpera Soporte completo 15Safari Soporte completo 6WebView Android Soporte completo SiChrome Android Soporte completo 18Firefox Android Soporte completo 26Opera Android Soporte completo 14Safari iOS Soporte completo SiSamsung Internet Android Soporte completo Si
channelCountModeChrome Soporte completo 14Edge Soporte completo 12Firefox Soporte completo 25IE Sin soporte NoOpera Soporte completo 15Safari Soporte completo 6WebView Android Soporte completo SiChrome Android Soporte completo 18Firefox Android Soporte completo 26Opera Android Soporte completo 14Safari iOS Soporte completo SiSamsung Internet Android Soporte completo Si
channelInterpretationChrome Soporte completo 14Edge Soporte completo 12Firefox Soporte completo 25IE Sin soporte NoOpera Soporte completo 15Safari Soporte completo 6WebView Android Soporte completo SiChrome Android Soporte completo 18Firefox Android Soporte completo 26Opera Android Soporte completo 14Safari iOS Soporte completo SiSamsung Internet Android Soporte completo Si
connectChrome Soporte completo 14Edge Soporte completo 12Firefox Soporte completo 25IE Sin soporte NoOpera Soporte completo 15Safari Soporte completo 6WebView Android Soporte completo SiChrome Android Soporte completo 18Firefox Android Soporte completo 26Opera Android Soporte completo 14Safari iOS Soporte completo SiSamsung Internet Android Soporte completo Si
contextChrome Soporte completo 14Edge Soporte completo 12Firefox Soporte completo 25IE Sin soporte NoOpera Soporte completo 15Safari Soporte completo 6WebView Android Soporte completo SiChrome Android Soporte completo 18Firefox Android Soporte completo 26Opera Android Soporte completo 14Safari iOS Soporte completo SiSamsung Internet Android Soporte completo Si
disconnectChrome Soporte completo 14Edge Soporte completo 12Firefox Soporte completo 25IE Sin soporte NoOpera Soporte completo 15Safari Soporte completo 6WebView Android Soporte completo SiChrome Android Soporte completo 18Firefox Android Soporte completo 26Opera Android Soporte completo 14Safari iOS Soporte completo SiSamsung Internet Android Soporte completo Si
numberOfInputsChrome Soporte completo 14Edge Soporte completo 12Firefox Soporte completo 25IE Sin soporte NoOpera Soporte completo 15Safari Soporte completo 6WebView Android Soporte completo SiChrome Android Soporte completo 18Firefox Android Soporte completo 26Opera Android Soporte completo 14Safari iOS Soporte completo SiSamsung Internet Android Soporte completo Si
numberOfOutputsChrome Soporte completo 14Edge Soporte completo 12Firefox Soporte completo 25IE Sin soporte NoOpera Soporte completo 15Safari Soporte completo 6WebView Android Soporte completo SiChrome Android Soporte completo 18Firefox Android Soporte completo 26Opera Android Soporte completo 14Safari iOS Soporte completo SiSamsung Internet Android Soporte completo Si

Leyenda

Soporte completo  
Soporte completo
Sin soporte  
Sin soporte

See also