October 21, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Simple Sounds for Linux

  • January 23, 2001
  • By Jay Link
  • Send Email »
  • More Articles »

An even better option is to use the following code, provided by programming guru Matan Ziv-Av:

#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/kd.h>

int main(int argc, char *argv[])
{
   int fd, time, freq, arg;
   fd = open("/dev/tty0", O_RDONLY);
   if (argc > 2)
   {
       freq = atoi(argv[1]);
       time = atoi(argv[2]);
   } else {
       freq = 500; /* frequency in Hz */
       time = 50;  /* time in millisec */
   }
   arg = (time<<16)+(1193180/freq);
   return ioctl(fd,KDMKTONE,arg);
}

This simplified program works exactly like beep, only it's much faster. As such, you may need to increase the duration of your tones by a factor of two or more.

Further, it may not be obvious by looking at the code, but this program runs in the background. Therefore, you can move on and do other things while the tone plays uninterrupted for the designated timeframe. This feature can be a detriment, however, if you're trying to play multiple notes (i.e., a melody). In this case, you'll find each note canceling out the next, and no amount of interstitial resting will help.

To overcome this annoyance, you'll need to insert a "rest" within the program itself. I accomplished this with a call to usleep(), like so:
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/kd.h>

int main(int argc, char *argv[])
{
   int fd, time, freq, arg;
   fd = open("/dev/tty0", O_RDONLY);
   if (argc > 2)
   {
       freq = atoi(argv[1]);
       time = atoi(argv[2]);
   } else {
       freq = 500; /* frequency in Hz */
       time = 50;  /* time in millisec */
   }
   arg = (time<<16)+(1193180/freq);
   ioctl(fd,KDMKTONE,arg);
   usleep(time * 1000);
   return EXIT_SUCCESS;
}
Now the program rests for the same amount of time as the note plays, allowing you to play a chain of notes as before. Remember to #include !

In conclusion, there's no reason to exclude audio completely, just because you don't have a sound card. Simple sound bytes (if you'll pardon the pun) often add to a program's vitality and improve the user's experience. Now that you know how simple it is to code for the internal speaker, why not take advantage of this ever-present, yet often overlooked hardware feature?

Related Resources

1. www.ibiblio.org Massive, web-based repository of all things Linux. Formerly known as metalab.unc.edu and sunsite.unc.edu before that. Check it out!

2. The Sheet Music Archive One place to get free sheet music on the web.

3. The Free Sheet Music Directory Another repository of free sheet music.

4. Classical sheet music Look for the PDF Music link.

5. Free Sheet Music Guide The name says it all!

About Author

Jay Link is twentysomething and lives in Springfield, Illinois. Aside from Linux, his interests include mountain climbing and flying. He administrates InterLink BBS (an unintentionally not-for-profit Internet provider) in his fleeting spare moments, as well as working various odd jobs to pay the rent.





Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel