08-04-2022, 06:52 PM
If we create a long sound using SOUND inside a loop, the loop ends quickly but the sound keeps on playing in the background.
So it seems that whenever the SOUND command is called, it just adds the current frequency/duration to a queue, which then keeps playing in the background, without holding up the code.
The problem I'm having is, how do you make it stop early?
If there is no command for this, then maybe it should be added?
The below program demonstrates the issue and the strange behavior I get when trying to stop the sound with different workarounds...
Any help appreciated!
So it seems that whenever the SOUND command is called, it just adds the current frequency/duration to a queue, which then keeps playing in the background, without holding up the code.
The problem I'm having is, how do you make it stop early?
If there is no command for this, then maybe it should be added?
The below program demonstrates the issue and the strange behavior I get when trying to stop the sound with different workarounds...
Any help appreciated!
Code: (Select All)
' 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
Print "Now, how do we stop it playing or clear the SOUND queue?"
Print
Print "WARNING: "
Print "Options 1-3 can cause program to freeze or act strangely!"
Print
_KeyClear: While InKey$ <> "": Wend ' Clear the keyboard buffer
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"
Print
Input "Your choice"; in$: in$ = LCase$(_Trim$(in$))
Do
If in$ = "1" Then
Sound 0, 0
ElseIf in$ = "2" Then
Sound 37, 1
ElseIf in$ = "3" Then
Beep
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