1,105,344 Community Members

How do you make the computer speaker beep in C?

Member Avatar
Diode
Junior Poster in Training
70 posts since Jan 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello,

I am trying to output simple sound through the internal computer speaker. Is there a way to output certain sounds and control the frequency of the sound as well as the time it lasts?

I'm using Windows XP Pro but is there a "standard" way to do this?

Thanks a lot, :)
Diode

Member Avatar
Anonymusius
Posting Whiz in Training
223 posts since Aug 2006
Reputation Points: 5 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 0 [?]
 
0
 
cout << "/a";

untested but should work. it was something in that direction :P

Member Avatar
~s.o.s~
Failure as a human
10,399 posts since Jun 2006
Reputation Points: 2,496 [?]
Q&As Helped to Solve: 992 [?]
Skill Endorsements: 72 [?]
Administrator
Featured
 
0
 
cout << "/a";

untested but should work. it was something in that direction :P

No :)

Its like

printf ("\a") ;
// or
printf ("\7") ;
Member Avatar
Ene Uran
Posting Virtuoso
1,822 posts since Aug 2005
Reputation Points: 610 [?]
Q&As Helped to Solve: 278 [?]
Skill Endorsements: 10 [?]
 
1
 

If you have a Windows machine you can change the frequency and duration:

// simple sounds via Beep(frequency_hrz, duration_ms)
// on the PC internal speaker, a Windows Console program

#include <stdio.h>
#include <windows.h>   // WinApi header file

int main()
{
  puts("using winAPI Beep(frequency_hrz, duration_ms)...");
  Beep(523,500);  // 523 hertz (C5) for 500 milliseconds
  Beep(587,500);
  Beep(659,500);
  Beep(698,500);
  Beep(784,500);
  Sleep(500);    // 500 ms delay
  puts("\n\\a makes a beep on the internal speaker too ...");
  Sleep(500);
  puts("\a");
  Sleep(500);
  puts("\a");
  
  getchar(); // key wait
  return 0;
}
Member Avatar
jan1024188
Posting Whiz in Training
254 posts since Aug 2006
Reputation Points: 7 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

this works in windows

what about linux

#include <stdio.h>
#include <unistd.h>

int main()
{
  puts("using winAPI Beep(frequency_hrz, duration_ms)...");
  Beep(523,500);  /* 523 hertz (C5) for 500 milliseconds*/
  Beep(587,500);
  Beep(659,500);
  Beep(698,500);
  Beep(784,500);
  usleep(500);    // 500 ms delay
  puts("\n\\a makes a beep on the internal speaker too ...");
  usleep(500);
  puts("\a");
  usleep(500);
  puts("\a");
  
  getchar(); // key wait
  return 0;
}

i get compiler error

gjan@jan-desktop:~$ gcc beep.c
/tmp/cc2Nmnm7.o: In function `main':beep.c:(.text+0x38): undefined reference to `Beep'
:beep.c:(.text+0x4c): undefined reference to `Beep'
:beep.c:(.text+0x60): undefined reference to `Beep'
:beep.c:(.text+0x74): undefined reference to `Beep'
:beep.c:(.text+0x88): undefined reference to `Beep'
collect2: ld returned 1 exit status
jan@jan-desktop:~$
Member Avatar
Ene Uran
Posting Virtuoso
1,822 posts since Aug 2005
Reputation Points: 610 [?]
Q&As Helped to Solve: 278 [?]
Skill Endorsements: 10 [?]
 
0
 

Unless there is something like a ubeep(), you are out of luck.

Member Avatar
jan1024188
Posting Whiz in Training
254 posts since Aug 2006
Reputation Points: 7 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

no

ubeep

that doesnt work

Member Avatar
~s.o.s~
Failure as a human
10,399 posts since Jun 2006
Reputation Points: 2,496 [?]
Q&As Helped to Solve: 992 [?]
Skill Endorsements: 72 [?]
Administrator
Featured
 
0
 

The "beep" function mentioned above is an easy interface provided by Microsoft to access in built speakers and manipulate its frequency and the time of sounding. In short abstraction provided by the OS

If you want to do this in Linux....then err.. you are better off asking this question in this forums Linux Section since there is not much info available on the net either.

Member Avatar
jan1024188
Posting Whiz in Training
254 posts since Aug 2006
Reputation Points: 7 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 
Member Avatar
jan1024188
Posting Whiz in Training
254 posts since Aug 2006
Reputation Points: 7 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 
#include <stdio.h>
#include <unistd.h>
#if !(WIN32)
#define beep(x,y)
#endif

int main()
{

    puts("using winAPI Beep(frequency_hrz, duration_ms)...");
  beep(523,5000);  /* 523 hertz (C5) for 500 milliseconds*/
  beep(587,5000);
  beep(659,5000);
  beep(698,5000);
  beep(784,5000);
  usleep(500000);    
  puts("\n\\a makes a beep on the internal speaker too ...");
  usleep(500000);
  puts("\a");
  usleep(500000);
  puts("\a");
  
  getchar(); 
  return 0;
}

help.....

this compiles fine but there is no beep

Member Avatar
~s.o.s~
Failure as a human
10,399 posts since Jun 2006
Reputation Points: 2,496 [?]
Q&As Helped to Solve: 992 [?]
Skill Endorsements: 72 [?]
Administrator
Featured
 
0
 
#include <stdio.h>
#include <unistd.h>
#if !(WIN32)
 #define beep(x,y)
#endif

What does the above stmt stand for, what are you defining "beep" to be.
It actually defines "beep (x, y)" such that every occurance of the function is replaced by a space (' ').

Looks like you got the code snippet wrong, or you are missing something.

Member Avatar
cdub
Newbie Poster
5 posts since May 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I got this to compile with gcc using gentoo.
But I get the error:

cwc@tma ~/c_code$gcc -c beep2.c -o beep2
cwc@tma ~/c_code $chmod a+x beep2
cwc@tma ~/c_code $./beep2
bash: ./beep2: cannot execute binary file

#include <stdio.h>
#include <unistd.h>
#if !(WIN32)
#define beep(x,y)
#endif

int main()
{

    puts("using winAPI Beep(frequency_hrz, duration_ms)...");
  beep(523,5000);  /* 523 hertz (C5) for 500 milliseconds*/
  beep(587,5000);
  beep(659,5000);
  beep(698,5000);
  beep(784,5000);
  usleep(500000);    
  puts("\n\\a makes a beep on the internal speaker too ...");
  usleep(500000);
  puts("\a");
  usleep(500000);
  puts("\a");
  
  getchar(); 
  return 0;
}

help.....

this compiles fine but there is no beep

Member Avatar
Duoas
Postaholic
2,039 posts since Oct 2007
Reputation Points: 1,022 [?]
Q&As Helped to Solve: 229 [?]
Skill Endorsements: 10 [?]
Featured
 
0
 

Get rid of that -c and try again. Linux only executes ELFs, not OBJs.

Member Avatar
jephthah
Posting Maven
2,572 posts since Feb 2008
Reputation Points: 1,888 [?]
Q&As Helped to Solve: 196 [?]
Skill Endorsements: 5 [?]
 
0
 

i just realized this thread was 2 years old

oh well, whatever. here's a guy's webpage for making a beep in linux. http://frank-buss.de/beep/index.html

i have no idea if it works.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article