Skip to main content

Tapping Audio in Audio Graphs

TapAudioNode can be used to capture buffers going through any point of an audio graph. onBufferUpdate callback can be used to get access to AudioBuffer object. AudioBuffer has a helper method getInterleavedData that can return pointer to a interlaved audio data.

TapAudioNode is a Sink node, which means it does not have any output. To add this node in between two nodes you will need to use BusSplitterNode.

import SwitchboardSDK

var audioBuffer = SBAudioBuffer()
let tapAudioNode = SBTapAudioNode()
let splitterNode = SBBusSplitterNode()

tapAudioNode.onBufferUpdate = { [weak self] in
guard let self = self else { return }
audioBuffer = self.tapAudioNode.buffers

// get pointer to buffers
let buffers = audioBuffer.getReadPointer()

// you can also get pointer to interleaved buffer
let interleavedBuffer = audioBuffer.getInterleavedData()
}

audioGraph.addNode(tapAudioNode)
audioGraph.addNode(splitterNode)

audioGraph.connect(audioGraph.inputNode, to: splitterNode)
audioGraph.connect(splitterNode, to: tapAudioNode)
audioGraph.connect(splitterNode, to: audioGraph.outputNode)
note

onBufferUpdate is called on the audio thread, so make sure any file writing operations you do in the onBufferUpdate are on a separate thread to avoid blocking the audio thread.

Read more about the C++ classes in our API reference.