Sound Engine

BlitzMax Forums/BlitzMax Beginners Area/Sound Engine

I'm to the point where I'm putting sounds in my game. Which is a good sign because it's one of the last things I normally do.

Here's my take on a sound engine:

In my case I don't need a volume slider in the options menu. I'll be using a toggle that will go from:

No Sound (0) - Quiet(0.35) - Normal(0.65) - Loud(1.0)

There's a global master volume and global master music variable.

Here's a question about garbage collection though. Let me set up the environment with some code.


Global Snd1:TSound = LoadSound("mysound.wav")

Const MVol_OFF:Float = 0
Const MVol_LOW:Float = 0.35
Const MVol_MED:Float = 0.65
Const MVol_HI:Float = 1.0
Global MVol:Float = MVol_MED

Type SndFX
   Field chn:TChannel
   Function Play(snd:TSound,vol:Float=MVol,rate:float=1)
      Local s:SndFX = New SndFX
      s.chn = AllocChannel()
      SetChannelVolume(s.chn,vol)
      SetChannelRate(s.chn,rate)
      PlaySound(snd,s.chn)
   End Function
End Type

Now the above code works extremely well and allows you great control over your sounds. Making a new channel for each sound keeps you from cutting off previous sounds too. It's very easy to call also...like this:
SndFX.Play(Snd1)

My concern is that after the sound is finished, does the garbage collector delete the type or does it hang out and as each new sound is played...eat up memory? I'm assuming it gets deleted.

.
(sorry posted then realised I'd misread your post)

as it is blitzmax doesnt cut off sounds does it? havent experienced that

Check with gcmemalloced()
My guess is your sndfx instance is local so is no longer in scope when the function finishes so should be reclaimed next GC run.

Cool thx tonyg. I'll try that.

@ Cruis.In: It does if you try to play sounds the wrong way haha. (me) I just needed global volume and pitch control for all sounds played.

I tested it out and it stayed in the 70k range the whole time. Looks like it's good to go.

I dont know if its still the same as when i added sound to one of my games, but back when i did it i had problems sometimes that the sound stopped working compleatly after a while. So i started using a pool of channels instead and the problem dissaperaed.

anyway if it works for you it might be fixed long ago.

Space_guy: Yeah I isolated that biug and told BRL and Skidracer fixed it, no worries anymore.

thanks!. good to know :)