在 iOS 上将立体声音频拆分为单声道流

split stereo audio to mono streams on iOS

如果有人回答了这个问题,我们深表歉意。我见过很多问题,但没有好的答案。

我正在尝试将立体声音乐从我的 iPod 库导出到两个单声道 caf 文件。我如何在 iOS 上执行此操作?我目前正在使用 Objective C。

谢谢!

更新:我已经设法让示例代码从 apple 那里运行:https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/05_Export.html

我的代码现在将导入媒体文件并输出为可以正常播放的有效 caf 文件。我的问题是我不知道如何在写入之前修改缓冲区。这是我到目前为止的代码:

// Get the next audio sample buffer, and append it to the output file.
CMSampleBufferRef sampleBuffer = [self.assetReaderAudioOutput copyNextSampleBuffer];
if (sampleBuffer != NULL)
{
    /////////////////////////////////////////
    CMBlockBufferRef blockBuffer;
    AudioBufferList audioBufferList;
    NSMutableData *data= [NSMutableData data];

    CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, NULL, &audioBufferList, sizeof(AudioBufferList), NULL, NULL, kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment, &blockBuffer);

    AudioBuffer audioBuffer = audioBufferList.mBuffers[0];

    for( int y=0; y< audioBufferList.mNumberBuffers; y=y+4 ){
        [data appendBytes:(audioBuffer.mData+y) length:2];
    }

    // how do I replace data in sampleBuffer with new data?

    CFRelease(blockBuffer);

    //////////////////////////////////////////

    BOOL success = [self.assetWriterAudioInput appendSampleBuffer:sampleBuffer];

    CFRelease(sampleBuffer);
    sampleBuffer = NULL;
    completedOrFailed = !success;
}
else
{
    completedOrFailed = YES;
}

我通过手动创建 header 并写入文件来设法做到这一点。这个 post 给了我大部分我需要的东西:https://whosebug.com/a/8677726/313272