Running Commands in Main Simultaneously

Hello,

I created the following program that runs each Beep() successfully, but one at a time. How can I run all the Beep commands at once?

#include <iostream>
#include <windows.h> // WinApi header

using namespace std;

int main() {
{
Beep(397,10000);
Beep(431,10000);
Beep(464,10000);
Beep(497,10000);
Beep(531,10000);
Beep(565,10000);
Beep(598,10000);
Beep(632,10000);
Beep(665,10000);//

//cin.get();
return 0;
}
}
In general, you could use std::async to have each command be performed asynchronously.
However, your case is much more specific; I think Windows itself will limit it so that subsequent Beep() commands overwrite the previous Beep() commands playing, even if Beep() is played over 2 different processes at once.

I assume you want a superposition of all of those pure frequencies to be played for 10 seconds? You might have better luck with PlaySound (requires linking with winmm, "windows multimedia"). For example, you could independently write and embed/load a WAVE file and have PlaySound play it. If you want to be even fancier, you could generate the WAVE file header and waveform data in-memory and then call PlaySound with a pointer to the data to avoid having to make a .wav file in the first place.

https://stackoverflow.com/questions/32320825/load-wavs-into-memory-then-play-sounds-asynchronously-using-win32-api

There are also other third-party multimedia libraries you could use to load and play sounds, such as SFML.
https://www.sfml-dev.org/tutorials/2.5/audio-sounds.php
Last edited on
Even if windows did allow it to beep in parallel (I agree, it prob does not allow) I think threading it is going to fail apart from say testing / playing code. I mean if you beeped them all at once for 2 seconds duration, it would sort of work but if you tried to just beep them for a milisecond, they would likely not all happen exactly at the same time unless you added additional code in the threads to go at a specific time slice; it may be very challenging to do that in a way that the human ear believes they were all in synch.
You are correct, the beep isn't allowed in parallel. Thanks for the input, actually will try a few things. Appreciate the responses.

Best,
dgdg
Topic archived. No new replies allowed.