45

How to make the hardware beep sound with c++?

cigien
  • 57,834
  • 11
  • 73
  • 112
user319854
  • 3,980
  • 14
  • 42
  • 45
  • toot is a cross-platform C file that try to call several sound generators to produce the beep. http://github.com/vareille/toot – renataflow Nov 11 '17 at 06:13

12 Answers12

74

Print the special character ASCII BEL (code 7)

cout << '\a';

Source

usr-local-ΕΨΗΕΛΩΝ
  • 26,101
  • 30
  • 154
  • 305
69

If you're using Windows OS then there is a function called Beep()

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

using namespace std;

int main() 
{ 
    Beep(523,500); // 523 hertz (C5) for 500 milliseconds     
    cin.get(); // wait 
    return 0; 
}

Source: http://www.daniweb.com/forums/thread15252.html

For Linux based OS there is:

echo -e "\007" >/dev/tty10

And if you do not wish to use Beep() in windows you can do:

echo "^G"

Source: http://www.frank-buss.de/beep/index.html

Johan Råde
  • 20,480
  • 21
  • 73
  • 110
RobertPitt
  • 56,863
  • 21
  • 114
  • 161
  • For the last one, this does NOT work when I enter `^` and `G`. It only works when pressing `Ctrl+G`. Even though the strings look the same when entered, they are different and are also printed differently. – Felix Dombek Jul 16 '19 at 12:03
  • 1
    `Beep()` function plays sound via speakers, not through motherboard's physical buzzer. According to microsoft website : "because of the lack of hardware to communicate with, support for Beep was dropped in Windows Vista and Windows XP 64-Bit Edition. In Windows 7, Beep was rewritten to pass the beep to the default sound device for the session" – 0xB00B Jun 22 '21 at 15:49
  • that's a beautiful beep – ycomp Dec 16 '22 at 15:26
8

There are a few OS-specific routines for beeping.

  • On a Unix-like OS, try the (n)curses beep() function. This is likely to be more portable than writing '\a' as others have suggested, although for most terminal emulators that will probably work.

  • In some *BSDs there is a PC speaker device. Reading the driver source, the SPKRTONE ioctl seems to correspond to the raw hardware interface, but there also seems to be a high-level language built around write()-ing strings to the driver, described in the manpage.

  • It looks like Linux has a similar driver (see this article for example; there is also some example code on this page if you scroll down a bit.).

  • In Windows there is a function called Beep().

asveikau
  • 39,039
  • 2
  • 53
  • 68
  • '\a' is defined by the C++ standard, and is extremely portable. Of course if you're using broken terminal software all bets are off, but the Win32 console subsystem and most xterm clones all process '\a' properly. – Ben Voigt Oct 30 '10 at 20:48
  • 7
    @Ben Voigt: Correct me if I'm wrong, but the C++ standard only specify that '\a' will represent an ASCII BEL character; but it never specifies what the programs' behavior should be when sending such character to stdout. The part that ASCII BEL == '\a' is extremely portable, as you said, but the beeping part is a totally undefined behavior. – Lie Ryan Oct 30 '10 at 21:27
  • 1
    @Ben: as far as I'm concerned, terminal software is broken if it *doesn't* have a way of switching off the bell. – Steve Jessop Oct 30 '10 at 21:52
  • @Steve: I agree, but I don't read this question is "How do you make a beep when the users has explicitly turned sounds off?" – Ben Voigt Oct 30 '10 at 23:30
5

alternatively in c or c++ after including stdio.h

char d=(char)(7);
printf("%c\n",d);

(char)7 is called the bell character.

Namit Sinha
  • 1,415
  • 3
  • 16
  • 30
5

You could use conditional compilation:

#ifdef WINDOWS
#include <Windows.h>
void beep() {
  Beep(440, 1000);
}
#elif LINUX
#include <stdio.h>
void beep() {
  system("echo -e "\007" >/dev/tty10");
}
#else
#include <stdio.h>
void beep() {
  cout << "\a" << flush;
}
#endif
ndrewxie
  • 172
  • 3
  • 10
  • 2
    It is better to let LINUX part to be flexible with frequency and time too, using 'system("beep -f 5000 -l 50 -r 2") ' ( see https://wiki.archlinux.org/index.php/PC_speaker ) – Vit Apr 01 '17 at 16:35
4
std::cout << '\7';
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
3
#include<iostream>
#include<conio.h>
#include<windows.h>
using namespace std;

int main()
{

    Beep(1568, 200);
    Beep(1568, 200);
    Beep(1568, 200);
    Beep(1245, 1000);
    Beep(1397, 200);
    Beep(1397, 200);
    Beep(1397, 200);
    Beep(1175, 1000);

cout<<endl;
_getch()

return 0
}
Muhammad Usman
  • 1,366
  • 5
  • 18
  • 33
Krys
  • 31
  • 1
3

Here's one way:

cout << '\a';

From C++ Character Constants:

Alert: \a

Ani
  • 111,048
  • 26
  • 262
  • 307
3

I tried most things here, none worked on my Ubuntu VM.

Here is a quick hack (credits goes here):

#include <iostream>
int main() {
  system("(speaker-test -t sine -f 1000)& pid=$!; sleep 1.0s; kill -9 $pid");
}

It will basically use system's speaker-test to produce the sound. This will not terminate quickly though, so the command runs it in background (the & part), then captures its process id (the pid=$1 part), sleeps for a certain amount that you can change (the sleep 1.0s part) and then it kills that process (the kill -9 $pid part).

sine is the sound produced. You can change it to pink or to a wav file.

Rafael
  • 7,002
  • 5
  • 43
  • 52
2

Easiest way is probbaly just to print a ^G ascii bell

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
0

The ASCII bell character might be what you are looking for. Number 7 in this table.

ubiquibacon
  • 10,451
  • 28
  • 109
  • 179
0
cout << "\a";

In Xcode, After compiling, you have to run the executable by hand to hear the beep.

dams
  • 65
  • 1
  • 9