July 30, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Simple Sounds for Linux

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

Using the beep program, it's easy to play simple melodies. Here are the frequencies for some basic notes:

      262   C - "middle C" 
      277   C# 
      294   D 
      311   D# 
      330   E
      349   F 
      370   F# 
      392   G 
      415   G# 
      440   A
      466   A# 
      494   B 
To find the frequencies of subsequent notes, simply multiply the highest note you know by 1.0595, and then round up or down as appropriate. For example, 494 * 1.0595 = 523.393, or 523 Hz, which should be the "C" note that's one octave above middle C.

Here, then, is a script that plays a portion of Beethoven's F|r Elise:

#!/bin/bash

beep 659 120  #  Treble E
beep 0 120
beep 622 120  #  Treble D#
beep 0 120

beep 659 120  #  Treble E
beep 0 120
beep 622 120  #  Treble D#
beep 0 120
beep 659 120  #  Treble E
beep 0 120
beep 494 120  #  Treble B
beep 0 120
beep 587 120  #  Treble D
beep 0 120
beep 523 120  #  Treble C
beep 0 120

beep 440 120  #  Treble A
beep 0 140
beep 262 120  #  Middle C
beep 0 120
beep 330 120  #  Treble E
beep 0 120
beep 440 120  #  Treble A
beep 0 120

beep 494 120  #  Treble B
beep 0 140
beep 330 120  #  Treble E
beep 0 120
beep 415 120  #  Treble G#
beep 0 120
beep 494 120  #  Treble B
beep 0 120

beep 523 120  #  Treble C
beep 0 140
beep 330 120  #  Treble E
beep 0 120
beep 659 120  #  Treble E
beep 0 120
beep 622 120  #  Treble D#
beep 0 120

beep 659 120  #  Treble E
beep 0 120
beep 622 120  #  Treble D#
beep 0 120
beep 659 120  #  Treble E
beep 0 120
beep 494 120  #  Treble B
beep 0 120
beep 587 120  #  Treble D
beep 0 120
beep 523 120  #  Treble C
beep 0 120

beep 440 120  #  Treble A
beep 0 140
beep 262 120  #  Middle C
beep 0 120
beep 330 120  #  Treble E
beep 0 120
beep 440 120  #  Treble A
beep 0 120

beep 494 120  #  Treble B
beep 0 140
beep 330 120  #  Treble E
beep 0 120
beep 523 120  #  Treble C
beep 0 120
beep 494 120  #  Treble B
beep 0 140
beep 440 120  #  Treble A
The only problem with "beep" is that it's not very efficient. That is, you're consistently calling a program, and as such, the timing might not be absolutely perfect. As I said, Linux is often used for servers, and it's highly likely that other processes will be running at the same time. Therefore, you may experience some delays between notes.

One solution is to include beep's code within your own C program(s). This will definitely speed things up, as the program won't have to be called 400 times to play a tune.





Page 2 of 3



Comment and Contribute

 


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

 

 


Sitemap | Contact Us

Rocket Fuel