UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on 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...............

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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.