For winapi, there is a standard windows.h library with the Beep () function.

Is there an equivalent in gnu / linux , or is it necessary to use third-party libraries?

  • Uh ... Well, such a function is not in the C ++ standard, so third-party libraries are needed anyway. windows.h is also quite a third-party library. - VladD
  • @VladD the habit of calling all that comes with Visual Studio - "standard" - ParanoidPanda
  • one
    Have you tried frank-buss.de/beep ? - Croessmah

2 answers 2

If there is conio.h , you can use

 putch('\a'); 

About putchar there was a topic that does not work, but I think putch should.

  • I need the beep () function from the windows.h library. That is, I need the full functionality of this function. Read about it please msdn.microsoft.com/en-us/library/windows/desktop/… - ParanoidPanda
  • one
    @ParanoidPanda, it was worth writing in question. - Qwertiy
  • Once worked. - sercxjo

You can use the features of the console Linux. To do this, log in to the text terminal. Further use control sequences (in printf format):

  • \ 033 [10;% d] - set the frequency.
  • \ 033 [11;% d] - set the duration in milliseconds.
  • \ a - actually give a signal of a set frequency and duration.

Before issuing a new signal, you need to wait until the current one ends, otherwise it will be interrupted and the new one will immediately sound. You may need to load the kernel module to support pc-speaker pcspkr or snd_pcsp and use alsamixer to include everything related to speaker and beep.

Code:

 #include <stdio.h> #include <unistd.h> void Beep(int Hz, int msec) { fprintf(stderr,"\033[10;%d]\033[11;%d]\a", Hz, msec); usleep(msec*1000); } int main(int argc, char* argv[]) { Beep(494, 250); Beep(523, 250); Beep(587, 500); Beep(523, 250); Beep(494, 250); Beep(440, 500); return 0; } 

You need to run from the text console or redirect the output of stderr to it.

I think at present, a more convenient way is to use one of the ported libraries. Therefore, I propose the implementation of Beep using SDL in which I used the idea of ​​the Bresenham algorithm. In fairness, I would say that for high frequencies, the sound of the console is better.

 #include <SDL.h> #include <SDL_audio.h> #include <unistd.h> typedef struct { SDL_AudioSpec spec; int freq; int t; int err; int newfreq; int newt; Uint8 v; } udata_t; static udata_t uData; static void fill_audio(void *udata, Uint8 *stream, int len) { udata_t * const d= udata; while(len--) { if(!d->t) { if(d->newt) d->t = d->newt; d->freq = d->newfreq; d->newt = 0; } if(d->t) { d->t--; d->err += d->freq; if(d->err >= d->spec.freq) { d->err -= d->spec.freq*2; d->v= ~d->v; } } else if(!len) { SDL_PauseAudio(1); } *stream++ = d->v; } } void Beep(int Hz, int msec) { while(uData.newt) { usleep((uData.t*1000+uData.spec.freq-1)/uData.spec.freq*1000); } uData.newfreq= Hz*4; uData.newt= uData.spec.freq*msec/1000; if(!uData.t) SDL_PauseAudio(0); } void wait_audio() { while(uData.newt || uData.t) { usleep((uData.t*1000+uData.spec.freq-1)/uData.spec.freq*1000); } } void init_audio() { SDL_AudioSpec wanted; /* Set the audio format */ wanted.freq = 65535; wanted.format = AUDIO_U8; wanted.channels = 1; /* 1 = mono, 2 = stereo */ wanted.samples = 1024; /* Good low-latency value for callback */ wanted.callback = fill_audio; wanted.userdata = &uData; /* Open the audio device, forcing the desired format */ if ( SDL_OpenAudio(&wanted, &uData.spec) < 0 ) { fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError()); exit(1); } uData.err= uData.spec.freq; } int main(int argc, char* argv[]) { SDL_Init(SDL_INIT_AUDIO); init_audio(); Beep(494, 250); Beep(523, 250); Beep(587, 500); Beep(523, 250); Beep(494, 250); Beep(440, 500); wait_audio(); return 0; } 

Compilation:

 gcc beepsdl.c `sdl2-config --cflags --libs` 

or sdl-config instead of sdl2-config depending on the version. You may need to install libsdl2-dev .