Blitz3D+ Command Reference

LoopSound sound

Parameters

sound - handle of a sound loaded with LoadSound

Description

Marks a loaded sound as looping, so the next time it is played it repeats forever.

It does not make any noise by itself. This is a Sound command, which means it sets a property on the loaded sound rather than on a playback: call it once after LoadSound, then PlaySound to actually start it. Every play of that sound from then on loops, so mark the sounds that should loop and leave the one-shots alone.

It is what you want for background music kept in memory, engine hum, rain, a siren, a machine gun's rattle - anything that should run until you decide otherwise. Keep the channel handle PlaySound gives you, because a looping sound will not stop on its own: StopChannel is the only way to end it, and PauseChannel the way to hold it.

The loop is seamless - the sample restarts the instant it ends - so any silence at the start or end of the file becomes an audible gap on every repeat. Trim your loops in an audio editor, not in code.

There is no matching command to switch looping back off. If you need the same audio both ways, load it twice and mark only one of the handles.

See also: PlaySound, LoadSound, StopChannel, ChannelPlaying, PlayMusic.

Example

; LoopSound Example
; -----------------

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

; Load a short tone to use as an engine hum
snd_hum=LoadSound("media/beep.wav")

; LoopSound only MARKS the sound to repeat endlessly - it does not
; start playback. PlaySound is still needed to actually hear it.
LoopSound snd_hum

channel=0

While Not KeyDown(1)

    ; Space toggles the looping hum on and off
    If KeyHit(57) Then
        If channel=0 Then
            channel=PlaySound(snd_hum)
        Else
            StopChannel channel
            channel=0
        End If
    End If

    Cls

    ; A pulsing speaker circle while the loop is running
    If channel<>0 Then
        r=40+Sin(MilliSecs()/2.0)*10
        Color 80,255,80
        Oval 320-r,240-r,r*2,r*2,False
        Color 255,255,255
        Text 320,300,"looping endlessly...",True
    Else
        Color 70,70,70
        Oval 280,200,80,80,False
        Color 160,160,160
        Text 320,300,"silent",True
    End If

    Color 255,255,255
    Text 0,0,"Space: start/stop the loop   Esc: exit"
    Text 0,20,"LoopSound snd_hum   (a short beep repeated seamlessly)"

    Flip

Wend

End

Index