Archive for the 'Little Computers (iPhone Dev)' Category

Making a sound-activated iPhone camera app

SoundSnapp Initial Sketches

Apple’s camera interface sucks. It sucks for the user, who has to hit a button on the screen (thereby shaking the camera and virtually guaranteeing a blurry result), and it sucks for the developer, who is forced to use a modal UIImagePicker view to access the phone’s camera.

For my iPhone development class, I wanted to create a minimal sound-activated camera interface. Since my knowledge of Objective-C is pretty rudimentary, I broke the task up into a series of subtasks:

  1. Accessing the camera without using UIImagePicker:
  2. Scouring the web for information on circumventing Apple’s camera interface led me to discover the approach that many jailbroken apps use: accessing the camera’s hidden classes through a private framework and then saving the pixels of the preview as a UIImage. After an aborted attempt to create a toolchain, I gave up and eventually found Norio Nomura’s extremely helpful Github repository that includes a class called CameraTest, which provides three different ways of capturing photos by invoking classes from the private framework dynamically at runtime—that means no toolchain and no weird compilation requirements.

  3. Metering and displaying sound levels:
  4. I played with SpeakHere, Apple’s example app that does just this and almost cried. It is the most horrendously complicated thing I’ve ever seen—OpenGL, eight classes, tons of C. I suspect that Stephen Celis, the creator of the extremely simple and helpfully documented SCListener class must have been inspired in part by a similar sense of despair. SCListener outputs the peak and the average levels on the microphone and requires nothing but the AudioToolbox framework. I linked the peak to an animated UISlider and presto, a sound meter.

  5. Creating an intuitive way to set the sound threshold:
  6. One of the benefits of using a UISlider as the sound meter is that it is also a slider! When the user touches the slider, it stops monitoring the sound and responds to his finger. Wherever he releases it, that’s the new threshold. I still need to add some persistent feedback, possibly a small colored bar or other subtle indication of the threshold’s current value. Even without it, it works pretty well.

  7. Having a countdown timer option:
  8. For hands-free operation or self-portraits. This was a trivial matter of setting an NSTimer. The most difficult thing was figuring out how to create a tab bar button that would change its image with every user touch.

  9. Creating an automatic email feature:
  10. This seemed like it was going to require a complicated notification system and PHP scripts on a web server but Apple has opened up apps’ access to email in the forthcoming 3.0 SDK, so this feature is on hold until then.

Wazzzzup! My first iPhone App

First off, let me admit, this is not much of an app. It’s hello world for the post-ethnic world. It’s also endorses a product which deserves no endorsement, though I guess it is technically a Belgian beer now. Anyway, apparently there’s a nine-year old in Singapore who can do a lot more, but being a total noob when it comes to Cocoa development and Objective C, I’m pretty psyched it’s working at all. Screenshots follow.

iPhone Screen 1
click on the app…

iPhone Screen 2
…push the button…

iPhone Screen 3


Introduction to C

So I thought C was going to be impossible to understand, but so far (which is admittedly not that far), it’s not too different from the other programming languages I’ve encountered—though malloc (it even sounds ominous) and all the $ and * loom threateningly over the horizon.

I’m struggling with the finer points of pointers (more a syntactical problem I think than a conceptual one), as is evident from my first piece of C code, which takes a string (right now one that’s hard-coded into the program) and reverses it. I realized while I was coding that what takes so long about learning new languages is figuring out how to do the little easy stuff you take for granted. For instance, I wanted to find the length of the array in which I was storing my string but had no idea what the syntax was in C (I have since learned you can use sizeof()), so I ended up writing my own function instead.

This is the code:

by alex kauffmann

This is a totally inelegant solution but it
works, though I couldn't fully wrap my head around
the pointers (which is why I have both an array and
a more C-ish asterisk thingie).  

#include <stdio.h>

void main() {

  int i,a,b;
  char *s;

  s="satan oscillate my metallic sonatas";

  printf("The original string is: %s\n", s);
  printf("The reversed string is: ");

  for (i = 0; *s != '\0'; s++)  // counts the characters
     i++;                       // in the string

  char rs[i];                   // holds the reversed string

  for (a=i; a>0; s--) {
     rs[i-a]=*(s-1);            // (s-1) offsets the first
     a--;                       // character to skip the '\0'

  for (b=0; b