Make Sounds (Beep) with C++

make sounds (beep) with c++

Print the special character ASCII BEL (code 7)

cout << '\a';

Source

How to make a Beep sound in C on Windows?

The C standard recommends that writing '\a' to standard output produce an audible or visible alert signal, but it will not work if standard output is redirected. Likewise, some newer computers lack the PC beeper on which Windows Beep() and some terminals rely. To cause a Windows PC to play an alert sound in a desktop application, you can call the Windows-specific MessageBeep function, which plays a sound "asynchronously" (in the background while your program continues to run). The user can configure which sound is associated with each of these four values in the Sound control panel.

#include <windows.h>

/* Include one of these in a function */
MessageBeep(MB_OK); /* play Windows default beep */
MessageBeep(MB_ICONINFORMATION); /* play asterisk sound */
MessageBeep(MB_ICONQUESTION); /* play question sound */
MessageBeep(MB_ICONWARNING); /* play warning sound */

MessageBeep() is defined in User32.dll, so if this gives you link errors, make sure you're linking to the corresponding import library. In MinGW GCC (the compiler in Code::Blocks), add -lUser32 to the list of libraries passed to the linker.

How can I make the computer beep in C#?

In .Net 2.0, you can use Console.Beep.

// Default beep
Console.Beep();

You can also specify the frequency and length of the beep in milliseconds.

// Beep at 5000 Hz for 1 second
Console.Beep(5000, 1000);

Using multithreading to get continuous beep sound

Your first example fails to compile because you are actually calling beepTone() and then trying to pass its void return value to the start_address parameter of _beginthread(), which will not work. You need to pass beepTone() itself to that parameter, not its return value.

Your second example is correctly passing beepTone() itself to _beginThread(), but is not passing any data to beepTone().

Now, to accomplish what you want, _beginthread() has an arglist parameter that you can use to pass user data to your thread function. That is what you need to use to send your beep values to the thread so it can then pass them to Beep().

Try something like this:

#include "stdafx.h"
#include <Windows.h>
#include <process.h>
#include <iostream>

using namespace std;

struct beepParams
{
int freq;
int mil;
};

void __cdecl beepTone(void *arg)
{
beepParams *params = static_cast<beepParams*>(arg);
Beep(params->freq, params->mil);
delete params;
_endthread();
}

int main()
{
int freq = 0, sec = 0, mil = 0;

cout << "Frequency?" << endl;
cin >> freq;
cout << "Duration?" << endl;
cin >> sec;
mil = 1000 * sec;

beepParams *params = new beepParams;
params->freq = freq;
params->mil = mil;

if (_beginthread(&beepTone, 0, params) == -1)
delete params;

cout << "Test Threading";
//...
cin.get();

return 0;
}

That being said, if you are using C++11 or later, consider using std::thread instead:

#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <thread>

using namespace std;

struct beepParams
{
int freq;
int mil;
};

void beepTone(beepParams params)
{
Beep(params.freq, params.mil);
}

int main()
{
int freq = 0, sec = 0, mil = 0;

cout << "Frequency?" << endl;
cin >> freq;
cout << "Duration?" << endl;
cin >> sec;
mil = 1000 * sec;

beepParams params;
params.freq = freq;
params.mil = mil;

thread t(beepTone, params);
t.detach();

cout << "Test Threading";
//...
cin.get();

return 0;
}


Related Topics



Leave a reply



Submit