Audio Playback
About Audio Playback
Audio playback refers to the process of playing back recorded audio files. This can be either loaded from memory or streamed from a file or a web-based audio stream. Audio playback is an essential part of many industries, including music, film, and broadcasting. It allows for the reproduction of sound in a way that is faithful to the original recording, allowing listeners to experience the full range of sounds and nuances in the recording.
Audio Player
This example loads an mp3 audio file and plays it on the audio output using AudioPlayerNode
. Playback can be controlled by the play()
, pause()
and stop()
methods.
Code Example
- JSON
- Swift
- Kotlin
- C++
- JavaScript
{
"nodes": {
{ "id": "audioPlayerNode", "type": "AudioPlayerNode" },
},
"connections": {
{ "sourceNode": "audioPlayerNode", "destinationNode": "outputNode" }
},
"tracks": {
"audioPlayerNode": "example.mp3"
}
}
import SwitchboardSDK
class AudioPlayerExample {
let audioEngine = SBAudioEngine()
let audioGraph = SBAudioGraph()
let audioPlayerNode = SBAudioPlayerNode()
init() {
audioPlayerNode.load("example.mp3", withFormat: .mp3)
audioGraph.addNode(audioPlayerNode)
audioGraph.connect(audioPlayerNode, audioGraph.outputNode)
audioEngine.start(audioGraph)
}
func play() {
audioPlayerNode.play()
}
func pause() {
audioPlayerNode.pause()
}
func stop() {
audioPlayerNode.stop()
}
}
import com.synervoz.switchboard.sdk.AudioEngine
import com.synervoz.switchboard.sdk.Codec
import com.synervoz.switchboard.sdk.audiograph.AudioGraph
import com.synervoz.switchboard.sdk.audiographnodes.AudioPlayerNode
class AudioPlayerExample(context: Context) {
val audioEngine = AudioEngine(context)
val audioGraph = AudioGraph()
val audioPlayerNode = AudioPlayerNode()
init {
audioPlayerNode.load("example.mp3", Codec.MP3)
audioGraph.addNode(audioPlayerNode)
audioGraph.connect(audioPlayerNode, audioGraph.outputNode)
audioEngine.start(audioGraph)
}
fun play() {
audioPlayerNode.play()
}
fun pause() {
audioPlayerNode.pause()
}
fun stop() {
audioPlayerNode.stop()
}
}
#include "AudioGraph.hpp"
#include "AudioPlayerNode.hpp"
using namespace switchboard;
class AudioPlayerExample {
public:
AudioPlayerExample() {
// Adding nodes to audio graph
audioGraph.addNode(audioPlayerNode);
// Connecting audio nodes
audioGraph.connect(audioPlayerNode, audioGraph.getOutputNode());
// Loading the track and starting the player
audioPlayerNode.load("example.mp3", ::Mp3);
audioPlayerNode.play();
// Starting the graph
audioGraph.start();
}
// Example method called by the audio processing pipeline on each buffer
bool process(float** buffers, const uint numberOfChannels, const uint numberOfFrames, const uint sampleRate) {
AudioBuffer<float> outBuffer = AudioBuffer<float>(numberOfChannels, numberOfFrames, false, sampleRate, buffers);
const bool result = audioGraph->processBuffer(nullptr, &outBuffer);
return result;
}
private:
AudioGraph audioGraph;
AudioPlayerNode audioPlayerNode;
};
Coming soon...
Synchronized Audio Players
In this example two audio players are synchronized using a subgraph. The SubgraphSourceNode
makes sure that the audio players are started and stopped exactly at the same time.
Code Example
- Swift
- Kotlin
- C++
- JSON
- JavaScript
import SwitchboardSDK
class SynchronizedAudioPlayersExample {
let audioEngine = SBAudioEngine()
let audioGraph = SBAudioGraph()
let subgraphNode = SBSubgraphProcessorNode()
let internalAudioGraph = SBAudioGraph()
let drumsPlayerNode = SBAudioPlayerNode()
let bassPlayerNode = SBAudioPlayerNode()
let mixerNode = SBMixerNode()
init() {
drumsPlayerNode.loadFile("drums.mp3")
bassPlayerNode.loadFile("bass.mp3")
internalAudioGraph.addNode(drumsPlayerNode)
internalAudioGraph.addNode(bassPlayerNode)
internalAudioGraph.addNode(mixerNode)
internalAudioGraph.connect(drumsPlayerNode, to: mixerNode)
internalAudioGraph.connect(bassPlayerNode, to: mixerNode)
internalAudioGraph.connect(mixerNode, to: internalAudioGraph.outputNode)
subgraphNode.setAudioGraph(internalAudioGraph)
audioGraph.addNode(subgraphNode)
audioGraph.connect(subgraphNode, to: audioGraph.outputNode)
audioEngine.start(audioGraph)
}
func play() {
// The audio players won't start playing until the internal audio graph is started
drumsPlayerNode.play()
bassPlayerNode.play()
internalAudioGraph.start()
}
func pause() {
// Stopping the internal audio graph first to make sure that both audio players are stopped at the same time
internalAudioGraph.stop()
drumsPlayerNode.pause()
bassPlayerNode.pause()
}
func stop() {
// Stopping the internal audio graph first to make sure that both audio players are stopped at the same time
internalAudioGraph.stop()
drumsPlayerNode.pause()
bassPlayerNode.pause()
}
func loadBass(url: String, format: SBCodec) {
drumsPlayerNode.load(url, withFormat: format) }
func loadDrums(url: String, format: SBCodec) {
bassPlayerNode.load(url, withFormat: format)
}
}
import com.synervoz.switchboard.sdk.audiographnodes.AudioPlayerNode
import com.synervoz.switchboard.sdk.audioengine.AudioEngine
import com.synervoz.switchboard.sdk.audiograph.AudioGraph
import com.synervoz.switchboard.sdk.audiographnodes.MixerNode
import com.synervoz.switchboard.sdk.audiographnodes.SubgraphSourceNode
class SynchronizedAudioPlayersExample(context: Context) {
val audioEngine = AudioEngine(context)
val audioGraph = AudioGraph()
val internalAudioGraph = AudioGraph()
val subgraphNode = SubgraphSourceNode()
val drumsPlayerNode = AudioPlayerNode()
val bassPlayerNode = AudioPlayerNode()
val mixerNode = MixerNode()
init {
internalAudioGraph.addNode(drumsPlayerNode)
internalAudioGraph.addNode(bassPlayerNode)
internalAudioGraph.addNode(mixerNode)
internalAudioGraph.connect(drumsPlayerNode, mixerNode)
internalAudioGraph.connect(bassPlayerNode, mixerNode)
internalAudioGraph.connect(mixerNode, internalAudioGraph.outputNode)
subgraphNode.setAudioGraph(internalAudioGraph)
audioGraph.addNode(subgraphNode)
audioGraph.connect(subgraphNode, audioGraph.outputNode)
audioEngine.start(audioGraph)
}
fun play() {
// The audio players won't start playing until the internal audio graph is started
drumsPlayerNode.play()
bassPlayerNode.play()
internalAudioGraph.start()
}
fun pause() {
// Stopping the internal audio graph first to make sure that both audio players are stopped at the same time
internalAudioGraph.stop()
drumsPlayerNode.pause()
bassPlayerNode.pause()
}
fun stop() {
// Stopping the internal audio graph first to make sure that both audio players are stopped at the same time
internalAudioGraph.stop()
drumsPlayerNode.stop()
bassPlayerNode.stop()
}
fun close() {
audioGraph.close()
internalAudioGraph.close()
}
func loadBass(path: String, format: Codec) {
bassPlayerNode.load(path, format) }
func loadDrums(path: String, format: Codec) {
drumsPlayerNode.load(path, format)
}
}
#include "AudioGraph.hpp"
#include "SubgraphSourceNode.hpp"
#include "AudioPlayerNode.hpp"
#include "MixerNode.hpp"
using namespace switchboard;
class SynchronizedAudioPlayersExample {
public:
SynchronizedAudioPlayersExample() {
// Adding nodes to the internal audio graph
internalAudioGraph.addNode(drumsPlayerNode);
internalAudioGraph.addNode(bassPlayerNode);
internalAudioGraph.addNode(mixerNode);
// Connecting audio nodes
internalAudioGraph.connect(drumsPlayerNode, mixerNode);
internalAudioGraph.connect(bassPlayerNode, mixerNode);
internalAudioGraph.connect(mixerNode, internalAudioGraph.getOutputNode());
// Setting the internal graph on the node
subgraphNode.setAudioGraph(internalAudioGraph);
audioGraph.addNode(subgraphNode);
audioGraph.connect(subgraphNode, audioGraph.outputNode);
// Loading the tracks and starting the players
drumsPlayerNode.load("drums.mp3", ::Mp3);
bassPlayerNode.load("bass.mp3", ::Mp3);
drumsPlayerNode.play();
bassPlayerNode.play();
// Starting the graphs
internalAudioGraph.start();
audioGraph.start();
}
// Example method called by the audio processing pipeline on each buffer
bool process(float** buffers, const uint numberOfChannels, const uint numberOfFrames, const uint sampleRate) {
AudioBuffer<float> outBuffer = AudioBuffer<float>(numberOfChannels, numberOfFrames, false, sampleRate, buffers);
const bool result = audioGraph->processBuffer(nullptr, &outBuffer);
return result;
}
private:
AudioGraph audioGraph;
AudioGraph internalAudioGraph;
SubgraphSourceNode subgraphNode;
AudioPlayerNode drumsPlayerNode;
AudioPlayerNode bassPlayerNode;
MixerNode mixerNode;
};
Coming soon...
Coming soon...