AudioNode
The AudioNode
interface serves as a versatile interface for constructing an audio processing graph, representing individual units of audio processing functionality.
Each AudioNode
is associated with a certain number of audio channels that facilitate the transfer of audio data through processing graph.
We usually represent the channels with the standard abbreviations detailed in the table below:
Name | Number of channels | Channels |
---|---|---|
Mono | 1 | 0: M - mono |
Stereo | 2 | 0: L - left 1: R - right |
Quad | 4 | 0: L - left 1: R - right 2: SL - surround left 3: SR - surround right |
Stereo | 6 | 0: L - left 1: R - right 2: C - center 3: LFE - subwoofer 4: SL - surround left 5: SR - surround right |
Mixing
When node has more then one input or number of inputs channels differs from output up-mixing or down-mixing must be conducted.
There are three properties involved in mixing process: channelCount
, ChannelCountMode
, ChannelInterpretation
.
Based on them we can obtain output's number of channels and mixing strategy.
Properties
Name | Type | Description | |
---|---|---|---|
context | BaseAudioContext | Associated context. | Read only |
numberOfInputs | number | Integer value representing the number of input connections for the node. | Read only |
numberOfOutputs | number | Integer value representing the number of output connections for the node. | Read only |
channelCount | number | Integer used to determine how many channels are used when up-mixing or down-mixing node's inputs. | Read only |
channelCountMode | ChannelCountMode | Enumerated value that specifies the method by which channels are mixed between the node's inputs and outputs. | Read only |
channelInterpretation | ChannelInterpretation | Enumerated value that specifies how input channels are mapped to output channels when number of them is different. | Read only |
Examples
Connecting node to node
import { OscillatorNode, GainNode, AudioContext } from 'react-native-audio-api';
function App() {
const audioContext = new AudioContext();
const oscillatorNode = audioContext.createOscillator();
const gainNode = audioContext.createGain();
gainNode.gain.value = 0.5; //lower volume to 0.5
oscillatorNode.connect(gainNode);
gainNode.connect(audioContext.destination);
oscillatorNode.start(audioContext.currentTime);
}
Connecting node to audio param (LFO-controlled parameter)
import { OscillatorNode, GainNode, AudioContext } from 'react-native-audio-api';
function App() {
const audioContext = new AudioContext();
const oscillatorNode = audioContext.createOscillator();
const lfo = audioContext.createOscillator();
const gainNode = audioContext.createGain();
gainNode.gain.value = 0.5; //lower volume to 0.5
lfo.frequency.value = 2; //low frequency oscillator with 2Hz
// by default oscillator wave values ranges from -1 to 1
// connecting lfo to gain param will cause the gain param to oscillate at 2Hz and its value will range from 0.5 - 1 to 0.5 + 1
// you can modulate amplitude by connecting lfo to another gain that would be responsible for this value
lfo.connect(gainNode.gain)
oscillatorNode.connect(gainNode);
gainNode.connect(audioContext.destination);
oscillatorNode.start(audioContext.currentTime);
lfo.start(audioContext.currentTime);
}
Methods
connect
The above method lets you connect one of the node's outputs to a destination.
Parameters | Type | Description |
---|---|---|
destination | AudioNode or AudioParam | AudioNode or AudioParam to which to connect. |
Errors:
Error type | Description |
---|---|
InvalidAccessError | If destination is not part of the same audio context as the node. |
Returns undefined
.
disconnect
The above method lets you disconnect one or more nodes from the node.
Parameters | Type | Description |
---|---|---|
destination Optional | AudioNode or AudioParam | AudioNode or AudioParam from which to disconnect. |
If no arguments provided node disconnects from all outgoing connections.
Returns undefined
.
Remarks
numberOfInputs
- Source nodes are characterized by having a
numberOfInputs
value of 0.
numberOfOutputs
- Destination nodes are characterized by having a
numberOfOutputs
value of 0.