2017-02-14 78 views
0

我想將2個CAF文件本地轉換爲一個文件。這兩個CAF文件是單聲道流,理想情況下,我希望它們是一個立體聲文件,這樣我就可以從一個聲道獲得麥克風,從另一個聲道獲得揚聲器。如何在iOS中將2個單聲道文件轉換爲單個立體聲文件?

我最初是通過使用AVAssetTrack和AVMutableCompositionTracks開始的,但是我無法解決混音問題。我的合併文件是一個單一的單一流,交錯兩個文件。所以我選擇了AVAudioEngine路線。

從我的理解,我可以通過我的兩個文件作爲輸入節點,將它們連接到混音器,並有一個能夠獲得立體聲混音的輸出節點。輸出文件具有立體聲佈局,但沒有音頻數據似乎寫入它,因爲我可以在Audacity中打開它並查看立體聲佈局。在installTapOnBus調用周圍放置dipatch sephamore信號也沒有多大幫助。 CoreAudio一直是一個難以理解的挑戰,因此我們將不勝感激。

// obtain path of microphone and speaker files 
NSString *micPath = [[NSBundle mainBundle] pathForResource:@"microphone" ofType:@"caf"]; 
NSString *spkPath = [[NSBundle mainBundle] pathForResource:@"speaker" ofType:@"caf"]; 
NSURL *micURL = [NSURL fileURLWithPath:micPath]; 
NSURL *spkURL = [NSURL fileURLWithPath:spkPath]; 

// create engine 
AVAudioEngine *engine = [[AVAudioEngine alloc] init]; 

AVAudioFormat *stereoFormat = [[AVAudioFormat alloc] initStandardFormatWithSampleRate:16000 channels:2]; 

AVAudioMixerNode *mainMixer = engine.mainMixerNode; 

// create audio files 
AVAudioFile *audioFile1 = [[AVAudioFile alloc] initForReading:micURL error:nil]; 
AVAudioFile *audioFile2 = [[AVAudioFile alloc] initForReading:spkURL error:nil]; 

// create player input nodes 
AVAudioPlayerNode *apNode1 = [[AVAudioPlayerNode alloc] init]; 
AVAudioPlayerNode *apNode2 = [[AVAudioPlayerNode alloc] init]; 

// attach nodes to the engine 
[engine attachNode:apNode1]; 
[engine attachNode:apNode2]; 

// connect player nodes to engine's main mixer 
stereoFormat = [mainMixer outputFormatForBus:0]; 
[engine connect:apNode1 to:mainMixer fromBus:0 toBus:0 format:audioFile1.processingFormat]; 
[engine connect:apNode2 to:mainMixer fromBus:0 toBus:1 format:audioFile2.processingFormat]; 
[engine connect:mainMixer to:engine.outputNode format:stereoFormat]; 

// start the engine 
NSError *error = nil; 
if(![engine startAndReturnError:&error]){ 
    NSLog(@"Engine failed to start."); 
} 

// create output file 
NSString *mergedAudioFile = [[micPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"merged.caf"]; 
[[NSFileManager defaultManager] removeItemAtPath:mergedAudioFile error:&error]; 
NSURL *mergedURL = [NSURL fileURLWithPath:mergedAudioFile]; 
AVAudioFile *outputFile = [[AVAudioFile alloc] initForWriting:mergedURL settings:[engine.inputNode inputFormatForBus:0].settings error:&error]; 

// write from buffer to output file 
[mainMixer installTapOnBus:0 bufferSize:4096 format:[mainMixer outputFormatForBus:0] block:^(AVAudioPCMBuffer *buffer, AVAudioTime *when){ 
    NSError *error; 
    BOOL success; 
    NSLog(@"Writing"); 
    if((outputFile.length < audioFile1.length) || (outputFile.length < audioFile2.length)){ 
     success = [outputFile writeFromBuffer:buffer error:&error]; 
     NSCAssert(success, @"error writing buffer data to file, %@", [error localizedDescription]); 
     if(error){ 
      NSLog(@"Error: %@", error); 
     } 
    } 
    else{ 
     [mainMixer removeTapOnBus:0]; 
     NSLog(@"Done writing"); 
    } 
}]; 

}

+0

你持有的強引用你寫的AVAudioFile? – dave234

+0

@ Dave,outputFile在寫入之前不存在。在強引用方面,我將audioFile設置爲寫入mergedURL,這是mergedAudioFile的fileURLWithPath。沒有其他對象/變量引用outputFile,並且在installTapOnBus調用之後我沒有銷燬它。 – A21

