如何设置AudioStreamBasicDescription

概念

参考:AudioStreamBasicDescription

音频流:连续的表示声音的数据流,可以是pcm也是编码后的数据流,例如aac

声道:声道(Sound Channel) 是指声音在录制或播放时在不同空间位置采集或回放的相互独立的音频信号,所以声道数也就是声音录制时的音源数量或回放时相应的扬声器数量。单声道有一个声道,立体声有两个声道。

采样点: 音频流中单个声道的一个采样点的数字表示。例如采样率16000,表示1秒内单个声道会有16000个采样点。

: 同一时间点的多个声道的采样集合构成一帧。对于一个立体声的pcm数据,一帧有两个采样,一个来自左声道,一个来自右声道。

: 一个或多个连续帧的集合。pcm中一个包中只有一帧,在压缩格式中,一个包里面会有多个帧。例如aac,一个包里一般有1024个帧。

采样率: 对pcm来说,一秒钟的采样个数,常见的有16000,32000,44100等。

数据结构

struct AudioStreamBasicDescription
{
    Float64             mSampleRate;
    AudioFormatID       mFormatID;
    AudioFormatFlags    mFormatFlags;
    UInt32              mBytesPerPacket;
    UInt32              mFramesPerPacket;
    UInt32              mBytesPerFrame;
    UInt32              mChannelsPerFrame;
    UInt32              mBitsPerChannel;
    UInt32              mReserved;
};

Float64 mSampleRate

采样率,16000,32000,44100 …

AudioFormatID mFormatID

四个char字符标识的音频数据格式。

CF_ENUM(AudioFormatID)
{
    kAudioFormatLinearPCM               = 'lpcm',
    kAudioFormatAppleIMA4               = 'ima4', 
    kAudioFormatMPEG4AAC                = 'aac ',
    kAudioFormatMPEGLayer3              = '.mp3',
    kAudioFormatOpus                    = 'opus'
    ...
};

用AudioUnit采集的通常就是pcm的裸流,编码后的数据,根据编码类型,可以是aac,mp3,opus 等。

AudioFormatFlags mFormatFlags

描述采样点表示的格式(用int还是用float,是大端还是小端),声道布局(是平面还是交错),可以组合使用。

ffmpeg 中描述采样点格式如下:

$ ffmpeg -formats | grep PCM
 DE alaw            PCM A-law
 DE f32be           PCM 32-bit floating-point big-endian
 DE f32le           PCM 32-bit floating-point little-endian
 DE f64be           PCM 64-bit floating-point big-endian
 DE f64le           PCM 64-bit floating-point little-endian
 DE mulaw           PCM mu-law
 DE s16be           PCM signed 16-bit big-endian
 DE s16le           PCM signed 16-bit little-endian
 DE s24be           PCM signed 24-bit big-endian
 DE s24le           PCM signed 24-bit little-endian
 DE s32be           PCM signed 32-bit big-endian
 DE s32le           PCM signed 32-bit little-endian
 DE s8              PCM signed 8-bit
 DE u16be           PCM unsigned 16-bit big-endian
 DE u16le           PCM unsigned 16-bit little-endian
 DE u24be           PCM unsigned 24-bit big-endian
 DE u24le           PCM unsigned 24-bit little-endian
 DE u32be           PCM unsigned 32-bit big-endian
 DE u32le           PCM unsigned 32-bit little-endian
 DE u8              PCM unsigned 8-bit

标识采样点数据类型

CF_ENUM(AudioFormatFlags)
{
    kAudioFormatFlagIsFloat                     = (1U << 0),     // 0x1
    kAudioFormatFlagIsSignedInteger             = (1U << 2),     // 0x4
};

kAudioFormatFlagIsFloat 和 kAudioFormatFlagIsSignedInteger 不可以一起使用。

存在kAudioFormatFlagIsFloat标识,使用float类型,否则使用int类型。

存在kAudioFormatFlagIsSignedInteger标识,使用有符号int,否则使用无符号int,缺省为无符号int。

采样点大小端标识(有此标记代表大端,无此标记为native endian)

CF_ENUM(AudioFormatFlags)
{
    kAudioFormatFlagsNativeEndian,
    kLinearPCMFormatFlagIsBigEndian             = kAudioFormatFlagIsBigEndian,
};

声道布局,平面型或交错型

CF_ENUM(AudioFormatFlags)
{
    kAudioFormatFlagIsNonInterleaved            = (1U << 5),     // 0x20
};

双声道的情况

  • 交错类型的存储为,只有一个通道,通道内左右声道交错存储 LRLRLRLRLRLR
  • 非交错情况为, 左右声道分开存储,每个平面,存单独的声道
    • 平面1 LLLLLLLL
    • 平面2 RRRRRRR

