08-04-2022, 10:21 PM
(08-04-2022, 10:05 PM)Pete Wrote: Yeah, QB64 never did bring the SOUND(0, 0) along from QBasic. I mean if you are desperate, you could close the program and shell another instance, using a file to direct the new instance to a different part of the program. That file could also hold and put back any variables into the new instance.
If you run the code, be sure you understand the first line about the file it creates and what you have to name the file or change the SHELL name to get it to work when it gets compiled. Here's what I'm talking about...
Code: (Select All)' Caution, this makes a file called blahblahblah.tmp. ALSO IMPORTANT: You must name and compile this file as untitled(9). or change to another name and use that name in the SHELL call.
IF _FILEEXISTS("blahblahblah.tmp") THEN KILL "blahblahblah.tmp": GOTO 101
' Q: how do you stop a sound playing with the Sound command?
CONST FALSE = 0
CONST TRUE = NOT FALSE
DIM iLoop AS INTEGER
DIM in$
DIM bQuit AS INTEGER
DO
bQuit = FALSE
CLS
PRINT "Start playing some sounds..."
' Quick ascending tone signals that we are starting
FOR iLoop = 400 TO 500 STEP 5: SOUND iLoop, .3: NEXT iLoop
' Middle sound (goes on a long time, how to turn it off?)
FOR iLoop = 1 TO 2000
SOUND 25000 - (iLoop * 10), .1
NEXT iLoop
' Quick descending tone signals that we're done
FOR iLoop = 500 TO 400 STEP -5: SOUND iLoop, .3: NEXT iLoop
PRINT "Finished generating sound."
PRINT "Now, how do we stop it playing or clear the SOUND queue?"
PRINT "WARNING: "
PRINT "Options 1-3 can cause program to freeze or act strangely!"
PRINT "1. Try stopping the sound with SOUND 0,0"
PRINT "2. Try stopping the sound with SOUND 37,1"
PRINT "3. Try stopping the sound with BEEP"
PRINT "4. Play it again"
PRINT "5. Quit"
DO
in$ = INKEY$
IF in$ = "1" THEN
OPEN "blahblahblah.tmp" FOR APPEND AS #1: CLOSE #1
SHELL _DONTWAIT "untitled(9).exe"
STOP
ELSEIF in$ = "2" THEN
SOUND 37, 1
ELSEIF in$ = "3" THEN
BEEP
END
ELSEIF in$ = "4" THEN
bQuit = FALSE: EXIT DO
ELSEIF in$ = "5" OR in$ = "q" THEN
bQuit = TRUE: EXIT DO
END IF
LOOP
IF bQuit = TRUE THEN EXIT DO
LOOP
101
PRINT "Ha! It worked!"
SLEEP
So the part that sucks is you momentarily lose your program window. I mean the whole approach sucks, but losing the window really sucks. Now if @Spriggsy swings by, he might know of a way using Windows API to kill the sound, but if you are on Linux, that won't help you.
@Dav works a lot with sound, but I don't know if he also works on Linux, or just Windows.
Hopefully someone else has a better idea than mine. It will work, but it's ugly.
Pete
Thanks for your reply!
Oy vey, the workaround you mention would not work for me, because I just want to stop the sound during gameplay in a video game, so we cannot kill the window (not to mention that would be the biggest kludge ever, LoL!)
I think the solution for me will be to simply record the audio from that code as a sound file and play it back with _SNDPLAY. It's OK if it's a static WAV file, the sound parameters don't vary.
I do think finally adding SOUND(0, 0) to QB64 would make SOUND more useful in a lot of cases. I'm not sure how hard it would be - all that needs to be done is when there's a SOUND(0, 0) command, just shut down whatever audio is queued for SOUND...
Thanks again!