关闭

CMSampleBufferRef与byte*互相转换

79人阅读 评论(0) 收藏 举报
分类:

**** audio ****

  • byte*转CMSampleBufferRef

-(AudioStreamBasicDescription) getAudioFormat{
    AudioStreamBasicDescription format;
    format.mSampleRate = 44100;
    format.mFormatID = kAudioFormatLinearPCM;
    format.mFormatFlags = kLinearPCMFormatFlagIsPacked | kLinearPCMFormatFlagIsSignedInteger;
    format.mBytesPerPacket = 2*2;
    format.mFramesPerPacket = 1;
    format.mBytesPerFrame = 2*2;
    format.mChannelsPerFrame = 2;
    format.mBitsPerChannel = 16;
    format.mReserved = 0;
    
    return format;
}
- (CMSampleBufferRef)createAudioSample:(void *)audioData frames:(UInt32)len
{
    int channels = 2;
    AudioBufferList audioBufferList;
    audioBufferList.mNumberBuffers = 1;
    audioBufferList.mBuffers[0].mNumberChannels=channels;
    audioBufferList.mBuffers[0].mDataByteSize=len;
    audioBufferList.mBuffers[0].mData = audioData;
    
    AudioStreamBasicDescription asbd = [self getAudioFormat];
    
    CMSampleBufferRef buff = NULL;
    static CMFormatDescriptionRef format = NULL;
    
    CMTime time = CMTimeMake(len/2 , 44100);
    CMSampleTimingInfo timing = {CMTimeMake(1,44100), time, kCMTimeInvalid };
    
    OSStatus error = 0;
    if(format == NULL)
        error = CMAudioFormatDescriptionCreate(kCFAllocatorDefault, &asbd, 0, NULL, 0, NULL, NULL, &format);
    
    error = CMSampleBufferCreate(kCFAllocatorDefault, NULL, false, NULL, NULL, format, len/(2*channels), 1, &timing, 0, NULL, &buff);
    
    if ( error ) {
        CLS_LOG(@"CMSampleBufferCreate returned error: %ld", (long)error);
        return NULL;
    }
    
    error = CMSampleBufferSetDataBufferFromAudioBufferList(buff, kCFAllocatorDefault, kCFAllocatorDefault, 0, &audioBufferList);
    
    if( error )
    {
        CLS_LOG(@"CMSampleBufferSetDataBufferFromAudioBufferList returned error: %ld", (long)error);
        return NULL;
    }
    
    return buff;
}



  • CMSampleBufferRef转byte*

-(void) getAudioData: (CMSampleBufferRef)sampleBuffer{

        AudioBufferList audioBufferList;
        CMBlockBufferRef blockBuffer;
        
        CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, NULL, &audioBufferList, sizeof(audioBufferList), NULL, NULL, 0, &blockBuffer);
        
        for( int y=0; y<audioBufferList.mNumberBuffers; y++ )
        {
            AudioBuffer audioBuffer = audioBufferList.mBuffers[y];
            void* audio = audioBuffer.mData;//这里获取
        }
        
        CFRelease(blockBuffer);
}





注:原文来自:这里






0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6344次
    • 积分:441
    • 等级:
    • 排名:千里之外
    • 原创:37篇
    • 转载:5篇
    • 译文:0篇
    • 评论:3条
    文章分类
    最新评论