Thursday, April 01, 2010

SoundToy - iPad app in 24hrs

The story started when a day before a public holiday here in Malta (31st March), I got a reminder from Apple Developer Center to submit any iPad app for review for the great launch of the iPad. And I thought why not create something simple in just a 24hr - code jam. My wife was going to be busy doing figolli (traditional Easter sweets), so I said let me a do a coding marathon where in less than 24 hours I have to submit an iPad app. This wouldn't have been possible if I didn't have experience building Buttonia for the iPhone.

Problem 1: I didn't have Snow Leopard, so I bought it after finishing a tutorial with my students on that day. And that evening I installed it together with XCode 3.2 / iPhone SDK for the iPad. Installation went smooth, although took a while to finish due to updates. I still made a TimeMachine backup before just in case :)

Problem 2: What can I do in less than 24 hours? I thought what would I want to do if I had a big multitouch screen? I would want to touch it with both hands and swipe and do some nice visual effects. That would be quite easy to do. Then I said what if I add some sound to it? That would make it more engaging and it would be ideal for kids to play around with it. I'm not really an audio programmer, but a sine wave generator wouldn't be difficult to code. Then if I get some sales, I might add some more sound effects to it and improve it later.

Problem 3: I didn't have an iPad, so I was bound to use the iPad simulator which although it works, its a bit slow on my machine. Plus I cannot tilt it or shake it, or actually multitouch it. So once I had a semi-decent touch trail effect, and a sine-wave generator, whose pitch changes according to where you touch the screen, I ported the code into another project to try it out on my iPod Touch. Now I know I could have done a universal build, i.e. an app that works both on the iPod Touch and iPad, but I didn't want that as it is not the same thing when you have a small screen. The main idea of porting it was to test the multitouch, but also to hope that the sound glitches I was experiencing in the simulator where a bug in the simulator and not in my code. Obviously I was wrong! The sound glitch still occurred and it only occurred when a sound starts, and when it ends. It also wasn't working nicely when blending multiple sine waves.

As I said I'm no audio programmer, so probably this would have been a no-brainer for some experienced audio programmers. The problem was in fact in the blending of the waves. I didn't want to use (A+B)/2, where A and B are two samples, because if one of them is silence, then outcome would be half the volume. I tried this as a test, even though it might half the volume and with this I didn't have any glitches. So I concluded my blending formula of (A+B) - A.B (not exactly this, but a variant of it), was not working right. My mind was already slowing, especially after coding an all-nighter with just 4 hours of sleep (5am - 9pm). I tried to find on the internet how to do correct blending. I discovered a lot of other unrelated algorithms, but not what I wanted. After telling the problem to my wife, I got to the solution :). Basically I used A*a/t+B*b/t where a is the current sine wave's volume, b is the previous waves' volume and t is the total volume (a+b). Now consider A as being the current wave we are mixing, and B as the previous wave (which is a mix of other waves). Then b is simply the maximum volume of all the waves mixed so far. This is probably not the correct way how to do, but I was pressed with time. It worked and no glitches :)

The polishing stage was the shortest cycle I have ever done on any project. There wasn't much to polish since it was a small project. I just added some concepts like double tap to hold a note, and shaking to clear the notes. Then made a quick logo and some simple text instructions while the app is loading.

All in all, it was a fun coding experience. I learned a lot, and definitely I need to get my hands deeper into audio programming/DSP. Waiting for Apple's review...

No comments: