Blitz3D+ Command Reference

FreeSound sound

Parameters

sound - handle of a sound loaded with LoadSound

Description

Deletes a loaded sound and releases the memory it was using.

Sounds are held in memory in full, so a game with a different sound set per level will grow steadily unless you free the old ones as you go. Free the sounds belonging to a level or menu you have left, then load the next set.

This frees the sound, not the playbacks that came from it. If you want a currently audible channel silenced, call StopChannel on its channel handle first - keeping track of your channels is the only way to do that, since freeing the sound gives you no way to reach them.

The handle is dead after the call, and any other variable holding the same number is stale too. Setting the variable to 0 afterwards makes it obvious, and gives you a cheap "is this loaded?" test.

You do not have to free anything before your program ends - that is handled for you. Free during play because you care about memory, not out of duty.

See also: LoadSound, StopChannel, PlaySound, FreeImage.

Example

; FreeSound Example
; -----------------

Graphics 640,480,0,2
SetBuffer BackBuffer()

; Load the explosion into memory
snd_boom=LoadSound("media/boom.wav")

While Not KeyDown(1)

    ; Space plays the sound - only while it is loaded
    If KeyHit(57) And snd_boom<>0 Then PlaySound snd_boom

    ; F frees the sound: its memory is released and the handle is dead.
    ; Do this when changing levels or swapping sound sets.
    If KeyHit(33) And snd_boom<>0 Then
        FreeSound snd_boom
        snd_boom=0
    End If

    ; R loads it back in, giving a fresh handle
    If KeyHit(19) And snd_boom=0 Then
        snd_boom=LoadSound("media/boom.wav")
    End If

    Cls

    If snd_boom<>0 Then
        Color 80,255,80
        Text 320,220,"LOADED - handle "+snd_boom,True
    Else
        Color 160,160,160
        Text 320,220,"FREED - memory released, handle dead",True
    End If

    Color 255,255,255
    Text 0,0,"Space: play   F: FreeSound   R: reload   Esc: exit"

    Flip

Wend

End

Index