As a throwaway project for the iPhone to get me up to speed with Objective C and the iPhone libraries, I’ve been trying to create an app that will play different kinds of random noise.
I’ve been constructing the noise as an array of floats normalized from [-1,1].
Where I’m stuck is in playing that generated data. It seems like this should be fairly simple, but I’ve looked into using AudioUnit and AVAudioPlayer, and neither of these seem optimal.
AudioUnit requires apparently a few hundred lines of code to do even this simple task, and AVAudioPlayer seems to require me to convert the audio into something CoreAudio can understand (as best I can tell, that means LPCM put into a WAV file).
Am I overlooking something, or are these really the best ways to play some sound data stored in array form?
Here’s some code to use AudioQueue, which I’ve modified from the SpeakHere example. I kind of pasted the good parts, so there may be something dangling here or there, but this should be a good start if you want to use this approach:
The code above refers to this output callback. Each time this callback executes, fill the buffer passed in with your generated audio. Here, I’m filling it with random noise.