How to play the sound of an object sliding on another object for a variable duration

I would like to add sound effects to a basic 2D game. For example, a stone sphere is rolling on wood surface. Let's say I have a 2 second audio recording of this. How could I use the sample to add sound for an arbitrary duration ?

So far I have two solutions in mind:

a/ record the sound for an amount of time that is greater than the maximum expected duration, and play only a part of it;

b/ extract a small portion of the sample and play it in a loop for the duration of the move; however I'm not sure if it makes sense with an audio wave.

• Use an audio library? Seriously though, I think this question is better suited to avp.stackexchange.com simply because it sounds like you're after the sound design aspect of this rather than the sound programming element. Jun 5 '12 at 23:52
• @RayDey The asker is wondering how they're meant to design sound for games, so it's a game development question and completely appropriate here. Jun 6 '12 at 13:49
• Yeah after re-reading this today I can see the interactive element to it. That being said, I'm still a bit iffy about it. If you take out the first sentence, what difference is there if you wanted to have an arbitrarily long sample in a song you were producing? Jun 6 '12 at 21:31

The typical method is to have a sample with loop points in the middle. It loops for as long as necessary then it can cross-fade into the end of the sample when it is supposed to stop. To do this, you need to know the loop points within your sample, and know where the end section of the sample is, and you need a sound library capable of doing these operations (which depends on the language and platform you're using).

If an API allows you to use wave samples of an arbitrary length and play them with specified volume and pitch, you can make a sliding sound volume and pitch depend on the speed of a sliding object. The faster object slides, the louder the sample plays and it's more pitched up.

For a length of a sample - I would use a looping sample of longer duration, let's say 10s, with extra texture in it (e.g. a mixed grain of sand in a friction sound for a sliding cube, or some louder bumps in sound of a rolling sphere on a wooden surface). It would improve overall quality of this trick. Short samples tend to be perceived as repeatable.

• I'm going to use CoreAudio (OS X audio API) Jun 6 '12 at 13:34
• As I quickly peeked into CoreAudio docs, a part of a CoreAudio API is a MacOS X's implementation of OpenAL so it's definitely possible to play pitched sample data. Jun 6 '12 at 14:02

Your ideas are good. I would attempt to go with something of a mixture between option A and B. Make a sound that's long enough for most scenarios, but also loopable. This way you can get a sound that doesn't sound like it's on a tight loop, but which won't get "cut off" if you were to have a situation where the sound might play too long. I haven't started the sounds part of my game yet, so I don't know a lot about what tools are available. There are a few that I have bookmarked for future use, which might be useful.

1. bfxr This one and number two are for generating sounds effects. I have no idea what 75% of the sliders do, but I just slide them around until I get something like what I want.
2. sfxr
3. Dynamically composing music and sound effects - Don't follow that one too closely ;)
4. Creating and playing synthetic sound - Just something to get started with, then create algorithms for the sounds I want.
5. Audacity I use this now for editing the voice overs on my videos, great program. Unfortunately it doesn't help the feeling that I sound weird to myself.
• Point 3 reminds me of iMuse used in Monkey Island 2 for the musical transitions between scenes. This was incredible the first time I heard it. Jun 6 '12 at 11:56