Mo DeJong Consulting

Greetings iPhone / iOS developers.

This page exists to help developers by providing working examples of interesting things one might want to do with the iPhone / iOS SDK (updated for Xcode 3.2.5 and iOS 4.2). This information is intended for developers only. The source code is provided "as is" and is placed in the public domain.

  1. UITableView with multiple levels
  2. PNG Animation
  3. Reading/Writing CAF files with the ExtendedAudioFile API
  4. PCM Mixer built with CoreAudio
  5. AutoPropertyRelease class
  6. Implementing audio fade out with CoreAudio
  7. 7zip decompresson SDK

Example 1 : UITableView with multiple levels

Do you find the UITableView class a bit hard to use? I found myself wishing there was an easier way to quickly setup a multiple level table, so I created this example. It provides code that implements a multiple level table and provides a simple way to define a class that will be shown when a specific table cell is selected. The table supports sections, but it is limited to text only labels for table cells. Just download and open up in Xcode, then take a look at the TextTableData class to see how to customize a table. (30Kb)

Example 2 : PNG Animation

Ever want to pull your hair out while dealing with MPMoviePlayerController? The most lame part of the iPhone SDK has got to be MPMoviePlayerController, it is so lacking in functionality that you basically need to roll your own audio/video implementation to do anything interesting. One thing I needed to do was show a simple animation in a loop, but MPMoviePlayerController could not do what I needed it to. There is also the UIImageView.animationImages API, but it quickly sucks up all the system memory when using more than a couple of decent size images. I wanted to show a full screen animation that lasts 2 seconds, at 15 FPS that is a total of 30 PNG images of size 480x320. This example implements an animation oriented view controller that simply waits to read the PNG image data for a frame until it is needed. Instead of alllocating many megabytes, this class run in about a half a meg of memory with about a 5-10% CPU utilization on a 2nd gen iPhone. This example has also been updated to include the ability to optionally play an audio file via AVAudioPlayer as the animation is displayed. (165Kb)

Example 3 : Reading/Writing CAF files with the ExtendedAudioFile API

CoreAudio is really quite a challenge to work with, the main reason is a lack of working examples to demonstrate how to use CoreAudio APIs. I recently ran into a problem when playing two audio streams with 2 AVAudioPlayer instances. The music in the two tracks would not stay in sync, so I decided to dive into CoreAudio and fix the problem by mixing the tracks before sending them to the audio device. This meant that I needed to be able to read the pcm audio data from two audio files, but the audio files I am using are IMA4 compressed caff files and it was not obvious how to read them on the iPhone. After many many hours of searching through mailing lists and example code, I learned that the right way to do this is with the ExtendedAudioFile API in the AudioToolbox framework. This API supports reading or writing from caff audio files with IMA4 compression (one can also decode/encode AAC audio files on 3g and newer iPhones and the iPad). This example shows how to make use of the ExtendedAudioFile to read a IMA4 compressed file and how to write a IMA4 compressed file to disk. Just open up the example in Xcode and run in the Simulator to see how it works. I hope this saves you many many long hours of searching through CoreAudio docs.

ExtAudioFileDemo.tar.bz2 (30Kb)

Example 4 : PCM Mixer built with CoreAudio

This example is a really simple pcm mixer that reads data using the regular AudioFile API. This example provides the solution to the audio sync problem mentioned in the previous example. Once two audio streams have been mixed, the audio can be played with a single AVAudioPlayer, so you will not need to mess around with audio graphs. This example includes an iPhone application example and a command line util that works on Mac OS X. You can test the mixing functionality on the desktop before putting it on the phone (really handy if you want to generate a checksum for the mixed audio). This download is rather large because it includes a couple of unmixed pcm audio files. Note that this mixer does not implement any sort of audio limiter, if the mixed audio would clip then the mixer will return an error code.

MixTwoAudioFilesDemo.tar.bz2 (2300Kb)

Example 5 : AutoPropertyRelease class

Why on earth does one need to release refs to retained properties in Objective-C? The compiler should just do that automatically, it already has all the info about the properties. I find that most memory leaks happen when one changes the properties but then forgets to update the dealloc method to ensure that all the properties are cleaned up. The entire process is just so error prone that I ended up writing this class to do it automatically. Your code would look like this:

#import "AutoPropertyRelease.h"

@implementation MyClass
@synthesize ...;

- (void) dealloc {
    [AutoPropertyRelease releaseProperties:self thisClass:[MyClass class]];
    [super dealloc];
} (24Kb)

Example 6 : Implementing audio fade out with CoreAudio

I recently ran into a situation where I needed to be able to truncate an audio file to a shorter length. It is easy to truncate by just copying audio data up to a certain number of samples, but this naive approach sounds quite bad. The audio volume needs to fade out at the end of the clip, and the fade must use a log scale. The attached file contains a Python implementation of a fade out algorithm along with a Mac OS X command line program and an iPhone example app. The fade out algorithm will fade to -60 dB over an envelope, both the truncate length and the envelope length can be configured. The envelope fade table is pre-calculated so that the algorithm will run efficiently on even a 1st gen iPhone. This software demonstrates use of CoreAudio and ExtAudioFile API in AudioToolbox Framework.

ClipFade.tar.bz2 (110Kb)

Example 7 : 7zip decompresson SDK

7zip is a useful replacement for zlib and bzip2. In some cases, 7zip can achive drastically better compression ratios than both zlib and bzip2. One can always go to to find the source code. This example provides the lzma SDK (release 9.22 beta) configured as an iPhone project. Only the decompression modules are included, and CRC checking is disabled to improve performance. A simple Objective-C interface is included to support decompressing .7z archives included as iOS project resources. The code has been modified to support decoding of very large files without consuming all the iOS memory and crashing the application (up to about 650 megs). (120Kb)

The lzmaSDK code is also available as a github project here.

Happy Hacking!
Mo DeJong