Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use PC speaker in linux?

Tags:

c++

linux

audio

How to use PC speaker in linux for c/c++ programming ? Can I control the beep time and freq?

like image 426
1.618 Avatar asked Dec 15 '10 06:12

1.618


People also ask

How do I make my computer beep Linux?

The easiest way to make the motherboard beep is to open a terminal and type "echo -e \\a". However, that may not work on some Linux systems. To fix the issue, try running "modprobe pcspkr" in a terminal with Root privileges. Now, try running "echo -e \\a".

What is Pcspkr?

pcspkr : PC-Speaker driver. snd_pcsp : PC-Speaker driver.

What is the use of the beep command?

beep allows the user to control the pc-speaker with precision, allowing different sounds to indicate different events. While it can be run quite happily on the command line, it's intended place of residence is within shell/perl scripts, notifying the user when something interesting occurs.


1 Answers

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;
}
like image 118
liorda Avatar answered Sep 19 '22 20:09

liorda