Creating simple waveforms with CoreAudio Creating simple waveforms with CoreAudio objective-c objective-c

Creating simple waveforms with CoreAudio


There are a number of errors in the previous answer. I, the legendary :-) James McCartney, not James Harkins wrote the sinewavedemo, I also wrote SuperCollider which is what the audiosynth.com website is about. I also now work at Apple on CoreAudio. The sinewavedemo DOES use CoreAudio, since it uses AudioHardware.h from CoreAudio.framework as its way to play the sound.

You should not use the sinewavedemo. It is very old code and it makes dangerous assumptions about the buffer layout of the audio hardware. The easiest way nowadays to play a sound that you are generating is to use the AudioQueue, or to use an output audio unit with a render callback set.


The best and easiest way to do that without files is to prepare a single cycle buffer, containing one cycle of the wave (this is called technically a wavetable)

In the playback function called by CoreAudio thread, fill the output buffer with samples read from the wave buffer.

Note however that you will face two problems very quickly :- for the sine wave, if the playback frequency is not an integer multiple of the desired sine frequency, you will probably need to implement an interpolator if you want to have a good quality. Using only integer pointers will generate a significant level of harmonic noise.

  • for the square wave, avoid to just program an array with +1 / -1 values. Such a signal is not bandlimited and will alias a lot. Do not forget that the spectrum of a square wave is virtually infinite!

To get good algorithms for signal generation, take a look to musicdsp.org, that's probably one of the best resource for that


Are you new to audio programming in general? As a starting point i would check out

http://www.audiosynth.com/sinewavedemo.html

This is a minimum osx sinewave implementation by the legendary James Harkins. Note, it doesn't use CoreAudio at all.

If you specifically want to use CoreAudio for your sinewave you need to create an output unit (RemoteIO on the iphone, AUHAL on osx) and supply an input callback, where you can pretty much use the code from the above example. Check out

http://developer.apple.com/mac/library/technotes/tn2002/tn2091.html

The benefits of CoreAudio are chiefly, chain other effects with your sinewave, write plugins for hosts like Logic & provide the interfaces for them, write a host (like Logic) for plugins that can be chained together.

If you don't wont to write a plugin, or host plugins then CoreAudio might not actually be for you. But one of the best things about using CoreAudio is that once you get your sinewave callback working it is easy to add effects, or mix multiple sines together

To do this you need to put your output unit in a graph, to which you can effects, mixers, etc.

Here is some help on setting up graphs http://timbolstad.com/2010/03/16/core-audio-getting-started-pt2/

It isn't as difficult as it looks. Apple provides C++ helper classes for many things (/Developer/Examples/CoreAudio/PublicUtility) and even if you don't want to use C++ (you don't have to!) they can be a useful guide to the CoreAudio API.