Skip to main content

Mixer

This example loads two mp3 audio files and mixes them together using the MixerNode. The two GainNode instances can be used to control the volume of the individual audio players.

import SwitchboardSDK

class MixerExample {

let audioEngine = SBAudioEngine()
let audioGraph = SBAudioGraph()
let drumsPlayerNode = SBAudioPlayerNode()
let drumsGainNode = SBGainNode()
let bassPlayerNode = SBAudioPlayerNode()
let bassGainNode = SBGainNode()
let mixerNode = SBMixerNode()

init() {
drumsPlayerNode.loadFile("drums.mp3")
bassPlayerNode.loadFile("bass.mp3")

audioGraph.addNode(drumsPlayerNode)
audioGraph.addNode(drumsGainNode)
audioGraph.addNode(bassPlayerNode)
audioGraph.addNode(bassGainNode)
audioGraph.addNode(mixerNode)

audioGraph.connect(drumsPlayerNode, to: drumsGainNode)
audioGraph.connect(bassPlayerNode, to: bassGainNode)
audioGraph.connect(drumsGainNode, to: mixerNode)
audioGraph.connect(bassGainNode, to: mixerNode)
audioGraph.connect(mixerNode, to: audioGraph.outputNode)

audioEngine.start(audioGraph)
}

func play() {
drumsPlayerNode.play()
bassPlayerNode.play()
}

func pause() {
drumsPlayerNode.pause()
bassPlayerNode.pause()
}

func setDrumsVolume(_ newValue: Float) {
drumsGainNode.setGain(newValue)
}

func setBassVolume(_ newValue: Float) {
bassGainNode.setGain(newValue)
}
}