Is this how loopsound should work?

Blitz3D Forums/Blitz3D Programming/Is this how loopsound should work?

I've noticed this for a long time but it hasn't been too much of a problem before, and I didn't test it fully to see how it works before now.

soundhandle=loadsound("some sound.wav")
loopsound soundhandle
soundchn=playsound(soundhandle)
delay 10000 ;simply to allow it to play for a while and loop
stopchannel soundchn
freesound soundhandle

soundhandle=loadsound("some sound.wav")
loopsound soundhandle
soundchn=playsound(soundhandle)
waitkey
end



On my machine if I do this the looped sound, despite being freed earlier will continue from where it left off. Quite annoying when you want to load a new level and have the music start from the beginning again. Is this the way it is meant to work and therefore should I use the playmusic() command - which also seems to play some songs continually and others just once!

Hey Matty,

not sure if this helps but here's how you could have either Looping Music sound files play when you want or not, nothing major big, just loading the two sound files, pausing them at the start, then when you hit a key it resumes the play looping of the sound, but as you'll see it resumes right where it left off, and not starting over:

; Load Sounds

soundhandle1 = LoadSound("C:\windows\media\Windows XP Startup.wav")
soundhandle2 = LoadSound("C:\windows\media\Windows XP Shutdown.wav")

; Set them to Looping

LoopSound soundhandle1
LoopSound soundhandle2

; Assign each sound file to a channel

soundchn1 = PlaySound(soundhandle1)
soundchn2 = PlaySound(soundhandle2)

; Pause BOTH channels for now

PauseChannel soundchn1
PauseChannel soundchn2

While Not KeyHit(1)


; Hit 1 to play Sound Loop 1

	If KeyHit(2)
		ResumeChannel soundchn1
	EndIf

; Hit 2 to play Sound Loop 2

	If KeyHit(3)
		ResumeChannel soundchn2
	EndIf

; To Stop Both Channels hit SPACE

	If KeyHit(57) 
		PauseChannel soundchn1
		PauseChannel soundchn2
	EndIf

	
Wend
End


But what I would recommend for more control is this little piece of code that allows you more control over playing sounds either by themself or mixed together. You could have music playing in the background while other sounds like Menu Clicks and such could be heard.

; ###################################################################################################
; #                    Play Sound Bites and Samples as Needed									    #
; ###################################################################################################

Type ActiveChannel

    Field SampleID%
    Field Address%
	Field Pitch%

End Type
 
Function PlaySample ( SampleAddress% , SampleID% );= -1 )

	Local Channel.ActiveChannel
    Local Simultaneous% = False
    Local Playing% = False
 
    If SampleID < 0 Then Simultaneous = True
    For Channel = Each ActiveChannel

      If Channel\SampleID = SampleID Then Playing = True
      If Not ChannelPlaying ( Channel\Address ) Then Delete Channel

    Next

    If Not ( Simultaneous = False And Playing = True )
      Channel = New ActiveChannel
      Channel\SampleID = SampleID
      Channel\Address  = PlaySound ( SampleAddress )

    EndIf

End Function
 
Function UpdateChannels ()

    Local Channel.ActiveChannel
    For Channel = Each ActiveChannel
      If Not ChannelPlaying ( Channel\Address ) Then Delete Channel
    Next

End Function

Function SoundOff ()					; Force a 'Sound Off' (a variation of UpdateChannels)

    Local Channel.ActiveChannel  		; Channel cursor
    For Channel = Each ActiveChannel  	; Iterate through channels
      StopChannel Channel\Address  		; Stop playing
      Delete Channel  					; Remove channel from active channels
    Next

End Function


To use the Functions load in your sound files ahead of time:
Global Sound1 = LoadSound("soundfile1.wav")
Global Sound2 = LoadSound("soundfile2.wav")


To Play a sound and/or loop:
PlaySample (Sound1,-1)

PlaySample (Sound2,1)


-1 Means only play the once once until asked to play again or Simultaneous Off
1 Mean Play the sound as many times as asked for, like if your shooting a gun over and over and the sound overlay eachother.

Well not sure if this helps and but my 3 cents worth.

Topknot ;)

Thanks Topknot but what I am asking is really whether the way loopsound works is the way the command is meant to work.

If I load a level, load a sound, set it up as a looped sound, I can play it fine. Then when I finish the level I use freesound to free the sound which was loaded. All good so far. Then on the next level if I decide to load the sound again as soon as I start playing it, it begins to play from where it finished at the point at which the previous level ended.

Given I have freed the sound resource from memory, why if I load it again into memory does it play from where it left off instead of from the start.

I realise I could just use a simple 'playsound' without using the 'loopsound' command and simply detect if the channel is playing or not and then replay the sound again but I just want to find out why loopsound 'remembers' where a piece of music was upto despite the sound being freed from memory and then loaded again.

Well leave it to me to go to the Moon and Back with an answer lol, but changed the original tester code and I can't get the problem you mentioned, even with freeing the sound recource.

; Load Sounds

soundhandle1 = LoadSound("C:\windows\media\Windows XP Startup.wav")


	LoopSound soundhandle1
	soundchn1 = PlaySound(soundhandle1)


While Not KeyHit(1)


; Hit 1 to play Sound Loop 1

	If KeyHit(2)
		StopChannel Soundchn1
		FreeSound soundhandle1
	EndIf

; Hit 2 to play Sound Loop 1 (reloaded)

	If KeyHit(3)
		soundhandle1 = LoadSound("C:\windows\media\Windows XP Startup.wav")		
		LoopSound soundhandle1
		PlaySound (soundhandle1)

	EndIf

	
Wend
End


Hitting 1 shuts it down, and 2 reloads and loops and it plays from the beginning instead of in the middle or such.

So maybe I'm still missing your Loopsound issues completely, but in trying to emulate it I can't see how.

Topknot ;)

I must apologise very much. I must have been hearing things. There is no problem with the loopsound command. My mistake.

Ignore my previous posts.

Sorry about that.