AVAudioBuffer and μLaw

I'd like to use AVAudioConverter to convert audio captured from the microphone to μLaw.


Unfortunately, when I try to create an output buffer to convert into, I get an exception. I've tried both AVAudioPCMBuffer and AVAudioCompressedBuffer, and neither works for me.


Is this supposed to work?


Thanks!


let format = AVAudioFormat(settings: [AVFormatIDKey: NSNumber(value: kAudioFormatULaw), AVSampleRateKey: 8000, AVNumberOfChannelsKey: 1])

let buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: 1000)
// required condition is false: isPCMFormat


let buffer = AVAudioCompressedBuffer(format: format, packetCapacity: 1000)
// required condition is false: !(fmt.IsPCM() || fmt.mFormatID == kAudioFormatALaw || fmt.mFormatID == kAudioFormatULaw)
  • Did you find any solution for this i am also struck in the same problem

Add a Comment

Replies

I got this working by creating the AVAudioFormat instance from an AudioStreamBasicDescription struct:


var description = AudioStreamBasicDescription(mSampleRate: 8000, mFormatID: kAudioFormatULaw, mFormatFlags: 0, mBytesPerPacket: 1, mFramesPerPacket: 1, mBytesPerFrame: 1, mChannelsPerFrame: 1, mBitsPerChannel: 8, mReserved: 0)
let format = AVAudioFormat(streamDescription: &description)
let buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: 800)