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

807 views Asked by At

I'm able to get the audio description from AVCaptureDeviceFormat.

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

But would like to get directly to the mSampleRate and mBitsPerChannel property.

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)}
}

How might I go about this? I have been looking into the AudioFormatGetProperty() in the AudioToolBox framework, but am getting lost. All help greatly appreciated.

1

There are 1 answers

0
Gordon Childs On BEST ANSWER

You can get the AudioStreamBasicDescription from the format description, and from that the data you need:

let asbd = CMAudioFormatDescriptionGetStreamBasicDescription(format.formatDescription)

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