Simple Sounds for Linux
Remember the "bad old days", when users had a CLUE (Command Line User Environment) and PC speakers went beep, beep? That all changed in the mid-1980s, when Apple gave us the Mac, and Commodore introduced its Amiga. Since then, new applications have continually pushed the envelope in terms of display resolutions and audio capabilities. While this development is admittedly a good thing, it's also spurred the frequent purchase of expensive hardware, and created a myriad of compatibility problems.
On the flip side of the coin, one of the nice things about Linux is that it allows you to recycle all of your old equipment. Why throw away that slovenly 486, for example, when you can use it as your DNS server or as a telnet-capable workstation? Further, Linux is inherently good at multitasking, and as such, it's ideal for use as a server. Therefore, it's not uncommon to find Linux-based machines running *without* sound cards.
If this happens to be your scenario, does this mean that you have to forego sound entirely? Of course not! It's time to remember an old friend, the internal speaker. No sound card is required; just a PC with a regular speaker in its case.
After all, do you really need symphonic audio when displaying error messages?
This doesn't mean that you have to be content with mere beeps, however. Instead, you'll progress well beyond the standard "bell" sound (ASCII code 007, or Ctrl-G). Using low-grade wizardry, you'll be able to vary the pitch and length of each tone. As such, you'll soon have the ability to play a simple melody by typing a few brief commands. It will also be trivial to include these sounds within your programs.
Getting Re-acquainted With Your Internal Speaker
It's fairly common knowledge that -- with a sound card -- you can play an audio file on Linux by typing:cat sample.au > /dev/audio |
echo -e "\a" |
print "\007"; |
char beep[] = {7, '\0'}; printf("%c", beep); |
http://www.ibiblio.org/pub/Linux/apps/sound/misc/console_beep-0.1.tar.gz
Once you've downloaded this, unpack it as usual.
Within the tarball, you'll find two programs to make: beep.c, and beep.c.direct. The first, beep.c, takes commands from a file, whereas beep.c.direct can be accessed directly from the command line. To compile beep.c.direct, you'll need to rename it "beep.c" -- after renaming the original, of course.
Usage is simple. Both programs take two arguments: the tonal frequency in Hertz, and the duration in milliseconds. So, let's say you compiled beep.c.direct into "beep". Here's all you'd need to do:
This will play a 440 Hz tone for 200 milliseconds, or a fifth of a second. beep 440 200
To play multiple notes, it's often necessary to insert a "rest" between them, like so:
Note that the "spacer" note, or rest, plays at zero Hertz (i.e., it doesn't play at all), but it continues for the same duration as the other notes.
beep 440 200
beep 0 200
beep 700 200
Page 1 of 3
This article was originally published on January 23, 2001