Compile Speex For iPhone

Speex is an audio codec specially designed for voice audio. I’m sorry, a codec is a software component used for encoding and decoding something. Oops, I’m terribly sorry! An iPhone is kinda like a phone but it has no wires, y’see… and the letter i is prefixed to it because… Hi, I’m Cliff. You’re probably here because you already know what an iPhone is. Maybe you’ve seen one of the commercials. Maybe your cousin’s best friend’s uncle’s nephew’s best friend’s cousin has one. (In case you missed it, that was a really really verbose way of saying “your cousin”!) Maybe you’ve been around the block once or twice and happen to know about both codecs and Speex. (Maybe it was dem fools throwin’ bones by the liquor store that taught you about Speex. Don’t laugh, cause you’d be surprised by what you learn on the streets these days.) Whatever the case, I’m going to explain how I got a clean compile on the Speex library using XCode. Hold tight because the remainder of this post is designed to be informative… that is all jokes and nonsense to the side.

  1. Download the Speex C source bundle
  2. Unpack it
  3. you might wanna try building it from the cmd line. Run “./configure;make” from the folder where you unpacked it.
  4. Create a new project in XCode. This can be an iPhone or an OSX project but for consistency’s sake (and because it’s what I put in the title) let’s use an iPhone project.
  5. Create an actual folder, not an XCode group, under the root of your new project and call it “CSource”. This is where we’ll put the Speex source code.
  6. Drag/drop the folder into your XCode project or create a group that points to that folder.
  7. Double click your project icon in the left hand tree in XCode to edit the project settings.
  8. Goto the build tab and type “header” in the search field to filter your choices to the things that deal with headers. (Yes I just blatantly included a “goto” in a modern day technical writeup.)
  9. Look for the “Header Search paths” build setting. It should fall under the search paths category somewhere toward the middle of the screen. If you don’t see it finish typing the term “header search paths” in the search bar above. It does an incremental search as you type.
  10. Double click the Header Search Paths build setting to bring up the edit dialog then double click in the value field and set the value to “$(SRCROOT)/CSource”
  11. Open the folder that you unpacked Speex to in Finder and drag the “libspeex” folder directly into the CSource grouping you created in XCode. Choose yes to copy the files.
  12. Back in Finder, navigate to the include folder under the Speex unpack directory. Drag the “speex” folder out of here and next to the libspeex folder you added to your project in XCode. Choose yes to copy the files.
  13. Back in Finder, drag the “config.h” file out of the root of the speex unpacked folder and into your XCode project.
  14. Back in XCode, hit Cmd+Shift+D and type “arch.h” to find and open the arch.h file we’ve imported into our project.
  15. Add a “#include” to include “config.h” at the top.
  16. Remove the “echo_diagnostic.m” file under libspeex from the project as it will just cause complications.
  17. Hit compile and wait for the errors to roll in!

If you followed the above steps correctly then you should only see a few errors relating to duplicate symbols. If you get thousands of errors then it’s likely related to missing header files. You probably have to make sure you imported the speex folder with all the headers and double check your header search path to make sure that it points to the directory containing the imported “speex” folder. You might find a bunch of errors if you don’t remove the “echo_diagnostic.m” file from compilation. The last order of busniess would be to import the ogg container source.I downloaded libogg-1.1.4 which appears to work with speex-1.2rc1. Including the “ogg.h”, “os_types.h”, “bitwise.c”, and “framing.c” files allows me to compile code included from the “speexen.c” and “speexdec.c” examples.

