lib/Peta/Synth/Analysis.pm

filter: Statementstmt 99.2% 248 / 250 Branchbran 63.5% 61 / 96 Conditioncond 45.0% 32 / 71 MC/DCmcdc 7.3% 3 / 41 Subroutinesub 100% 24 / 24 Podpod 100% 14 / 14 totaltotal 77.0% 382 / 496 CC 111 SCAR 24.5
CC
111
Cov
82%
_samples
29.1
response
17.5
read_wav
14.7
CRAP 11.6
« lib/Peta/Synth/MIDI/Stream.pm ↑ Summary lib/Peta/Vorbis/Encode/Psy.pm »
1 package Peta::Synth::Analysis;
2 # ABSTRACT: measuring sound: a note's partials, its level and brightness over time
3  
4 # Measuring sound: what a note's partials are and do, how loud and how
5 # bright it is over time; to compare this synthesizer's instruments with
6 # recordings (and with what FluidSynth plays from a SoundFont, see
7 # Peta::Synth::Fluid) and with themselves.
8 #
9 # Sound is an array of samples (floats, full scale 1) at a rate. Levels are
10 # in dB of amplitude: a sine of amplitude 1 is 0 dB, as its partial and as
11 # its peak; RMS levels are of the samples (a sine of amplitude 1 is -3 dB).
12  
13 2 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Peta::Synth::FFT;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 our $VERSION = '0.2610180';
17  
18 2 ▶use constant PI => 3.141592653589793;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # A WAV file's sample formats.
21 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 PCM => 1,
23 FLOAT => 3,
24 EXTENSIBLE => 0xfffe, # its own format a few bytes further on
25 2 };
26  
27 # A WAV file: { rate, channels => [ [samples], ... ] }. PCM of 8, 16, 24 or
28 # 32 bits, or floats of 32 or 64; WAVE_FORMAT_EXTENSIBLE as the format it
29 # extends.
30 3 ▶sub read_wav ($file) {
Subroutine read_wav: called CC=13 SCAR=26.9
Pod documented
31 3 ▶ open my $fh, '<:raw', $file or die "cannot read '$file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or die "cannot read '${file}': $!\n" 3 0 3
32 3 my $data = do { local $/; <$fh> };
33 3 ▶ die "$file: not a WAV file\n" unless substr($data, 0, 4) eq 'RIFF' && substr($data, 8, 4) eq 'WAVE';
Branch: 1 branch
BranchTrueFalseTotal
unless substr($data, 0, 4) eq "RIFF" and substr($data, 8, 4) eq "WAVE" 0 3 3
Condition33%1/3
substr($data, 0, 4) eq "RIFF" and substr($data, 8, 4) eq "WAVE"
!ll&&!rl&&r
003
MC/DC0%0/2
substr($data, 0, 4) eq "RIFF" and substr($data, 8, 4) eq "WAVE"
substr($data, 0, 4) eq "RIFF" substr($data, 8, 4) eq "WAVE"
Truth table33%1/3
A and B
A= substr($data, 0, 4) eq "RIFF"B= substr($data, 8, 4) eq "WAVE"
ABresult
0X0
100
111
34  
35 3 my ($format, $count, $rate, $bits, $samples);
36 3 my $pos = 12;
37 3 ▶ while ($pos + 8 <= length $data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos + 8 <= length $data) 6 3 9
38 6 my ($id, $size) = unpack 'a4 V', substr $data, $pos, 8;
39 6 ▶ $size = length($data) - $pos - 8 if $pos + 8 + $size > length $data; # a size never set (a pipe)
Branch: 1 branch
BranchTrueFalseTotal
if $pos + 8 + $size > length $data 0 6 6
40 6 ▶ if ($id eq 'fmt ') {
Branch: 2 branches
BranchTrueFalseTotal
if ($id eq 'fmt ') { } 3 3 6
elsif ($id eq 'data') { } 3 0 3
41 3 ($format, $count, $rate, $bits) = unpack 'v v V x6 v', substr $data, $pos + 8, 16;
42 3 ▶ $format = unpack 'v', substr $data, $pos + 32, 2 if $format == EXTENSIBLE;
Branch: 1 branch
BranchTrueFalseTotal
if $format == 65534 0 3 3
43 }
44 elsif ($id eq 'data') {
45 3 ▶ die "$file: data before the format\n" if !$format;
Branch: 1 branch
BranchTrueFalseTotal
unless $format 0 3 3
46 3 ▶ $samples = _samples($format, $bits, substr($data, $pos + 8, $size))
Condition50%1/2
_samples($format, $bits, substr($data, $pos + 8, $size)) // die("${file}: format $format with $bits bits is not read here\n")
l!l
30
MC/DC0%0/1
_samples($format, $bits, substr($data, $pos + 8, $size)) // die("${file}: format $format with $bits bits is not read here\n")
_samples($format, $bits, substr($data, $pos + 8, $size))
Truth table50%1/2
A
A= _samples($format, $bits, substr($data, $pos + 8, $size))
Aresult
11
00
47 // die "$file: format $format with $bits bits is not read here\n";
48 }
49 6 $pos += 8 + $size + ($size & 1);
50 }
51 3 ▶ die "$file: no samples\n" if !$samples;
Branch: 1 branch
BranchTrueFalseTotal
unless $samples 0 3 3
52  
53 3 my @channels = map { [] } 1 .. $count;
54 3 for my $i (0 .. $#$samples) {
55 6000 push @{ $channels[ $i % $count ] }, $samples->[$i];
56 }
57 3 return { rate => $rate, channels => \@channels };
58 }
59  
60 # The samples in a WAV file's data, interleaved, full scale 1; undef for a
61 # format not read here.
62 3 ▶sub _samples ($format, $bits, $raw) {
Subroutine _samples: called CC=13 SCAR=33.7
63 return $format == FLOAT && $bits == 32 ? [ unpack 'f<*', $raw ]
64 : $format == FLOAT && $bits == 64 ? [ unpack 'd<*', $raw ]
65 2000 ▶ : $format == PCM && $bits == 16 ? [ map { $_ / 32768 } unpack 's<*', $raw ]
Branch: 1 branch
BranchTrueFalseTotal
$format == 1 && $bits == 32 ? : 0 1 1
Condition33%1/3
$format == 1 and $bits == 32
!ll&&!rl&&r
010
MC/DC0%0/2
$format == 1 and $bits == 32
$format == 1 $bits == 32
Truth table33%1/3
A and B
A= $format == 1B= $bits == 32
ABresult
0X0
100
111
66 0 ▶ : $format == PCM && $bits == 32 ? [ map { $_ / 2147483648 } unpack 'l<*', $raw ]
Branch: 1 branch
BranchTrueFalseTotal
$format == 1 && $bits == 8 ? : 0 1 1
Condition33%1/3
$format == 1 and $bits == 8
!ll&&!rl&&r
010
MC/DC0%0/2
$format == 1 and $bits == 8
$format == 1 $bits == 8
Truth table33%1/3
A and B
A= $format == 1B= $bits == 8
ABresult
0X0
100
111
67 0 ▶ : $format == PCM && $bits == 8 ? [ map { ($_ - 128) / 128 } unpack 'C*', $raw ]
Branch: 1 branch
BranchTrueFalseTotal
$format == 1 && $bits == 24 ? : 1 0 1
Condition33%1/3
$format == 1 and $bits == 24
!ll&&!rl&&r
001
MC/DC0%0/2
$format == 1 and $bits == 24
$format == 1 $bits == 24
Truth table33%1/3
A and B
A= $format == 1B= $bits == 24
ABresult
0X0
100
111
68 3 ▶ : $format == PCM && $bits == 24 ? [ map { unpack('l<', "\0$_") / 2147483648 } unpack '(a3)*', $raw ]
Branch: 3 branches
BranchTrueFalseTotal
$format == 3 && $bits == 32 ? : 1 2 3
$format == 3 && $bits == 64 ? : 0 2 2
$format == 1 && $bits == 16 ? : 1 1 2
Condition55%5/9
$format == 3 and $bits == 32
!ll&&!rl&&r
201
$format == 3 and $bits == 64
!ll&&!rl&&r
200
$format == 1 and $bits == 16
!ll&&!rl&&r
011
MC/DC50%1/2
$format == 3 and $bits == 32
$format == 3 $bits == 32
MC/DC0%0/2
$format == 3 and $bits == 64
$format == 3 $bits == 64
MC/DC50%1/2
$format == 1 and $bits == 16
$format == 1 $bits == 16
Truth table66%2/3
A and B
A= $format == 3B= $bits == 32
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= $format == 3B= $bits == 64
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= $format == 1B= $bits == 16
ABresult
0X0
100
111
69 : undef;
70 }
71  
72 # The channels' mean.
73 7 ▶sub mono (@channels) {
Subroutine mono: called CC=4 SCAR=13.9
Pod documented
74 7 ▶ return $channels[0] if @channels == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @channels == 1 0 7 7
75  
76 7 my @sum = (0.0) x @{ $channels[0] };
77 7 for my $channel (@channels) {
78 14 for my $i (0 .. $#sum) {
79 1152004 $sum[$i] += $channel->[$i];
80 }
81 }
82 7 return [ map { $_ / @channels } @sum ];
83 }
84  
85 # The magnitudes of a frame: $size samples from $at under a Hann window,
86 # zero-padded $pad times; a sine of amplitude A peaks at A. Returns the
87 # magnitudes (up to half the rate) and the Hz a bin is.
88 161 ▶sub spectrum ($x, $rate, $at, $size, $pad = 4) {
Subroutine spectrum: called CC=5 SCAR=16.2
Pod documented
89 161 my $n = $size * $pad;
90 161 ▶ die "a frame of $size, padded $pad times: not a power of two\n" if $n & ($n - 1);
Branch: 1 branch
BranchTrueFalseTotal
if $n & $n - 1 0 161 161
91  
92 161 my @re = (0.0) x $n;
93 161 my @im = (0.0) x $n;
94 161 for my $i (0 .. $size - 1) {
95 458752 ▶ my $s = $x->[ $at + $i ] // 0;
Condition50%1/2
$$x[$at + $i] // 0
l!l
4587520
MC/DC0%0/1
$$x[$at + $i] // 0
$$x[$at + $i]
Truth table50%1/2
A
A= $$x[$at + $i]
Aresult
11
00
96 458752 $re[$i] = $s * (0.5 - 0.5 * cos(2 * PI * $i / $size));
97 }
98 161 Peta::Synth::FFT::fft(\@re, \@im);
99  
100 161 my $scale = 4 / $size;
101 161 return ([ map { $scale * sqrt($re[$_]**2 + $im[$_]**2) } 0 .. $n / 2 ], $rate / $n);
102 }
103  
104 # The strongest peak between two frequencies: its frequency and amplitude,
105 # both from a parabola through the three bins about it (in dB).
106 702 ▶sub peak ($magnitudes, $bin, $low, $high) {
Subroutine peak: called CC=10 SCAR=23.4
Pod documented
107 702 my $from = int($low / $bin) + 1;
108 702 my $to = int($high / $bin);
109 702 ▶ $from = 1 if $from < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $from < 1 0 702 702
110 702 ▶ $to = $#$magnitudes - 1 if $to > $#$magnitudes - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $to > $magnitudes->$#* - 1 0 702 702
111 702 ▶ return (($low + $high) / 2, 0) if $to < $from;
Branch: 1 branch
BranchTrueFalseTotal
if $to < $from 0 702 702
112  
113 702 my $best = $from;
114 702 for my $i ($from .. $to) {
115 23544 ▶ $best = $i if $magnitudes->[$i] > $magnitudes->[$best];
Branch: 1 branch
BranchTrueFalseTotal
if $magnitudes->[$i] > $magnitudes->[$best] 7987 15557 23544
116 }
117 702 ▶ my ($l, $m, $r) = map { _db($magnitudes->[$_] || 1e-12) } $best - 1, $best, $best + 1;
Condition50%1/2
$magnitudes->[$_] || 1e-12
l!l
21060
MC/DC0%0/1
$magnitudes->[$_] || 1e-12
$magnitudes->[$_]
Truth table50%1/2
A
A= $magnitudes->[$_]
Aresult
11
00
118 702 my $d = $l - 2 * $m + $r;
119 702 ▶ my $p = $d < 0 ? 0.5 * ($l - $r) / $d : 0;
Branch: 1 branch
BranchTrueFalseTotal
$d < 0 ? : 702 0 702
120 # At a range's edge the parabola can reach further: the peak is within
121 # half a bin of its bin.
122 702 ▶ $p = -0.5 if $p < -0.5;
Branch: 1 branch
BranchTrueFalseTotal
if $p < "-0.5" 1 701 702
123 702 ▶ $p = 0.5 if $p > 0.5;
Branch: 1 branch
BranchTrueFalseTotal
if $p > "0.5" 6 696 702
124 702 return (($best + $p) * $bin, 10**(($m - 0.25 * ($l - $r) * $p) / 20));
125 }
126  
127 # A note's partials over time: count partials of a note of about f0 Hz,
128 # each at the times given (seconds into the sound; a frame of size samples
129 # from each), each found as the strongest peak within 0.4 f0 of where the
130 # partials below it say it is (a stiff string's are sharp). Returns
131 # { times, freq => [k][t], level => [k][t] (dB) }, k from 1.
132 10 ▶sub partials ($x, %opt) {
Subroutine partials: called CC=10 SCAR=23.9
Pod documented
133 10 my ($rate, $f0) = @opt{qw(rate f0)};
134 10 ▶ my $count = $opt{count} // 16;
Condition50%1/2
$opt{"count"} // 16
l!l
100
MC/DC0%0/1
$opt{"count"} // 16
$opt{"count"}
Truth table50%1/2
A
A= $opt{"count"}
Aresult
11
00
135 10 ▶ my $size = $opt{size} // 4096;
Condition50%1/2
$opt{"size"} // 4096
l!l
010
MC/DC0%0/1
$opt{"size"} // 4096
$opt{"size"}
Truth table50%1/2
A
A= $opt{"size"}
Aresult
11
00
136 10 ▶ my @times = @{ $opt{times} // [ 0.02, 0.1, 0.3, 1, 2.5 ] };
Condition50%1/2
$opt{"times"} // ["0.02", "0.1", "0.3", 1, "2.5"]
l!l
100
MC/DC0%0/1
$opt{"times"} // ["0.02", "0.1", "0.3", 1, "2.5"]
$opt{"times"}
Truth table50%1/2
A
A= $opt{"times"}
Aresult
11
00
137  
138 10 my (@freq, @level);
139 10 for my $t (0 .. $#times) {
140 39 my $at = int($times[$t] * $rate);
141 39 ▶ last if $at + $size > @$x;
Branch: 1 branch
BranchTrueFalseTotal
if $at + $size > @$x 0 39 39
142  
143 39 my ($magnitudes, $bin) = spectrum($x, $rate, $at, $size);
144 39 my ($f1) = peak($magnitudes, $bin, $f0 * 0.97, $f0 * 1.03);
145 39 my $ratio = 1; # the last partial's frequency over k f1
146 39 for my $k (1 .. $count) {
147 450 my $centre = $k * $f1 * $ratio;
148 450 my ($f, $amp) = peak($magnitudes, $bin, $centre - 0.4 * $f1, $centre + 0.4 * $f1);
149 450 $freq[$k][$t] = $f;
150 450 ▶ $level[$k][$t] = _db($amp || 1e-12);
Condition50%1/2
$amp || 1e-12
l!l
4500
MC/DC0%0/1
$amp || 1e-12
$amp
Truth table50%1/2
A
A= $amp
Aresult
11
00
151 450 ▶ $ratio = $f / ($k * $f1) if $amp > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $amp > 0 450 0 450
152 }
153 }
154 10 ▶ return { times => [ @times[ 0 .. $#{ $freq[1] // [] } ] ], freq => \@freq, level => \@level };
Condition50%1/2
$freq[1] // []
l!l
100
MC/DC0%0/1
$freq[1] // []
$freq[1]
Truth table50%1/2
A
A= $freq[1]
Aresult
11
00
155 }
156  
157 # The stiffness that the partials' frequencies say: f_k = k f sqrt(1 + B k^2),
158 # fitted to (f_k / k)^2 = f^2 + f^2 B k^2 by least squares. Returns (B, f).
159 1 ▶sub stretch (@freq) {
Subroutine stretch: called CC=5 SCAR=18.4
Pod documented
160 1 ▶ my @k = grep { defined $freq[$_] && $freq[$_] > 0 } 1 .. $#freq;
Condition33%1/3
defined $freq[$_] && $freq[$_] > 0
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $freq[$_] && $freq[$_] > 0
defined $freq[$_] $freq[$_] > 0
Truth table33%1/3
A && B
A= defined $freq[$_]B= $freq[$_] > 0
ABresult
0X0
100
111
161 1 ▶ return (0, $freq[1] // 0) if @k < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @k < 2 0 1 1
Condition0%0/2
$freq[1] // 0
l!l
00
MC/DC0%0/1
$freq[1] // 0
$freq[1]
Truth table0%0/2
A
A= $freq[1]
Aresult
11
00
162  
163 1 my ($slope, $f2) = _line([ map { $_ * $_ } @k ], [ map { ($freq[$_] / $_)**2 } @k ]);
164 1 ▶ return $f2 > 0 ? ($slope / $f2, sqrt $f2) : (0, 0);
Branch: 1 branch
BranchTrueFalseTotal
$f2 > 0 ? : 1 0 1
165 }
166  
167 # A level's slope over time, dB a second, by least squares.
168 12 ▶sub slope ($times, $levels) {
Subroutine slope: called CC=2 SCAR=7.0
Pod documented
169 12 my @i = grep { defined $levels->[$_] } 0 .. $#$times;
170 12 ▶ return 0 if @i < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @i < 2 0 12 12
171  
172 12 my ($slope) = _line([ @$times[@i] ], [ @$levels[@i] ]);
173 12 return $slope;
174 }
175  
176 # The straight line through points by least squares: (slope, intercept),
177 # the slope 0 where the x are all the same.
178 13 ▶sub _line ($x, $y) {
Subroutine _line: called CC=3 SCAR=11.0
179 13 my $n = @$x;
180 13 my ($sx, $sy, $sxx, $sxy) = (0, 0, 0, 0);
181 13 for my $i (0 .. $n - 1) {
182 240 $sx += $x->[$i];
183 240 $sy += $y->[$i];
184 240 $sxx += $x->[$i] * $x->[$i];
185 240 $sxy += $x->[$i] * $y->[$i];
186 }
187 13 my $d = $n * $sxx - $sx * $sx;
188 13 ▶ my $slope = $d ? ($n * $sxy - $sx * $sy) / $d : 0;
Branch: 1 branch
BranchTrueFalseTotal
$d ? : 13 0 13
189 13 return ($slope, ($sy - $slope * $sx) / $n);
190 }
191  
192 # The RMS level (dB) of every step seconds.
193 3 ▶sub envelope ($x, $rate, $step = 0.05) {
Subroutine envelope: called CC=4 SCAR=13.9
Pod documented
194 3 my $n = int($step * $rate);
195 3 my @levels;
196 3 ▶ for (my $at = 0; $at + $n <= @$x; $at += $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($at + $n <= @$x) 205 3 208
197 205 my $e = 0;
198 205 $e += $_ * $_ for @$x[ $at .. $at + $n - 1 ];
199 205 push @levels, _power_db($e / $n + 1e-24);
200 }
201 3 return \@levels;
202 }
203  
204 # How bright a frame is: its spectral centroid (Hz), the magnitudes as weights.
205 1 ▶sub centroid ($x, $rate, $at, $size = 4096) {
Subroutine centroid: called CC=4 SCAR=13.9
Pod documented
206 1 my ($magnitudes, $bin) = spectrum($x, $rate, $at, $size, 1);
207 1 my ($sum, $weighted) = (0, 0);
208 1 for my $i (1 .. $#$magnitudes) {
209 2048 $sum += $magnitudes->[$i];
210 2048 $weighted += $magnitudes->[$i] * $i * $bin;
211 }
212 1 ▶ return $sum ? $weighted / $sum : 0;
Branch: 1 branch
BranchTrueFalseTotal
$sum ? : 1 0 1
213 }
214  
215 # The spectrum's power in bands (per an octave, centred on 1 kHz and its
216 # octaves), frames of size samples every half frame from $from to $to
217 # seconds averaged; up to half the rate, from 20 Hz. Returns [ { centre,
218 # level (dB) } ], low to high, bands with nothing in them left out.
219 1 ▶sub bands ($x, $rate, %opt) {
Subroutine bands: called CC=10 SCAR=23.6
Pod documented
220 1 ▶ my $size = $opt{size} // 4096;
Condition50%1/2
$opt{"size"} // 4096
l!l
01
MC/DC0%0/1
$opt{"size"} // 4096
$opt{"size"}
Truth table50%1/2
A
A= $opt{"size"}
Aresult
11
00
221 1 ▶ my $per = $opt{per} // 1;
Condition50%1/2
$opt{"per"} // 1
l!l
01
MC/DC0%0/1
$opt{"per"} // 1
$opt{"per"}
Truth table50%1/2
A
A= $opt{"per"}
Aresult
11
00
222 1 ▶ my $from = int(($opt{from} // 0) * $rate);
Condition50%1/2
$opt{"from"} // 0
l!l
10
MC/DC0%0/1
$opt{"from"} // 0
$opt{"from"}
Truth table50%1/2
A
A= $opt{"from"}
Aresult
11
00
223 1 ▶ my $to = int(($opt{to} // @$x / $rate) * $rate);
Condition50%1/2
$opt{"to"} // @$x / $rate
l!l
10
MC/DC0%0/1
$opt{"to"} // @$x / $rate
$opt{"to"}
Truth table50%1/2
A
A= $opt{"to"}
Aresult
11
00
224 1 ▶ $to = @$x if $to > @$x;
Branch: 1 branch
BranchTrueFalseTotal
if $to > @$x 0 1 1
225  
226 1 my (%power, $frames);
227 1 ▶ for (my $at = $from; $at + $size <= $to; $at += $size / 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($at + $size <= $to) 22 1 23
228 22 my ($m, $bin) = spectrum($x, $rate, $at, $size, 1);
229 22 for my $i (1 .. $#$m) {
230 45056 my $f = $i * $bin;
231 45056 ▶ next if $f < 20;
Branch: 1 branch
BranchTrueFalseTotal
if $f < 20 22 45034 45056
232 45034 $power{ _band($f, $per) } += $m->[$i]**2;
233 }
234 22 $frames++;
235 }
236 1 ▶ return [] if !$frames;
Branch: 1 branch
BranchTrueFalseTotal
unless $frames 0 1 1
237  
238 1 my @bands = sort { $a <=> $b } keys %power;
239 1 return [ map { { centre => _centre($_, $per), level => _power_db($power{$_} / $frames + 1e-30) } } @bands ];
240 }
241  
242 # How much a sound's level moves (tremolo, beating), from $from to $to
243 # seconds: its level every 10 ms less the straight line through it, the
244 # RMS of what is left (dB) and how often it goes up and down (Hz).
245 2 ▶sub wobble ($x, $rate, $from, $to) {
Subroutine wobble: called CC=7 SCAR=19.6
Pod documented
246 2 my $levels = envelope([ @$x[ int($from * $rate) .. int($to * $rate) - 1 ] ], $rate, 0.01);
247 2 my @t = map { $_ * 0.01 } 0 .. $#$levels;
248 2 ▶ return (0, 0) if @t < 4;
Branch: 1 branch
BranchTrueFalseTotal
if @t < 4 0 2 2
249  
250 2 my $slope = slope(\@t, $levels);
251 2 my ($mt, $ml) = (0, 0);
252 2 $mt += $_ / @t for @t;
253 2 $ml += $_ / @t for @$levels;
254 2 my @rest = map { $levels->[$_] - $ml - $slope * ($t[$_] - $mt) } 0 .. $#t;
255  
256 2 my ($sum, $crossings) = (0, 0);
257 2 $sum += $_ * $_ for @rest;
258 2 $crossings += ($rest[$_ - 1] < 0) != ($rest[$_] < 0) for 1 .. $#rest;
259 2 ▶ return (sqrt($sum / @rest), $crossings / 2 / ($t[-1] || 1));
Condition50%1/2
$t[-1] || 1
l!l
20
MC/DC0%0/1
$t[-1] || 1
$t[-1]
Truth table50%1/2
A
A= $t[-1]
Aresult
11
00
260 }
261  
262 # How much a note's pitch moves (vibrato), from $from to $to seconds: the
263 # strongest of its first four partials followed every 20 ms, the RMS of its
264 # frequency about its mean, in cents.
265 2 ▶sub vibrato ($x, $rate, $f0, $from, $to) {
Subroutine vibrato: called CC=9 SCAR=22.1
Pod documented
266 2 my $size = 2048;
267 2 my ($m, $bin) = spectrum($x, $rate, int($from * $rate), $size);
268 2 my ($k, $best) = (1, 0);
269 2 for my $j (1 .. 4) {
270 8 my (undef, $amp) = peak($m, $bin, ($j - 0.3) * $f0, ($j + 0.3) * $f0);
271 8 ▶ ($k, $best) = ($j, $amp) if $amp > $best;
Branch: 1 branch
BranchTrueFalseTotal
if $amp > $best 2 6 8
272 }
273  
274 2 my $end = int($to * $rate);
275 2 ▶ $end = @$x if $end > @$x;
Branch: 1 branch
BranchTrueFalseTotal
if $end > @$x 0 2 2
276 2 my $step = int(0.02 * $rate);
277 2 my @cents;
278 2 ▶ for (my $at = int($from * $rate); $at + $size <= $end; $at += $step) {
Branch: 1 branch
BranchTrueFalseTotal
if ($at + $size <= $end) 96 2 98
279 96 my ($mm, $b) = spectrum($x, $rate, $at, $size);
280 96 my ($f, $amp) = peak($mm, $b, ($k - 0.3) * $f0, ($k + 0.3) * $f0);
281 96 ▶ push @cents, 1200 * log($f / ($k * $f0)) / log(2) if $amp > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $amp > 0 96 0 96
282 }
283 2 ▶ return 0 if @cents < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @cents < 2 0 2 2
284  
285 2 my $mean = 0;
286 2 $mean += $_ / @cents for @cents;
287 2 my $sum = 0;
288 2 $sum += ($_ - $mean)**2 for @cents;
289 2 return sqrt($sum / @cents);
290 }
291  
292 # How much of a frame is not the note's partials (breath, bow, hammer,
293 # noise), in dB of the whole: the partials found as partials() finds them,
294 # where a peak stands out of its window, each with the bins of its peak (a
295 # Hann window's main lobe).
296 1 ▶sub noise ($x, $rate, $f0, $at, $size = 4096) {
Subroutine noise: called CC=11 SCAR=24.2
Pod documented
297 1 my ($m, $bin) = spectrum($x, $rate, $at, $size, 1);
298 1 my $whole = 0;
299 1 $whole += $m->[$_]**2 for 1 .. $#$m;
300 1 ▶ return 0 if !$whole;
Branch: 1 branch
BranchTrueFalseTotal
unless $whole 0 1 1
301  
302 1 my %taken;
303 1 my ($f1) = peak($m, $bin, $f0 * 0.97, $f0 * 1.03);
304 1 my $ratio = 1;
305 1 for my $k (1 .. int(0.45 * $rate / $f1)) {
306 108 my $low = ($k * $ratio - 0.4) * $f1;
307 108 my $high = ($k * $ratio + 0.4) * $f1;
308 108 my ($f, $amp) = peak($m, $bin, $low, $high);
309  
310 # A partial stands out of what is about it: 12 dB over the window's
311 # median. (A slice past the end, grepped, would lengthen @$m.)
312 108 my $last = int($high / $bin);
313 108 ▶ $last = $#$m if $last > $#$m;
Branch: 1 branch
BranchTrueFalseTotal
if $last > $m->$#* 0 108 108
314 108 my @window = sort { $a <=> $b } @$m[ int($low / $bin) + 1 .. $last ];
315 108 ▶ next if !@window || $amp < 4 * $window[ $#window / 2 ];
Branch: 1 branch
BranchTrueFalseTotal
if not @window or $amp < 4 * $window[$#window / 2] 97 11 108
Condition66%2/3
not @window or $amp < 4 * $window[$#window / 2]
l!l&&r!l&&!r
09711
MC/DC50%1/2
not @window or $amp < 4 * $window[$#window / 2]
not @window $amp < 4 * $window[$#window / 2]
Truth table66%2/3
not(A) or B
A= not @windowB= $amp < 4 * $window[$#window / 2]
ABresult
1X1
011
000
316  
317 11 $ratio = $f / ($k * $f1);
318 11 my $i = int($f / $bin + 0.5);
319 11 ▶ $taken{$_} = 1 for grep { $_ >= 1 && $_ <= $#$m } $i - 2 .. $i + 2;
Condition33%1/3
$_ >= 1 && $_ <= $m->$#*
!ll&&!rl&&r
0055
MC/DC0%0/2
$_ >= 1 && $_ <= $m->$#*
$_ >= 1 $_ <= $m->$#*
Truth table33%1/3
A && B
A= $_ >= 1B= $_ <= $m->$#*
ABresult
0X0
100
111
320 }
321  
322 1 my $rest = 0;
323 1 $rest += $m->[$_]**2 for grep { !$taken{$_} } 1 .. $#$m;
324 1 return _power_db($rest / $whole + 1e-12);
325 }
326  
327 # What a body (a violin's, a reed's chamber) does to the source's harmonics,
328 # from notes across a range: each note's partials (their power averaged over
329 # the times), less the source's own fall (k^-slope: 1 for a bowed or blown
330 # sawtooth), the note's whole level taken out; then by frequency, averaged
331 # in bands (per octave), the loudest band 0 dB. Notes: [ [\@x, $rate, $f0], ... ].
332 # Options: count (16), times ([0.5, 1]), slope (1), per (3 bands an octave).
333 # Returns [ { centre (Hz), level (dB), count (partials) }, ... ], low to high.
334 1 ▶sub response ($notes, %opt) {
Subroutine response: called CC=16 SCAR=28.6
Pod documented
335 1 ▶ my $count = $opt{count} // 16;
Condition50%1/2
$opt{"count"} // 16
l!l
10
MC/DC0%0/1
$opt{"count"} // 16
$opt{"count"}
Truth table50%1/2
A
A= $opt{"count"}
Aresult
11
00
336 1 ▶ my $times = $opt{times} // [ 0.5, 1 ];
Condition50%1/2
$opt{"times"} // ["0.5", 1]
l!l
10
MC/DC0%0/1
$opt{"times"} // ["0.5", 1]
$opt{"times"}
Truth table50%1/2
A
A= $opt{"times"}
Aresult
11
00
337 1 ▶ my $slope = $opt{slope} // 1;
Condition50%1/2
$opt{"slope"} // 1
l!l
01
MC/DC0%0/1
$opt{"slope"} // 1
$opt{"slope"}
Truth table50%1/2
A
A= $opt{"slope"}
Aresult
11
00
338 1 ▶ my $per = $opt{per} // 3;
Condition50%1/2
$opt{"per"} // 3
l!l
01
MC/DC0%0/1
$opt{"per"} // 3
$opt{"per"}
Truth table50%1/2
A
A= $opt{"per"}
Aresult
11
00
339  
340 1 my (%sum, %n);
341 1 for my $note (@$notes) {
342 3 my ($x, $rate, $f0) = @$note;
343 3 my $p = partials($x, rate => $rate, f0 => $f0, count => $count, times => $times);
344  
345 # Each partial's power, averaged over the times.
346 3 my (@k, @power);
347 3 for my $k (1 .. $count) {
348 30 ▶ my @levels = grep { defined } @{ $p->{level}[$k] // [] };
Condition50%1/2
$p->{"level"}[$k] // []
l!l
300
MC/DC0%0/1
$p->{"level"}[$k] // []
$p->{"level"}[$k]
Truth table50%1/2
A
A= $p->{"level"}[$k]
Aresult
11
00
349 30 ▶ next if !@levels || $k * $f0 > 0.45 * $rate;
Branch: 1 branch
BranchTrueFalseTotal
if not @levels or $k * $f0 > "0.45" * $rate 0 30 30
Condition33%1/3
not @levels or $k * $f0 > "0.45" * $rate
l!l&&r!l&&!r
0030
MC/DC0%0/2
not @levels or $k * $f0 > "0.45" * $rate
not @levels $k * $f0 > "0.45" * $rate
Truth table33%1/3
not(A) or B
A= not @levelsB= $k * $f0 > "0.45" * $rate
ABresult
1X1
011
000
350 30 my $power = 0;
351 30 $power += 10**($_ / 10) for @levels;
352 30 push @k, $k;
353 30 push @power, $power / @levels;
354 }
355 3 ▶ next if !@k;
Branch: 1 branch
BranchTrueFalseTotal
unless @k 0 3 3
356  
357 3 my $whole = 0;
358 3 $whole += $_ for @power;
359 3 for my $i (0 .. $#k) {
360 30 my $level = _power_db($power[$i] / $whole + 1e-30) + 20 * $slope * log($k[$i]) / log(10);
361 30 my $band = _band($k[$i] * $f0, $per);
362 30 $sum{$band} += $level;
363 30 $n{$band}++;
364 }
365 }
366 1 my @bands = sort { $a <=> $b } keys %sum;
367 1 ▶ return [] if !@bands;
Branch: 1 branch
BranchTrueFalseTotal
unless @bands 0 1 1
368  
369 1 my @curve = map { { centre => _centre($_, $per), level => $sum{$_} / $n{$_}, count => $n{$_} } } @bands;
370 1 my ($top) = sort { $b <=> $a } map { $_->{level} } @curve;
371 1 $_->{level} -= $top for @curve;
372 1 return \@curve;
373 }
374  
375 # dB of an amplitude, and of a power.
376 2556 ▶sub _db ($amplitude) { return 20 * log($amplitude) / log(10) }
Subroutine _db: called CC=1 SCAR=0.0
377 247 ▶sub _power_db ($power) { return 10 * log($power) / log(10) }
Subroutine _power_db: called CC=1 SCAR=0.0
378  
379 # The band, $per an octave, a frequency is nearest the centre of: band 0 is
380 # centred on 1 kHz, band $per an octave above it.
381 45064 ▶sub _band ($f, $per) { return int(log($f / 1000) / log(2) * $per + 1000.5) - 1000 }
Subroutine _band: called CC=1 SCAR=0.0
382 23 ▶sub _centre ($band, $per) { return 1000 * 2**($band / $per) }
Subroutine _centre: called CC=1 SCAR=0.0
383  
384 2 1;
385  
386 __END__
« lib/Peta/Synth/MIDI/Stream.pm ↑ Summary lib/Peta/Vorbis/Encode/Psy.pm »