Thanks for helping!
I have to admit that I am a total noob when it comes to audio stuff. Would be super great if you could help me out a bit here 🙂
So I went ahead an made an instance of an AudioConverter
let bus = 0
let inputFormat = audioEngine.inputNode!.inputFormatForBus(bus)
let outputFormat = AVAudioFormat(commonFormat: AVAudioCommonFormat.PCMFormatInt16, sampleRate: 8000.0, channels: 1, interleaved: false)
var audioConverterRef = AudioConverterRef()
let status = AudioConverterNew(inputFormat.streamDescription, outputFormat.streamDescription, &audioConverterRef)
That's working so far (status is noErr). Now I have a valid AudioConverter where I'd think that it would convert bytes that I give him in the format of inputFormat into outputFormat.
Now I install a tap on my inputNode and in the callback I do the following:
// I have those two params available inside my callback
(inputBuffer: AVAudioPCMBuffer, time:AVAudioTime)
// Now I'll get the underlying data ...
let inputAudioBufferList = inputBuffer.audioBufferList
let inputAudioBuffer = inputAudioBufferList[0]
let inputData = inputAudioBuffer.mBuffers.mData
let inputDataSize = inputAudioBuffer.mBuffers.mDataByteSize
// Here's my first question: Is this the correct way to do that?
// Can I expect that my input Data and size is correct?
// Now I prepare the buffer for the converted output
var outputBufferSize: UInt32 = 1024
var convertedOutputBuffer = UnsafeMutablePointer<Void>.alloc(Int(outputBufferSize))
let convertedOutputBufferSize = withUnsafeMutablePointer(&outputBufferSize, { $0 })
// And now I'll try to convert my input data
let status = AudioConverterConvertBuffer(audioConverterRef, inputDataSize, inputData, convertedOutputBufferSize, &convertedOutputBuffer)
// Problem here if that the status is -50 which means that one of my params isn't correct... But which one?
So it seems as if I am not smart enough to figure out the correct input params for AudioConverterConvertBuffer.
Also... when looking at the API... is this the correct way? What's the thing with kAudioUnitSubType_AUConverter? Where would that come into play?
Any help is very very much appreciated!