Idea for possible implementation of advanced sound support in QB64

by (no login)

 
QB64 SOUND SUPPORT CONCEPT

Important facts:
*Multiple sounds can be played at once
*ID numbers/handles can be used to manage a loaded/playing sound
*Secondary handles (optionally created) when you play a sound are automatically cleared/freed when the playing finishes or is stopped
*Calling CLEARSOUND will work while the sound is playing but the actual sound data won't be cleared until the actual raw sound data is no-longer needed

'Level 1 example:
_PLAYSOUNDFILE "mysound.mp3"

'Level 2 example:
handle=_LOADSOUND("mysound.mid")
_SOUNDVOLUME handle, 0.5 'Set volume to 50%
_PLAYSOUND handle
_CLEARSOUND handle
DO: LOOP WHILE _SOUNDPOS(handle)<_SOUNDLENGTH(handle)
PRINT "Finished playing MIDI file!"

'Level 3 example:
handle=_LOADSOUND("mysound.wav")
playinghandle=_PLAYSOUND(handle)
_CLEARSOUND handle
SLEEP 1
_STOPSOUND playinghandle

'Level 4 example:
handle=_LOADSOUND("mysound.mp3")
_SOUNDBALANCE handle, -1 'start with left speaker only
playinghandle=_PLAYSOUND(handle)
_CLEARSOUND handle
FOR i=-1 TO 1 STEP .1
_SOUNDBALANCE playinghandle, i 'slowly fade from left to right
SLEEP 1
NEXT
_SOUNDPAUSE playinghandle
DO: LOOP WHILE INKEY$=""
_PAUSESOUND playinghandle
DO: LOOP WHILE INKEY$=""
_STOPSOUND playinghandle

'Level 5 example:
'A swarm of angry bees leave their hive!
numbees=10
TYPE beestructure
sound AS LONG
posx AS SINGLE
posy AS SINGLE
posz AS SINGLE
END TYPE
DIM bee(1 TO 100) AS beestruct
handle=_LOADSOUND("buzzing.mp3")
INPUT "3D HIVE POSITION (X,Y,Z):";hivex,hivey,hivez
_SOUNDBALANCE handle, hivex, hivey, hivez
_LOOPSOUND handle
FOR i=1 TO 100
bee(i).posx=hivex
bee(i).posy=hivey
bee(i).posz=hivez
NEXT
FOR i=1 TO 100
bee(i).sound=_PLAYSOUND(handle);
NEXT
_CLEARSOUND handle 'don't need to use this file again
DO
LOCATE 1,1: PRINT "Bee #1 has been buzzing for "; _SOUNDPOSITION(bee(1).sound);"Milliseconds"
FOR i=1 TO 100
'move each bee randomly
bee(i).posx=bee(i).posx+rnd*0.2-0.1
bee(i).posy=bee(i).posy+rnd*0.2-0.1
bee(i).posz=bee(i).posz+rnd*0.2-0.1
'if bee is too far away, use this playing sound to simulate a new bee leaving the hive
IF ABS(bee(i).posx)+ABS(bee(i).posy)+ABS(bee(i).posz)>100 THEN
bee(i).posx=hivex
bee(i).posy=hivey
bee(i).posz=hivez
END IF
_SOUNDBALANCE bee(i).sound, bee(i).posx, bee(i).posy, bee(i).posz
NEXT
LOOP

Posted on Jan 24, 2008, 3:12 AM

Respond to this message   

Return to Index

Response TitleAuthor and Date
Oops... Level 2 example has an error! on Jan 24

 Copyright © 1999-2014 Network54. All rights reserved.   Terms of Use   Privacy Statement