Can you give me some advice on how to play sound effect efficiently?

I am making a 2D shooting game.

In my update(float td) function, I am doing collision check every frame between the enemies and bullets.

void update(float td) { 
   /*
   1) Loop through enemies -> get Rect {
             2) loop through bullets -> get Rec {
                      3) Do collision check between two Rects
                                   -> if overlap, play hitting sound
             }
    }
   */
}

But I realized that sometimes multiple sound effects are played almost at the same time for ‘multi-kill’.

which makes the game a bit noisy and dirty, even worse when the game gets 10+ multi-kill.

But is this an okay phenomenon in terms of memory usage and use experience?

My sound effects range from 3KB ~ 30KB.

Just keep a count of how many hits occured. Then play MAX(hits, 5) sounds or do some other thing based on hit count.

To avoid a “fileName” sound play more than 2 at the same time:

  • after playEffect insert the “fileName” and the id in an array (for example map<string, int>)
  • before playEffect get id with “fileName” key from the array and stop it before call playEffect.

or if you use AudioEngine:

  • after playEffect insert the “fileName” and the “id” in the array and setFinishCallBack to remove “fileName” key from array.
  • before playEffect check “fileName” key from the array - if NOT => play and do nothing if YES.