blib/lib/Audio/Chromaprint.pm

filter: Statementstmt 77.0% 84 / 109 Branchbran 28.5% 12 / 42 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 70.5% 24 / 34 Podpod 88.8% 16 / 18 totaltotal 63.8% 136 / 213 CC 25 SCAR 27.8
CC
25
Cov
61%
set_option
90.0
__ANON__
6.0
get_raw_fingerprint_size
6.0
CRAP 16.1
↑ Summary
1 package Audio::Chromaprint;
2 # ABSTRACT: Interface to the Chromaprint library
3  
4 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp qw< croak >;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use FFI::Platypus 0.88;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use FFI::CheckLib;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # This is in three statement so we could support 5.6.0,
11 # since hash version only came out in 5.8.0.
12 2 ▶use constant 'MIN_SILENCE_THRESHOLD' => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use constant 'MAX_SILENCE_THRESHOLD' => 32_767;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use constant 'BYTES_PER_SAMPLE' => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 our $HAS_SUBS;
17 2 our %SUBS = (
18 '_new' => [ ['int'] => 'opaque' ],
19 '_get_version' => [ [] => 'string' ],
20 '_free' => [ ['opaque'] => 'void' ],
21 '_set_option' => [ [ 'opaque', 'string', 'int' ] => 'int' ],
22 '_start' => [ [ 'opaque', 'int', 'int' ] => 'int' ],
23 '_finish' => [ ['opaque'] => 'int' ],
24 '_feed' => [ ['opaque', 'string', 'int' ] => 'int' ],
25  
26 '_get_fingerprint_hash' => [ [ 'opaque', 'uint32*' ], 'int' ],
27 '_get_fingerprint' => [ [ 'opaque', 'opaque*' ], 'int' ],
28 '_get_raw_fingerprint' => [ [ 'opaque', 'opaque*', 'int*' ], 'int' ],
29 '_get_num_channels' => [ [ 'opaque' ], 'int' ],
30 '_get_sample_rate' => [ [ 'opaque' ], 'int' ],
31 '_get_item_duration' => [ [ 'opaque' ], 'int' ],
32 '_get_item_duration_ms' => [ [ 'opaque' ], 'int' ],
33 '_get_delay' => [ [ 'opaque' ], 'int' ],
34 '_get_delay_ms' => [ [ 'opaque' ], 'int' ],
35 '_get_raw_fingerprint_size' => [ [ 'opaque', 'int*' ], 'int' ],
36 '_clear_fingerprint' => [ [ 'opaque' ], 'int' ],
37  
38 '_dealloc' => [ [ 'opaque' ] => 'void' ],
39 );
40  
41 5 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
42 5 ▶ $HAS_SUBS++
Branch: 1 branch
BranchTrueFalseTotal
$Audio::Chromaprint::HAS_SUBS++ and return 3 2 5
43 and return;
44  
45 2 my $ffi = FFI::Platypus->new;
46  
47 # Setting this mangler lets is omit the chromaprint_ prefix
48 # from the attach call below, and the function names used
49 # by perl
50 38 ▶ $ffi->mangler( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
51 38 my $name = shift;
52 38 $name =~ s/^_/chromaprint_/xms;
53 38 return $name;
54 2 } );
55  
56 2 $ffi->lib( find_lib_or_exit( 'lib' => 'chromaprint', alien => 'Alien::chromaprint' ) );
57  
58 38 $ffi->attach( $_, @{ $SUBS{$_} } )
59 2 for keys %SUBS;
60  
61 2 $ffi->attach_cast( '_opaque_to_string' => opaque => 'string' );
62 }
63  
64 subtype 'ChromaprintAlgorithm',
65 as 'Int',
66 5 ▶ where { /^[1234]$/xms },
Subroutine __ANON__: called CC=1 SCAR=0.0
67 0 ▶ message { 'algorithm must be 1, 2, 3 or 4' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
68  
69 subtype 'ChromaprintSilenceThreshold',
70 as 'Int',
71 0 ▶ where { $_ >= MIN_SILENCE_THRESHOLD() && $_ <= MAX_SILENCE_THRESHOLD() },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_ >= 0 and $_ <= 32767 0 0 0
72 0 ▶ message { 'silence_threshold option must be between 0 and 32767' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
73  
74 has 'algorithm' => (
75 'is' => 'ro',
76 'isa' => 'ChromaprintAlgorithm',
77 2 ▶ 'default' => sub {2},
Subroutine __ANON__: called CC=1 SCAR=0.0
78 2 );
79  
80 has 'cp' => (
81 'is' => 'ro',
82 'lazy' => 1,
83 'init_arg' => undef,
84 5 ▶ 'default' => sub {
Subroutine __ANON__: called CC=3 SCAR=13.3
85 5 my $self = shift;
86  
87 # subtract one from the algorithm so that
88 # 1 maps to 2 maps to CHROMAPRINT_ALGORITHM_TEST2
89 # (the latter has the value 1)
90 5 my $cp = _new( $self->algorithm - 1 );
91  
92 5 ▶ if ( $self->has_silence_threshold ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_silence_threshold) 0 5 5
93 0 ▶ _set_option(
Branch: 1 branch
BranchTrueFalseTotal
_set_option($cp, "silence_threshold", $self->silence_threshold) or croak("Error setting option silence_threshold") 0 0 0
94 $cp, 'silence_threshold' => $self->silence_threshold,
95 ) or croak('Error setting option silence_threshold');
96 }
97  
98 5 return $cp;
99 }
100 2 );
101  
102 2 has 'silence_threshold' => (
103 'is' => 'ro',
104 'isa' => 'ChromaprintSilenceThreshold',
105 'predicate' => 'has_silence_threshold',
106 );
107  
108 1 ▶sub get_version {
Subroutine get_version: called CC=2 SCAR=7.2
Pod documented
109 # generate chromaprint object
110 1 ▶ __PACKAGE__->can('_get_version')
Branch: 1 branch
BranchTrueFalseTotal
__PACKAGE__->can("_get_version") or __PACKAGE__->new 0 1 1
111 or __PACKAGE__->new();
112  
113 1 return _get_version();
114 }
115  
116 4 ▶sub start {
Subroutine start: called CC=4 SCAR=14.9
Pod documented
117 4 my ( $self, $sample_rate, $num_channels ) = @_;
118  
119 4 ▶ $sample_rate =~ /^[0-9]+$/xms
Branch: 1 branch
BranchTrueFalseTotal
$sample_rate =~ /^[0-9]+$/msx or croak("sample_rate must be an integer") 4 0 4
120 or croak 'sample_rate must be an integer';
121  
122 4 ▶ $num_channels =~ /^[12]$/xms
Branch: 1 branch
BranchTrueFalseTotal
$num_channels =~ /^[12]$/msx or croak("num_channels must be 1 or 2") 4 0 4
123 or croak 'num_channels must be 1 or 2';
124  
125 4 ▶ _start( $self->cp, $sample_rate, $num_channels )
Branch: 1 branch
BranchTrueFalseTotal
_start($self->cp, $sample_rate, $num_channels) or croak("Unable to start (start)") 4 0 4
126 or croak 'Unable to start (start)';
127 }
128  
129 0 ▶sub set_option {
Subroutine set_option: not called CC=9 SCAR=45.0
Pod documented
130 0 my ( $self, $name, $value ) = @_;
131  
132 0 ▶ $name && $value
Branch: 1 branch
BranchTrueFalseTotal
$name and $value or croak("set_option( name, value )") 0 0 0
Condition0%0/3
$name and $value
!ll&&!rl&&r
000
MC/DC0%0/2
$name and $value
$name $value
Truth table0%0/3
A and B
A= $nameB= $value
ABresult
0X0
100
111
133 or croak('set_option( name, value )');
134  
135 0 ▶ length $name
Branch: 1 branch
BranchTrueFalseTotal
length $name or croak("set_option requires a \"name\" string") 0 0 0
136 or croak('set_option requires a "name" string');
137  
138 0 ▶ $value =~ /^[0-9]+$/xms
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /^[0-9]+$/msx or croak("set_option requires a \"value\" integer") 0 0 0
139 or croak('set_option requires a "value" integer');
140  
141 0 ▶ if ( $name eq 'silence_threshold' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "silence_threshold") 0 0 0
142 0 ▶ $value >= MIN_SILENCE_THRESHOLD() && $value <= MAX_SILENCE_THRESHOLD()
Branch: 1 branch
BranchTrueFalseTotal
$value >= 0 and $value <= 32767 or croak("silence_threshold option must be between 0 and 32767") 0 0 0
Condition0%0/3
$value >= 0 and $value <= 32767
!ll&&!rl&&r
000
MC/DC0%0/2
$value >= 0 and $value <= 32767
$value >= 0 $value <= 32767
Truth table0%0/3
A and B
A= $value >= 0B= $value <= 32767
ABresult
0X0
100
111
143 or croak('silence_threshold option must be between 0 and 32767');
144 }
145  
146 0 ▶ _set_option( $self->cp, $name => $value )
Branch: 1 branch
BranchTrueFalseTotal
_set_option($self->cp, $name, $value) or croak("Error setting option $name (set_option)") 0 0 0
147 or croak("Error setting option $name (set_option)");
148 }
149  
150 4 ▶sub finish {
Subroutine finish: called CC=2 SCAR=7.2
Pod documented
151 4 my $self = shift;
152 4 ▶ _finish( $self->cp )
Branch: 1 branch
BranchTrueFalseTotal
_finish($self->cp) or croak("Unable to finish (finish)") 4 0 4
153 or croak('Unable to finish (finish)');
154 }
155  
156 2 ▶sub get_fingerprint_hash {
Subroutine get_fingerprint_hash: called CC=2 SCAR=7.0
Pod documented
157 2 my $self = shift;
158 2 my $hash;
159 2 ▶ _get_fingerprint_hash( $self->cp, \$hash )
Branch: 1 branch
BranchTrueFalseTotal
_get_fingerprint_hash($self->cp, \$hash) or croak("Unable to get fingerprint hash (get_fingerprint_hash)") 2 0 2
160 or croak('Unable to get fingerprint hash (get_fingerprint_hash)');
161 2 return $hash;
162 }
163  
164 2 ▶sub get_fingerprint {
Subroutine get_fingerprint: called CC=2 SCAR=7.0
Pod documented
165 2 my $self = shift;
166 2 my $ptr;
167 2 ▶ _get_fingerprint($self->cp, \$ptr)
Branch: 1 branch
BranchTrueFalseTotal
_get_fingerprint($self->cp, \$ptr) or croak("Unable to get fingerprint (get_fingerprint)") 2 0 2
168 or croak('Unable to get fingerprint (get_fingerprint)');
169 2 my $str = _opaque_to_string($ptr);
170 2 _dealloc($ptr);
171 2 return $str;
172 }
173  
174 1 ▶sub get_raw_fingerprint {
Subroutine get_raw_fingerprint: called CC=2 SCAR=7.0
Pod documented
175 1 my $self = shift;
176 1 my ( $ptr, $size );
177  
178 1 ▶ _get_raw_fingerprint( $self->cp, \$ptr, \$size )
Branch: 1 branch
BranchTrueFalseTotal
_get_raw_fingerprint($self->cp, \$ptr, \$size) or croak("Unable to get raw fingerprint (get_raw_fingerprint)") 1 0 1
179 or croak('Unable to get raw fingerprint (get_raw_fingerprint)');
180  
181 # not espeically fast, but need a cast with a variable length array
182 1 my $fp = FFI::Platypus->new->cast( 'opaque' => "uint32[$size]", $ptr );
183 1 _dealloc($ptr);
184 1 return $fp;
185 }
186  
187 1 ▶sub get_num_channels {
Subroutine get_num_channels: called CC=1 SCAR=0.0
Pod documented
188 1 my $self = shift;
189 1 return _get_num_channels($self->cp);
190 }
191  
192 1 ▶sub get_sample_rate {
Subroutine get_sample_rate: called CC=1 SCAR=0.0
Pod documented
193 1 my $self = shift;
194 1 return _get_sample_rate($self->cp);
195 }
196  
197 0 ▶sub get_item_duration {
Subroutine get_item_duration: not called CC=1 SCAR=6.9
Pod documented
198 0 my $self = shift;
199 0 return _get_item_duration($self->cp);
200 }
201  
202 0 ▶sub get_item_duration_ms {
Subroutine get_item_duration_ms: not called CC=1 SCAR=6.9
Pod documented
203 0 my $self = shift;
204 0 return _get_item_duration_ms($self->cp);
205 }
206  
207 0 ▶sub get_delay {
Subroutine get_delay: not called CC=1 SCAR=6.9
Pod documented
208 0 my $self = shift;
209 0 return _get_delay($self->cp);
210 }
211  
212 0 ▶sub get_delay_ms {
Subroutine get_delay_ms: not called CC=1 SCAR=6.9
Pod documented
213 0 my $self = shift;
214 0 return _get_delay_ms($self->cp);
215 }
216  
217 0 ▶sub get_raw_fingerprint_size {
Subroutine get_raw_fingerprint_size: not called CC=2 SCAR=17.9
Pod documented
218 0 my $self = shift;
219 0 my $size;
220 0 ▶ _get_raw_fingerprint_size($self->cp, \$size)
Branch: 1 branch
BranchTrueFalseTotal
_get_raw_fingerprint_size($self->cp, \$size) or croak("Unable to get raw fingerprint size (get_raw_fingerprint_size)") 0 0 0
221 or croak('Unable to get raw fingerprint size (get_raw_fingerprint_size)');
222 0 return $size;
223 }
224  
225 0 ▶sub clear_fingerprint {
Subroutine clear_fingerprint: not called CC=2 SCAR=17.9
Pod documented
226 0 my $self = shift;
227 0 ▶ _clear_fingerprint( $self->cp )
Branch: 1 branch
BranchTrueFalseTotal
_clear_fingerprint($self->cp) or croak("Unable to clear fingerprint (clear_fingerprint)") 0 0 0
228 or croak('Unable to clear fingerprint (clear_fingerprint)');
229 }
230  
231 261 ▶sub feed {
Subroutine feed: called CC=2 SCAR=7.2
Pod documented
232 261 my ( $self, $data ) = @_;
233 261 ▶ _feed( $self->cp, $data, length($data) / BYTES_PER_SAMPLE() )
Branch: 1 branch
BranchTrueFalseTotal
_feed($self->cp, $data, length($data) / 2) or corak("unable to feed") 261 0 261
234 or corak("unable to feed");
235 }
236  
237 5 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=1 SCAR=0.0
Pod undocumented
238 5 my $self = shift;
239 5 _free( $self->cp );
240 }
241  
242 # TODO: chromaprint_encode_fingerprint
243 # TODO: chromaprint_decode_fingerprint
244 # TODO: chromaprint_hash_fingerprint
245  
246 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
247 2 __PACKAGE__->meta->make_immutable;
248  
249 2 1;
250  
251 __END__
↑ Summary