Using AudioBufferList in Swift

I'm creating an AudioBufferList in Swift. In C, AudioBufferList contains a variable-length array of AudioBuffer objects, while in Swift it instead has a field of type '(AudioBuffer)'.


I'm setting up the AudioBufferList in the following way:


var buffer = [UInt8](count: 1024, repeatedValue: 0)
var audioBufferList = AudioBufferList(
    mNumberBuffers: 1,
    mBuffers: AudioBuffer(
        mNumberChannels: 2,
        mDataByteSize: UInt32(buffer.count),
        mData: &buffer
    )
)


This is working in practice, but I'd like to confirm this is actually correct and is how AudioBufferList is intended to be used in Swift. My searches on the topic have turned up a couple examples similar to the above, but I haven't found any official documentation or authoritative statement that this is how it's supposed to be done. (My concern is that maybe this can't really be expected to work reliably at this time, and that it's only due to circumstance that it's working for me currently.)


Just for peace of mind, can anyone confirm that the above is correct and, provided everything else is done correctly, can be counted on to work?

Accepted Answer

Your code should work as far as you give 1 to mNumberBuffers. (Of course other parameters should be consistent.)


You can find some discussions in the old dev forums archive, searching with `AudioBufferList Swift`.

Thanks very much for your reply. Yes, I'll only ever by using one audio buffer (not multiple). I just wanted to make sure using AudioBufferList as I'm doing (that is, with only a single buffer) wasn't one of the places where the Swift API isn't solid yet.


I did search the archives for 'AudioBufferList Swift' and read everything I could find on the topic. A lot of the discussion was on using multiple buffers with AudioBufferList rather than just one, which is a somewhat different problem (and probably a more difficult one). As I mentioned I did find a couple examples similar to mine (these I found elsewhere online), but the examples were somewhat speculative and/or offered with caveats, so I was just looking for something more authoritative.


Thanks again.

Using AudioBufferList in Swift
 
 
Q