January 20, 2021
Hot Topics:

Simple Sounds for Linux

  • By Jay Link
  • Send Email »
  • More Articles »

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
on the command line, where sample.au is the name of your sound file. However, an even simpler method is to type:
   echo -e "\a"
This sounds the "alarm", i.e., the internal speaker. You can also generate a "beep" by including lines like the following in your code: Perl:
   print "\007";
   char beep[] = {7, '\0'};
   printf("%c", beep);
To move beyond this basic beep, download the console_beep-0.1 package from www.ibiblio.org (formerly metalab.unc.edu, and sunsite.unc.edu before that). Amazingly, the compressed package is only 3K! For your convenience, the direct URL is:


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:

   beep 440 200
This will play a 440 Hz tone for 200 milliseconds, or a fifth of a second.

To play multiple notes, it's often necessary to insert a "rest" between them, like so:

   beep 440 200
   beep 0 200
   beep 700 200
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.

Page 1 of 3

This article was originally published on January 23, 2001

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date