13 thoughts on “Compile Speex For iPhone

  1. Thanks Cliff for this one and one only post about compilation of speex in XCode.. I tried this. But i couldn’t find config.h in root directory of speex. What am supposed to do with ? I am getting errors in 1) #error No other FFT implemented 2) bits.c:110: error: conflicting types for ‘speex_bits_read_from’
    3) bits.c:157: error: conflicting types for ‘speex_bits_read_whole_bytes’ . Can you please help me about config.h ? Thanks in advance

  2. Sorry… i solved my problem.Sometimes i was not getting config.h while am compiling speex in MAc terminal..Now its fine..Thanks for this wonderfull post….

    1. Sorry for taking so long to respond. It’s been crazy trying to keep up with things. I wanted to post more detail around getting this working because it’s not so straight forward. You have to make sure the config.h is included in the compile by adding it to the Header search path. (This is reminiscent of Java CLASSPATH drama.) There are also the test*.c files that also get in the way of the XCode build as well. I can’t post much now but hopefully I will get back around to doing some sort of deep dive around the topic.

  3. Thanks for this article, it was invaluable to getting speex to compile for me. I was wondering if there was any chance you might post a sample of how you are implementing it on iPhone to do encoding?

  4. whoops pasting these questions here too:

    • can my app just damn write the audio into speex ?
    • can any old web browser play speex ?
    • is speex way tinier than MP3 ?
    • is audio OK sounding with speex ? (this is just ppl talking mostly)
    • can QuickTime play speex ???

    1. Here’s the skinny on Speex. It’s a codec developed specifically for compressing vocal audio. That means it doesn’t do well for music and other audio. I haven’t done comparisons but I’d imagine you could get file sizes equal to or less than the mp3 equivalent. If you want an example of what it sounds like download MapQuest4Mobile on your iPhone and drive around with the navigation feature. In my opinion, the end result of speex compression is indistinguishable from the original audio with file sizes on the order of 25% of the original. Because the Speex codec is not preinstalled on Mac OSX or iPhone/iPad iOS you have to read the gibberish listed here and wait for me to follow up with the source examples in between jobs (are they hiring where you work?) and during bathroom breaks. (My wife wonders why I sit on the throne for so long but how can I explain the technical challenges of applying TDD to a C based project using Obj-C and Apple’s limited testing support?) You probably won’t be able to use it in MobileSafari until Apple gives a way to either load proprietary codecs or link into 3rd party frameworks. If you’re doing embedded development then you can use this post as a start on making the API available for your app. Like I said above, I’m following up with more source examples showing how to call into it. I managed to link it into CoreAudio using the audio queue api which is just as challenging as using Speex native API. There are binaries that you can download on your Mac to get it working in Quicktime.

  5. Hi there, I am quite new to iOS and I have lots of ideas… I think I need to come back to this when I have more knowledge and experience – I just started to lose track! With some effort I hope I can attain somewhere near your skills (or just find a developer! lol)

    1. Hi Pete,

      My level of skill is grossly exaggerated! I only pretend to know half of what I post and its only through the process of repeat “faking it” that I get enough familiarity to sound remotely knowledgable. In all honesty (thanx for the comment BTW) the lower levels of how compression works is still a mystery to me. I come from a basic level of understanding how to stitch things together. Getting the tool to compile was difficult enough for me which is why I blogged it. learning how to use it took a ton of trial and error. I’ve read up a little on DSP and compression and audio stuff but there’s just so much. Since you sound passionate enough I imagine that you’ll be able to write an equivalent tutorial after a couple of months of banging your head. The iOS platform is still tricky even after working with it over the past 3-4 years and I still get nervous when I need to find documentation on some new API or tool. Keep checking my site and I hope to slowly produce more on this and other subject matters.

  6. Hi,
    I am really desperate for help with a project I’m working on. It has nothing to do with iPhone, but the errors I am getting are coming from speex library. I googled every where and no answer. So, I thought you might be able to help me out somehow.
    These are just some of the many errors I am getting:
    1>c:\program files (x86)\ioquake3dev\code\libspeex\arch.h(65) : fatal error C1189: #error : You now need to define either FIXED_POINT or FLOATING_POINT
    1>c:\program files (x86)\ioquake3dev\code\libspeex\os_support.h(51) : error C2054: expected ‘(‘ to follow ‘inline’
    1>c:\program files (x86)\ioquake3dev\code\libspeex\os_support.h(52) : error C2085: ‘speex_alloc’ : not in formal parameter list
    1>c:\program files (x86)\ioquake3dev\code\libspeex\os_support.h(52) : error C2143: syntax error : missing ‘;’ before ‘{‘

    Any ideas of how to fix this?

    Any help would be appreciated.
    p.s. What bothers me the most is that it worked the day before yesterday. Then, all of a sudden just got these flood of errors…


  7. Hi,
    Where can I find some sample code using Remote IO to capture and audio from mic and encode using speex..?Anybody tried this.. show me some light..

  8. Hey Cliff,

    I did everything the same as in your awesome tutorial(big thanks for it) and got the lib compiled on the iPhone Simulator. But when I compile on the real device some errors emerge(like Unknown type name __m128, which is a standard C/C++ type defined in xmmintrin.h), in one word some C/C++ dependency errors. Should I include some linker flags or smth. Any help will be appreciated.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s