Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to play a system beep on Mac OS?

Tags:

c++

audio

beep

Is there a way to play a system beep on Mac OS using C++ and Xcode? I understand that I need to use a library. Is there a library that works across both the Mac and Windows platforms?

like image 300
Moshe Avatar asked Sep 13 '11 21:09

Moshe


2 Answers

I think you probably want to use NSBeep


NSBeep

Plays the system beep.

#include <AppKit/AppKit.h>

void NSBeep (void);

This seems to work OK for a command line tool:

#include <AppKit/AppKit.h>
#include <iostream>

using namespace std;

int main(void)
{
    cout << "Hello world !" << endl;
    NSBeep();
    sleep(1)
    return 0;
}

$ g++ -Wall -framework AppKit beep.cpp -o beep
$ ./beep

Update May 2021

While this solution worked in 2011, it seems that AppKit is now no longer C++-compatible, so you now need to treat the file as Objective-C++, i.e. rename beep.cpp to beep.mm.

like image 156
Paul R Avatar answered Oct 05 '22 09:10

Paul R


The cross platform way to play a beep is std::cout << "\007";. I had been trying to play it by passing in a char and then decrementing until 7. That didn't work. Explicitly outputting the code did work though.

like image 34
Moshe Avatar answered Oct 05 '22 07:10

Moshe