Friday, June 27, 2008

MikMod library ported to iPhone

I've always been a big fan of old skool computer music, like chiptunes or demoscene music. Perhaps because they remind me the Good Old Days (tm) of my Amiga. Or perhaps for the same reason I like demoscene: real-time computer art! Because it's definitely cool, I've decided to port MikMod to the iPhone! We'll see later if it might be useful to someone else :) Compiling libmikmod Here are the steps to follow to build libmikmod for your iPhone or iPod Touch. First of all, make sure that you have the necessary header file from Apple, namely AudioQueue.h. This file is part of the AudioQueue framework, which is available in the MacOS X 10.5 SDK. This means that the official iPhone SDK is not required. Then, download the latest libmikmod-3.2.0-beta2 available on the MikMod homepage, as well as this patch for iPhone support. For simplicity, let's consider that both files will be downloaded in the same directory. Once you have them, extract the archive and apply the patch:
tar -zxf libmikmod-3.2.0-beta2.tar.gz
cd libmikmod-3.2.0-beta2
gunzip -cd ../iphone-drv-mikmod-3.2.0-beta2.patch.gz | patch -p1
Among other things, the patch modifies various Makefiles and the configure script, so we have to cleanly regenerate all the autotool-related files:
aclocal
automake
autoconf
Now let's set up the necessary environment variables to configure and build libmikmod. First, where to find the AudioQueue header and where to install libmikmod:
export AQDIR=$HOME/local/audioqueue
export MMDIR=$HOME/local/mikmod-iphone
Make sure you are using absolute paths for the variables above. Then, let's set up some compilation flags and name the tools we'll use from the iPhone toolchain:
export CFLAGS="-I$AQDIR -DAVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER="
export CPPFLAGS="$CFLAGS"
export LDFLAGS="-framework AudioToolbox"
export CC=arm-apple-darwin-gcc
export RANLIB=arm-apple-darwin-ranlib
Okay, now it's time to let configure do its job:
./configure --enable-iphone --host=arm-apple-darwin --disable-oss --disable-esd --prefix=$MMDIR
We're almost done! But there's still a little quirk that must be addressed. The project is configured to build shared libraries, which for some reason refuse to link with the version of the open-source iPhone toolchain I use. To overcome this problem, you just need to patch the generated libtool configuration. Knowing no clever means to do so, I propose something like:
sed -r -i 's/^(allow_undefined_flag.*)"/\1 -Wl,-read_only_relocs,suppress"/' libtool
Phew, now we're done! Just type:
make CFLAGS="$CFLAGS"
make install
And voila! a fresh libmikmod with support for your beloved iPhone or iPod Touch! Oh, by the way: the library comes in both static and dynamic flavor, so it should be pretty usable. Of course, the obligatory example will follow soon :P EDIT: the link to the patch was pointing to a plain file instead of a gzip one, fixed!

8 comments:

gfm said...

Hi, are you compiling this on 10.5.3 w/ the iPhone OS 2.0 SDK? I'm trying to follow your instructions but configure reports that it can't locate "arm-apple-darwin-gcc".

Unknown said...

Hi gfm,

No, I'm using the openSDK. Actually I've used the official SDK.

The problem you're facing is that MikMod's configure (and more generaly GNU autoconf) searches for a C compiler named gcc prepended by the name of the host, in this case arm-apple-darwin-gcc.

By googling a bit, you'll find that in the official SDK, the executable is called arm-apple-darwin9-gcc-4.0.1.

So you have to adapt the build process a bit. Use --host=arm-apple-darwin9, or make some symbolic links to make GNU automake happy. The latter is explained in detail in this O'Reilly blog post.

Cheers!

Unknown said...

Eh eh, I mean "I've _never_ used the official SDK" :P

Unknown said...

Hi, The link to the patch file brings up a text file in the browser as opposed to downloading a .gz file. I don't know if its part of the problem but when applying the patch (I downloaded it to a .patch file and zipped it up) it fails on hunk #1 every time. I have ignored that and tried to compile it, it all goes fine until you get to the make install command, it fails every time on something to do with the linux alsa sound drivers :S

Unknown said...

Arrg...
I'm just plain stupid, the link to my patch points to a plain file instead of a gzip one! This is fixed now, sorry for the inconvenience :P

Applying the patch should be as easy as typing:

curl -L -O http://mikmod.raphnet.net/files/libmikmod-3.2.0-beta2.tar.gz
curl -L -O http://damien.ciabrini.free.fr/pub/mikmod-iphone/iphone-drv-mikmod-3.2.0-beta2.patch.gz
tar -zxf libmikmod-3.2.0-beta2.tar.gz
cd libmikmod-3.2.0-beta2
gunzip -cd ../iphone-drv-mikmod-3.2.0-beta2.patch.gz | patch -p1

Cheers!

NSeven said...

Heya there are you able to email be the ported library for iPhone.

Im soo desperate to create an .xm player for iPhone

Mark

Unknown said...

Hi NSeven,

Anything unclear with the instructions posted on th blog?

It's been years since I coded this patch. In the mean time, Apple has open its SDK and I didn't have time to compile the patch against it yet.

I'm implementing in my spare time a full player for modules based on MikMod. Stay tuned :)

Mark said...

Any source code examples of this? I'd love to do something like this in one of my iOS games. A real xmod player would be fantastic.