How to use AudioConverterConvertBuffer to convert between PCM and IMA4?

Not sure if this is the correct forum, because I couldn't find anything dedicated to AudioToolbox.


I am recording in 16 bit Linear PCM and want to convert it to IMA4 in realtime. I was hoping to use AudioConverterConvertBuffer as both of the formats I am using specify the same sample rate. However, when I invoke AudioConverterConvertBuffer with my PCM as an input buffer, I receive -50 as an OSStatus (which doesn't appear to map to any of the statuses returned by the AudioConverter APIs).


I've written this in Swift, so I may have messed up the translation of my Data objects into unsafe bytes, but here is the code I'm running:

var inputFormat = AudioStreamBasicDescription()
inputFormat.mSampleRate = 16000.0
inputFormat.mFormatID = kAudioFormatLinearPCM
inputFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked
inputFormat.mBytesPerPacket = 2
inputFormat.mFramesPerPacket = 1
inputFormat.mBytesPerFrame = 2
inputFormat.mChannelsPerFrame = 1
inputFormat.mBitsPerChannel = 16
inputFormat.mReserved = 0

var outputFormat = AudioStreamBasicDescription()
outputFormat.mSampleRate = 16000.0
outputFormat.mFormatID = kAudioFormatAppleIMA4
outputFormat.mFormatFlags = 0
outputFormat.mBytesPerPacket = 34
outputFormat.mFramesPerPacket = 64
outputFormat.mBytesPerFrame = 0
outputFormat.mChannelsPerFrame = 1
outputFormat.mBitsPerChannel = 0
outputFormat.mReserved = 0

var audioConverter: AudioConverterRef?
let inData = Data() // This contains 16000 bytes of 16-bit Linear PCM audio data
var outData = Data(count: 32 * 1024) // Should be more than enough space
var outByteCount = UInt32(outData.count)

let result = AudioConverterNew(&inputFormat, &outputFormat, &audioConverter)  // result is noErr here

let obc = inData.withUnsafeBytes { (inU8ptr: UnsafePointer) -> UInt32 in
     outData.withUnsafeMutableBytes { (outU8ptr: UnsafeMutablePointer) -> UInt32 in
          let result = AudioConverterConvertBuffer(audioConverter!,
                                                   UInt32(inData.count), inU8ptr,
                                                   &outByteCount, outU8ptr)
          if result != noErr {
               return UInt32(0)
          }

          return outByteCount
     }
}

print("outputBytes after conversion = \(obc)"


Does anyone have any experience with this that could help out?


Thanks,


Sean