+0

這種方法的一個弱點是,你將不得不等待文件的持續時間被渲染爲一個。這就是說,如果你堅持使用AVAudioEngine,你可能會試着讓這兩個文件先玩。然後,一旦該步驟完成,安裝輕擊並寫入文件。但如果我自己做,我會使用C API。 – dave234

回答

2

ExtAudioFile這樣做涉及三個文件和三個緩衝區。兩個單聲道讀取,一個立體聲寫入。在一個循環中,每個單聲道文件會將一段音頻讀取到其單聲道輸出緩衝區,然後複製到正確的「一半」立體聲緩衝區中。然後在立體聲緩衝器中充滿數據,將該緩衝器寫入輸出文件,直到兩個單聲道文件完成讀取(如果一個單聲道文件比另一個單聲道文件長,則寫入零)。

對於我來說最成問題的領域是正確獲取文件格式,核心音頻需要非常特定的格式。幸運的是,存在AVAudioFormat以簡化一些常用格式的創建。

每個音頻文件讀取器/寫入器有兩種格式,一種代表數據存儲的格式(file_format),另一種指定進出讀寫器格式(client_format)。如果格式不同,格式轉換器內置給讀者/作者。

下面是一個例子:

-(void)soTest{ 


    //This is what format the readers will output 
    AVAudioFormat *monoClienFormat = [[AVAudioFormat alloc]initWithCommonFormat:AVAudioPCMFormatInt16 sampleRate:44100.0 channels:1 interleaved:0]; 

    //This is the format the writer will take as input 
    AVAudioFormat *stereoClientFormat = [[AVAudioFormat alloc]initWithCommonFormat:AVAudioPCMFormatInt16 sampleRate:44100 channels:2 interleaved:0]; 

    //This is the format that will be written to storage. It must be interleaved. 
    AVAudioFormat *stereoFileFormat = [[AVAudioFormat alloc]initWithCommonFormat:AVAudioPCMFormatInt16 sampleRate:44100 channels:2 interleaved:1]; 




    NSURL *leftURL = [NSBundle.mainBundle URLForResource:@"left" withExtension:@"wav"]; 
    NSURL *rightURL = [NSBundle.mainBundle URLForResource:@"right" withExtension:@"wav"]; 

    NSString *stereoPath = [documentsDir() stringByAppendingPathComponent:@"stereo.wav"]; 
    NSURL *stereoURL = [NSURL URLWithString:stereoPath]; 

    ExtAudioFileRef leftReader; 
    ExtAudioFileRef rightReader; 
    ExtAudioFileRef stereoWriter; 


    OSStatus status = 0; 

    //Create readers and writer 
    status = ExtAudioFileOpenURL((__bridge CFURLRef)leftURL, &leftReader); 
    if(status)printf("error %i",status);//All the ExtAudioFile functins return a non-zero status if there's an error, I'm only checking one to demonstrate, but you should be checking all the ExtAudioFile function returns. 
    ExtAudioFileOpenURL((__bridge CFURLRef)rightURL, &rightReader); 
    //Here the file format is set to stereo interleaved. 
    ExtAudioFileCreateWithURL((__bridge CFURLRef)stereoURL, kAudioFileCAFType, stereoFileFormat.streamDescription, nil, kAudioFileFlags_EraseFile, &stereoWriter); 


    //Set client format for readers and writer 
    ExtAudioFileSetProperty(leftReader, kExtAudioFileProperty_ClientDataFormat, sizeof(AudioStreamBasicDescription), monoClienFormat.streamDescription); 
    ExtAudioFileSetProperty(rightReader, kExtAudioFileProperty_ClientDataFormat, sizeof(AudioStreamBasicDescription), monoClienFormat.streamDescription); 
    ExtAudioFileSetProperty(stereoWriter, kExtAudioFileProperty_ClientDataFormat, sizeof(AudioStreamBasicDescription), stereoClientFormat.streamDescription); 


    int framesPerRead = 4096; 
    int bufferSize = framesPerRead * sizeof(SInt16); 

    //Allocate memory for the buffers 
    AudioBufferList *leftBuffer = createBufferList(bufferSize,1); 
    AudioBufferList *rightBuffer = createBufferList(bufferSize,1); 
    AudioBufferList *stereoBuffer = createBufferList(bufferSize,2); 

    //ExtAudioFileRead takes an ioNumberFrames argument. On input the number of frames you want, on otput it's the number of frames you got. 0 means your done. 
    UInt32 leftFramesIO = framesPerRead; 
    UInt32 rightFramesIO = framesPerRead; 



    while (leftFramesIO || rightFramesIO) { 
     if (leftFramesIO){ 
      //If frames to read is less than a full buffer, zero out the remainder of the buffer 
      int framesRemaining = framesPerRead - leftFramesIO; 
      if (framesRemaining){ 
       memset(((SInt16 *)leftBuffer->mBuffers[0].mData) + framesRemaining, 0, sizeof(SInt16) * framesRemaining); 
      } 
      //Read into left buffer 
      leftBuffer->mBuffers[0].mDataByteSize = leftFramesIO * sizeof(SInt16); 
      ExtAudioFileRead(leftReader, &leftFramesIO, leftBuffer); 
     } 
     else{ 
      //set to zero if no more frames to read 
      memset(leftBuffer->mBuffers[0].mData, 0, sizeof(SInt16) * framesPerRead); 
     } 

     if (rightFramesIO){ 
      int framesRemaining = framesPerRead - rightFramesIO; 
      if (framesRemaining){ 
       memset(((SInt16 *)rightBuffer->mBuffers[0].mData) + framesRemaining, 0, sizeof(SInt16) * framesRemaining); 
      } 
      rightBuffer->mBuffers[0].mDataByteSize = rightFramesIO * sizeof(SInt16); 
      ExtAudioFileRead(rightReader, &rightFramesIO, rightBuffer); 
     } 
     else{ 
      memset(rightBuffer->mBuffers[0].mData, 0, sizeof(SInt16) * framesPerRead); 
     } 


     UInt32 stereoFrames = MAX(leftFramesIO, rightFramesIO); 

     //copy left to stereoLeft and right to stereoRight 
     memcpy(stereoBuffer->mBuffers[0].mData, leftBuffer->mBuffers[0].mData, sizeof(SInt16) * stereoFrames); 
     memcpy(stereoBuffer->mBuffers[1].mData, rightBuffer->mBuffers[0].mData, sizeof(SInt16) * stereoFrames); 

     //write to file 
     stereoBuffer->mBuffers[0].mDataByteSize = stereoFrames * sizeof(SInt16); 
     stereoBuffer->mBuffers[1].mDataByteSize = stereoFrames * sizeof(SInt16); 
     ExtAudioFileWrite(stereoWriter, stereoFrames, stereoBuffer); 

    } 

    ExtAudioFileDispose(leftReader); 
    ExtAudioFileDispose(rightReader); 
    ExtAudioFileDispose(stereoWriter); 

    freeBufferList(leftBuffer); 
    freeBufferList(rightBuffer); 
    freeBufferList(stereoBuffer); 

} 

