I am new to both fmod and sound programming. I have written a small program to simulate the sound requirements of a project I am working on.

The program plays a single sound many times at random intervals. Imagine the sound of a hundred balls bouncing in a room; this is the effect I am after.

The program works but the output doesn’t sound right. When I crank up the number of objects (balls) and up the probability of a collision the sound obtains a sort of rythm like a fast drum beat rather than the intended noisy chaos. (Listen to the example linked below).

So there is the problem. I don’t know where to start with this, nor what questions to ask. If anybody has any thoughts at all please reply. Thanks!

Here is an example of the output:


Here is the full source:


Here is a code fragment showing my loop:

void playRandomSound(int numChannels, int numObjects, float collisionProbability, float loopPeriod, const char * soundFile)
FSOUND_Init(44100, numChannels, 0);
srand( (unsigned)::time( NULL ) );
FSOUND_SAMPLE * sample1 = FSOUND_Sample_Load(FSOUND_FREE, soundFile, FSOUND_16BITS, 0, 0);

float accumulatedTime = 0.0f;
float absoluteTime = time();

while (true)
    float newTime = time();
    float deltaTime = newTime - absoluteTime;
    absoluteTime = newTime;
    accumulatedTime += deltaTime;

    if (accumulatedTime >= loopPeriod)
        for (int i = 0; i < numObjects; i++)
            if (collisionProbability * accumulatedTime > (float)rand() / (float)RAND_MAX)
                FSOUND_PlaySound(FSOUND_FREE, sample1);
        accumulatedTime = 0.0f;



  • You must to post comments

Thanx for the speedy reply Brett.

Firstly, don’t worry about the debugging – I have a million monkeys downstairs to do my evil bidding 😉

The accumulatedTime is consumed when I calculate the probability, hence I set it to zero after that. This is there to fix the timestep independently from the loop, which is a habit I have contracted (from the monkeys).

So the loop boundary, the ‘real voices’, discarded sounds, etc. is where I will concentrate my monkeys now. You will have to bear with me here, because this is where my knowledge lacks.

Lining up on the loop boundary – how to tackle this? I could increase the loop frequency. This is difficult in this simulation because the Sleep() function is crap. Do you know a better way to time my loop? I have played with higher frequencies in other ways and the sound became distorted. Is that a matter of adequately filtering and condensing my events?

‘real voices’ – I deduce that you are talking about (software) channels? How many does one typically use?

Can I/should I let the existing sounds play and discard new sounds if all channels are in use?

I could also restrict the number of sounds being made globally or per object or something like that…


  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.