AVAudioPlayerNode多声道音频控制



我已经成功地使用AVAudioPlayerNode播放立体声和单声道文件。我想使用具有 3+ 声道的文件(环绕声文件(,并能够以非线性方式路由音频。例如,我可以将文件通道 0 分配给输出通道 2,将文件通道 4 分配给输出通道 1。

音频

接口的输出数量将是未知的(2-40(,这就是为什么我需要能够允许用户按照他们认为合适的方式路由音频。WWDC 2015 507 中让用户更改音频 Midi 设置中的路由的解决方案不是一个可行的解决方案。

我能想到的只有 1 种可能性(我对其他人持开放态度(:为每个频道创建一个播放器,并为每个频道加载一个频道的缓冲区,类似于这篇文章。但即使海报承认,也存在问题。

所以我正在寻找一种将文件的每个通道复制到 AudioBuffer 的方法,例如:

let file = try AVAudioFile(forReading: audioURL)
let fullBuffer = AVAudioPCMBuffer(pcmFormat: file.processingFormat, 
                                  frameCapacity: AVAudioFrameCount(file.length))
try file.read(into: fullBuffer)
// channel 0
let buffer0 = AVAudioPCMBuffer(pcmFormat: file.processingFormat,
                               frameCapacity: AVAudioFrameCount(file.length))
// this doesn't work, unable to get fullBuffer channel and copy
// error on subscripting mBuffers
buffer0.audioBufferList.pointee.mBuffers.mData = fullBuffer.audioBufferList.pointee.mBuffers[0].mData
// repeat above buffer code for each channel from the fullBuffer

我能够弄清楚,所以这是让它工作的代码。注意:下面的代码分隔立体声(2 声道(文件。这可以很容易地扩展为处理未知数量的通道。

let file = try AVAudioFile(forReading: audioURL)
let formatL = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: file.processingFormat.sampleRate, channels: 1, interleaved: false)
let formatR = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: file.processingFormat.sampleRate, channels: 1, interleaved: 
let fullBuffer = AVAudioPCMBuffer(pcmFormat: file.processingFormat, frameCapacity: AVAudioFrameCount(file.length))
let bufferLeft = AVAudioPCMBuffer(pcmFormat: formatL, frameCapacity: AVAudioFrameCount(file.length))
let bufferRight = AVAudioPCMBuffer(pcmFormat: formatR, frameCapacity: AVAudioFrameCount(file.length))
try file.read(into: fullBuffer)
bufferLeft.frameLength = fullBuffer.frameLength
bufferRight.frameLength = fullBuffer.frameLength
for i in 0..<Int(file.length) {
    bufferLeft.floatChannelData![0][i] = fullBuffer.floatChannelData![0][i]
    bufferRight.floatChannelData![0][i] = fullBuffer.floatChannelData![1][i]
}

最新更新