playsound

Previous topic - Next topic

SamB

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 :

Code (glbasic) Select
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

Moru

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 :-)

SamB

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

Moru

That's ok, there is a solution for that too. See this post: KeyHit

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.

SamB

ok
it was just logic. here's is my final code without using function ( too complicated for the noob i'm for the moment :)

Code (glbasic) Select

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

Kitty Hello

Did you know, that PLAYSOUND returns a handle?

Code (glbasic) Select

handle = PLAYSOUND(1,1,1)

WHILE ISSOUNDPLAYING(handle)
    PRINT "I'm waiting...",0,0
    SHOWSCREEN
WEND



Schranz0r

All functions with () return anything !

Like:

MySound = PLAYSOUND(0,0,1)
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard