File Coverage

blib/lib/Audio/Beep/Linux/PP.pm
Criterion Covered Total %
statement 17 37 45.9
branch 0 10 0.0
condition n/a
subroutine 6 10 60.0
pod 0 3 0.0
total 23 60 38.3


line stmt bran cond sub pod time code
1             package Audio::Beep::Linux::PP;
2              
3             $Audio::Beep::Linux::PP::VERSION = 0.11;
4              
5 1     1   6 use strict;
  1         3  
  1         36  
6 1     1   7 use Carp;
  1         2  
  1         82  
7 1     1   1002 use IO::File;
  1         18219  
  1         222  
8 1     1   9 use constant KIOCSOUND => 0x4B2F; #from linux/kd.h
  1         1  
  1         82  
9 1     1   6 use constant CLOCK_TICK_RATE => 1193180; #a magic number - see NOTES
  1         1  
  1         423  
10              
11             sub new {
12 1     1 0 3 my $class = shift;
13 1         5 return bless {}, $class;
14             }
15              
16             sub play {
17 0     0 0   my $self = shift;
18 0           my ($freq, $duration) = @_;
19 0           my $console;
20 0     0     local $SIG{INT} = sub { _sigint( $console ) };
  0            
21 0 0         $console = IO::File->new("/dev/console", O_WRONLY)
22             or confess "Cannot open console: $!";
23 0 0         ioctl($console, KIOCSOUND, int(CLOCK_TICK_RATE / $freq))
24             or confess "Call to ioctl failed: $!";
25 0           select undef, undef, undef, $duration / 1000;
26 0 0         ioctl($console, KIOCSOUND, 0)
27             or confess "Call to ioctl failed: $!";
28 0           $console->close;
29 0           return 1;
30             }
31              
32             sub rest {
33 0     0 0   my $self = shift;
34 0           my ($duration) = @_;
35 0           select undef, undef, undef, $duration / 1000;
36 0           return 1;
37             }
38              
39             sub _sigint {
40 0     0     my $console = shift;
41 0 0         if (defined $console) {
42 0 0         ioctl($console, KIOCSOUND, 0)
43             or confess "Call to ioctl failed: $!";
44 0           $console->close;
45             }
46 0           exit;
47             }
48              
49             =head1 NAME
50              
51             Audio::Beep::Linux::PP - PurePerl implementation of an Audio::Beep player
52              
53             =head1 SYNOPSIS
54              
55             my $player = Audio::Beep::Linux::PP->new();
56              
57             =head1 USAGE
58              
59             The C class method will return you a new player object.
60             No other option is available right now.
61              
62             =head1 NOTES
63              
64             You need to be root to play something using this module.
65             Otherwise your script should be SUID root (but i won't suggest that).
66             Or you could own the tty where you execute this, but it cannot be an xterm.
67             It's better to install the B program by Johnathan Nightingale and
68             then SUID that small program.
69             This module is just a rewriting of the core function of the B program.
70             I took everything from there so credit goes again to Johnathan Nightingale.
71             As this is a PurePerl module i had to do some assumption, like the
72             KIOCSOUND constant to be 0x4B2F (which may not be your case).
73             The CLOCK_TICK_RATE is also taken from B.
74             Follows what you can read there:
75              
76             I don't know where this number comes from, I admit that freely. A
77             wonderful human named Raine M. Ekman used it in a program that played
78             a tune at the console, and apparently, it's how the kernel likes its
79             sound requests to be phrased. If you see Raine, thank him for me.
80              
81             June 28, email from Peter Tirsek (peter at tirsek dot com):
82              
83             This number represents the fixed frequency of the original PC XT's
84             timer chip (the 8254 AFAIR), which is approximately 1.193 MHz. This
85             number is divided with the desired frequency to obtain a counter value,
86             that is subsequently fed into the timer chip, tied to the PC speaker.
87             The chip decreases this counter at every tick (1.193 MHz) and when it
88             reaches zero, it toggles the state of the speaker (on/off, or in/out),
89             resets the counter to the original value, and starts over. The end
90             result of this is a tone at approximately the desired frequency. :)
91              
92             =head1 BUGS
93              
94             None known.
95              
96             =head1 COPYRIGHT
97              
98             Copyright 2003-2004 Giulio Motta L.
99              
100             This library is free software; you can redistribute it and/or
101             modify it under the same terms as Perl itself.
102              
103             =cut
104              
105             1;