Program PC speaker.

anyone can give me a simple C program that can make the PC speaker beep at a certain frequency and certain length in Linux.  Thank you very much.

Regards.
rameoAsked:
Who is Participating?
 
serena77Connect With a Mentor Commented:
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/kd.h>
main() {
int pitch = 1000;
int duration = 50;
int fd;
fd = open("/dev/console", "w");
ioctl(fd, KDMKTONE, ((1193190 / pitch) & 0xffff)
      | ((unsigned long)duration*2 << 16));
}

Dont forget to run this program as root ;)

Regards

Serena
0
 
IexpertCommented:
#include <stdlib.h>

'                             NOTE FREQUENCIES (Hz)
'      +----------------------------------------------------------------------+
'      |                            OCTAVE                                    |
'+-----+----------------------------------------------------------------------+
'|NOTE |  0 |  1 |   2 |  3  |  4  |  5  |   6  |   7  |   8  |   9   |  10   |
'+-----+----------------------------------------------------------------------+
'|  A  |13.7|27.5| 55.0|110.0|220.0|440.0| 880.0|1760.0|3520.0| 7040.0|14080.0|
'|A#/Bb|14.6|29.1| 58.3|116.5|233.1|466.2| 932.4|1864.7|3729.4| 7458.9|14917.8|
'|  B  |15.4|30.9| 61.7|123.5|247.0|493.9| 987.8|1975.7|3951.3| 7902.7|15805.3|
'|  C  |16.4|32.7| 65.4|130.8|261.6|523.3|1046.6|2093.2|4186.5| 8372.9|16745.8|
'|C#/Db|17.3|34.6| 69.3|138.6|277.2|554.4|1108.8|2217.7|4435.5| 8871.1|17742.1|
'|  D  |18.4|36.7| 73.4|146.8|293.7|587.4|1174.8|2349.7|4699.5| 9398.9|18797.8|
'|D#/Eb|19.4|38.9| 77.8|155.6|311.2|622.4|1244.8|2489.5|4979.1| 9958.1|19161.3|
'|  E  |20.6|41.2| 82.4|164.9|329.7|659.4|1318.8|2637.7|5275.3|10550.6|21101.3|
'|  F  |21.8|43.7| 87.3|174.7|349.3|698.7|1397.3|2794.6|5589.2|11178.4|22356.8|
'|F#/Gb|23.1|46.2| 92.5|185.1|370.1|740.2|1480.4|2960.8|5921.8|11843.5|23687.1|
'|  G  |24.4|49.0| 98.0|196.1|392.1|784.3|1568.2|3137.1|6274.1|12548.2|25096.4|
'|G#/Ab|26.0|51.9|103.9|207.7|415.5|830.9|1661.9|3323.7|6647.4|13294.8|29589.7|
'+-----+----------------------------------------------------------------------+

const C4 = 261.6;
const C5 = 523.3;

static void tone_on(void)
{
      __asm
      {
          mov   al,0b6h    ; timer control code
          out   43h,al     ; write it to c2
          mov   al,bl      ; bx = count value
          out   42h,al     ; write low value
          mov   al,bh      ; high count value
          out   42h,al     ; write high value
          in    al,61h     ; get control word
          or    al,03h     ; set low two bits, speaker on!
          out   61h,al     ; write back control word
      }
}

void sound(long freq/*in Hz*/)
{
      int count=1193180/*frequency of counter*/
                /freq;

      __asm mov bx, count

      tone_on();
}

void nosound(void)
{
      __asm
      {
          in al,61h        ; get control word
          and al,0FCh      ; clear low two bits
          out 61h,al       ; write back control word
      }
}

int main(void)
{
      #define SECOND *1000
      #define SECONDS SECOND

      sound(C4);
      sleep(1 SECOND); 'Play for 1 second
      nosound();
      sound(C5);
      sleep(2 SECONDS); 'Play for 2 seconds
      nosound();
}

0
 
rameoAuthor Commented:
I'm actually looking for a simple ioctl system call that can let me do it.  not down to assembler level.


0
All Courses

From novice to tech pro — start learning today.