how do you stop a sequence of sounds created with the Sound command? - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10) +---- Thread: how do you stop a sequence of sounds created with the Sound command? (/showthread.php?tid=729) |
how do you stop a sequence of sounds created with the Sound command? - madscijr - 08-04-2022 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! Code: (Select All) ' Q: how do you stop a sound playing with the Sound command? RE: how do you stop a sequence of sounds created with the Sound command? - Pete - 08-04-2022 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. 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 RE: how do you stop a sequence of sounds created with the Sound command? - madscijr - 08-04-2022 (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. 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! RE: how do you stop a sequence of sounds created with the Sound command? - SpriggsySpriggs - 08-05-2022 I will have to read through this forum thread and see what's going on but if I'm able to be of any assistance then I'll see what I can do. RE: how do you stop a sequence of sounds created with the Sound command? - madscijr - 08-05-2022 (08-05-2022, 02:25 AM)Spriggsy Wrote: I will have to read through this forum thread and see what's going on but if I'm able to be of any assistance then I'll see what I can do. Hey, long time no see! I decided I can use a WAV file in this case, but implementing SOUND(0,0) to stop playing sound immediately & clear the buffer, would help make the SOUND command more useful. RE: how do you stop a sequence of sounds created with the Sound command? - SpriggsySpriggs - 08-05-2022 This appears to be quite the conundrum. However, there is a definite way to do background music in a loop that can be killed whenever you want. You'd need to either open up a WAV file and then just allow it to continue playing using SNDLOOP until a certain key is pressed OR you can do something using the PlaySound function from Win32. You'd still need to use a WAV file OR learn how to make the WAVE resource in memory and pass it to the function. The function can run asynchronously and then you can kill the sound whenever you want. I've never messed with the SOUND function before but maybe you should look into SNDOPENRAW, SNDRAW, and SNDCLOSE instead since you are working with frequencies and such. Those would allow you to kill the sound whenever without having to do some hacky thing with the C backend, Win32, threading, or premade WAV files. RE: how do you stop a sequence of sounds created with the Sound command? - madscijr - 08-05-2022 (08-05-2022, 03:09 AM)Spriggsy Wrote: This appears to be quite the conundrum. However, there is a definite way to do background music in a loop that can be killed whenever you want. You'd need to either open up a WAV file and then just allow it to continue playing using SNDLOOP until a certain key is pressed OR you can do something using the PlaySound function from Win32. You'd still need to use a WAV file OR learn how to make the WAVE resource in memory and pass it to the function. The function can run asynchronously and then you can kill the sound whenever you want. For now I'm going to just record it as a digital audio file and use playsound. I saw an example of SNDRAW but had questions, got busy, etc. Need to finally absorb the WAV file specification. That said, why have a SOUND command if you can't stop it? I hope they add "support for SOUND(0,0) to stop sounds from playing" onto the backlog. UPDATE: Here's a thought... The SOUND command is easy. That is in the spirit of BASIC. So why not create a function that emulates the SOUND command using SNDRAW etc.? Then we can support SOUND(0,0) ourselves, within that function! You could even extend it with all sorts of commands / functions that do things like change the waveform, set the volume and panning, etc. Are you comfortable enough with SNDRAW to create a PLAYSOUND procedure? RE: how do you stop a sequence of sounds created with the Sound command? - Dav - 08-05-2022 I had a conversation about this at the old forum once, and if I remember right the answer is that once sound data is sent to the buffer with PLAY, SOUND, _SNDRAW then it has to play out, can’t stop it. I was asking at the time if there’s a way to detect when a PLAY statement was finished playing, and there wasn’t a way, in pure QB64 that is. - Dav RE: how do you stop a sequence of sounds created with the Sound command? - SMcNeill - 08-05-2022 We see the same thing in other places as well. Netflix can start playing a movie preview with the sound on, you can close the tab Netflix is in -- or even close your browser! -- and you'll still hear sound until the buffer plays out and finishes. Once it's in the buffer, I don't know of any way to get it out once it starts playing. It tends to have to just run its course. RE: how do you stop a sequence of sounds created with the Sound command? - bplus - 08-05-2022 But I think you CAN control how long Sounds are made, so don't make next sound until ready for it, be a clock watcher. |