Monday, November 29, 2010

Your Japanese Name for iOS, part 6

Got some good suggestions for playing audio. Looked at Finch and OpenAL. One thing in common seems to be that background loops are played with AVAudioPlayer. Apple's "oalTouch" example seems to be easiest for background music playback. It didn't loop though, but docs say it should be possible to get it to loop, so I'm hopeful. Also a bit optimistically I'm hoping that I could instantiate multiple AVAudioPlayers for each sound. None of the examples seems to be doing that, so maybe there's a reason why it doesn't work, but docs say it should.

Got background music to play. Bought sound from iStockphoto. Converted from WAV to M4A through iTunes and customized a snippet from oalTouch example:

bgURL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:@"koto" ofType:@"m4a"]];
bgPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:bgURL error:nil];
bgPlayer.numberOfLoops = -1;
[bgPlayer play];

It really does seem to be a perfectly seamless loop. Listened for a few minutes and got tired of it already :-)

Next I should see what happens when headphones are plugged / unplugged. In Mega Jump the music just continues playing. Apple guidelines say the music should stop, but I don't think that's suitable for me because there would be no way for playback to continue, just like there isn't in Mega Jump. Well, I suppose I could show a big pause button, but it seems like overkill.

Great, seems the sound continues by default, so don't need to handle it at all.

Next, how to fade in and out the sound of chimes when finger is moved around. Cool, found fade out example code.

Works as advertised. Found a new case again. User puts finger down, drags, lifts finger up, drags at another point. Sound abruptly ending and new starting sounds painful. Instead should let chimes from old movement fade out while the new sound starts. I was wrong to think I only need 3 channels. Luckily AVAudioPlayer handles this transparently, so I just need to instantiate a new player each time the user puts their finger down. May cause problems if user taps a hundred times within a second. That would allocate a hundred players with all the sounds mixed together, will have to test to see if it's a problem.

Now chimes overlap and fade out as I originally planned. And yes, tapping too fast turns out to be a problem. This device is slow enough that by mixing too much this naively I can apparently max out the CPU. Well, I'll just have to cancel the oldest sound if some maximum mixing amount is reached.

Done. Next should start planning how to separate Japanese characters into drawable blocks. The structure is such that each word consists of several characters, each character consists of several strokes and each stroke consists of several segments. Each segment has a start and end waypoint that need to be touched in order for the segment to be completed.