How to loop alot of sounds perfectly.

Previous topic - Next topic

Hemlos

Hi

If you want to loop alot of sounds...like for instance....multiple engines sounds in a race.

First you want to load the sounds in your header.
4 AudioBuffers will allow up to 4 engine sounds.

Code (glbasic) Select

AudioBuffers=4
SoundID_Engine=GENSOUND()
LOADSOUND "./Media/Engine.wav" , SoundID_Engine , AudioBuffers


In order to loop your sound perfectly, you must know how long it is, and standardize all sound loop sizes!

So with windows, sndrec32.exe in the entertainment menu or from run box, open your engine sound. If the sound is 1.25 seconds long....move the selector to 1.0 seconds and then choose delete after current position. Because we will be looping all looped sounds every one second.


The rule of thumb here is consistancy, so make a standard size of loop, this way you can loop multiple types of sounds with one sound timer routine, in your main loop.
This routine below will only attempt to play a sound every one second, put in main loop:
Code (glbasic) Select

EngineTimer=EngineTimer+GETTIMER()
IF EngineTimer >= 999.0;
  EngineTimer = EngineTimer - 999.0;
  PlayAudioLoops1=TRUE;
ENDIF


Now the timer is running, every one second it is going to tell your next function to play all your loops.
Code (glbasic) Select

IF PlayAudioLoops1=TRUE;
  PlayAudioLoops1=FALSE;
  PLAYSOUND( SoundID_Engine , 0 , 1.0 )
ENDIF


The sound is being played every second, and your sound is exactly one second long.
Now you dont need SOUNDPLAYING(), which will cause a delay in the audio loop, if you do.
Dont use SOUNDPLAYING() with this loop timer...youll get mixed results.

You can set all your sound buffers up to 4 now.

If your audio loops are smaller than 1 second, like a bullet for instance...
Set the sound to some hundreds of millseconds....for instance, 40% of a second...400ms in sndrec32.exe.

Here you need a second timer:

Code (glbasic) Select

EngineTimer=EngineTimer+FrameTimeMs
IF EngineTimer >= 399.0;
EngineTimer = EngineTimer - 399.0;
  PlayAudio2=TRUE;
ENDIF


So, this is why you need to standardize all your sounds....make some loops that are one second long, and a few 400ms long.....and you the above timers to trigger all of them, perfectly timed.

Code (glbasic) Select

IF PlayAudio2=TRUE;
  PlayAudio2=FALSE;
  If TriggerPulled=TRUE;
    TriggerPulled=FALSE
    PLAYSOUND( SoundID_Guns , 0 , 1.0 )
  ENDIF
ENDIF


I use sndrec32.exe because it is easy and fast, plus you can make sure it is set to 44khz stereo 16 bit.
Futhermore, you can use any software of your choice to make the loop some standard length.


With these timers, i was able to create multiple helicopters engines sounds, and machine guns firing, without interuptions or glitches in the sound loops.

Remember, if your looped sounds arent all the same length, you will need multiple timers to make audiably seamless sound loops.

Bing ChatGpt is pretty smart :O

Wampus

Just wanted to say thanks for this. I wouldn't have thought of this myself and it works perfectly, i.e. the loop is imperceptible.  :booze:

Hemlos


Thats awesome, im glad it works for you too!

This is how i looped my helicopter blades sounds, they had to be perfect since its a constant sound effect.
I also used it for the choppers warning sounds etc.

ALso, You can loop music like this too by the way....mix it up with a custom midi track and record it into a wav file, and edit it just as any of the other standard sized sound loops.


Bing ChatGpt is pretty smart :O