views:

838

answers:

4

I need to play a "horn" sound when a user touches the screen.

The horn sound needs to play for as long as the user's finger remains in contact with the screen.

The ending of the sound effect will be different. Imagine the horn "petering" out at the end as airflow decreases.

Would I have to stitch 2 sound files together? Do I need to use Remote I/O since the timing will be so critical?

This library looks like a possibility: http://www.surina.net/soundtouch/

It seems like I could play a sound indefinitely, but I haven't implemented it yet, so I am not sure.

I feel like this would be a common problem in game programming, but I have little experience in that area.

I've never used audio to this extent before so I am at a loss of where to begin. (I have only required the use of AVAudioPlayer in my apps until now).

Any suggestions on a library to use or at a technology to use would be great.

A: 

I'm not experienced with audio programming on the iphone. I think you can create a good duration sound file that includes the final 'petering out' sound at the end. What you can do is, as long as the user is touching the screen, play the sound file and keep seeking back to the beginning as soon as you get to the beginning of the petering out, effectively looping it. If the user stops touching the screen, then you can stop seeking to the beginning and finish playing the entire sound, which finishes off with the fade out effect (Or for a more immediate reaction, seek to the beginning of the fade out).

I'm not sure if seeking like this is possible, but that would be one way to do it. Sorry if I misunderstood your question.

Jorge Israel Peña
+5  A: 

To make it realistic, you need three sounds:

  1. Attack. This is the start of the sound; it is the most important part of the sound, as it is the part that the brain uses to identify what the sound is. The sound then decays slightly, and becomes the...

  2. Sustain. This is the part of the sound that you will need to loop continuously until the user lifts his finger. You will have to craft this part of the sound carefully to avoid "ticks" or other artifacts while looping the sound.

  3. Release This is the sound that you play when the user lifts his finger.

There are audio tools such as Audacity that will make the creation of these three files easier. You can use an actual recording of a horn to create the three sound pieces.

Robert Harvey
+1: this is the proper way to approach this problem. Although you should mention "decay" (it's not entirely necessary but makes for a better sound)
TM
The "Decay" part of the sound should be included in the first sound file for this application.
Robert Harvey
Crossfade looping is about as easy to implement as the rest of this, and takes care of the artifacts.
MusiGenesis
Also, an easier way of handling the release is to just continue the looping part but fade it out logarithmically, rather than trying to transition to a pre-recorded release segment.
MusiGenesis
If you want the three sounds to play together without popping, make sure the head and tail of each are trimmed at the zero-crossing point of the waveforms. Easy to do for mono, PITA for stereo.
willc2
A: 

Achieving this with a recording of a horn (as a WAV or MP3 file or whatever) is a relatively difficult programming task, especially for someone who's never worked with audio before. If the iPhone supports MIDI (or if there's an easy-to-use MIDI component that you can incorporate into your application), this would be a more practical route for you to take. With MIDI, you basically tell the synthesizer to start a note at a particular pitch (like you're pressing a key on piano) and the synthesizer will play that note until you tell it to stop playing (part of the stop instruction involves telling the synthesizer how gradually to "release" the note). MIDI synthesizers all support a basic set of 128 instruments (not counting drums) including various horns.

MusiGenesis
You can do MIDI on the iPhone, but it's third-party; see http://thrill.artificialeyes.tv/i3L
Robert Harvey
I skimmed the link, but it appeared to say that i3L only works on a jailbroken iPhone.
MusiGenesis
+1  A: 

You will want to checkout Audio Queues.

The IPhone-DJ project on Google Code might provide some inspiration.

And there is are a couple of blog entries introducing Audio Queues here and here.

Jeff Youel