FREE TRIAL: Accelerate your app development career with Hacking with Swift+! >>

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?

https://github.com/derekli66/Learning-Core-Audio-Swift-SampleCode

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

https://github.com/derekli66/Learning-Core-Audio-Swift-SampleCode/blob/master/CH04_Recorder-Swift/CH04_Recorder-Swift/main.swift

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

1      

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 Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

Get started

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.