我已经成功地使用AVAudioPlayerNode播放立体声和单声道文件。我想使用具有3+通道的文件(环绕文件),并能够以非线性方式路由音频。例如,我可以将文件通道0分配给输出通道2,将文件通道4分配给输出通道1。
音频接口的输出数量将是未知的(2-40),这就是为什么我需要能够允许用户按他们认为合适的方式路由音频。而在WWDC 2015 507中让用户在音频Midi设置中改变路由的解决方案不是一个可行的解决方案。
我能想到的可能性只有一种(我对其他人开放):为每个通道创建一个播放器,并为每个通道加载仅相当于一个通道的缓冲区similar to this post。但即使是通过海报承认,也存在一些问题。
因此,我正在寻找一种将文件的每个通道复制到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发布于 2017-06-02 10:16:16
我能够弄清楚它,所以这是让它工作的代码。注:下面的代码分隔立体声(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]
}https://stackoverflow.com/questions/44222672
复制相似问题