How to play sound at the same time as the program? - c++

I want to play sound while the program is running in c++. I am using the code:
void fighterplanesound()
{PlaySound(TEXT("bombdrop.wav"), NULL, SND_FILENAME|SND_ASYNC);}
I thought the SND_ASYNC flag would fix this problem but it did not. What is happening is the program will pause or control will not move on until the sound is done playing. How can I play the sound while the program continues to run. To clarify, the program is a Windows console program that operates in an MS-DOS window.

How about playing the sound in a different thread?

Ok. What I figured out was that you can't include the PlaySound method in the function because even though the ASYNC flag was there, control had to fully execute the PlaySound method before returning, so don't put it in the function unless there is more code in the function(in which case the sound will play while that code is executing) or in other words, control will not return from the function until PlaySound is finished executing and because there is no other code in the function to execute, control waits until PlaySound method is finished and then moves on.

Actually i had the same problem on my iOS app. You know the time that how long does it take to play your .wav file. You can create a Timer that calls your fighterplanesound() method each period of ending time of your file. Not the best solution but it works for me.

Related

Force foreground processing in WindowsAPI

I have an executable program that performs latency measurements. C++ pseudo-code below:
void main(){
lock_priority();
start_measurements();
work();
end_measurements();
}
The work() creates multiple threads and takes a long time to complete, so ideally I'd like to minimize the executable console when the process is running, just to save screen space. This, however, reduces the output latency by around 50% compared to when not minimized.
I'd like to implement the lock_priority() function so that even when minimized, the process does not go into PROCESS_MODE_BACKGROUND_BEGIN mode.
What I've tried so far
SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS); - did not work
Created a thread that every few seconds calls the function above - it did work, but, scientifically speaking, "it looks ugly"
I have tried to find a method to attach a callback to the SetPriorityClass() function so that after it finishes if the PriorityClass was anything but REALTIME_PRIORITY_CLASS, it'd re-set it again (or at least set PROCESS_MODE_BACKGROUND_END priority). This sounds like a perfect solution, but I could not find anything in the docs about this.
I discovered there is a way to set the processor to prefer foreground/background tasks (reference) - however even if this was possible to be configured through code, I still need a way to bind this theoretical function to the priority change.
Any help would be very appreciated!
How about redirecting the programm output from console to a file or just buffer it, like here:
Redirect both cout and stdout to a string in C++ for Unit Testing
This way, you don't have any console latency at all - if this is alright for your testing.

How to start/stop wav-sounds under C++ Windows 7

I tried hard to find a solution for the follwing requirement under C++/Visual Studio:
It's simple: For purpose of alarming I have to play a number of wav sounds from files sequentially in an endless loop, each separated by a short delay:
sound-1 ...sound-2 ... sound-n ... sound-1 ... sound-2 ...
At any time it should be possible to cancel this sequence of sounds.
I tried to play around with PlaySound(), but the problem is, that I cannot stop the sounds properly once they are running.
https://msdn.microsoft.com/en-us/library/Dd743680(v=VS.85).aspx
When a sound is played asynchronously by flag, it can be stopped by specifying a NULL for parameter 1, but then I can't know when sound-1 is finished before sound-2 is started. In fact, starting sound-2 would interrupt sound-1.
In synchroneous mode however, interruption of a sound is not possible.
To be able to cancel sounds I tried to put PlaySound into a Poco::Thread, but how to cancel a thread? It is not foreseen to cancel a thread...Using Poco::Task instaed gives an unhandled exception at 0x01002d62 ... upon cancelling, so it doesn't work either.
What would be the preferred solution for that?
Is there a better solution?
Thanks!
They're all WAV files, you know how long they last. When you start playing sound1, store the time it will end. There's no need to guess.

Is windows.h Sleep(int) function wasteful?

I am now trying to write a program that waits two minutes and 25 seconds in C++. I use the Sleep function of like that:
Sleep(145000);
Now, my laptop heats up every time I run this function, and the fan starts working.
Now to the question - is this function known for being wasteful? Should I even use it? do I have a better option?
The Windows Sleep() function puts the current thread to sleep. It doesn't run a busy-waiting while-loop or anything like that, it just re-schedules the thread to start up again after the sleep period specified as the function parameter. If your fan is starting up, I suggest looking at the currently running processes using Task Manager.

Exiting a program outside main function

I m making a project with the allegro game library ,when the x button is clicked in the game
a function will be called ,i could then do std::exit(0) or the built in exit function of allegro "allegro_exit()" but my program won't exit well and it will be still open but not responding because i didn't return from the main function.
the only chance if i have is to do a while loop in the main function that performs stuff when the x button isn't clicked ,after the loop is done i could normally return from the main function,
however i don't think the while loop is good idea and it won't fit my program.
any chance of doing this in another way?
std::exit() function does all necessary cleanups and terminates a program in a normal way. If your program freezes and doesn't respond then consider to carefully review the code that is responsible for resources releasing (destructors etc.). Some libraries can react to exit function in some strange way so take this case in account. You can read more about std::exit() here.
Don't use std::abort() for normal program termination, because it is not aimed to be used for that and it doesn't cleanup after your application.
Anyway, I recommend you to review the architecture of your project and provide a return to the main function after all cleanups. while loop is ok, because it must exist somewhere to wait for user input.

How to use the playsound function multiple times?

PlaySound works perfectly fine if i need a single beep. The following illustrates my code snippet:
PlaySound(TEXT("C:\\Test1.wav"), NULL, SND_ASYNC);
My question is, how to use this function twice or more than twice, since it plays only once regardless of the amount of repetition I put together. I have also tried this:
PlaySound(TEXT("C:\\Test1.wav"), NULL,SND_FILENAME|SND_LOOP|SND_ASYNC);
which plays the beep in a continuous loop. How to play this twice or thrice etc.?
Using a loop doesn't help either.
You have two options with that API. Either play synchronous, or asynchronous.
If you play synchronous, then your thread will block until the sound has finished playing. You can put it in a loop and call it as many times as you like, but then your thread will block whilst the sound is being played.
If you play asynchronous, then your thread will not block but you don't have any reliable way of controlling the number of repetitions.
I see two solutions for you: either create a new sound from the original, with the correct number of repeats added and play that once, or create a background thread to play the sound the specified number of times - in this background thread you can safely use the synchronous play method as described above.
The problem is the SND_ASYNC flag. It says that you want this call to return immediately and play the sound in background.
If you do this in a loop, since it will return with the sound still playing, it will overlap with the next call and do nothing, because IIRC, an application can only do one call to PlaySound at the same time.
The solution is to replace SND_ASYNC with SND_SYNC (or remove it, since it is the default). If you need the call to be asynchronous, you can create a thread and equeue the sounds you want to play. The thread will play synchronously, but your other threads won't notice.