NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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...............


Maybe teaching my grandma to suck eggs, but have you checked out this repo?

It takes the code from the book (Obj-C) and gives the equivalent in Swift 5. There is an example use of AudioQueueInputCallback (in Swift) inside the following file (line 190 onwards).

Hope this helps, but I guess it can't hurt. :-) Dave


Thanks a lot Dave, I used examples updated to work in Swift 3 and was not aware of this link. Makes life a lot easier.

:) Leo


Hacking with Swift is sponsored by MadMachine

SPONSORED Want to explore your Swift skill outside of the Apple world? Join the MadMachine community and start to program microcontrollers in Swift.

Get it now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.