File Coverage

blib/lib/ByteBeat.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package ByteBeat;
2             our $VERSION = '0.0.2';
3              
4 1     1   1231 use Mo;
  1         412  
  1         4  
5 1     1   815 use Getopt::Long;
  1         9362  
  1         5  
6 1     1   604 use ByteBeat::Compiler;
  1         2  
  1         59  
7              
8             has args => ();
9              
10             my $code = '';
11             my $file;
12             my $second = 2**13;
13             my $length = 0;
14             my $debug = 0;
15             my $shell = 0;
16             my $play = 0;
17              
18 1     1   202 use XXX;
  0            
  0            
19             sub run {
20             my ($self) = shift;
21             $self->get_options;
22              
23             if ($shell or not $code) {
24             require ByteBeat::Shell;
25             ByteBeat::Shell->new(file => $file)->run;
26             return;
27             }
28              
29             my $function = ByteBeat::Compiler->new(code => $code)->compile;
30              
31             if ($debug) {
32             print "RPN: @{$function->{rpn}}\n";
33             for (my $t = 1; $t <= $length; $t++) {
34             print $function->run($t) % 256, "\n";
35             }
36             return;
37             }
38              
39             $length ||= 60 * $second;
40              
41             if ($play) {
42             require IPC::Run;
43             my $bytes;
44             my $out;
45              
46             my $process = IPC::Run::start(['aplay'], \$bytes, \$out, \$out);
47             for (my $t = 1; $t <= $length; $t++) {
48             $bytes .= chr ($function->run($t) % 256);
49             IPC::Run::pump($process);
50             }
51             }
52              
53             for (my $t = 1; $t <= $length; $t++) {
54             print chr ($function->run($t) % 256);
55             }
56             }
57              
58             sub get_options {
59             my ($self) = shift;
60             local @ARGV = @{$self->args};
61             GetOptions(
62             'file=s' => \$file,
63             'length=s' => \$length,
64             'debug' => \$debug,
65             'shell' => \$shell,
66             'play' => \$play,
67             );
68             $length =
69             $length =~ /^(\d+)s$/ ? $1 * $second :
70             $length =~ /^(\d+)m$/ ? $1 * $second * 60 :
71             $length =~ /^(\d+)$/ ? $1 :
72             die "Invalid value for '-l'\n";
73             $code = @ARGV ? shift(@ARGV) : -t STDIN ? '' : <>;
74             chomp $code;
75             }
76              
77             1;