blib/lib/Audio/Analyzer.pm

filter: Statementstmt 76.8% 256 / 333 Branchbran 47.9% 46 / 96 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 83.9% 47 / 56 Podpod 41.6% 5 / 12 totaltotal 69.5% 356 / 512 CC 52 SCAR 27.0
CC
52
Cov
69%
init
60.3
get_fileless_samples
12.0
combine_fft
12.0
CRAP 14.9
↑ Summary
1 package Audio::Analyzer;
2  
3 1 our $VERSION = '0.22';
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Math::FFT;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use constant DEFAULT_DFT_SIZE => 2 ** 11;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use constant DEFAULT_SAMPLE_RATE => 44100;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use constant DEFAULT_CHANNELS => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use constant DEFAULT_BITS_PER_SAMPLE => 16;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 #value can be undef if fileless operation is being used
18 1 ▶use constant INPUT => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use constant FILE_NAME => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use constant DFT_SIZE => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use constant SEEK_STEP => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use constant READ_SIZE => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use constant CHANNELS => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use constant BYTES_PER_SAMPLE => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use constant SAMPLE_RATE => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use constant FFT => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use constant FREQ_CACHE => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use constant SCALER => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use constant BYTES => 11;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use constant EOF_FOUND => 12;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use constant FILELESS_BUFFER => 13;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
34 1 my ($class, %opts) = @_;
35  
36 1 my $self = [];
37  
38 1 bless($self, $class);
39  
40 1 $self->init(%opts);
41  
42 1 return $self;
43 }
44  
45 0 ▶sub add_samples {
Subroutine add_samples: not called CC=1 SCAR=6.9
Pod undocumented
46 0 my ($self, @samples) = @_;
47 0 push(@{$self->[FILELESS_BUFFER]}, @samples);
48 0 return;
49 }
50  
51 0 ▶sub sample_buffer_size {
Subroutine sample_buffer_size: not called CC=1 SCAR=6.9
Pod undocumented
52 0 my ($self) = @_;
53 0 return scalar(@{$self->[FILELESS_BUFFER]});
54 }
55  
56 33 ▶sub next {
Subroutine next: called CC=4 SCAR=14.7
Pod documented
57 33 my ($self) = @_;
58 33 my @samples;
59 my $chunk;
60
61 33 ▶ if (defined($self->[INPUT])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->[0]) { } 33 0 33
62 33 my $pcm = $self->read_pcm;
63
64 33 ▶ if (! defined($pcm)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pcm) 1 32 33
65 1 return undef;
66 }
67
68 32 @samples = $self->convert_pcm($pcm);
69
70 } else {
71 0 @samples = $self->get_fileless_samples;
72 0 ▶ return undef unless @samples;
Branch: 1 branch
BranchTrueFalseTotal
unless @samples 0 0 0
73 }
74  
75  
76 32 my $channels = $self->split_channels(@samples);
77  
78 32 $chunk = Audio::Analyzer::Chunk->new($self, $channels);
79  
80 32 return $chunk;
81 }
82  
83 0 ▶sub progress {
Subroutine progress: not called CC=1 SCAR=6.9
Pod documented
84 0 my ($self) = @_;
85 0 my $bytes = $self->[BYTES];
86 0 my $input = $self->[INPUT];
87 0 my $size = (stat($input))[7];
88  
89 0 return int($bytes / $size * 100);
90 }
91  
92 2 ▶sub freqs {
Subroutine freqs: called CC=3 SCAR=11.0
Pod documented
93 2 my ($self) = @_;
94 2 my $sample_rate = $self->[SAMPLE_RATE];
95 2 my $dft_size = $self->[DFT_SIZE];
96 2 my $freq_cache = $self->[FREQ_CACHE];
97 2 my @freqs;
98  
99 2 ▶ if (defined($freq_cache)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $freq_cache) 1 1 2
100 1 return $freq_cache;
101 }
102  
103 1 ▶ for(my $i = 0; $i < $dft_size / 2; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $dft_size / 2) 32 1 33
104 32 $freqs[$i] = $i / $dft_size * $sample_rate;
105 }
106  
107 1 $self->[FREQ_CACHE] = \@freqs;
108  
109 1 return $self->[FREQ_CACHE];
110 }
111  
112 #private interface starts here
113  
114 1 ▶sub init {
Subroutine init: called CC=21 SCAR=41.0
Pod undocumented
115 1 my ($self, %opts) = @_;
116 1 my $file;
117 my $dft_size;
118 1 my $seek_step;
119 1 my $channels;
120 1 my $bits_per_sample;
121 1 my $sample_rate;
122 1 my $read_size;
123 1 my $scaler;
124 1 my $fps;
125  
126 1 ▶ if (exists($opts{'file'})) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $opts{'file'}) { } 1 0 1
elsif (not defined $opts{'fileless'}) { } 0 0 0
127 1 ▶ if (! defined($file = $opts{'file'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($file = $opts{"file"})) 0 1 1
128 0 croak "if the file option is given it must have a value";
129 }
130 } elsif (! defined $opts{fileless}) {
131 0 croak "no file was specified and fileless operation is not configured";
132 }
133  
134 1 ▶ if (! defined($dft_size = $opts{'dft_size'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($dft_size = $opts{"dft_size"})) 0 1 1
135 0 $dft_size = DEFAULT_DFT_SIZE;
136 }
137  
138 1 ▶ if (! defined($sample_rate = $opts{'sample_rate'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($sample_rate = $opts{"sample_rate"})) 0 1 1
139 0 $sample_rate = DEFAULT_SAMPLE_RATE;
140 }
141  
142 1 ▶ if (! defined($channels = $opts{'channels'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($channels = $opts{"channels"})) 0 1 1
143 0 $channels = DEFAULT_CHANNELS;
144 }
145  
146 1 ▶ if (defined($bits_per_sample = $opts{'bits_per_sample'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($bits_per_sample = $opts{'bits_per_sample'})) { } 0 1 1
147 0 ▶ if ($bits_per_sample != 8 && $bits_per_sample != 16) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bits_per_sample != 8 and $bits_per_sample != 16) 0 0 0
Condition0%0/3
$bits_per_sample != 8 and $bits_per_sample != 16
!ll&&!rl&&r
000
MC/DC0%0/2
$bits_per_sample != 8 and $bits_per_sample != 16
$bits_per_sample != 8 $bits_per_sample != 16
Truth table0%0/3
A and B
A= $bits_per_sample != 8B= $bits_per_sample != 16
ABresult
0X0
100
111
148 0 croak("bits_per_sample must be 8 or 16");
149 }
150 } else {
151 1 $bits_per_sample = DEFAULT_BITS_PER_SAMPLE;
152 }
153  
154 1 $read_size = $dft_size * $channels * $bits_per_sample / 8;
155  
156 1 ▶ if (defined($fps = $opts{'fps'})) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined($fps = $opts{'fps'})) { } 0 1 1
elsif (defined $seek_step and not defined $file) { } 0 1 1
elsif (not defined($seek_step = $opts{'seek_step'})) { } 1 0 1
157 0 ▶ croak "unable to use audio/visual sync with fileless operation"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 0 0
158 unless defined $file;
159 0 $seek_step = $sample_rate / $fps * $bits_per_sample / 8 * $channels;
160 ▶ } elsif (defined $seek_step && ! defined $file) {
Condition33%1/3
defined $seek_step and not defined $file
!ll&&!rl&&r
100
MC/DC0%0/2
defined $seek_step and not defined $file
defined $seek_step not defined $file
Truth table33%1/3
A and not(B)
A= defined $seek_stepB= not defined $file
ABresult
0X0
100
111
161 0 croak "unable to use seek_step with fileless operation";
162 } elsif (! defined($seek_step = $opts{'seek_step'})) {
163 1 $seek_step = $read_size;
164 }
165  
166 1 ▶ if (defined($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 1 0 1
167 1 ▶ if (ref($file) eq 'GLOB') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file eq 'GLOB') { } 0 1 1
168 0 $self->[INPUT] = $file;
169 0 $self->[FILE_NAME] = scalar($file);
170 } else {
171 1 ▶ croak "could not open $file: $!" unless open(PCM, $file);
Branch: 1 branch
BranchTrueFalseTotal
unless open PCM, $file 0 1 1
172
173 1 $self->[INPUT] = \*PCM;
174 1 $self->[FILE_NAME] = $file;
175 }
176 }
177  
178 1 $self->[BYTES_PER_SAMPLE] = $bits_per_sample / 8;
179 1 $self->[CHANNELS] = $channels;
180 1 $self->[SAMPLE_RATE] = $sample_rate;
181 1 $self->[DFT_SIZE] = $dft_size;
182 1 $self->[SEEK_STEP] = $seek_step;
183 1 $self->[READ_SIZE] = $read_size;
184 1 $self->[BYTES] = 0;
185 1 $self->[EOF_FOUND] = 0;
186
187 1 ▶ unless(defined($file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 0 1 1
188 0 $self->[FILELESS_BUFFER] = [];
189 }
190  
191 1 ▶ if (! exists($opts{scaler})) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $opts{'scaler'}) { } 1 0 1
elsif (defined $opts{'scaler'}) { } 0 0 0
192 1 $scaler = Audio::Analyzer::ACurve->new($self);
193 } elsif(defined($opts{scaler})) {
194 0 my $requested = $opts{scaler};
195  
196 0 $scaler = $requested->new($self);
197 }
198  
199 1 $self->[SCALER] = $scaler;
200  
201 1 return $self;
202 }
203  
204 32 ▶sub split_channels {
Subroutine split_channels: called CC=2 SCAR=6.9
Pod undocumented
205 32 my ($self, @samples) = @_;
206 32 my $channels = $self->[CHANNELS];
207 32 my @split;
208 32 my $size = scalar(@samples);
209  
210 32 ▶ for(my $i = 0; $i < $size; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size) 2048 32 2080
211 2048 my $chan = int($i % $channels);
212 2048 push(@{$split[$chan]}, $samples[$i]);
213 }
214  
215 32 return \@split;
216 }
217  
218  
219 #converts PCM into floating point representation
220 32 ▶sub convert_pcm {
Subroutine convert_pcm: called CC=3 SCAR=11.1
Pod undocumented
221 32 my ($self, $pcm) = @_;
222 32 my $bytes_per_sample = $self->[BYTES_PER_SAMPLE];
223 32 my @samples;
224  
225 32 ▶ if ($bytes_per_sample == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes_per_sample == 2) { } 32 0 32
226 32 ▶ while(length($pcm) >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $pcm >= 2) 2048 32 2080
227 2048 my $sample = unpack('s<', substr($pcm, 0, 2, ''));
228 2048 push(@samples, $sample);
229 }
230 } else {
231 0 die "8 bit PCM isn't implemented yet";
232 }
233  
234 32 return @samples;
235 }
236  
237 0 ▶sub get_fileless_samples {
Subroutine get_fileless_samples: not called CC=3 SCAR=24.8
Pod undocumented
238 0 my ($self) = @_;
239 0 my $input = $self->[INPUT];
240 0 my $read_size = $self->[READ_SIZE];
241 0 my $fileless_buffer = $self->[FILELESS_BUFFER];
242 0 my $samples_needed = $read_size / $self->[BYTES_PER_SAMPLE];
243
244 0 ▶ if (defined $input) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input) 0 0 0
245 0 die "read_buffer() was called for fileless operation but there was an input file ref?";
246 }
247  
248 0 ▶ return () unless scalar(@$fileless_buffer) >= $samples_needed;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$fileless_buffer >= $samples_needed 0 0 0
249 0 return splice(@$fileless_buffer, 0, $samples_needed);
250 }
251  
252 34 ▶sub read_pcm {
Subroutine read_pcm: called CC=9 SCAR=23.3
Pod undocumented
253 34 my ($self) = @_;
254 34 my $input = $self->[INPUT];
255 34 my $read_size = $self->[READ_SIZE];
256 34 my $seek_step = $self->[SEEK_STEP];
257 34 my $bytes = $self->[BYTES];
258 34 my $EOF_found = $self->[EOF_FOUND];
259 34 my $buf;
260 my $ret;
261 34 my $rewind;
262
263 34 ▶ die "there was no input filehandle ref" unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 34 34
264
265 34 $ret = read($input, $buf, $read_size);
266  
267 34 ▶ if (! defined($ret)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $ret) { } 0 34 34
elsif ($ret == 0) { } 1 33 34
elsif ($ret < $read_size) { } 1 32 33
268 0 die "could not read: $!";
269 } elsif ($ret == 0) {
270 1 return undef;
271 } elsif ($ret < $read_size) {
272 #hit the end and did not get enough data for the FFT - seek
273 #backwards a whole read_size and finish the last reading
274 #as best as possible
275 1 my $size = (stat($input))[7];
276
277 1 $self->[EOF_FOUND] = 1;
278  
279 1 ▶ seek($input, $size - $read_size, 0) or die "could not seek: $!";
Branch: 1 branch
BranchTrueFalseTotal
seek $input, $size - $read_size, 0 or die "could not seek: $!" 1 0 1
280  
281 1 return $self->read_pcm;
282 }
283  
284 32 $bytes += $seek_step;
285  
286 32 $rewind = $read_size - $seek_step;
287  
288 32 ▶ if ($rewind && ! $EOF_found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rewind and not $EOF_found) 0 32 32
Condition33%1/3
$rewind and not $EOF_found
!ll&&!rl&&r
3200
MC/DC0%0/2
$rewind and not $EOF_found
$rewind not $EOF_found
Truth table33%1/3
A and not(B)
A= $rewindB= not $EOF_found
ABresult
0X0
100
111
289 0 ▶ seek($input, $rewind * -1, 1) or die "could not seek: $!";
Branch: 1 branch
BranchTrueFalseTotal
seek $input, $rewind * -1, 1 or die "could not seek: $!" 0 0 0
290 }
291  
292 32 $self->[BYTES] = $bytes;
293  
294 32 return $buf;
295 }
296  
297 32 ▶sub scaler {
Subroutine scaler: called CC=1 SCAR=0.0
Pod documented
298 32 my ($self) = @_;
299
300 32 return $self->[SCALER];
301 }
302  
303 package Audio::Analyzer::Chunk;
304  
305 1 our $VERSION = '0.02';
306  
307 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
308 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
309  
310 32 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
311 32 my ($class, $analyzer, $channels) = @_;
312 32 my $self = {};
313  
314 32 $self->{analyzer} = $analyzer;
315 32 $self->{channels} = $channels;
316  
317 32 bless($self, $class);
318  
319 32 return $self;
320 }
321  
322 0 ▶sub pcm {
Subroutine pcm: not called CC=1 SCAR=6.9
323 0 my ($self) = @_;
324  
325 0 return $self->{channels};
326 }
327  
328 32 ▶sub fft {
Subroutine fft: called CC=2 SCAR=6.9
329 32 my ($self) = @_;
330 32 my $channels = $self->{channels};
331 32 my @mags;
332  
333 32 ▶ for(my $i = 0; $i < scalar(@$channels); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$channels) 32 32 64
334 32 $mags[$i] = $self->do_fft($channels->[$i]);
335 }
336  
337 32 return \@mags;
338 }
339  
340 0 ▶sub rms {
Subroutine rms: not called CC=2 SCAR=17.9
341 0 my $self = shift(@_);
342 0 my $size = scalar(@_);
343 0 my $sum;
344  
345 0 ▶ for(my $i = 0; $i < $size; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size) 0 0 0
346 0 $sum += $_[$i] ** 2;
347 }
348  
349 0 $sum /= $size;
350  
351 0 return sqrt($sum);
352 }
353  
354 0 ▶sub combine_fft {
Subroutine combine_fft: not called CC=3 SCAR=24.8
355 0 my ($self, $channels) = @_;
356 0 my $num_channels = scalar(@$channels);
357 0 my $length = scalar(@{$channels->[0]});
358 0 my @new;
359  
360 0 ▶ for(my $i = 0; $i < $length; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $length) 0 0 0
361 0 my @row;
362  
363 0 ▶ for(my $j = 0; $j < $num_channels; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $num_channels) 0 0 0
364 0 push(@row, $channels->[$j][$i]);
365 }
366  
367 0 $new[$i] = $self->rms(@row);
368 }
369  
370 0 return \@new;
371 }
372  
373 32 ▶sub analyzer {
Subroutine analyzer: called CC=1 SCAR=0.0
374 32 my ($self) = @_;
375  
376 32 return $self->{analyzer};
377 }
378  
379 #private methods
380  
381 32 ▶sub do_fft {
Subroutine do_fft: called CC=2 SCAR=6.9
382 32 my ($self, $samples) = @_;
383 32 my $fft = Math::FFT->new($samples);
384 32 my $coeff = $fft->rdft;
385 32 my $size = scalar(@$coeff);
386 32 my $k = 0;
387 32 my @mag;
388  
389 32 $mag[$k] = sqrt($coeff->[$k*2]**2);
390  
391 32 ▶ for($k = 1; $k < $size / 2; $k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $size / 2) 992 32 1024
392 992 $mag[$k] = sqrt(($coeff->[$k * 2] ** 2) + ($coeff->[$k * 2 + 1] ** 2));
393 }
394  
395 32 $self->scale(\@mag);
396  
397 32 return \@mag;
398 }
399  
400 32 ▶sub scale {
Subroutine scale: called CC=2 SCAR=7.0
401 32 my ($self, $mags) = @_;
402 32 my $scaler = $self->analyzer->scaler;
403
404 32 ▶ if (defined($scaler)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $scaler) 32 0 32
405 32 $scaler->scale($mags);
406 }
407 }
408  
409 package Audio::Analyzer::ACurve;
410  
411 1 our $VERSION = '0.02';
412  
413 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
414 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
415  
416 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
417
418 1 ▶use constant SCALE => 5000000; #tested by running some Prodigy
Subroutine BEGIN: called CC=1 SCAR=0.0
419 #through the system
420  
421 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
422 1 my ($class, $analyzer) = @_;
423 1 my $self = {};
424  
425 1 $self->{analyzer} = $analyzer;
426  
427 1 ▶ if (! defined($analyzer)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $analyzer) 0 1 1
428 0 croak "I need an analyzer";
429 }
430  
431 1 bless($self, $class);
432  
433 1 $self->init;
434  
435 1 return $self;
436 }
437  
438 1 ▶sub init {
Subroutine init: called CC=3 SCAR=11.1
439 1 my ($self) = @_;
440 1 my $analyzer = $self->{analyzer};
441 1 my @correction;
442 1 my $freqs = $analyzer->freqs;
443  
444 1 ▶ for(my $i = 0; $i < scalar(@$freqs); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$freqs) 32 1 33
445 32 my $freq = $freqs->[$i];
446
447 32 ▶ if ($freq < 10000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($freq < 10000) { } 32 0 32
448 32 $correction[$i] = $self->solve_one_A($freq);
449 } else {
450 0 $correction[$i] = 1;
451 }
452  
453 }
454  
455 1 $self->{correction} = \@correction;
456 }
457  
458 32 ▶sub solve_one_A {
Subroutine solve_one_A: called CC=1 SCAR=0.0
459 32 my ($self, $freq) = @_;
460 32 my $term_1 = ($freq ** 2) + (20.6 ** 2);
461 32 my $term_2 = ($freq ** 2) + (12200 ** 2);
462 32 my $term_3 = sqrt(($freq ** 2) + (107.7 ** 2));
463 32 my $term_4 = sqrt(($freq ** 2) + (737.9 ** 2));
464
465 32 return (12200 ** 2) * ($freq ** 4) / ($term_1 * $term_2 * $term_3 * $term_4);
466 }
467  
468 32 ▶sub scale {
Subroutine scale: called CC=3 SCAR=11.1
469 32 my ($self, $mags) = @_;
470 32 my $correction = $self->{correction};
471 32 my $size = scalar(@$mags);
472  
473 32 ▶ for(my $i = 0; $i < $size; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size) 1024 32 1056
474 1024 $mags->[$i] *= $correction->[$i];
475 1024 $mags->[$i] /= SCALE;
476  
477 1024 ▶ if ($mags->[$i] > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mags->[$i] > 1) 0 1024 1024
478 0 $mags->[$i] = 1;
479 }
480 }
481 }
482  
483 package Audio::Analyzer::AutoScaler;
484  
485 1 our $VERSION = '0.02';
486  
487 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
488 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
489  
490 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
491 0 my ($class) = @_;
492 0 my $self = {};
493  
494 0 $self->{peak} = 0;
495  
496 0 bless($self, $class);
497  
498 0 return $self;
499 }
500  
501 0 ▶sub scale {
Subroutine scale: not called CC=3 SCAR=24.8
502 0 my ($self, $readings) = @_;
503 0 my $size = scalar(@$readings);
504  
505 0 ▶ for(my $i = 0; $i < $size; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size) 0 0 0
506 0 my $one = $readings->[$i];
507  
508 0 ▶ if ($one > $self->{peak}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($one > $self->{"peak"}) 0 0 0
509 0 $self->{peak} = $one;
510 }
511  
512 0 $one /= $self->{peak};
513  
514 0 $readings->[$i] = $one;
515 }
516 }
517  
518 1 1;
519  
520 __END__
↑ Summary