| 1 | package Audio::Beep::Linux::PP; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | $Audio::Beep::Linux::PP::VERSION = 0.11; | |||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use IO::File; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use constant KIOCSOUND => 0x4B2F; #from linux/kd.h | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use constant CLOCK_TICK_RATE => 1193180; #a magic number - see NOTES | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 12 | 1 | my $class = shift; | |||||||||
| 13 | 1 | return bless {}, $class; | |||||||||
| 14 | } | ||||||||||
| 15 | |||||||||||
| 16 | 0 | ▶ | sub play { | ||||||||
|
Subroutine
play: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 17 | 0 | my $self = shift; | |||||||||
| 18 | 0 | my ($freq, $duration) = @_; | |||||||||
| 19 | 0 | my $console; | |||||||||
| 20 | 0 | ▶ | local $SIG{INT} = sub { _sigint( $console ) }; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 21 | 0 | ▶ | $console = IO::File->new("/dev/console", O_WRONLY) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | or confess "Cannot open console: $!"; | ||||||||||
| 23 | 0 | ▶ | ioctl($console, KIOCSOUND, int(CLOCK_TICK_RATE / $freq)) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | or confess "Call to ioctl failed: $!"; | ||||||||||
| 25 | 0 | select undef, undef, undef, $duration / 1000; | |||||||||
| 26 | 0 | ▶ | ioctl($console, KIOCSOUND, 0) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | or confess "Call to ioctl failed: $!"; | ||||||||||
| 28 | 0 | $console->close; | |||||||||
| 29 | 0 | return 1; | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | 0 | ▶ | sub rest { | ||||||||
|
Subroutine
rest: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 33 | 0 | my $self = shift; | |||||||||
| 34 | 0 | my ($duration) = @_; | |||||||||
| 35 | 0 | select undef, undef, undef, $duration / 1000; | |||||||||
| 36 | 0 | return 1; | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 0 | ▶ | sub _sigint { | ||||||||
|
Subroutine
_sigint: not called CC=3 SCAR=24.8
| |||||||||||
| 40 | 0 | my $console = shift; | |||||||||
| 41 | 0 | ▶ | if (defined $console) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 0 | ▶ | ioctl($console, KIOCSOUND, 0) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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<new> 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<beep> 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<beep> 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<beep>. | ||||||||||
| 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<giulienk@cpan.org>. | ||||||||||
| 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 | 1; | |||||||||