AVAudioEngine播放中如何访问多个通道?

How do you access multiple channels in AVAudioEngine playback?

我有一个 5.1 声道的音频文件。我如何在播放期间访问包含所有这些信息的缓冲区?

设置大致是这样

    engine.attach(playerNode)
        
    engine.connect(playerNode, to: engine.mainMixerNode, format: audioFile.processingFormat)
    engine.prepare()
        
    try? engine.start()

我认为文件加载和播放非常标准。

    let audioFile: AVAudioFile = try? .init(forReading: url, commonFormat: AVAudioCommonFormat.pcmFormatFloat32, interleaved: false)

    // ...
    
    playerNode.scheduleFile(audioFile, at: nil)

然后我在总线上使用了一个 tap 来访问缓冲区。

    let format: AVAudioFormat = engine.mainMixerNode.outputFormat(forBus: 0)

    engine.mainMixerNode.installTap(
        onBus: 0,
        bufferSize: 1024,
        format: format
    ) { buffer, time in

        // Do something cool with buffer, but buffer only has two channels.

        for channel in 0..<buffer.format.channelCount {
        }
    }

问题:

playerNode 上安装您的水龙头,无需费心指定格式:

playerNode.installTap(
    onBus: 0,
    bufferSize: 1024,
    format: nil
) { buffer, time in

    // 6 channel buffer
    for channel in 0..<buffer.format.channelCount {
    }
}