TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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

3      

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

4      

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

3      

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.