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

cout << "/a";

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

cout << "/a";

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

No :)

Its like

printf ("\a") ;
// or
printf ("\7") ;

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;
}

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:~$

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

no

ubeep

that doesnt work

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.

#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

#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.

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.