0

I'm trying to access mSampleRate and mChannelsPerFrame and assign the values to global variables.

Method:

func setAudioFormat(format: CMFormatDescriptionRef) {
    let asbd: UnsafePointer<AudioStreamBasicDescription> = CMAudioFormatDescriptionGetStreamBasicDescription(format)

    sampleRate = asbd.memory.mSampleRate // breakpoint
    channels = asbd.memory.mChannelsPerFrame

}

Method Call:

func captureOutput(captureOutput: AVCaptureOutput!, var didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
    ...

    let format: CMFormatDescriptionRef = CMSampleBufferGetFormatDescription(sampleBuffer)!
    self.setAudioFormat(format)

    ...
}

am I doing something wrong? is there a better way to get AudioStreamBasicDescription from capture output samplebuffer

Edit:

format is holding these values:

<CMAudioFormatDescription 0x14516150 [0x346c08a0]> {
mediaType:'soun' 
mediaSubType:'lpcm' 
mediaSpecific: {
    ASBD: {
        mSampleRate: 44100.000000 
        mFormatID: 'lpcm' 
        mFormatFlags: 0xc 
        mBytesPerPacket: 2 
        mFramesPerPacket: 1 
        mBytesPerFrame: 2 
        mChannelsPerFrame: 1 
        mBitsPerChannel: 16     } 
    cookie: {(null)} 
    ACL: {(null)} 
} 
extensions: {(null)}
}
justin shores
  • 687
  • 7
  • 24

1 Answers1

1

Once you have a CMFormatDescriptionRef instance, you can use this code (in Objective-C, sorry) to retrieve the ASBD data:

const AudioFormatListItem *audioFormatListItem = CMAudioFormatDescriptionGetFormatList(formatDescription, nil);
AudioStreamBasicDescription asbd = audioFormatListItem->mASBD;
float sampleRate = asbd.mSampleRate;
Ely
  • 8,259
  • 1
  • 54
  • 67