File Coverage

lib/Music/AirGuitar/Performance.pm
Criterion Covered Total %
statement 17 43 39.5
branch 0 8 0.0
condition 0 2 0.0
subroutine 6 11 54.5
pod 2 2 100.0
total 25 66 37.8


line stmt bran cond sub pod time code
1             # Copyright (c) 2025 Philipp Schafft
2              
3             # licensed under Artistic License 2.0 (see LICENSE file)
4              
5             # ABSTRACT: Interface for air guitars performances
6              
7              
8             package Music::AirGuitar::Performance;
9              
10 1     1   1433 use v5.16;
  1         3  
11 1     1   6 use strict;
  1         2  
  1         51  
12 1     1   6 use warnings;
  1         2  
  1         49  
13              
14 1     1   6 use Carp;
  1         1  
  1         146  
15              
16 1     1   8 use Music::AirGuitar;
  1         2  
  1         65  
17              
18             our $VERSION = v0.04;
19              
20             {
21 1     1   8 no strict 'refs'; # This is an invalid access, but it is the only one working in perl v5.24.1, the correct one segfaults.
  1         2  
  1         635  
22             foreach my $attribute (qw(guitar duration)) {
23 0     0     *$attribute = sub { my ($self, @args) = @_; return $self->Music::AirGuitar::_attribute($attribute, @args); };
  0            
24             }
25              
26             # proxy methods
27             foreach my $attribute (qw(player)) {
28 0     0     *$attribute = sub { my ($self, @args) = @_; return $self->guitar->_attribute($attribute, @args); };
  0            
29             }
30             }
31              
32              
33             sub record {
34 0     0 1   my ($self, $fn) = @_;
35 0           my $filelen = ($self->duration * 48000 * 1 * (16/8)) + 44;
36 0           my $fh;
37              
38 0 0         if (ref $fn) {
39 0           $fh = $fn;
40             } else {
41 0 0         open($fh, '>', $fn) or croak 'Cannot open file: '.$fn.': '.$!;
42             }
43              
44 0           $fh->binmode;
45 0           $fh->print(pack('a4Va4', 'RIFF', $filelen - 8, 'WAVE'));
46 0           $fh->print(pack('a4VvvVVvv', 'fmt ', 16, 1, 1, 48000, 48000 * 1 * 16/8, 1 * 16/8, 16));
47 0           $fh->print(pack('a4V', 'data', $filelen - 44));
48 0           $fh->truncate($filelen);
49              
50 0           return undef;
51             }
52              
53              
54             # ---- special getters ----
55              
56              
57             sub rating {
58 0     0 1   my ($self, %opts) = @_;
59 0   0       my $scale = $opts{scale} // 'of10';
60 0           my $rating = $self->{rating};
61              
62 0 0         if ($scale eq 'of10') {
63 0           return sprintf('%2.1f', $rating * 10);
64             }
65              
66 0           croak 'Unknown/unsupported scale: '.$scale;
67             }
68              
69             # ---- Private helpers ----
70              
71             sub _new {
72 0     0     my ($pkg, %opts) = @_;
73              
74             # Find a rating, be a bit more on the positive side.
75 0           $opts{rating} = rand(1.2);
76 0 0         $opts{rating} = 1 if $opts{rating} > 1;
77              
78 0           my $self = bless \%opts, $pkg;
79             }
80              
81              
82             1;
83              
84             __END__