AudioBufferList *createBufferList(int bufferSize, int numberBuffers){ 
    assert(bufferSize > 0 && numberBuffers > 0); 
    int bufferlistByteSize = sizeof(AudioBufferList); 
    bufferlistByteSize += sizeof(AudioBuffer) * (numberBuffers - 1); 
    AudioBufferList *bufferList = malloc(bufferlistByteSize); 
    bufferList->mNumberBuffers = numberBuffers; 
    for (int i = 0; i < numberBuffers; i++) { 
     bufferList->mBuffers[i].mNumberChannels = 1; 
     bufferList->mBuffers[i].mData = malloc(bufferSize); 
    } 
    return bufferList; 
}; 
void freeBufferList(AudioBufferList *bufferList){ 
    for (int i = 0; i < bufferList->mNumberBuffers; i++) { 
     free(bufferList->mBuffers[i].mData); 
    } 
    free(bufferList); 
} 
NSString *documentsDir(){ 
    static NSString *path = NULL; 
    if(!path){ 
     path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, 1).firstObject; 
    } 
    return path; 
} 
+0

我正在取回每個通道沒有輸出的立體聲文件。輸入單聲道文件是CAF型的,但我不希望格式偏差太大。 – A21

+0

你在檢查所有的ExtAudioFile返回值嗎? – dave234

+0

是的,注意到這個問題是EAF輸出文件的創建。我傳入的網址是擴展名 - 「.caf」與您的「.wav」相比較。給我一個1718449215的OSStatus錯誤,它指的是kAudioFormatUnsupportedDataFormatError。 – A21

相關問題