I tried mixing the user mic and an AVAudioPlayerNode
, and writing it to a file. At same time,I want to keep both individual streams to do something.
I use below code to make connections. By using these connections, tap on recorderMixNode
callback empty buffer. tap on mainMixer
and inputMixNode
callback correctly.
try input.setVoiceProcessingEnabled(true)
let output = engine.outputNode
let mainMixer = engine.mainMixerNode
engine.attach(player)
engine.attach(recorderMixNode)
engine.attach(inputMixNode)
let newFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 24000, channels: 1, interleaved: true)!
engine.connect(player, to: [AVAudioConnectionPoint(node: recorderMixNode, bus: 0), AVAudioConnectionPoint(node: mainMixer, bus: 0)], fromBus: 0, format: newFormat)
engine.connect(mainMixer, to: output, format: nil)
engine.connect(input, to: [AVAudioConnectionPoint(node: recorderMixNode, bus: recorderMixNode.nextAvailableInputBus), AVAudioConnectionPoint(node: inputMixNode, bus: 0)], fromBus: 0, format: newFormat)//
recorderMixNode.installTap(onBus: 0, bufferSize: 1600, format: newFormat) { [weak self] buffer, when in
//write buffer to file
}
inputMixNode.installTap(onBus: 0, bufferSize: 1600, format: newFormat) { [weak self] buffer, when in
//handle input
}
mainMixer.installTap(onBus: 0, bufferSize: 2400, format: nil) { [weak self] buffer, when in
//handle output
}
engine.prepare()
What I have tried:
removing player's connection to
recorderMixNode
: can record input correctly.removing input's connection to
recorderMixNode
: can not record player correctly.new
AVAudioPlayerNode
only connect torecorderMixNode
: crash when callingAVAudioPlayerNode.play()
, got playerstarted when in a disconnected state
. guessing causerecorderMixNode
not connecting to an output.
My question:
I wonder where can AVAudioPlayerNode
make a one-to-many connection.
If not, are there any suggested settings to make player stream to both nodes?