Search code examples
swiftcore-audioaudiounitunsafe-pointers

Using AudioBufferList with Swift once again


Refering to Using AudioBufferList with Swift

I found the following solution here on Stack Overflow for playing some sound with audio unit. My problem here is that I'm not able to put actual data in my buffer like sin wave data. I tried it with memcpy instead of memset but this UnsafePointer stuff is pretty exhausting.

So is somebody going to clarify how I would put actual data to my buffer or should I grab an Objective-C book and do it all over again?

func renderCallback(ioData: UnsafeMutablePointer<AudioBufferList>) -> OSStatus 
{
    let abl = UnsafeMutableAudioBufferListPointer(ioData)
    for buffer in abl {
        memset(buffer.mData, 0, Int(buffer.mDataByteSize))
    }
    return noErr
}

Solution

  • You can copy data into a byte or Int16 array, element by element, in a "for" loop. Swift might optimize this loop reasonably well compared to memset, such that processor data cache effects end up dominating the performance.