hi,
I'm writing a little app that plays sounds when you hit a key.
I've a problem finding what is the right way to let a sound being played two times quicky.
here's my source code :
LOADSOUND "gc.wav", 0, 1
WHILE TRUE
IF KEY(57) = 1
PLAYSOUND (0,0,1)
ENDIF
SHOWSCREEN
WEND
the sound sample length is 1 second
if i press the key (spacebar is this case) faster than 1s, I've to wait the file being completly read before it can be played a second time.
if in insert a "hush" before the playsound instruction, the file is played like a pneumatic drill
Hi :-)
Try raising the value at the end of your LOADSOUND to 4. This creates more buffers for your sound to play from so you can play more than one sound at the same time.
I haven't tried it but that is what I can understand from the help-file.
Also when you post code, use the button with a # on it in the editor, that way it looks much nicer :-)
thanks it works, i can play the sound without waiting after his end. The other problem is about the key press.
Until you release the key, it send the information to play the sound ( pneumatic drill effect ), so i've to press extremely fast and it's not really usable
That's ok, there is a solution for that too. See this post: KeyHit (http://www.glbasic.com/forum/index.php?topic=679.0)
The basic ide is that you should save the state from the last keypress for each key. If it was already 1 when you come during the second loop then you don't play the sound because you know the user already pressed the button.
When you get 0 back from the key() command you know the button was depressed so you reset the state of that button so you can use it again.
ok
it was just logic. here's is my final code without using function ( too complicated for the noob i'm for the moment :)
LOADSOUND "gc.wav", 0, 2
gc_state=1
WHILE TRUE
IF KEY(57) = 1 AND gc_state=1
PLAYSOUND (0,0,1)
gc_state=0
ELSEIF KEY(57)=0
gc_state=1
ENDIF
SHOWSCREEN
WEND
Did you know, that PLAYSOUND returns a handle?
handle = PLAYSOUND(1,1,1)
WHILE ISSOUNDPLAYING(handle)
PRINT "I'm waiting...",0,0
SHOWSCREEN
WEND
All functions with () return anything !
Like:
MySound = PLAYSOUND(0,0,1)