使用原始PCM 16000采样率流中的CMSampleTimingInfo,CMSampleBuffer和AudioBufferList [英] Using CMSampleTimingInfo, CMSampleBuffer and AudioBufferList from raw PCM 16000 sample rate stream

查看:694
本文介绍了使用原始PCM 16000采样率流中的CMSampleTimingInfo,CMSampleBuffer和AudioBufferList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从外部接收音频数据和大小,音频似乎是线性PCM,签名为int16,但是当使用AssetWriter录制音频时,它会保存到音频文件中,失真度高且音调高。

I recevie audio data and size from outside, the audio appears to be linear PCM, signed int16, but when recording this using an AssetWriter it saves to the audio file highly distorted and higher pitch.

#define kSamplingRate       16000
#define kNumberChannels     1

UInt32 framesAlreadyWritten = 0;

-(AudioStreamBasicDescription) getAudioFormat {
AudioStreamBasicDescription format;
format.mSampleRate = kSamplingRate;
format.mFormatID = kAudioFormatLinearPCM;
format.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
format.mChannelsPerFrame = 1;  // mono
format.mBitsPerChannel = 16;
format.mBytesPerFrame = sizeof(SInt16);
format.mFramesPerPacket = 1;
format.mBytesPerPacket = format.mBytesPerFrame * format.mFramesPerPacket;
format.mReserved = 0;
return format;
}

 - (CMSampleBufferRef)createAudioSample:(const void *)audioData frames:    (UInt32)len {

AudioStreamBasicDescription asbd = [self getAudioFormat];

CMSampleBufferRef buff = NULL;
static CMFormatDescriptionRef format = NULL;
OSStatus error = 0;

if(format == NULL) {
    AudioChannelLayout acl;
    bzero(&acl, sizeof(acl));
    acl.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;
    error = CMAudioFormatDescriptionCreate(kCFAllocatorDefault, &asbd, sizeof(acl), &acl, 0, NULL, NULL, &format);
}

CMTime duration = CMTimeMake(1, kSamplingRate);
CMTime pts = CMTimeMake(framesAlreadyWritten, kSamplingRate);
NSLog(@"-----------pts");
CMTimeShow(pts);
CMSampleTimingInfo timing = {duration , pts, kCMTimeInvalid };
error = CMSampleBufferCreate(kCFAllocatorDefault, NULL, false, NULL, NULL, format, len, 1, &timing, 0, NULL, &buff);
framesAlreadyWritten += len;

if (error) {
    NSLog(@"CMSampleBufferCreate returned error: %ld", (long)error);
    return NULL;
}

AudioBufferList audioBufferList;
audioBufferList.mNumberBuffers = 1;
audioBufferList.mBuffers[0].mNumberChannels = asbd.mChannelsPerFrame;
audioBufferList.mBuffers[0].mDataByteSize = (UInt32)(number_of_frames * audioFormat.mBytesPerFrame);
audioBufferList.mBuffers[0].mData = audioData;

error = CMSampleBufferSetDataBufferFromAudioBufferList(buff, kCFAllocatorDefault, kCFAllocatorDefault, 0, &audioBufferList);
if(error) {
    NSLog(@"CMSampleBufferSetDataBufferFromAudioBufferList returned error: %ld", (long)error);
    return NULL;
}
return buff;
}


推荐答案

不确定您为什么将 len 除以2,但是您的时间应该进步而不是保持不变,例如

Not sure why you're dividing len by two, but your time should progress instead of being constant, something like

CMTime time = CMTimeMake(framesAlreadyWritten , kSamplingRate);

这篇关于使用原始PCM 16000采样率流中的CMSampleTimingInfo,CMSampleBuffer和AudioBufferList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