首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVAudioPlayerNode多声道音频控制

AVAudioPlayerNode多声道音频控制
EN

Stack Overflow用户
提问于 2017-05-28 08:13:04
回答 1查看 528关注 0票数 2

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

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

我能想到的可能性只有一种(我对其他人开放):为每个通道创建一个播放器,并为每个通道加载仅相当于一个通道的缓冲区similar to this post。但即使是通过海报承认,也存在一些问题。

因此,我正在寻找一种将文件的每个通道复制到AudioBuffer中的方法,如下所示:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2017-06-02 10:16:16

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

代码语言:javascript
复制
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]
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44222672

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档