tags:

views:

2304

answers:

4

I'm looking to create an app that emulates a physical instrument. I've got audio samples but I want to be able to increase the pitch/frequency dynamically so I don't have to load from too many files.

Any idea which audio API will be able to do this? I reckon either OpenAL or Audio Queue Services but am not sure which is suitable. Any links to guides/sample code is also much appreciated.

Thanks in advance.

A: 

It sounds, a bit, like you're creating essentially the wavetable synthesis method of playing MIDI files. You might be able to find a MIDI synthesizer for the iPhone that you can use, and then use your audio samples to build a wavetable set. Anytime you'd want to play tones, you would simply send the MIDI event into the iPhone MIDI synth with your loaded wavetable set.

sheepsimulator
To be irritatingly technical (I'm a synth nerd), it sounds more like a simple sampled instrument than wavetable synthesis. I've done both. http://en.wikipedia.org/wiki/Wavetable_synthesis
Nosredna
I'm not a synth nerd, and I have very little experience with sound programming; I simply wrote the answer because I thought I saw a similarity from my body of general computer knowledge, and no answers were posted at the time I wrote mine, so thought I'd put in my two cents. I'm glad people with far more experience in this area than I are posting better answers :)
sheepsimulator
I wasn't voting you down or anything, just eager to point out how cool wavetable synths are. :-)
Nosredna
@Nosredna - Didn't think you were attacking; actually quite glad you wrote that comment and pointed me to Wikipedia; you're right, wavetable != sampled instrument.
sheepsimulator
+1  A: 

Try creating an Audio Unit. I'm doing something similar an AU worked well for me. Initially I used an audio queue as it was simpler (higher level?) and synchronous, however it was lacking in responsiveness, so I dumped it for the Audio Unit.

Rhythmic Fistman
+9  A: 

I went down this road, trying Audio Toolkit, Audio Queue Services, openAL, and finally settling on the RemoteIO AudioUnit.

Audio Toolkit is fine for basic triggered sound effects, but it wasn't able to change frequencies or loop samples.

Audio Queue Services can loop samples, but the only way I could find to adjust the playback frequency of a sample was to re-read the data from the file -- very painful. Plus, the framework is tremendously cumbersome - I'd only use it if I was trying to stream something off the Internet.

OpenAL was a godsend - was up and running with it in under an hour, after getting my hands on the no-longer-available-from-Apple "CrashLanding" iPhone sample app. I found OpenAL to be ideally suited to games or even a musical instrument -- samples could be pre-loaded, adjusting the frequency was easy, and looping was no problem. The deal-breaker for me was that starting and stopping a looped sample would result in a nasty "pop" almost every time. Also the builtin 3d positional audio mixer was a bit too CPU-intensive for my liking.

If your instrument does not use looped samples, I'd suggest trying the OpenAL route first - the learning curve is much less intimidating. Try to track down "SoundEngine.h", "CrashLanding" or "TouchFighter", or check out the following link:

http://benbritten.com/blog/2008/11/06/openal-sound-on-the-iphone/

Since looped samples was a requirement for me, I finally settled on AudioUnits (which, on the iPhone, is referred to as "RemoteIO" if you want to do input or output). It was tremendously difficult to implement - very similar to Audio Queue Services, in that the core of your implementation will be inside a "buffer callback", being called several times per second to fill a buffer of outbound audio with raw SInt16 values.

Ultimately, I got my instrument working beautifully with multi-note polyphony, looped samples, no popping, and minimal latency.

Unfortunately, RemoteIO is not well documented. Michael Tyson was one of the first in the field to write about RemoteIO at length, and his posts (and the comments) were very useful to me:

http://michael.tyson.id.au/2008/11/04/using-remoteio-audio-unit/

Good luck!

Glenn Barnett
Thanks for the tip. I'm trying OpenAL with the oalTouch sample code at the moment but I can't find the setting to change pitch or frequency on the fly. Any tip in the right direction is much appreciated!
Matthew
Nevermind. I found out how to adjust the pitch. The sample code for oalTouch only manages one sound file, so I guess I need to look at soundManager afterall.
Matthew
THe soundEngine code works really well on iPhone OS 2.21 SDK but fails to compile on 3.0 of the SDK. Any idea if there's a new version out there?
Matthew
A: 

Did you ever find a way to adjust playback frequency with AudioUnits? Im using remoteIO for my app and I need to ajust the playback rate of two sound files separately. The only thing i have managed so far is to change the sample rate, but that can only be done before the graph is initialized and i need to be able to change the playback rate on the spot.

Thanks

Ata