MENU

Internal PC Speaker Music Programming (Linux)

A long time ago on a high school when we had a programming class I got this idea to make a song played my this PC speaker. You know, the speakers are in most cases not present at school computers and the only sound you computer does is the beep sound when you turn it on. I thought that it would be a great idea to use it! So I wrote a simple program in Pascal, then rewrote that in C (since I learned I can do it in C as well) and played it. The teacher had no idea what's going on. Haha yea always trolling...

Anyway, this PC speaker or internal speaker is a simple sound generating device that was mainly used in the old computers for a diagnostic messages, old games and many stuff that needed this sound output without using the modern speakers. For example BIOS uses this PC speaker for example to let you know that your hardware is alright and starting.

This is nothing difficult, just a funny piece of programming that sounds pretty cool, at least to me. In Linux you can use a beep command (apt-get install beep) where you simply give a frequency and duration. The best way is to make a shell script like that with notes.

beep -f 500 -l 250 beep -f 240 -l 500 sleep 0.2 beep -f 350 -l 200 ...

The tone frequencies can be found here: https://courses.engr.illinois.edu/ece390/books/labmanual/io-devices-speaker.html

If you want to use C, well let's take a look at the beep command implementation here: http://www.johnath.com/beep/beep.c. I have put a simplified version below.

int fd = open("/dev/console", O_WRONLY); ioctl(fd, KIOCSOUND, (int)(1193180/500)); usleep(250000); ioctl(fd, KIOCSOUND, 0); ioctl(fd, KIOCSOUND, (int)(1193180/240)); usleep(500000); ioctl(fd, KIOCSOUND, 0); usleep(200000); ioctl(fd, KIOCSOUND, (int)(1193180/350)); usleep(200000); ioctl(fd, KIOCSOUND, 0); ... close(fd);

Be sure to type sudo modprobe -v pcspkr before and run the compiled program with sudo as well.

Sources: Bash version of Tetris Theme and C template with example (was too lazy to rewrite the whole song).

Oh and here is a demo...

2016-05-30 03:33:41

Keywords: beep,old school,code,software,script,bash