2017-05-28 21 views
0

AVAudioPlayerNodeを使用して、ステレオおよびモノラルファイルを正常に再生しました。私は3つ以上のチャンネル(サラウンドファイル)を持つファイルを使用し、オーディオを非線形にルーティングできるようにしたいと考えています。たとえば、出力チャンネル2にファイルチャンネル0を割り当て、出力チャンネル1にファイルチャンネル4を割り当てることができます。AVAudioPlayerNodeマルチチャンネルオーディオコントロール

オーディオインターフェイスの出力数は不明です(2〜40)。これが理由ですユーザーが適切に見えるように音声をルーティングすることを可能にする。また、ユーザーがAudio Midi Setupでルーティングを変更したというWWDC 2015 507の解決策は実行可能な解決策ではありません。

1つのチャンネルにつき1つのプレイヤーを作成し、1チャンネル分のバッファーだけをロードすると、私は考えることができます(私は他の人にも公開しています)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 

答えて

0

。注:以下のコードは、ステレオ(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] 
} 
関連する問題