2017-09-04 99 views
1

我有15個WAV文件,需要按順序播放各個頻道。我開始嘗試使用左/右立體聲分離處理兩個文件。多個聲道播放多個WAV AVAudioEngine

我正在創建一個音頻引擎,一個混音器和兩個AVAudioPlayerNodes。音頻文件是單聲道的,我試圖從PlayerA得到文件出來左邊的頻道,並從PlayerB的文件出來右邊的頻道。我無法理解的是AudioUnitSetProperty是如何工作的。它似乎只涉及單個文件,似乎只能有一個每個audioUnit?我想知道是否有一種方法可以將文件與audioUnit關聯起來?我似乎無法返回與每個軌道關聯的audioUnit對象。

func testCode(){ 

    // get output hardware format 
    let output = engine.outputNode 
    let outputHWFormat = output.outputFormat(forBus: 0) 
    // connect mixer to output 
    let mixer = engine.mainMixerNode 
    engine.connect(mixer, to: output, format: outputHWFormat) 


    //then work on the player end by first attaching the player to the engine 
    engine.attach(playerA) 
    engine.attach(playerB) 


    //find the audiofile 
    guard let audioFileURLA = Bundle.main.url(forResource: "test", withExtension: "wav") else { 
     fatalError("audio file is not in bundle.") 
    } 

    guard let audioFileURLB = Bundle.main.url(forResource: "test2", withExtension: "wav") else { 
     fatalError("audio file is not in bundle.") 
    } 

    var songFileA:AVAudioFile? 
    do { 
     songFileA = try AVAudioFile(forReading: audioFileURLA) 
     print(songFileA!.processingFormat) 

     // connect player to mixer 
     engine.connect(playerA, to: mixer, format: songFileA!.processingFormat) 

    } catch { 
     fatalError("canot create AVAudioFile \(error)") 
    } 


    let channelMap: [Int32] = [0, -1] //play channel in left 


    let propSize: UInt32 = UInt32(channelMap.count) * UInt32(MemoryLayout<sint32>.size) 

    print(propSize) 

    let code: OSStatus = AudioUnitSetProperty((engine.inputNode?.audioUnit)!, 
               kAudioOutputUnitProperty_ChannelMap, 
               kAudioUnitScope_Global, 
               1, 
               channelMap, 
               propSize); 
    print(code) 


    let channelMapB: [Int32] = [-1, 0] //play channel in left 

    var songFileB:AVAudioFile? 
    do { 
     songFileB = try AVAudioFile(forReading: audioFileURLB) 
     print(songFileB!.processingFormat) 

     // connect player to mixer 
     engine.connect(playerB, to: mixer, format: songFileB!.processingFormat) 

    } catch { 
     fatalError("canot create AVAudioFile \(error)") 
    } 

    let codeB: OSStatus = AudioUnitSetProperty((engine.inputNode?.audioUnit)!, 
               kAudioOutputUnitProperty_ChannelMap, 
               kAudioUnitScope_Global, 
               1, 
               channelMapB, 
               propSize); 

    print(codeB) 


    do { 
     try engine.start() 
    } catch { 
     fatalError("Could not start engine. error: \(error).") 
    } 

    playerA.scheduleFile(songFileA!, at: nil) { 
     print("done") 
     self.playerA.play() 
    } 
    playerB.scheduleFile(songFileA!, at: nil) { 
     print("done") 
     self.playerB.play() 
    } 

    playerA.play() 
    playerB.play() 

    print(playerA.isPlaying) 

} 

回答

0

engine.connect(混頻器,以:輸出,格式:outputHWFormat)

這是沒有必要的,混頻器將被隱式訪問時連接。

至於平移:AudioUnitSetProperty也是沒有必要的。 AVAudioPlayerNode符合AVAudioMixing,這樣以來有來自玩家下游的混頻器節點,所有你需要做的是這樣的:

playerA.pan = -1 
playerB.pan = 1 
+0

感謝您的建議戴夫。我曾經看過泛功能,但我最終試圖將15個文件輸出到15個獨立的頻道,因此無法在該應用程序中進行平移。 – tsugua