NAME
Audio::Beep - a module to use your computer beeper in fancy ways
SYNOPSIS
#functional simple way
use Audio::Beep;
beep($freq, $milliseconds);
#OO more musical way
use Audio::Beep;
my $beeper = Audio::Beep->new();
# lilypond subset syntax accepted
# relative notation is the default
# (now correctly implemented)
my $music = "g' f bes' c8 f d4 c8 f d4 bes c g f2";
# Pictures at an Exhibition by Modest Mussorgsky
$beeper->play( $music );
USAGE
Exported Functions
- beep([FREQUENCY], [DURATION]);
-
Plays a customizable beep out of your computer beeper.
FREQUENCY is in Hz. Defaults to 440.
DURATION is in milliseconds. Defaults to 100.
OO Methods
- Audio::Beep->new([%options])
-
Returns a new "beeper" object. Follows the available options for the new method to be passed in hash fashion.
- player => [player object | player module]
-
You can initialize your player object and then give it to the Audio::Beep object. Player objects come from Audio::Beep submodules (like Audio::Beep::Linux::beep). If you're lazy (as any good programmer should be) you can give a string as a player, like
"Audio::Beep::Linux::PP"
or even just"Linux::PP"
: the method will prepend theAudio::Beep
namespace, require the module and call the new method on it for you. The new method will try to look up the best player on your platform if you don't specify one. So the following is all valid:use Audio::Beep; #super lazy (should do the right thing most of the time) my $beeper = Audio::Beep->new(); #still lazy my $beeper2 = Audio::Beep->new(player => 'Linux::PP'); #medium lazy my $beeper3 = Audio::Beep->new( player => 'Audio::Beep::Win32::API' ); #not so lazy, but more versatile require Audio::Beep::Linux::beep; my $beeper4 = Audio::Beep->new( player => Audio::Beep::Linux::beep->new( path => '/home/foo/bin/beep/' ) );
- rest => [ms]
-
Sets the rest in milliseconds between every sound played (and even pause). This is useful for users which computer beeper has problems and would just stick to the first sound played. For example on my PowerbookG3 i have to set this around 120 milliseconds. In that way i can still hear some music. Otherwise is just a long single beep.
- $beeper->play( $music )
-
Plays the "music" written in $music. The accepted format is a subset of lilypond.org syntax. The string is a space separated list of notes to play. See the NOTATION section below for more info.
- $beeper->player( [player] )
-
Sets the player object that will be used to play your music. See the player option above at the
new
method for more info. With no parameter it just gives you back the current player. - $beeper->rest( [ms] )
-
Sets the extra rest between each note. See the rest option above at the
new
method for more info. With no parameter it gives you back the current rest.
NOTATION
The defaults at start are middle octave C and a quarter length. Standard notation is the relative notation. Here is an explanation from Lilypond documentation:
If no octave changing marks are used, the basic interval between
this and the last note is always taken to be a fourth or less
(This distance is determined without regarding alterations;
a fisis following a ceses will be put above the ceses)
The octave changing marks ' and , can be added to raise or lower
the pitch by an extra octave.
You can switch from relative to non relative notation (in which you specify for every note the octave) using the \norel
and \rel
commands (see below)
Notes
Every note has the following structure:
[note][flat|sharp][octave][duration][dots]
NB: previous note duration is used if omitted. "Flatness", "Sharpness" and "Dottiness" are reset after each note.
- note
-
A note can be any of [c d e f g a b] or [r] for rest.
- flat or sharp
-
A sharp note is produced postponing a "is" to the note itself (like "cis" for a C#). A flat note is produced adding a "es" or "s" (so "aes" and "as" are both an A flat).
- octave
-
A ' (apostrophe) raise one octave, while a , (comma) lower it.
- duration
-
A duration is expressed with a number. A 4 is a beat, a 1 is a whole 4/4 measure. Higher the number, shorter the note.
- dots
-
You can add dots after the duration number to add half its length. So a4. is an A note long 1/4 + 1/8 and gis2.. is a G# long 7/8 (1/2 + 1/4 + 1/8)
- special note: "r"
-
A r note means a rest. You can still use duration and dots parameters.
Special Commands
Special commands always begin with a "\". They change the behavior of the parser or the music played. Unlike in the Lilypond original syntax, these commands are embedded between notes so they have a slightly different syntax.
- \bpm(\d+)
-
You can use this option to change the tempo of the music. The only parameter you can use is a number following the bpm string (like "bpm144"). BPM stands for Beats Per Minute. The default is 120 BPM. You can also invoke this command as
\tempo
- \norel
-
Switches the relative mode off. From here afterward you have to always specify the octave where the note is.
- \rel
-
Switches the relative mode on. This is the default.
- \transpose([',]+)
-
You can transpose all your music up or down some octave. ' (apostrophe) raise octave. , (comma) lowers it. This has effect just if you are in non-relative mode.
Comments
You can embed comments in your music the Perl way. Everything after a # will be ignored until end of line.
Music Examples
my $scale = <<'EOS';
\rel \bpm144
c d e f g a b c2. r4 # a scale going up
c b a g f e d c1 # and then down
EOS
my $music = <<'EOM'; # a Smashing Pumpkins tune
\bpm90 \norel \transpose''
d8 a, e a, d a, fis16 d a,8
d a, e a, d a, fis16 d a,8
EOM
my $love_will_tear_us_apart = <<'EOLOVE'; # a happier tune
\bpm160
d'8
e1 fis4 g8 fis4 e8 d4
b2.. d8 a2.. d8
e1 fis4 g8 fis4 e8 d4
b2.. d8 a1
EOLOVE
There should be extra examples in the "music" directory of this tarball.
EXAMPLES
#a louder beep
perl -MAudio::Beep -ne 'print and beep(550, 1000) if /ERROR/i' logfile
#turn your PC in Hofmann mode (courtesy of Thomas Klausner)
perl -MAudio::Beep -e 'beep(21 + rand 1000, rand 300) while 1'
#your new music player
perl -mAudio::Beep -0777e 'Audio::Beep->new->play(<>)' musicfile
REQUIREMENTS
Linux
Requires either the "beep" program by Johnathan Nightingale (you should find sources in this tarball) SUID root or you to be root (that's because we need writing access to the /dev/console device). If you don't have the "beep" program this library will also assume some kernel constants which may vary from kernel to kernel (or not, i'm no kernel expert). Anyway this was tested on a 2.4.20 kernel compiled for i386. With the same kernel i have problems on my PowerBook G3 (it plays a continous single beep). See the rest
method if you'd like to play something anyway.
Windows
Requires Windows NT, 2000 or XP and the Win32::API module. You can find sources on CPAN. Some PPM precompiled packages are at http://dada.perl.it/PPM/ No support is available for Windows 95, 98 and ME yet: that would require some assembler and an XS module.
BACKEND
If you are a developer interested in having Audio::Beep working on your platform, you should think about writing a backend module. A backend module for Beep should offer just a couple of methods:
NB: FREQUENCY is in Hertz. DURATION in milliseconds
- new([%options])
-
This is kinda obvious. Take in the options you like. Keep the hash fashion for parameters, thanks.
- play(FREQUENCY, DURATION)
-
Plays a single sound.
- rest(DURATION)
-
Rests a DURATION amount of time
TODO
This module works for me, but if someone wants to help here is some cool stuff to do:
- an XS backend
- an XS Windoze backend (look at the Prima project for some useful code)
- some work is needed on Windows Makefile part
BUGS
Some of course.
COPYRIGHT
Copyright 2003 Giulio Motta <giulienk@cpan.org>.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.