关闭

CMSampleBufferRef与byte*互相转换

183人阅读 评论(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
查看评论

CMSampleBufferRef与byte*互相转换(audio)

byte*转CMSampleBufferRef -(AudioStreamBasicDescription) getAudioFormat{     AudioStreamBasicDescription format;     format...
  • u011270282
  • u011270282
  • 2016-06-30 17:31
  • 3591

在直播应用中添加Faceu效果

在我写的上篇文章 中,介绍了美颜滤镜的实现原理,已经能够体会到GPUImage 的强大。本文将要介绍的Faceu贴纸效果也是基于GPUImage实现的,demo我放在了GitHub上。 1.核心原理        Faceu贴纸效果其实就是在人脸上贴一些图...
  • donwei8
  • donwei8
  • 2016-07-20 16:12
  • 7061

avsubtitleWriter demo解析(二):创建CMSampleBufferRef

这里有必要要阐述下字幕wen'ji
  • Mamong
  • Mamong
  • 2014-04-13 07:57
  • 7393

CMSampleBufferRef与byte*互相转换(audio)

byte*转CMSampleBufferRef -(AudioStreamBasicDescription) getAudioFormat{     AudioStreamBasicDescription format;     format...
  • yangzm
  • yangzm
  • 2017-01-28 02:47
  • 605

CMSampleBufferRef转换为nsdata对象

CMSampleBufferRef TO NSData CMBlockBufferRef blockBufferRef = CMSampleBufferGetDataBuffer(sampleBuffer); size_t length = CMBlockBufferGetDataLengt...
  • zzqangel
  • zzqangel
  • 2014-07-08 15:43
  • 4146

iOS视频软编码

软编码介绍 软编码主要是利用CPU进行编码的过程, 具体的编码通常会用FFmpeg+x264FFmpeg FFmpeg是一个非常强大的音视频处理库,包括视频采集功能、视频格式转换、视频抓图、给视频加水印等。FFmpeg在Linux平台下开发,但它同样也可以在其它操作系统环境中编译运行,包括Win...
  • qq_450351763
  • qq_450351763
  • 2017-02-28 16:28
  • 781

get buffer from CMSampleBufferRef

从CMSampleBufferRef获得数据buffer。 NSData* imageToBuffer( CMSampleBufferRef source) { CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sour...
  • u011270282
  • u011270282
  • 2015-12-30 18:48
  • 980

CMSampleBufferRef与byte*互相转换

**** audio **** byte*转CMSampleBufferRef -(AudioStreamBasicDescription) getAudioFormat{ AudioStreamBasicDescription format; forma...
  • a997013919
  • a997013919
  • 2017-09-21 17:43
  • 183

Ios 实现麦克风捕获和AAC编码

在Ios中,实现打开和捕获麦克风大多是用的AVCaptureSession这个组件来实现的,它可以不仅可以实现音频捕获,还可以实现视频的捕获。本文将主要实现麦克风音频的捕获和编码。 针对打开麦克风和捕获音频的代码,网上也有一些,我就简单的整理了一下: 首先,我们需要定义一个AVCaptureSe...
  • shenyi0106
  • shenyi0106
  • 2015-07-22 15:22
  • 5735

CMSampleBufferRef 与 UIImage 的转换

转载自:http://iteches.com/archives/34496  (这里面还有其他 很多的信息 可看看) CMSampleBufferRef 与 UIImage 的转换 在取得  CMSampleBufferRef 之后,还必须透过一连串的转换才能够得...
  • chenyong05314
  • chenyong05314
  • 2013-12-26 10:40
  • 11199
    个人资料
    • 访问:11131次
    • 积分:579
    • 等级:
    • 排名:千里之外
    • 原创:45篇
    • 转载:5篇
    • 译文:0篇
    • 评论:6条
    文章分类
    最新评论