I am using swift's AVFoundation framework to play some audio. I create an AVAudioSourceNode that will pass some samples to be played.
let srcNode = AVAudioSourceNode { _, timeStamp, frameCount, AudioBufferList -> OSStatus in
let ablPointer = UnsafeMutableAudioBufferListPointer(AudioBufferList)
}
I then attach this to the audio Engine. and connect it to the mixer.
engine.attach(srcNode)
engine.connect(srcNode, to: engine.mainMixerNode, format: srcNode.inputFormat(forBus:0))
If I start the engine, the audio plays. However when I delve in deeper, and probe the format of this SourceNode by printing format, the sampleRate is 44,100. The sampleRate of my audio hardware is 48,000.
What I cannot figure out, is that if I have a pre-generated array of samples, to be played at 48kHz. The sourceNode will read this at a speed of 44.1kHz, and then the engine will convert it to 48kHz to be played on my hardware, meaning the actual audio played is longer than the intended original.
How can I actually specify the format of this AVAudioSourceNode so that it works at the same rate? (Again, whilst I can change the format in the engine.connect call, this only changes the connection format, not the rate at which the sourceNode operates.