如何在 AudioFormat 描述中获取采样率和每通道位数

How can I get the Sample Rate & Bits Per Channel on a CMAudioFormatDescription

我能够从 AVCaptureDeviceFormat 获取音频描述。

let formats = device.formats
for format in formats {
print(format.formatDescription)
}

但是想直接得到mSampleRate和mBitsPerChannel属性.

CMAudioFormatDescription 0x60000010b880 [0x7fffadb29d80] {

    mediaType:'soun' 
    mediaSubType:'lpcm' 
    mediaSpecific: {
        ASBD: {
            mSampleRate: 44100.000000 
            mFormatID: 'lpcm' 
            mFormatFlags: 0x9 
            mBytesPerPacket: 8 
            mFramesPerPacket: 1 
            mBytesPerFrame: 8 
            mChannelsPerFrame: 2 
            mBitsPerChannel: 32     } 
        cookie: {(null)} 
        ACL: {Stereo (L R)}
        FormatList Array: {(null)} 
    } 
    extensions: {(null)}
}

我该怎么做? 我一直在研究 AudioToolBox 框架中的 AudioFormatGetProperty(),但我迷路了。 非常感谢所有帮助。

您可以从格式说明中得到 AudioStreamBasicDescription,并从中得到您需要的数据:

let asbd = CMAudioFormatDescriptionGetStreamBasicDescription(format.formatDescription)

if let asbd = asbd?.pointee {
    print("Sample rate: \(asbd.mSampleRate), bits per channel: \(asbd.mBitsPerChannel)")
}