How to use PC speaker in linux? How to use PC speaker in linux? linux linux

How to use PC speaker in linux?


Taken from here:

#include <sys/ioctl.h>#include <unistd.h>#include <linux/kd.h>int main(void){    int freq[] = { /* C   D    E    F    G    A    B    C */                    523, 587, 659, 698, 784, 880, 988, 1046 };    int i;    for (i=0; i<8; i++)    {            ioctl(STDOUT_FILENO, KIOCSOUND, 1193180/freq[i]);            usleep(500000);    }    ioctl(STDOUT_FILENO, KIOCSOUND, 0); /*Stop silly sound*/    return 0;}


Yes, open a console device (such as /dev/console or /dev/tty0), then issue the KIOCSOUND ioctl to it, as described in the console_ioctl(4) man page.

It's yucky and Linux-specific, but I think it answers your question.


EDIT: Unbelivably, there's a PC-speaker driver in the kernel for ALSA which gives you digital sound playback in the PC speaker. Its sound quality will be poor and it will use a lot of CPU though :)


If you really want to do this, check out the source code for the beep command:http://www.johnath.com/beep/beep.c