iPhone Core Audio Getting Started- Correction

At some point since I wrote the iPhone Core Audio Getting Started post, a small bug in my AudioStreamBasicDescription on the multi-Channel Mixer input went from not mattering to making the output a garbled mess. Those stream descriptions are tricky devils!

To fix it, you need  to change the line.

desc.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagsNativeEndian | kLinearPCMFormatFlagIsNonInterleaved;

to

desc.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;

or

desc.mFormatFlags = kAudioFormatFlagsCanonical;

Ahh, pure, clean sine wave.

iOS Rotary Slider Controls – AKA Knobs

We’re now 4 versions into iOS, and Apple still hasn’t brought UISlider up to speed with NSSlider. OS X gets horizontal, vertical, and rotary sliders while iOS is stuck with only horizontal.  There’s probably some UI guideline which states that rotary sliders are no good for touch interfaces but honestly, it is really hard to make an attractive layout with a lot of controls using only horizontal sliders.  Music apps tend to need a lot of controls, and making EQ adjustments using a slider reminds me of the bad old days of generic AudioUnit interfaces.

After a cursory Google search, I couldn’t find any code out there for rotary sliders so I wrote my own UIControl subclass. I tried to stick as closely as possible to the UISlider interface so that it won’t be too hard to switch when Apple gets around to making a standard control. There is a precision property that changes how quickly the knob rotates, this lets you make very accurate adjustments.

XCode Project

iPhone Core Audio Part 3 – Audio Callback

Previous: Setting up the AUGraph.

In the previous two posts, we set up the project and hooked up the audio plumbing. Now we finally get to the actual work. Amazingly, all there’s just one function left to write. The audio render callback is the function you provided earlier to be the input to the mixer AudioUnit.  Whenever the Mixer needs new audio input, it calls the render call back and it is up to you to fill a buffer up with audio samples. It is a C function that  has this specific set of parameters.

  • inRefCon – A pointer to an object that is used to pass in parameters.
  • AudioUnitRenderActionFlags – Indicates special states, we won’t need it here.
  • AudioTimeStamp – Used if you need to synchronize multiple sources.
  • inBusNumber – The specific bus of the Audio Unit that is called the function.
  • inNumberFrames – The number of frames of sample data that will be passed in.
  • ioData – An AudioBufferList, which is a struct containing an array of buffers representing sample data and a count of those buffers.

Here’s what we’re going to be doing.

  • Getting a pointer “THIS” so we can access AudioController variables.
  • Getting a pointer to the buffer we want to write to. (here there is only one buffer, it will be at index [0]).
  • Performing some preliminary setup for the sine wave.
  • Looping through an inNumberFrames length loop, calculating a sine wave and writing sample values to the buffer.
  • Saving any AudioController variables that need to be remembered across calls to the render function.

Continue reading