Tag: development

Tips on how to setup, configure, initialize, and START/STOP Audio Units for iPhone

As part of Apple’s poorly documented information on Audio Units and Remote I/O, there are some subtle issues that cause RENDER CALLBACKS to not work properly.

Make sure not to set any “callbacks” until after Remote I/O unit is fully initialized (i.e. after calling AudioUnitInitialize)

An audio unit’s general opeartions are:
Open an audio unit (AudioComponentInstanceNew)
Configure it based on the context – AudioUnitSetProperty
Initialise the audio unit (AudioUnitInitialize)
– at this point the audio unit is in a state where it can render audio
Render audio (AudioUnitRender)

An important part of a render operation for an audio unit is to manipulate the various controls that the unit provides
to change the render effects; for instance to change the decay time of a reverb, the cut off frequency of a filter, etc.
These are called parameters, and AudioUnitGetParameter and AudioUnitSetParameter are used to interact with these.

If any reconfiguration of the audio unit is required, then:
uninitialise (AudioUnitUninitialise)
Configure it based on the context – AudioUnitSetProperty
Initialise the audio unit (AudioUnitInitialize)

Once the host is finished with an audio unit, it closes it:
Dispose audio unit (AudioComponentInstanceDispose)

Audio units can be used programmatically (for instance a mixers could be used to render audio for a game, a generator
to play audio files, etc), or they can be hosted in Digital Audio Workstation (DAW) applications such as Logic, Garage Band.
In the DAW case, it is common for an audio unit to provide a custom view to allow the user to interact with what can be
complex DSP opearations that the audio unit performs. The view is retrieved from an audio unit through AudioUnitGetProperty
and then the host instantiates it (see <AudioUnit/AUCocoaUIView.h>)

iPhone Audio Programming Tips

After extensive research into Apple’s poorly documented audio programming Objective-C classes and API, here are some helpful links.

Using RemoteIO audio unit

Decibel metering from an iPhone audio unit

Analyse Audio with RemoteIO

iPhone Core Audio tutorial

  1. http://timbolstad.com/2010/03/16/core-audio-getting-started-pt1/
  2. http://timbolstad.com/2010/03/16/core-audio-getting-started-pt2/
  3. http://timbolstad.com/2010/03/16/core-audio-getting-started-pt3/

It’s hard. Jens Alfke put it thusly:

“Easy” and “CoreAudio” can’t be used in the same sentence. :P CoreAudio is very powerful, very complex, and under-documented. Be prepared for a steep learning curve, APIs with millions of tiny little pieces, and puzzling things out from sample code rather than reading high-level documentation.

  • Media is hard because you’re dealing with issues of hardware I/O, real-time, threading, performance, and a pretty dense body of theory, all at the same time. Webapps are trite by comparison.

  • On the iPhone, Core Audio has three levels of opt-in for playback and recording, given your needs, listed here in increasing order of complexity/difficulty:

    1. AVAudioPlayer – File-based playback of DRM-free audio in Apple-supported codecs. Cocoa classes, called with Obj-C. iPhone 3.0 adds AVAudioRecorder (wasn’t sure if this was NDA, but it’s on the WWDC marketing page).
    2. Audio Queues – C-based API for buffered recording and playback of audio. Since you supply the samples, would work for a net radio player, and for your own formats and/or DRM/encryption schemes (decrypt in memory before handing off to the queue). Inherent latency due to the use of buffers.
    3. Audio Units – Low-level C-based API. Very low latency, as little as 29 milliseconds. Mixing, effects, near-direct access to input and output hardware.
  • Other important Core API’s not directly tied to playback and recording: Audio Session Services (for communicating your app’s audio needs to the system and defining interaction with things like background iPod player, ring/silent switch) as well as getting audio H/W metadata, Audio File Services for reading/writing files, Audio File Stream Services for dealing with audio data in a network stream, Audio Conversion Services for converting between PCM and compressed formats (and vice versa), Extended Audio File Services for combining file and conversion Services (e.g., given PCM, write out to a compressed AAC file).

  • Setting a property on an audio unit requires declaring the “scope” that the property applies to. Input scope is audio coming into the AU, output is going out of the unit, and global is for properties that affect the whole unit. So, if you set the stream format property on an AU’s input scope, you’re describing what you will supply to the AU.
  • Make the RemoteIO unit your friend. This is the AU that talks to both input and output hardware. Its use of buses is atypical and potentially confusing. Enjoy the ASCII art:

    | i o |
    -- BUS 1 -- from mic --> | n REMOTE I/O u | -- BUS 1 -- to app -->
    | p AUDIO t |
    -- BUS 0 -- from app --> | u UNIT p | -- BUS 0 -- to speaker -->
    | t u |
    | t |

    Ergo, the stream properties for this unit are

    Bus 0 Bus 1
    Input Scope: Set ASBD to indicate what you’re providing for play-out Get ASBD to inspect audio format being received from H/W
    Output Scope: Get ASBD to inspect audio format being sent to H/W Set ASBD to indicate what format you want your units to receive
  • That said, setting up the callbacks for providing samples to or getting them from a unit take global scope, as their purpose is implicit from the property names: kAudioOutputUnitProperty_SetInputCallback and kAudioUnitProperty_SetRenderCallback.