是否占满整个channel

CF_ENUM(AudioFormatFlags)
{
    kAudioFormatFlagIsPacked  
};

Set if the sample bits occupy the entire available bits for the channel,
clear if they are high or low aligned within the channel. Note that even if
this flag is clear, it is implied that this flag is set if the
AudioStreamBasicDescription is filled out such that the fields have the
following relationship:
((mBitsPerSample / 8) * mChannelsPerFrame) == mBytesPerFrame

其他字段

mBitsPerChannel

单个声道,单个采样占据的bit数(16位, 32位,64位)

mChannelsPerFrame

一个帧里面有多少个声道(单声道1, 双声道2)

mBytesPerFrame

一帧有多少个字节,一帧是各个声道的采样点的集合, mBytesPerFrame = mChannelsPerFrame * mBitsPerChannel

mFramesPerPacket

一个包里面有多少个帧(pcm 一个包一个帧,aac 一个包1024个帧)

mBytesPerPacket

一个包里面有多少字节 mBytesPerPacket = mBytesPerFrame * mFramesPerPacket

mReserved ( 一般传 0 )

Pads the structure out to force an even 8 byte alignment.

示例

pcm s16le

+ (AudioStreamBasicDescription)signed16FormatWithNumberOfChannels:(UInt32)channels
                                                   sampleRate:(float)sampleRate
                                                isInterleaved:(BOOL)isInterleaved
{
   AudioStreamBasicDescription asbd;
   UInt32 bytesPerSample = sizeof(sint16);
   asbd.mChannelsPerFrame = channels;
   asbd.mBitsPerChannel = 8 * bytesPerSample;
   asbd.mBytesPerFrame = channels * asbd.mBitsPerChannel;
   asbd.mFramesPerPacket = 1;
   asbd.mBytesPerPacket = asbd.mFramesPerPacket * asbd.mBytesPerFrame;
   if (isInterleaved) {
       asbd.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
   } else {
       asbd.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsNonInterleaved;
   }
   asbd.mFormatID = kAudioFormatLinearPCM;
   asbd.mSampleRate = sampleRate;
   asbd.mReserved = 0;
   return asbd;
}

pcm f32le

+ (AudioStreamBasicDescription)float32FormatWithNumberOfChannels:(UInt32)channels
                                                    sampleRate:(float)sampleRate
                                                 isInterleaved:(BOOL)isInterleaved
{
    AudioStreamBasicDescription asbd;
    UInt32 bytesPerSample = sizeof(float);
    asbd.mChannelsPerFrame = channels;
    asbd.mBitsPerChannel = 8 * bytesPerSample;
    asbd.mBytesPerFrame = channels * asbd.mBitsPerChannel / 8;
    asbd.mFramesPerPacket = 1;
    asbd.mBytesPerPacket = asbd.mFramesPerPacket * asbd.mBytesPerFrame;
    if (isInterleaved) {
        asbd.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked;
    } else {
        asbd.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsNonInterleaved;
    }
    asbd.mFormatID = kAudioFormatLinearPCM;
    asbd.mSampleRate = sampleRate;
    asbd.mReserved = 0;
    return asbd;
}

通过AVAudioFormat来设置

+ (AudioStreamBasicDescription)intFormatWithNumberOfChannels:(UInt32)channels
                                                  sampleRate:(float)sampleRate
                                               isInterleaved:(BOOL)isInterleaved
{
    AVAudioFormat *format = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatInt16 sampleRate:sampleRate channels:channels interleaved:isInterleaved];
    AudioStreamBasicDescription desc = *(format.streamDescription);
    format = nil;
    return desc;
}

注意

kAudioFormatFlagIsNonInterleaved 会影响AudioUnit 采集的 AudioBufferList 的布局。

Typically, when an ASBD is being used, the fields describe the complete layout
of the sample data in the buffers that are represented by this description -
where typically those buffers are represented by an AudioBuffer that is
contained in an AudioBufferList.

However, when an ASBD has the kAudioFormatFlagIsNonInterleaved flag, the
AudioBufferList has a different structure and semantic. In this case, the ASBD
fields will describe the format of ONE of the AudioBuffers that are contained in
the list, AND each AudioBuffer in the list is determined to have a single (mono)
channel of audio data. Then, the ASBD's mChannelsPerFrame will indicate the
total number of AudioBuffers that are contained within the AudioBufferList -
where each buffer contains one channel. This is used primarily with the
AudioUnit (and AudioConverter) representation of this list - and won't be found
in the AudioHardware usage of this structure.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,406评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,732评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,711评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,380评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,432评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,301评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,145评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,008评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,443评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,649评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,795评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,501评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,119评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,731评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,865评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,899评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,724评论 2 354

推荐阅读更多精彩内容