GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Sep-2008, 13:23
vicky_brsh vicky_brsh is offline
New Member
 
Join Date: Dec 2007
Posts: 25
vicky_brsh has a little shameless behaviour in the past

How to invoke system beep in Linux


Hi All,

I want to invoke the system beeper from Linux OS after executing few instructions in the code.

Example

CPP / C++ / C Code:
#include <iostream.h>
         using namespace std;

          int main()
          {
             cout << "Hello World" << endl;
             cout << "Hello World" << endl;
          
               Beep() // Dont know which system call to use

               cout << "Hello World" << endl;
             cout << "Hello World" << endl;
              return 1;
             }

Can any one help me to know whether there is any system call available to do so?
Last edited by admin : 04-Sep-2008 at 19:14. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 04-Sep-2008, 13:43
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to invoke sytem beep in Linux


Quote:
Originally Posted by vicky_brsh
I want to invoke the system beeper from Linux OS

The simplest thing that I can think of is to put the alarm special character '\a' to stdout. Depending on your system setup (what terminal program you are using and how it is configured), you just might hear it. See Footnote.

CPP / C++ / C Code:
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    const char BeepChar = '\a'; /* the "alarm" special char */
    cout << "Maybe you can hear a beep here---" << BeepChar << endl;
    cout << "The beep char (\\a) is 0x" 
         << hex << setfill('0') << setw(2) 
         << int(BeepChar)
         << endl;
    return 0;
}

Output on my Centos system, using gnome-terminal with default installation setup:

Code:
Maybe you can hear a beep here--- The beep char (\a) is 0x07

And, Indeed, I did hear a beep.

It works for me, but since system hardware behavior is implementation-dependent, I hedge with my usual warning: Your Mileage May Vary.


Regards,

Dave

Footnote: 0x07 is the keyboard equivalent to Ctrl-G. In ASCII tables, it's sometimes called "BEL" for "bell." Old mechanical teletype machines actually had a little bell, kind of like a bicycle bell, that was struck by a hammer when they received the BEL character. The idea was that a teletype operator (usually working alone in a small compartment in the bowels of a ship or some other isolated communications center) could could take a little nap, and when an important message came in, it might be preceded by one or more BEL characters to awaken him/her.

The C standard (C++, too) defines '\a' as a special literal constant, but doesn't guarantee what its actual numerical value is. Also, the standards writers and compiler writers have no idea of whether your system will do anything special when a program puts '\a' to stdout, but the idea is that maybe, just maybe, the implementation-dependent part of the run-time library that gets linked into your program will so something meaningful if the hardware supports it.
  #3  
Old 04-Sep-2008, 15:13
vicky_brsh vicky_brsh is offline
New Member
 
Join Date: Dec 2007
Posts: 25
vicky_brsh has a little shameless behaviour in the past

Re: How to invoke sytem beep in Linux


Hi Dave,

Thank you for quick reply.

I tried printing '\a' to the STDOUT. But, i am not able to hear the beep.

Can you please let me know if there is anything i shud do to enable the beep.

I am using Suse 10 & 11 for testing of this functionality. Or even if it is on windows it is OK.


Thanks & Regards
Vikram
  #4  
Old 04-Sep-2008, 15:31
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to invoke sytem beep in Linux


Quote:
Originally Posted by vicky_brsh
...I tried printing...

If you compiled and executed my test program and heard nothing, then there's not much else that you can do with C or C++. It's a Hardware thing. Or an Operating System Sound System Setup Thing. Or something outside the control of C/C++.

I don't have Suse. I have never had Suse. I don't imagine that I will ever have Suse (but it's possible, some day).

Therefore, I can't walk you through sound setup stuff.


That's what I meant by "Your Mileage May Vary."

Sorry,

Dave
  #5  
Old 04-Sep-2008, 18:54
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to invoke sytem beep in Linux


Quote:
Originally Posted by vicky_brsh
...if it is on windows it is OK...

I don't know if this helps but: That little test program creates beeps on my Windows XP platform when I compile it with various Borland bcc32.exe compilers and with various Microsoft Visual C++ compilers, but not with a cygwin/g++ compiler.

YMMV (but I said that already).

Regards,

Dave
  #6  
Old 05-Sep-2008, 00:03
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: How to invoke system beep in Linux


In Fedora 6 - fluxbox and gnome xterms and the tty console I'm getting a beep from either of these:
CPP / C++ / C Code:
  cout.put(0x07);     // BeepChar
  cout.put('\a');     // BeepChar
  cout << '\a';       // BeepChar 
  cout << (char)0x07; // BeepChar
  #7  
Old 07-Sep-2008, 15:01
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to invoke sytem beep in Linux


Quote:
Originally Posted by davekw7x
I don't know if this helps but: That little test program creates beeps on my Windows XP platform when I compile it with various Borland bcc32.exe compilers and with various Microsoft Visual C++ compilers, but not with a cygwin/g++ compiler

An interesting update (interesting to me, that is): I don't routinely have speakers attached to my Windows PC, and I don't routinely listen to the headset plugged into my sound card.

Why is that interesting to me?

Well, as I mentioned, when I compile the program with various Borland and Microsoft compilers on my Windows XP platform, it plays the "beep" through the crummy little sound device on the motherboard.

On the other hand:

When I compile the little test program with GNU compilers on my XP box, it uses the sound card, and plays the system "Default Beep," which is the "Windows XP Ding.wav" file in the Windows\media directory. Since I wasn't listening to the headset when I ran the tests, I didn't hear it.

That emphasizes the reason that I keep repeating my mantra for non-standard and, therefore, implementation-dependent, functions and functionality (all together now): Your Mileage May Vary.

Regards,

Dave

Footnote: How the heck did they do that? Is it just possible that the cygwin port of the GNU tools is, in some respects, more sophisticated than "native" Microsoft tools? My flabber is, like, totally gasted!
Last edited by davekw7x : 07-Sep-2008 at 15:34.
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[HOWTO] Belkin 54G Wireless card in linux (Broadcom chip) dsmith Computer Software Forum - Linux 83 16-May-2008 14:17
How To Remove Linux And Install Windows? rockaway Computer Software Forum - Windows 3 06-Mar-2008 22:00
Help Old Computer Linux Network Planning 6795 Computer Software Forum - Linux 1 09-Dec-2007 22:56
Question regarding EditBox control! gopikomanduri MS Visual C++ / MFC Forum 0 12-Jun-2007 07:10
Linux Kernel Upgrade Mini Howto dsmith Computer Software Forum - Linux 3 05-Apr-2004 23:10

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:48.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.