Code sample for capturing audio from a Mac in Cocoa and saving to file?

MacOS

Question or issue on macOS:

I’m due to work on a small application that captures audio from the Mac’s Audio Queue and needs to save it to disk in some reasonable audio format.

Does anyone have a some decent sample code (Cocoa / Objective-C) that they can share?

I specifically need to capture the audio that is being passed to the Built-in Output device in order to record it. Any insights? The answers so far have been helpful, but have not helped me understand how the data going to the output can be captured, agnostic of the input source.

How to solve this problem?

Solution no. 1:

Working with audio in Mac OS X involves interfacing with Core Audio. For a quick overview, take a look at the Core Audio Overview.

You will need to interface with the AUHAL to perform input and output; a technical note exists detailing the steps required to do so. This code seems to usually be written in C++, as that is the procedure taken in the SimplePlayThru demo.

This doesn’t cover the actual steps required to capture that audio input. However, these links should provide you with enough sample code to begin interfacing with your input device. I’ll post more links in this answer if I happen across them.


Take a look at /Developer/Example/CoreAudio/Services/AudioFileTools. Specifically, look at afrecord.cpp. Admittedly, this is not Cocoa per se; Cocoa itself doesn’t seem to have any specific capabilities for recording. If you’ll want to interface with the C++ file there, you’ll likely need to write some Objective C++ like in SimplePlayThru.

Solution no. 2:

There is a good example code at Ulli Kusterers Github Repository
Cocoadev also has an article about that topic. The source code at the bottom of the page uses QuickTimes Sequence Grabber API. I would go with Core Audio.

Hope this helps!