JG Electronics

Audio Tool

I did some audio programming in Objective C++ with CodeWarior in OS9 before OSX was launched.

In my FRG CAT project (see HAM Radio) I need to record and playback audio files so I decided to write the audio record and playback classes in Cocoa. I started to read Apple’s documentation and started to study the Core Audio code examples from Apple and I discovered that the examples are not in Cocoa.

I looked for books about Core Audio and I found a book ‘Core Audio’ from the authors Kevin Avila and Chris Adamson.

On the web I also found not much example code so I decided to start this blog and publish all the examples I studied and converted to Cocoa from the Apple Objective C++ and C/C++ Carbon examples.


Changing the extension of the class to.mm.

If you have C/C++ code in your Cocoa class you have to change the extension of the class to .mm.

If you avoid routines / functions in C / C++ except the callback functions in C and you avoid C++ expressions like i.e. new (use for C++ new  C malloc instead) then you don’t have to change the extension of the Cocoa Class to .mm. Cocoa accepts C expressions but no C++ expressions unless the file extension is .mm.

If you get an error ‘not compatible with C99’ means that you use something which is not C compatible.

If it is C++ compatible then an expression like new will work if you change the extension of the class to .mm

Remember that an Objective C++ class like a Cocoa class is in fact a structure and as long as we stay in the structure of our Cocoa class we don’t have to change the extension of the class to.mm. It is possible to use a structure outside the Cocoa class to pass data from the callback functions but then we have to change the class extension to .mm.

I also will make no use of Apple’s Public Utilities for Core Audio because the lack of documentation.


The first Apple Example I studied was ‘Default Output Unit’ a simple example to play a sine wave through the default audio output.

I converted it from C to Cocoa and added controls for frequency and amplitude control.

I was puzzled by the way the data in the callback method is passed in and out and found 3 ways how it can be done. Method 3 is the Cocoa solution and with this solution it is not necessary to change the class name extension to .mm.


MVC means Model-View-Controller. In object-oriented programming it’s common to use the MVC design pattern. I try to follow this design pattern. AudioTool has a Model class that describes the data, in this case the audio data. Audio Tool has a Controller class that controls the flow of the application, in this case the values for frequency, amplitude and start / stop.

The View is part of the Graphic User Interface (GUI) with the buttons and sliders.

The Application Delegate class is empty and should be used for application dependent events i.e. if the user quits the application there can be a method in the Application Delegate class that will be called and informs the user that the data is not saved.

A good Model class includes notting about the interface and can be used in several applications.

All Cocoa Source Code snippets are in fact try-outs, the Model classes could be used in future projects or projects I work on at the moment.


The build and runtime requirements for this project are Mac OSX10.6 and XCode4.0.1.


The application icon is from Vincent Garnier.

http://benjigarner.deviantart.com/art/Fruity-Apples-80805804


If you have questions about programming topics please ask these questions on a developer mailing list from Apple. There you find people with far more knowledge then I.

For questions concerning Core Audio goto: coreaudio-api@lists.apple.com

For books about the foundation of Cocoa Programming visit: www.bignerdranch.com

Other useful books:

C++ From the Beginning, author Jan Skansholm.

Cocoa Programming Developer’s Handbook, author David Chisnall.

Core Audio, authors Kevin Avila and Chris Adamson.


If you have remarks or if you find bugs please send me an email: jgelectronics@icloud.com


Click on the picture to download the application and the source code.


Happy Coding!


John

© JG Electronics