views:

68

answers:

2

How to output a frequency 1kHz and power/volume = 60% in Linux (Ubuntu)?

I need sample code in C/C++. Thanks!

+1  A: 

Below is how to output a beep of user-defined frequency (in hertz) and length (in milliseconds). How to set the volume, that I'm not sure. Note that this C program just does a system() call to 'echo -e' so technically you don't even need C for this, but you asked so I gave.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
   long freq;
   long len;
   char cmd[60];

   if (argc != 3)
   {
       printf("Usage: %s freq_hz length_ms\n", argv[0]);
       return 1;
   }

   freq = strtol(argv[1],(char**)NULL,10);
   len = strtol(argv[2],(char**)NULL,10);

   sprintf(cmd,"echo -e \"\33[10;%ld]\33[11;%ld]\a\33[10]\33[11]\"",freq,len);
   system(cmd);

   return 0;
}
SiegeX
+1 Thanks SiegeX! This looks cool. I need it to use library directly though. Anyway nice piece.
Viet
+2  A: 

Take a look at the portaudio library. http://www.portaudio.com/

It is very easy to use, cross platform and comes with lots of little example programs. Among other things you'll find one that synthesizes and plays a sine-wave.

Changing frequency and volume is trivial.. If I remember right the example was just a single page of code including initialization and everything.

Nils Pipenbrinck
+1 Thanks Nils! Wow, this one is even cross-platform. I'm looking into that.
Viet