Can AVAudioPlayerNode make a one-to-many connetion

22 views Asked by At

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:

  1. removing player's connection to recorderMixNode: can record input correctly.

  2. removing input's connection to recorderMixNode: can not record player correctly.

  3. new AVAudioPlayerNode only connect to recorderMixNode: crash when calling AVAudioPlayerNode.play(), got player started when in a disconnected state. guessing cause recorderMixNode 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?

0

There are 0 answers