Beep.pm

filter: Statementstmt 35.1% 38 / 108 Branchbran 10.6% 7 / 66 Conditioncond 10.0% 1 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 53.8% 7 / 13 Podpod 100% 5 / 5 totaltotal 27.8% 58 / 208 CC 41 SCAR 55.0
CC
41
Cov
25%
play
552.0
beep
30.0
_duration
12.0
CRAP 245.1
↑ Summary Beep/Linux/PP.pm »
1 package Audio::Beep;
2  
3 1 $Audio::Beep::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 Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use vars qw(%NOTES @PITCH @EXPORT @EXPORT_OK @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 @ISA = qw(Exporter);
10 1 @EXPORT = qw(beep);
11 1 @EXPORT_OK = qw(beep);
12  
13  
14 ### GLOBALS
15  
16 1 %NOTES = (
17 c => 0,
18 d => 2,
19 e => 4,
20 f => 5,
21 g => 7,
22 a => 9,
23 b => 11,
24 );
25  
26 1 @PITCH = (
27 261.6, 277.2,
28 293.6, 311.1,
29 329.6,
30 349.2, 370.0,
31 392.0, 415.3,
32 440.0, 466.1,
33 493.8,
34 );
35  
36  
37 ### OO METHODS
38  
39 1 ▶sub new {
Subroutine new: called CC=6 SCAR=21.6
Pod documented
40 1 my $class = shift;
41 1 ▶ carp "Odd number of parameters where hash expected" if @_ % 2 and $^W;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 and $^W 0 1 1
Condition33%1/3
@_ % 2 and $^W
!ll&&!rl&&r
100
MC/DC0%0/2
@_ % 2 and $^W
@_ % 2 $^W
Truth table33%1/3
A and B
A= @_ % 2B= $^W
ABresult
0X0
100
111
42 1 my (%h) = @_;
43 1 ▶ if ( $h{player} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h{'player'}) { } 0 1 1
44 0 ▶ $h{player} = _player_from_string( $h{player} ) unless ref $h{player};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $h{"player"} 0 0 0
45 } else {
46 1 $h{player} = _best_player();
47 }
48 carp "No player found. You should specify one before playing anything."
49 1 ▶ unless $h{player};
Branch: 1 branch
BranchTrueFalseTotal
unless $h{"player"} 0 1 1
50 1 return bless \%h, $class;
51 }
52  
53 1 ▶sub player {
Subroutine player: called CC=3 SCAR=12.5
Pod documented
54 1 my $self = shift;
55 1 my ($player) = @_;
56 1 ▶ $self->{player} = ref $player ? $player : _player_from_string($player)
Branch: 2 branches
BranchTrueFalseTotal
if $player 0 1 1
ref $player ? : 0 0 0
57 if $player;
58 1 return $self->{player};
59 }
60  
61 0 ▶sub rest {
Subroutine rest: not called CC=2 SCAR=17.9
Pod documented
62 0 my $self = shift;
63 0 my ($rest) = @_;
64 0 ▶ $self->{rest} = $rest if defined $rest;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rest 0 0 0
65 0 return $self->{rest};
66 }
67  
68 0 ▶sub play {
Subroutine play: not called CC=23 SCAR=63.1
Pod documented
69 0 my $self = shift;
70 0 my ($music) = @_;
71
72 0 my %p = (
73 note => 'c',
74 duration => 4,
75 octave => 0,
76 bpm => 120,
77 pitch_mod => 0,
78 dot => 0,
79 relative => 1,
80 transpose => 0,
81 );
82
83 0 ▶ while ($music =~ /\G(?:([^\s#]+)\s*|#[^\n]*\n|\s*)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($music =~ /\G(?:([^\s#]+)\s*|#[^\n]*\n|\s*)/g) 0 0 0
84 0 ▶ local $_ = $1 or next;
Branch: 1 branch
BranchTrueFalseTotal
local $_ = $1 or next 0 0 0
85
86 0 ▶ if ( /^\\(.+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\\(.+)/) 0 0 0
87 COMMAND: {
88 0 local $_ = $1;
89 0 ▶ /^(?:bpm|tempo)(\d+)/ and do {$p{bpm} = $1; last};
Branch: 1 branch
BranchTrueFalseTotal
/^(?:bpm|tempo)(\d+)/ and do { $p{"bpm"} = $1; last } 0 0 0
90 0 ▶ /^rel/ and do {$p{relative} = 1; last};
Branch: 1 branch
BranchTrueFalseTotal
/^rel/ and do { $p{"relative"} = 1; last } 0 0 0
91 0 ▶ /^norel/ and do {$p{relative} = 0; last};
Branch: 1 branch
BranchTrueFalseTotal
/^norel/ and do { $p{"relative"} = 0; last } 0 0 0
92 0 ▶ /^transpose([',]+)/ and do {
Branch: 1 branch
BranchTrueFalseTotal
/^transpose([',]+)/ and do { local $_ = $1; $p{"transpose"} = tr/'// - tr/,//; last } 0 0 0
93 0 local $_ = $1;
94 0 $p{transpose} = tr/'/'/ - tr/,/,/;
95 0 last;
96 };
97 0 ▶ carp qq|Command "$_" is unparsable\n| if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
98 }
99 0 next;
100 }
101
102 0 my ($note, $mod, $octave, $dur, $dot) =
103 /^\W*([cdefgabr])(is|es|s)?([',]+)?(\d+)?(\.+)?\W*$/;
104
105 0 ▶ unless ($note) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($note) 0 0 0
106 0 ▶ carp qq|Note "$_" is unparsable\n| if $^W;
Branch: 1 branch
BranchTrueFalseTotal
if $^W 0 0 0
107 0 next;
108 }
109
110 0 ▶ $p{duration} = $dur if $dur;
Branch: 1 branch
BranchTrueFalseTotal
if $dur 0 0 0
111  
112 0 $p{dot} = 0;
113 0 ▶ do{ $p{dot} += tr/././ for $dot } if $dot;
Branch: 1 branch
BranchTrueFalseTotal
if ($dot) 0 0 0
114
115 0 ▶ if ( $note eq 'r' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($note eq 'r') { } 0 0 0
116 0 $self->player->rest( _duration(\%p) );
117 } else {
118 0 ▶ if ( $p{relative} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p{'relative'}) { } 0 0 0
119 0 my $diff = $NOTES{ $p{note} } - $NOTES{ $note };
120 0 ▶ $p{octave} += $diff < 0 ? -1 : 1 if abs $diff > 5;
Branch: 2 branches
BranchTrueFalseTotal
if abs $diff > 5 0 0 0
$diff < 0 ? : 0 0 0
121 } else {
122 0 $p{octave} = $p{transpose};
123 }
124
125 0 ▶ do{ $p{octave} += tr/'/'/ - tr/,/,/ for $octave } if $octave;
Branch: 1 branch
BranchTrueFalseTotal
if ($octave) 0 0 0
126
127 0 $p{pitch_mod} = 0;
128 0 ▶ $p{pitch_mod} = $mod eq 'is' ? 1 : -1 if $mod;
Branch: 2 branches
BranchTrueFalseTotal
if $mod 0 0 0
$mod eq 'is' ? : 0 0 0
129
130 0 $p{note} = $note;
131 0 $self->player->play( _pitch(\%p), _duration(\%p) );
132 }
133
134 0 ▶ select undef, undef, undef, $self->{rest} / 1000 if $self->{rest};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"rest"} 0 0 0
135 }
136 }
137  
138  
139 ### UTILITIES
140  
141 0 ▶sub _pitch {
Subroutine _pitch: not called CC=1 SCAR=6.9
142 0 my $p = shift;
143 return $PITCH[($NOTES{ $p->{note} } + $p->{pitch_mod}) % 12] *
144 0 (2 ** $p->{octave});
145 }
146  
147 0 ▶sub _duration {
Subroutine _duration: not called CC=3 SCAR=24.8
148 0 my $p = shift;
149 0 my $dur = 4 / $p->{duration};
150 0 ▶ if ( $p->{dot} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"dot"}) 0 0 0
151 0 my $half = $dur / 2;
152 0 ▶ for (my $i = $p->{dot}; $i--; ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i--) 0 0 0
153 0 $dur += $half;
154 0 $half /= 2;
155 }
156 }
157 0 return int( $dur * (60 / $p->{bpm}) * 1000 );
158 }
159  
160 1 ▶sub _best_player {
Subroutine _best_player: called CC=4 SCAR=14.1
161 1 my %os_modules = (
162 linux => [
163 'Audio::Beep::Linux::beep',
164 'Audio::Beep::Linux::PP',
165 ],
166 MSWin32 => [
167 'Audio::Beep::Win32::API',
168 ],
169 freebsd => [
170 'Audio::Beep::BSD::beep',
171 ],
172 );
173
174 1 for my $mod ( @{ $os_modules{$^O} } ) {
175 2 ▶ if (eval "require $mod") {
Branch: 1 branch
BranchTrueFalseTotal
if (eval "require $mod") 2 0 2
176 2 my $player = $mod->new();
177 2 ▶ return $player if defined $player;
Branch: 1 branch
BranchTrueFalseTotal
if defined $player 1 1 2
178 }
179 }
180  
181 0 return;
182 }
183  
184 0 ▶sub _player_from_string {
Subroutine _player_from_string: not called CC=2 SCAR=17.9
185 0 my ($mod) = @_;
186 0 my $pack = __PACKAGE__;
187 0 $mod =~ s/^(${pack}::)?/${pack}::/;
188 0 ▶ eval "require $mod" or croak "Cannot load $mod : $@";
Branch: 1 branch
BranchTrueFalseTotal
eval "require $mod" or croak("Cannot load $mod : $@") 0 0 0
189 0 return $mod->new();
190 }
191  
192  
193 ### EXPORTED FUNCTIONS
194  
195 { #SCOPE FOR CACHED PLAYER
196  
197 1 my $player;
198  
199 0 ▶sub beep {
Subroutine beep: not called CC=5 SCAR=34.0
Pod documented
200 0 my ($pitch, $duration) = @_;
201 0 ▶ $pitch ||= 440;
Condition0%0/2
$pitch ||= 440
l!l
00
MC/DC0%0/1
$pitch ||= 440
$pitch
Truth table0%0/2
A
A= $pitch
Aresult
11
00
202 0 ▶ $duration ||= 100;
Condition0%0/2
$duration ||= 100
l!l
00
MC/DC0%0/1
$duration ||= 100
$duration
Truth table0%0/2
A
A= $duration
Aresult
11
00
203 0 ▶ $player ||= _best_player() or croak "Couldn't find a working player";
Branch: 1 branch
BranchTrueFalseTotal
$player ||= _best_player() or croak("Couldn't find a working player") 0 0 0
Condition0%0/3
$player ||= _best_player()
l!l&&r!l&&!r
000
MC/DC0%0/2
$player ||= _best_player()
$player _best_player()
Truth table0%0/3
A ||= B
A= $playerB= _best_player()
ABresult
1X1
011
000
204 0 $player->play($pitch, $duration);
205 }
206  
207 }
208  
209 1 1;
↑ Summary Beep/Linux/PP.pm »