Recompile PHP, IMAP-SSL, and Apache for MAMP 1.9

Finally figured how to compile things on my Mac to support IMAP-SSL.  MAMP does not have this compiled in by default.  Since I want to do local development without having to upload to a Linux server to test every single time, getting MAMP to support IMAP-SSL was critical.
IMAP-SSL is used for things like Gmail access.

It was not easy to compile PHP5.3.2 on MAMP. First of all, you need to download httpd2.0.63 and PHP5.3.2. (I’m using MAMP 1.9 and Mac OSX 10.6.2)

STEP 1: Backup original MAMP

Backup your MAMP Applicaiton first in case things go wrong.
It is in /Application/MAMP

STEP 2: Download source files

Download software packages and save in a “source download” folder somewhere (I used ~/src/)

  • HTTPD-2.0.63
  • PHP-5.3.2  (http://php.net/downloads.php)

STEP 3: Build apache httpd

This step is needed to provide MAMP some build & include files from Apache httpd.

% tar jxvf httpd-2.0.63.tar.bz2
% cd httpd-2.0.63

Now you need to edit srclib/apr/include/apr.h like “#define APR_HAS_SENDFILE 0″ after run configure because get an error.

./configure \
--prefix=/tmp/httpd-2.0.63 \
--with-php \
--with-mysql \
--enable-rewrite \
--enable-speling \
--enable-ssl \
--enable-deflate \
--enable-mods-shared=all \
--with-included-apr \
--enable-so \
--enable-proxy \
--enable-proxy_connect \
--enable-proxy_ftp \
--enable-echo \
--enable-file_cache \
--enable-mem_cache \
--enable-bucketeer \
--enable-cache \
--enable-case_filter \
--enable-case_filter_in \
--enable-cgid \
--enable-charset_lite \
--enable-disk_cache \

The configuration settings enable the default modules that MAMP builds into their application. This will let you use the MAMP interface to turn on/off the various modules as needed (because MAMP UI controls HTTPD.CONF file).

% make
% make install
% cd /tmp/httpd-2.0.63
% cp -r build /Application/MAMP/Library/
% cp -r include /Application/MAMP/Library/
% cp -r modules/mod_* /Applications/MAMP/Library/modules/

STEP 4: Build PHP-5.3.2

Step 4a

% tar jxvf php-5.3.2
% cd php-5.3.2

The below configuration file uses a combination of MAMP-specific folders (/Applications/MAMP/) and Apple-provided files (/usr/).
For those that decide to use DarwinPorts to get some components, these will be located in (/opt/local/).  This is a way to avoid having to go to different websites to download specific modules.

My previous MAMP version was 1.8.? and had a PHP configuration for T1LIB (–with-t1lib=/Applications/MAMP/Library).  I removed T1LIB b/c not installed on MACOSX.

./configure \
--prefix=/Applications/MAMP/bin/php5.3 \
--exec-prefix=/Applications/MAMP/bin/php5.3 \
--sysconfdir=/Applications/MAMP/conf/php5.3 \
--with-config-file-path=/Applications/MAMP/conf/php5.3 \
--mandir=/usr/share/man \
--infodir=/usr/share/info \
--with-apxs2=/Applications/MAMP/Library/bin/apxs \
--enable-cli \
--with-libxml-dir=/Applications/MAMP/Library \
--with-openssl=/usr \
--with-kerberos=/usr \
--with-zlib=/usr \
--enable-bcmath \
--with-bz2=/usr \
--enable-calendar \
--with-curl=/Applications/MAMP/Library \
--enable-exif \
--enable-ftp \
--with-gd \
--with-jpeg-dir=/usr/local/libjpeg \
--with-png-dir=/usr/local/libpng \
--enable-gd-native-ttf \
--with-ldap=/usr \
--with-ldap-sasl=/usr \
--enable-mbstring=all \
--enable-mbregex \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-mysql-sock=/tmp/mysql.sock \
--with-iodbc=/usr \
--enable-shmop \
--with-snmp=/usr \
--enable-soap \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--with-xmlrpc \
--with-iconv-dir=/usr \
--with-xsl=/usr \
--with-pcre-regex \
--with-imap \
--with-imap-ssl \
--with-mcrypt \
--with-mhash \
--with-freetype-dir=/usr/X11 \
--enable-zend-multibyte \
--with-gettext \

Step 4b

NOTE: You may need to install some packages before compile PHP for some components that are not included.
You can use DarwinPorts, or download individually.

Manual method ==> http://www.teamonetickets.com/software/howto-setup-php-apache-mysql-dev-environment-on-snow-leopard.html
MacPorts method ==> http://zuzara.com/blog/2010/04/08/compiling-php5-3-2-on-mamp/

Instructions for JPEG & PNG missing files (http://www.bill.eccles.net/bills_words/2010/01/building-a-mac-os-x-server-106.html)

libjpeg, libpng, libmcrypt, IMAP c-client, libintl, gettext, libxml

Step 4c

Now, we need to work around a bug with inconv linking. Luckily, Apple provides a patch. Either copy the text of that patch and save it as a file or download it here.

curl -O http://www.teamonetickets.com/software/iconv.patch
patch -p1 < iconv.patch

Step 4d

Now that iconv will link properly, continue with our make.

% make -j3
% make install

Step 4e

After you have finished making PHP, it should have copied LIBPHP5.SO to your “apache” location.
libphp5.so is located where you set –prefix for apache.
It’s needed to copy to /Applications/MAMP/Library/modules.

SIDE NOTE: May need to build IMAP-SSL individually.

I am not sure if this step was needed, but I also compiled a separate IMAP.SO module (which may already be build during the PHP build).
Compile IMAP first    (http://www.activecollab.com/forums/topic/5844/)

cd imap-2007e/
make osx EXTRACFLAGS="-arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp"
sudo cp c-client/*.h /opt/local/include/
sudo cp c-client/*.c /opt/local/lib/
sudo cp c-client/c-client.a /opt/local/lib/libc-client.a

Reroute to MAMP directory

sudo cp c-client/*.h /opt/local/include/
sudo cp c-client/*.c /opt/local/lib/
sudo cp c-client/c-client.a /opt/local/lib/libc-client.a

An alternate method is to go into PHP source file into ext/imap/ and then you can compile just the module.
Use below to CONFIGURE custom IMAP.SO compile

./configure --with-php-config=/Applications/MAMP/bin/php5.3/bin/php-config --with-imap=/usr/local --with-kerberos --with-imap-ssl=/usr

Reference Links

Original IMAP-SSL problem on MAMP


Password Security

Just read this article and it has good pointers for anyone putting passwords on their website.


In summary, if we’re storing passwords, we’re probably storing those passwords incorrectly. If it isn’t obvious by now, cryptography is hard, and the odds of us getting it right on our own are basically nil. That’s why we should rely on existing frameworks, and the advice of experts like Thomas. What higher praise is there than that of praise from your sworn enemy?

Let’s recap:

  1. Do not invent your own “clever” password storage scheme. I know, you’re smart, and you grok this crypto stuff. But through this door lies madness– and abominations like LMHash that have ongoing, worldwide security ramifications we’re still dealing with today. Take advantage of whatever password storage tools your framework provides, as they’re likely to be a heck of a lot better tested and more battle-proven than any crazy scheme you and your team can come up with on your own. Security vulnerabilities, unlike functionality bugs in your application, run deep and silent. They can lay dormant for years.
  2. Never store passwords as plaintext. This feels like security 101 and is completely obvious in retrospect. But not everyone knows what you know — just ask Reddit. Store the hashes, never the actual passwords. Educate your fellow developers.
  3. Add a long, unique random salt to each password you store. The point of a salt (or nonce, if you prefer) is to make each password unique and long enough that brute force attacks are a waste of time. So, the user’s password, instead of being stored as the hash of “myspace1”, ends up being stored as the hash of 128 characters of random unicode string + “myspace1”. You’re now completely immune to rainbow table attack.
  4. Use a cryptographically secure hash. I think Thomas hates MD5 so very much it makes him seem a little crazier than he actually is. But he’s right. MD5 is vulnerable. Why pick anything remotely vulnerable, when you don’t have to? SHA-2 or Bcrypt would be a better choice.

Starting to develop iPhone apps

Building iPhone apps is no small feat.  You have to be a good C/C++ programmer, you have to learn the Objective-C programming method, then learn the Cocoa frameworks.

But it’s great that a lot of people have created sample code and examples that showcase functionality.

Copyright 2009-2010 ZeroInverse.com