Sound Engine

BlitzMax Forums/BlitzMax Beginners Area/Sound Engine

Chroma(Posted 2006) [#1]
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.


Grey Alien(Posted 2006) [#2]
.
(sorry posted then realised I'd misread your post)


Cruis.In(Posted 2006) [#3]
as it is blitzmax doesnt cut off sounds does it? havent experienced that


tonyg(Posted 2006) [#4]
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.


Chroma(Posted 2006) [#5]
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.


Chroma(Posted 2006) [#6]
I tested it out and it stayed in the 70k range the whole time. Looks like it's good to go.


Space_guy(Posted 2006) [#7]
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.


Grey Alien(Posted 2006) [#8]
Space_guy: Yeah I isolated that biug and told BRL and Skidracer fixed it, no worries anymore.


Space_guy(Posted 2006) [#9]
thanks!. good to know :)