NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

Pointer confusion in core audio

Forums > macOS

Hello everyone, I posted a question yesterday, but was not very clear about what problems I encounter. The CoreAudio and AudioToolbox frameworks still involve a lot of C-coding. Translating example code from the book 'Learning Core Audio' into Swift 5.1 code is in a lot of cases quite straightforward, but some parts are tough. Especially the parts involving memory allocation and the different kind of pointers that one should use are prone to all kind of system warnings about having the wrong type. Most of it is solved, but in one situation I'm really stuck. I try to write an audiorecorder using the standard inputport using AudioQueue and buffers. I set up a class to hold the inUserData:

let kNumberRecordBuffers = 3

class Recorder { var recordFile: AudioFileID? = nil var recordPacket: Int64 = 0 var running: Bool = false }

While recording the AudioQueue gets callbacks and in setting up this function, trying to create in instance of the Recorder class, I get into trouble All arguments used in AudioQueueInputCallback are defined in the framework and cannot be changed.

let AQInputCallback: AudioQueueInputCallback = {(inUserData, inQueue, inBuffer, inStartTime, inNumPackets, inPacketDesc) -> () in let recorder = UnsafeMutablePointer<Recorder>(inUserData).memory //Here it goes wrong !!!

(cannot invoke initializer for type 'UnsafeMuteblePointer<Recorder> with argument list of type '(UnsafeMutableRawPointer?)')

..................... }

AudioQueueInputCallback passes inUserData as a UnsafeMutableRawPointer and variable 'recorder' tries to use this pointer to allocate an instance of the recorderclass, but uses a UnsafeMutablePointer to assign this pointer with the class instance and the memory space it occupies. I have tried so many different possibilities to make it work, without any positive result. I assume that I'm making a very simple mistake in my approach but can't figure it out...............


Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Would you describe yourself as knowledgeable, but struggling when you have to come up with your own code? Fernando Olivares has a new book containing iOS rules you can immediately apply to your coding habits to see dramatic improvements, while also teaching applied programming fundamentals seen in refactored code from published apps.

Try the book!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.