Results 1 to 8 of 8

Thread: beep in c++

  1. #1
    Join Date
    May 2009
    Beans
    1

    Smile beep in c++

    Is there a easy way to get a beep or to play a small soundfile with c++?

    I have been searching on google for a answer but haven't found one.

    /Martin

  2. #2
    Join Date
    Oct 2008
    Beans
    561

    Re: beep in c++

    the only way i know for a beep is ncurses

    Code:
    #include <ncurses.h>
    
    initscr();
    c = getch();
    if ( c == KEY_F(1)){ beep();}
    endwin();
    should work i think (you will need to download ncurses, its in the repos)

    edit: comile with the -lncurses flag

    for a small sound file, have a look at the sdl library

    depending on what you want it might just be easiest to use a system call to another program to play the file.
    Check out my little app. Tnote

  3. #3
    Join Date
    Aug 2007
    Beans
    949

    Re: beep in c++

    If the terminal is configured for audible beeps, printing the ASCII 'bell' character ('\a') will elicit a beep in the console. This depends on user-defined settings (other actions can be configured; for example, it might cause the window to "flash", a visual cue, or it might not do anything at all).

    If you want to play an actual sound file, you may want to look at how to use Linux sound via ALSA, or use a cross-platform media library, such as SDL.

    EDIT.

    If you don't want to mess with linux sound, and all you want to do is play a sound through the speakers with no bells or whistles, use the system() function to run a command such as

    Code:
    aplay sound_file.wav
    In the background. You may use other similar command line tools to accomplish the job, and though this is an inefficient method (forking a console, replacing the process image, loading another program, then loading and playing the sound file, then exiting on every call), it will get the job done with minimal code interference.
    Last edited by soltanis; May 6th, 2009 at 01:20 AM.

  4. #4
    Join Date
    Oct 2007
    Location
    Here
    Beans
    479
    Distro
    Ubuntu 18.04 Bionic Beaver

    Re: beep in c++

    The easiest thing to do is use the '\a' character.

    cout<<"\a";
    ~ Sanity is the sign of an unused mind.
    ~ I poked a badger with a spoon!

  5. #5
    Join Date
    Oct 2008
    Beans
    561

    Re: beep in c++

    cout << "\a";

    doh
    should have remembered that one.
    Check out my little app. Tnote

  6. #6
    Join Date
    Dec 2008
    Location
    Littleton, Colorado USA
    Beans
    362
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: beep in c++

    Do a "man ascii" and send the BEL character to the terminal. Which in C/C++ is '/a' or octal 007. The BEL character could be a visual bell if the terminal is setup that way. As per M4rotku.

  7. #7
    Join Date
    Mar 2008
    Location
    Poland, Europe
    Beans
    211
    Distro
    Ubuntu

    Smile Re: beep in c++

    Thanks, aplay was a really good suggestion.

  8. #8
    Join Date
    Jun 2008
    Location
    New York, USA
    Beans
    781

    Re: beep in c++

    Quote Originally Posted by marer907 View Post
    Is there a easy way to get a beep or to play a small soundfile with c++?

    I have been searching on google for a answer but haven't found one.

    /Martin
    You could open /dev/dsp and write sound data to it.

    By default, /dev/dsp is set to 8000 hz, 8 bits signed (i.e. no sound is 0x80 and it ranges from 0x00 to 0xFF).

    With system calls, you can change the sample rate, the bit depth, etc...

    All you need to figure it all out is right HERE.

    Have fun!

    -- Roger
    Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •