lib/Peta/Vorbis/Encode.pm

filter: Statementstmt 93.8% 475 / 506 Branchbran 76.0% 111 / 146 Conditioncond 66.1% 45 / 68 MC/DCmcdc 40.4% 17 / 42 Subroutinesub 86.0% 43 / 50 Podpod 100% 9 / 9 totaltotal 85.2% 700 / 821 CC 146 SCAR 20.9
CC
146
Cov
88%
new
22.7
_floor_posts
16.0
_residue
12.1
CRAP 8.1
« lib/Peta/Synth/Arpeggiator.pm ↑ Summary lib/Peta/MIDI/Encoder.pm »
1 package Peta::Vorbis::Encode;
2 # ABSTRACT: Ogg Vorbis encoder
3  
4 # An Ogg Vorbis encoder: samples go in, Ogg pages come out. The work is
5 # shared out like this:
6 #
7 # MDCT blocks of 2048 samples (256 around attacks), half overlapped,
8 # in Vorbis' window (::PDL does the same with PDL, faster)
9 # Psy the quantization step each bin may have
10 # Book the codebooks: this encoder's own, sent in the setup header
11 # Ogg the pages
12 #
13 # This module makes the headers and the audio packets. A block's spectrum
14 # is divided by a floor (Vorbis' floor 1: straight lines in decibels
15 # between some 32 points, fitted below the steps the model allows) and
16 # rounded: that is the residue. The residue is coded in partitions of 16
17 # bins, each of a class by its largest value: nothing for zeros, small
18 # vectors for small values, and for large ones a cascade, a coarse value (a
19 # multiple of 5, 45 or 765) with finer ones added, down to -2 .. 2. So a
20 # partition of zeros with one peak costs little more than the peak.
21 #
22 # The codebooks' code lengths come from how often each entry is used in
23 # encoding sample material (Peta::Vorbis::Encode::Weights, made by
24 # devbin/vorbis-train); without them, from assumed distributions.
25 #
26 # The bitrate varies with quality (0 to 10): how far below the masking the
27 # noise is put. Two channels are coupled with Vorbis' square polar mapping,
28 # on the integer residues and so without loss; where they are alike, the
29 # second is mostly zeros.
30 #
31 # Blocks are long, or short around attacks, so that an attack's noise does
32 # not spread to before it (pre-echo). Each block's centre follows the last
33 # one's by a quarter of each one's size; the next block is short when an
34 # attack lies within what a long one would cover, so no long block holds an
35 # attack. A long block beside a short one has that side's slope short.
36 # Floor, residue and mapping are each there twice, for the two sizes.
37  
38 1 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use List::Util qw(any max min);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use Peta::Hearing;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use Peta::Vorbis::Encode::Book;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use Peta::Vorbis::Encode::MDCT;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use Peta::Vorbis::Encode::Ogg;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use Peta::Vorbis::Encode::Psy;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 1 our $VERSION = '0.2610180';
47  
48 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 BLOCK => 2048,
50 HALF => 1024,
51 SHORT => 256,
52 SEGMENT => 64, # samples the attack detector looks at, at a time
53 ATTACK => 10, # a segment this many times louder than the eight before it
54 AUDIBLE => 1e-6, # and at least this loud (energy per sample)
55 MULTIPLIER => 2, # floor 1: Y values 0 .. 127, steps of 2 in the table
56 RANGE => 128,
57 POSTS => 32, # points of a long block's floor (a short one's: 8)
58 PARTITION => 16, # residue partition size
59 LARGEST => 4972, # the largest residue value the cascade codes
60 VENDOR => 'Peta::Vorbis::Encode',
61 1 };
62  
63 # floor1_inverse_dB_table, as the specification gives it: Y * multiplier
64 # into a linear amplitude, -140 dB to 0 dB.
65 1 my @DB = (
66 1.0649863e-07, 1.1341951e-07, 1.2079015e-07, 1.2863978e-07, 1.3699951e-07, 1.4590251e-07, 1.5538408e-07, 1.6548181e-07,
67 1.7623575e-07, 1.8768855e-07, 1.9988561e-07, 2.1287530e-07, 2.2670913e-07, 2.4144197e-07, 2.5713223e-07, 2.7384213e-07,
68 2.9163793e-07, 3.1059021e-07, 3.3077411e-07, 3.5226968e-07, 3.7516214e-07, 3.9954229e-07, 4.2550680e-07, 4.5315863e-07,
69 4.8260743e-07, 5.1396998e-07, 5.4737065e-07, 5.8294187e-07, 6.2082472e-07, 6.6116941e-07, 7.0413592e-07, 7.4989464e-07,
70 7.9862701e-07, 8.5052630e-07, 9.0579828e-07, 9.6466216e-07, 1.0273513e-06, 1.0941144e-06, 1.1652161e-06, 1.2409384e-06,
71 1.3215816e-06, 1.4074654e-06, 1.4989305e-06, 1.5963394e-06, 1.7000785e-06, 1.8105592e-06, 1.9282195e-06, 2.0535261e-06,
72 2.1869758e-06, 2.3290978e-06, 2.4804557e-06, 2.6416497e-06, 2.8133190e-06, 2.9961443e-06, 3.1908506e-06, 3.3982101e-06,
73 3.6190449e-06, 3.8542308e-06, 4.1047004e-06, 4.3714470e-06, 4.6555282e-06, 4.9580707e-06, 5.2802740e-06, 5.6234160e-06,
74 5.9888572e-06, 6.3780469e-06, 6.7925283e-06, 7.2339451e-06, 7.7040476e-06, 8.2047000e-06, 8.7378876e-06, 9.3057248e-06,
75 9.9104632e-06, 1.0554501e-05, 1.1240392e-05, 1.1970856e-05, 1.2748789e-05, 1.3577278e-05, 1.4459606e-05, 1.5399272e-05,
76 1.6400004e-05, 1.7465768e-05, 1.8600792e-05, 1.9809576e-05, 2.1096914e-05, 2.2467911e-05, 2.3928002e-05, 2.5482978e-05,
77 2.7139006e-05, 2.8902651e-05, 3.0780908e-05, 3.2781225e-05, 3.4911534e-05, 3.7180282e-05, 3.9596466e-05, 4.2169667e-05,
78 4.4910090e-05, 4.7828601e-05, 5.0936773e-05, 5.4246931e-05, 5.7772202e-05, 6.1526565e-05, 6.5524908e-05, 6.9783085e-05,
79 7.4317983e-05, 7.9147585e-05, 8.4291040e-05, 8.9768747e-05, 9.5602426e-05, 0.00010181521, 0.00010843174, 0.00011547824,
80 0.00012298267, 0.00013097477, 0.00013948625, 0.00014855085, 0.00015820453, 0.00016848555, 0.00017943469, 0.00019109536,
81 0.00020351382, 0.00021673929, 0.00023082423, 0.00024582449, 0.00026179955, 0.00027881276, 0.00029693158, 0.00031622787,
82 0.00033677814, 0.00035866388, 0.00038197188, 0.00040679456, 0.00043323036, 0.00046138411, 0.00049136745, 0.00052329927,
83 0.00055730621, 0.00059352311, 0.00063209358, 0.00067317058, 0.00071691700, 0.00076350630, 0.00081312324, 0.00086596457,
84 0.00092223983, 0.00098217216, 0.0010459992, 0.0011139742, 0.0011863665, 0.0012634633, 0.0013455702, 0.0014330129,
85 0.0015261382, 0.0016253153, 0.0017309374, 0.0018434235, 0.0019632195, 0.0020908006, 0.0022266726, 0.0023713743,
86 0.0025254795, 0.0026895994, 0.0028643847, 0.0030505286, 0.0032487691, 0.0034598925, 0.0036847358, 0.0039241906,
87 0.0041792066, 0.0044507950, 0.0047400328, 0.0050480668, 0.0053761186, 0.0057254891, 0.0060975636, 0.0064938176,
88 0.0069158225, 0.0073652516, 0.0078438871, 0.0083536271, 0.0088964928, 0.009474637, 0.010090352, 0.010746080,
89 0.011444421, 0.012188144, 0.012980198, 0.013823725, 0.014722068, 0.015678791, 0.016697687, 0.017782797,
90 0.018938423, 0.020169149, 0.021479854, 0.022875735, 0.024362330, 0.025945531, 0.027631618, 0.029427276,
91 0.031339626, 0.033376252, 0.035545228, 0.037855157, 0.040315199, 0.042935108, 0.045725273, 0.048696758,
92 0.051861348, 0.055231591, 0.058820850, 0.062643361, 0.066714279, 0.071049749, 0.075666962, 0.080584227,
93 0.085821044, 0.091398179, 0.097337747, 0.10366330, 0.11039993, 0.11757434, 0.12521498, 0.13335215,
94 0.14201813, 0.15124727, 0.16107617, 0.17154380, 0.18269168, 0.19456402, 0.20720788, 0.22067342,
95 0.23501402, 0.25028656, 0.26655159, 0.28387361, 0.30232132, 0.32196786, 0.34289114, 0.36517414,
96 0.38890521, 0.41417847, 0.44109412, 0.46975890, 0.50028648, 0.53279791, 0.56742212, 0.60429640,
97 0.64356699, 0.68538959, 0.72993007, 0.77736504, 0.82788260, 0.88168307, 0.9389798, 1.0,
98 );
99 1 my $DB_STEP = log(1 / $DB[0]) / 255; # natural log per table step
100  
101 # The residue's classes: [ the largest value, the books of its passes ].
102 # The books are those of %LATTICE below.
103 1 my @CLASSES = (
104 [ 0, [] ],
105 [ 1, ['r1'] ],
106 [ 2, ['r2'] ],
107 [ 4, ['r4'] ],
108 [ 12, [ 'by5', 'r2' ] ],
109 [ 22, [ 'by5wide', 'r2' ] ],
110 [ 382, [ 'by45', 'by5wide', 'r2' ] ],
111 [ LARGEST, [ 'by765', 'by45', 'by5wide', 'r2' ] ],
112 );
113  
114 # The cascades of the classes that have them: per pass the step its book's
115 # values are multiples of, and the most steps it holds. The last pass
116 # carries what is left, -2 .. 2.
117 1 my %CASCADE = (
118 4 => [ [ 5, 2 ] ],
119 5 => [ [ 5, 4 ] ],
120 6 => [ [ 45, 8 ], [ 5, 4 ] ],
121 7 => [ [ 765, 6 ], [ 45, 8 ], [ 5, 4 ] ],
122 );
123  
124 # The residue books' values: [ dimensions, number of values, minimum, step ].
125 1 my %LATTICE = (
126 r1 => [ 4, 3, -1, 1 ],
127 r2 => [ 2, 5, -2, 1 ],
128 r4 => [ 2, 9, -4, 1 ],
129 by5 => [ 2, 5, -10, 5 ],
130 by5wide => [ 2, 9, -20, 5 ],
131 by45 => [ 1, 17, -360, 45 ],
132 by765 => [ 1, 13, -4590, 765 ],
133 );
134  
135 # How fast the assumed weights of a residue book fall off from the middle
136 # value, where no counted weights are at hand.
137 1 my %SPREAD = (r1 => 0.9, r2 => 0.8, r4 => 0.4, by5 => 1.0, by5wide => 0.8, by45 => 0.8, by765 => 0.8);
138  
139 # All the books, in the order of the setup header.
140 my @BOOKS = (
141 'floor',
142 'classes',
143 1 ▶ sort { $LATTICE{$a}[3] <=> $LATTICE{$b}[3] || $LATTICE{$a}[1] <=> $LATTICE{$b}[1] } keys %LATTICE,
Condition66%2/3
$LATTICE{$a}[3] <=> $LATTICE{$b}[3] || $LATTICE{$a}[1] <=> $LATTICE{$b}[1]
l!l&&r!l&&!r
1040
MC/DC0%0/2
$LATTICE{$a}[3] <=> $LATTICE{$b}[3] || $LATTICE{$a}[1] <=> $LATTICE{$b}[1]
$LATTICE{$a}[3] <=> $LATTICE{$b}[3] $LATTICE{$a}[1] <=> $LATTICE{$b}[1]
Truth table66%2/3
A || B
A= $LATTICE{$a}[3] <=> $LATTICE{$b}[3]B= $LATTICE{$a}[1] <=> $LATTICE{$b}[1]
ABresult
1X1
011
000
144 );
145  
146 2 ▶sub new ($class, %arg) {
Subroutine new: called CC=15 SCAR=31.2
Pod documented
147 2 ▶ my $rate = $arg{rate} // 44100;
Condition50%1/2
$arg{"rate"} // 44100
l!l
20
MC/DC0%0/1
$arg{"rate"} // 44100
$arg{"rate"}
Truth table50%1/2
A
A= $arg{"rate"}
Aresult
11
00
148 2 ▶ my $channels = $arg{channels} // 2;
Condition50%1/2
$arg{"channels"} // 2
l!l
20
MC/DC0%0/1
$arg{"channels"} // 2
$arg{"channels"}
Truth table50%1/2
A
A= $arg{"channels"}
Aresult
11
00
149 2 ▶ my $quality = $arg{quality} // 5;
Condition50%1/2
$arg{"quality"} // 5
l!l
20
MC/DC0%0/1
$arg{"quality"} // 5
$arg{"quality"}
Truth table50%1/2
A
A= $arg{"quality"}
Aresult
11
00
150  
151 2 ▶ die "channels: 1 or 2\n" if $channels != 1 && $channels != 2;
Branch: 1 branch
BranchTrueFalseTotal
if $channels != 1 and $channels != 2 0 2 2
Condition33%1/3
$channels != 1 and $channels != 2
!ll&&!rl&&r
020
MC/DC0%0/2
$channels != 1 and $channels != 2
$channels != 1 $channels != 2
Truth table33%1/3
A and B
A= $channels != 1B= $channels != 2
ABresult
0X0
100
111
152 2 ▶ die "quality: 0 to 10\n" if $quality < 0 || $quality > 10;
Branch: 1 branch
BranchTrueFalseTotal
if $quality < 0 or $quality > 10 0 2 2
Condition33%1/3
$quality < 0 or $quality > 10
l!l&&r!l&&!r
002
MC/DC0%0/2
$quality < 0 or $quality > 10
$quality < 0 $quality > 10
Truth table33%1/3
A or B
A= $quality < 0B= $quality > 10
ABresult
1X1
011
000
153  
154 my $self = bless {
155 rate => $rate,
156 channels => $channels,
157 quality => $quality,
158 ogg => Peta::Vorbis::Encode::Ogg->new($arg{serial} // int(rand 0x7FFFFFFF)),
159  
160 2 ▶ pcm => [ map { [ (0.0) x HALF ] } 1 .. $channels ], # from half a block before the first sample
Condition100%2/2
$arg{"serial"} // int rand 2147483647
l!l
11
MC/DC100%1/1
$arg{"serial"} // int rand 2147483647
$arg{"serial"}
Truth table100%2/2
A
A= $arg{"serial"}
Aresult
11
00
161 base => -HALF, # the sample pcm starts at
162 center => 0, # the next block's centre
163 size => BLOCK, # and size
164 before => BLOCK, # the last block's size
165 attacks => [], # where attacks are, in samples
166 seen => 0, # the samples looked at for attacks
167 energies => [ (0.0) x 8 ],
168 tail => [ (0.0) x $channels ],
169  
170 packets => 0,
171 samples => 0,
172 bytes => 0,
173 shorts => 0, # short blocks
174 }, $class;
175  
176 2 ▶ my $mdct = _mdct($arg{mdct} // 'auto');
Condition50%1/2
$arg{"mdct"} // "auto"
l!l
02
MC/DC0%0/1
$arg{"mdct"} // "auto"
$arg{"mdct"}
Truth table50%1/2
A
A= $arg{"mdct"}
Aresult
11
00
177 2 $self->{config}{$_} = $self->_configure($_, $mdct) for SHORT, BLOCK;
178  
179 # The windows: short; long, with either side's slope long or short.
180 2 $self->{windows}{short} = Peta::Vorbis::Encode::MDCT::window(SHORT);
181 2 for my $left (SHORT, BLOCK) {
182 4 for my $right (SHORT, BLOCK) {
183 8 $self->{windows}{"$left/$right"} = Peta::Vorbis::Encode::MDCT::window(BLOCK, $left, $right);
184 }
185 }
186  
187 2 $self->_books;
188 0 ▶ if ($arg{count}) { $_->count for values %{ $self->{books} } }
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{"count"}) 0 2 2
189  
190 2 return $self;
191 }
192  
193 # The MDCT's class: 'pdl', 'perl', or 'auto' (PDL where it loads).
194 2 ▶sub _mdct ($name) {
Subroutine _mdct: called CC=4 SCAR=17.2
195 2 my $mdct = 'Peta::Vorbis::Encode::MDCT';
196  
197 2 ▶ return $mdct if $name eq 'perl';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "perl" 0 2 2
198 2 ▶ return "${mdct}::PDL" if eval { require Peta::Vorbis::Encode::MDCT::PDL; 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { require Peta::Vorbis::Encode::MDCT::PDL; 1 } } 2 0 2
199 0 ▶ return $mdct if $name ne 'pdl';
Branch: 1 branch
BranchTrueFalseTotal
if $name ne "pdl" 0 0 0
200  
201 0 die "the PDL MDCT needs PDL: $@";
202 }
203  
204 # What a block size needs: its MDCT, the model, where the coded residue
205 # ends, and its floor's points.
206 4 ▶sub _configure ($self, $n, $mdct_class) {
Subroutine _configure: called CC=3 SCAR=11.0
207 4 my $half = $n / 2;
208 4 my $mdct = $mdct_class->new($n, 4 / $n);
209  
210 # The level of a full-scale 1 kHz sine in the transform's bins.
211 4 my @sine = map { sin(2 * 3.141592653589793 * 1000 * $_ / $self->{rate}) } 0 .. $n - 1;
212 4 my $full = 0;
213 4 $full += $_ * $_ for @{ $mdct->transform(\@sine, Peta::Vorbis::Encode::MDCT::window($n)) };
214  
215 # The residue is coded up to where hearing ends, by quality, in whole
216 # partitions; a short block's to the same frequency.
217 4 my $top = 15000 + 600 * $self->{quality};
218 4 my $end = min(int($top / ($self->{rate} / 2) * HALF / PARTITION) * PARTITION, HALF);
219  
220 return {
221 n => $n,
222 half => $half,
223 mdct => $mdct,
224 end => int($end * $half / HALF / PARTITION) * PARTITION,
225 4 ▶ psy => Peta::Vorbis::Encode::Psy->new($self->{rate}, $half, $self->{quality}, $full),
Branch: 1 branch
BranchTrueFalseTotal
$n == 2048 ? : 2 2 4
226 $self->_floor_posts($half, $n == BLOCK ? POSTS : 8),
227 };
228 }
229  
230 0 ▶sub mdct ($self) { return ref($self->{config}{+BLOCK}{mdct}) =~ /PDL\z/ ? 'pdl' : 'perl' }
Subroutine mdct: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref($self->{'config'}{'2048'}{'mdct'}) =~ /PDL\z/u ? : 0 0 0
231  
232 0 ▶sub quality ($self) { return $self->{quality} }
Subroutine quality: not called CC=1 SCAR=6.9
Pod documented
233 0 ▶sub packets ($self) { return $self->{packets} }
Subroutine packets: not called CC=1 SCAR=6.9
Pod documented
234 0 ▶sub bytes ($self) { return $self->{bytes} }
Subroutine bytes: not called CC=1 SCAR=6.9
Pod documented
235 0 ▶sub shorts ($self) { return $self->{shorts} }
Subroutine shorts: not called CC=1 SCAR=6.9
Pod documented
236  
237 # With count => 1: how often each book's entries were written, by book.
238 0 ▶sub counts ($self) {
Subroutine counts: not called CC=1 SCAR=6.9
Pod documented
239 0 my $books = $self->{books};
240 0 return { map { $_ => [ @{ $books->{$_}->count } ] } keys %$books };
241 }
242  
243 # A floor's points for $half bins: X positions 0 and $half, then $posts
244 # points evenly spaced in Bark, in an order that halves the intervals (each
245 # point is predicted from its neighbours already coded); and for each point
246 # from the third, its neighbours among those before it.
247 4 ▶sub _floor_posts ($self, $half, $posts) {
Subroutine _floor_posts: called CC=16 SCAR=27.7
248 4 my $width = $self->{rate} / 2 / $half;
249 4 ▶ my $bark = sub ($bin) { return Peta::Hearing::bark($bin * $width) };
Subroutine __ANON__: called CC=1 SCAR=0.0
250  
251 4 my ($z0, $z1) = ($bark->(1), $bark->($half - 1));
252 4 my @x;
253 4 my $bin = 1;
254 4 for my $j (0 .. $posts - 1) {
255 80 my $z = $z0 + ($z1 - $z0) * $j / ($posts - 1);
256 80 ▶ $bin++ while $bin < $half - 1 && $bark->($bin) < $z;
Branch: 1 branch
BranchTrueFalseTotal
$bin < $half - 1 and &$bark($bin) < $z and do { ++$bin } 2296 80 2376
Condition100%3/3
$bin < $half - 1 and &$bark($bin) < $z
!ll&&!rl&&r
4762296
MC/DC100%2/2
$bin < $half - 1 and &$bark($bin) < $z
$bin < $half - 1 &$bark($bin) < $z
Truth table100%3/3
A and B
A= $bin < $half - 1B= &$bark($bin) < $z
ABresult
0X0
100
111
257 80 ▶ push @x, @x && $bin <= $x[-1] ? $x[-1] + 1 : $bin;
Branch: 1 branch
BranchTrueFalseTotal
@x && $bin <= $x[-1] ? : 0 80 80
Condition66%2/3
@x and $bin <= $x[-1]
!ll&&!rl&&r
4760
MC/DC0%0/2
@x and $bin <= $x[-1]
@x $bin <= $x[-1]
Truth table66%2/3
A and B
A= @xB= $bin <= $x[-1]
ABresult
0X0
100
111
258 }
259  
260 # Coarse to fine: the middle of each interval before its halves.
261 4 my @order;
262 4 my @todo = ([ 0, $#x ]);
263 4 ▶ while (@todo) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo) 24 4 28
264 24 my @next;
265 24 for my $span (@todo) {
266 164 my ($from, $to) = @$span;
267 164 ▶ next if $from > $to;
Branch: 1 branch
BranchTrueFalseTotal
if $from > $to 84 80 164
268  
269 80 my $mid = int(($from + $to) / 2);
270 80 push @order, $x[$mid];
271 80 push @next, [ $from, $mid - 1 ], [ $mid + 1, $to ];
272 }
273 24 @todo = @next;
274 }
275 4 my @list = (0, $half, @order);
276  
277 4 my (@low, @high);
278 4 for my $i (2 .. $#list) {
279 80 my ($lo, $hi) = (0, 1);
280 80 for my $j (0 .. $i - 1) {
281 1208 ▶ $lo = $j if $list[$j] < $list[$i] && $list[$j] > $list[$lo];
Branch: 1 branch
BranchTrueFalseTotal
if $list[$j] < $list[$i] and $list[$j] > $list[$lo] 124 1084 1208
Condition100%3/3
$list[$j] < $list[$i] and $list[$j] > $list[$lo]
!ll&&!rl&&r
404680124
MC/DC100%2/2
$list[$j] < $list[$i] and $list[$j] > $list[$lo]
$list[$j] < $list[$i] $list[$j] > $list[$lo]
Truth table100%3/3
A and B
A= $list[$j] < $list[$i]B= $list[$j] > $list[$lo]
ABresult
0X0
100
111
282 1208 ▶ $hi = $j if $list[$j] > $list[$i] && $list[$j] < $list[$hi];
Branch: 1 branch
BranchTrueFalseTotal
if $list[$j] > $list[$i] and $list[$j] < $list[$hi] 108 1100 1208
Condition100%3/3
$list[$j] > $list[$i] and $list[$j] < $list[$hi]
!ll&&!rl&&r
804296108
MC/DC100%2/2
$list[$j] > $list[$i] and $list[$j] < $list[$hi]
$list[$j] > $list[$i] $list[$j] < $list[$hi]
Truth table100%3/3
A and B
A= $list[$j] > $list[$i]B= $list[$j] < $list[$hi]
ABresult
0X0
100
111
283 }
284 80 $low[$i] = $lo;
285 80 $high[$i] = $hi;
286 }
287  
288 4 my $bits = 0;
289 4 ▶ $bits++ while (1 << $bits) < $half;
Branch: 1 branch
BranchTrueFalseTotal
1 << $bits < $half and do { ++$bits } 34 4 38
290  
291 return (
292 floor_x => \@list,
293 floor_low => \@low,
294 floor_high => \@high,
295 4 floor_sorted => [ sort { $list[$a] <=> $list[$b] } 0 .. $#list ],
296 rangebits => $bits,
297 );
298 }
299  
300 # The codebooks, with their numbers in the setup header. Their weights (how
301 # often each entry is expected) are counted ones where there are counts.
302 2 ▶sub _books ($self) {
Subroutine _books: called CC=7 SCAR=19.7
303 1 ▶ my %counted = eval { no warnings 'once'; require Peta::Vorbis::Encode::Weights; %Peta::Vorbis::Encode::Weights::WEIGHTS };
Subroutine BEGIN: called CC=1 SCAR=0.0
304 2 my $classes = @CLASSES;
305 2 my (%book, %number);
306  
307 2 for my $i (0 .. $#BOOKS) {
308 18 my $name = $BOOKS[$i];
309 18 $number{$name} = $i;
310  
311 18 ▶ if ($name eq 'floor') {
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq 'floor') { } 2 16 18
elsif ($name eq 'classes') { } 2 14 16
312 0 ▶ my $weights = _counted($counted{$name}, RANGE) // [ map { exp(-$_ / 5) } 0 .. RANGE - 1 ];
Condition50%1/2
_counted($counted{$name}, 128) // [map({exp -$_ / 5;} 0..127)]
l!l
20
MC/DC0%0/1
_counted($counted{$name}, 128) // [map({exp -$_ / 5;} 0..127)]
_counted($counted{$name}, 128)
Truth table50%1/2
A
A= _counted($counted{$name}, 128)
Aresult
11
00
313 2 $book{$name} = Peta::Vorbis::Encode::Book->new(entries => RANGE, weights => $weights);
314 }
315 elsif ($name eq 'classes') {
316 # Scalar, but its dimension says how many classes a word holds.
317 2 my $assumed = [ map { 0.6**int($_ / $classes) * 0.6**($_ % $classes) } 0 .. $classes**2 - 1 ];
318 2 ▶ my $weights = _counted($counted{$name}, $classes**2) // $assumed;
Condition33%1/3
_counted($counted{$name}, $classes ** 2) // $assumed
l!l&&r!l&&!r
200
MC/DC0%0/2
_counted($counted{$name}, $classes ** 2) // $assumed
_counted($counted{$name}, $classes ** 2) $assumed
Truth table33%1/3
A // B
A= _counted($counted{$name}, $classes ** 2)B= $assumed
ABresult
1X1
011
000
319 2 $book{$name} = Peta::Vorbis::Encode::Book->new(entries => $classes**2, dimensions => 2, weights => $weights);
320 }
321 else {
322 14 my ($dim, $m, $min, $step) = @{ $LATTICE{$name} };
323 14 ▶ my $weights = _counted($counted{$name}, $m**$dim) // _laplace($m, $dim, $SPREAD{$name});
Condition33%1/3
_counted($counted{$name}, $m ** $dim) // _laplace($m, $dim, $SPREAD{$name})
l!l&&r!l&&!r
1400
MC/DC0%0/2
_counted($counted{$name}, $m ** $dim) // _laplace($m, $dim, $SPREAD{$name})
_counted($counted{$name}, $m ** $dim) _laplace($m, $dim, $SPREAD{$name})
Truth table33%1/3
A // B
A= _counted($counted{$name}, $m ** $dim)B= _laplace($m, $dim, $SPREAD{$name})
ABresult
1X1
011
000
324 14 $book{$name} = Peta::Vorbis::Encode::Book->new(
325 dimensions => $dim,
326 values => $m,
327 minimum => $min,
328 delta => $step,
329 weights => $weights,
330 );
331 }
332 }
333  
334 2 $self->{books} = \%book;
335 2 $self->{book_number} = \%number;
336 2 return;
337 }
338  
339 # Counted weights, if there are counts for all the entries; each count
340 # raised by a half, so that no entry goes without a codeword.
341 18 ▶sub _counted ($counts, $entries) {
Subroutine _counted: called CC=3 SCAR=12.5
342 18 ▶ return undef if !$counts || @$counts != $entries;
Branch: 1 branch
BranchTrueFalseTotal
if not $counts or @$counts != $entries 0 18 18
Condition33%1/3
not $counts or @$counts != $entries
l!l&&r!l&&!r
0018
MC/DC0%0/2
not $counts or @$counts != $entries
not $counts @$counts != $entries
Truth table33%1/3
not(A) or B
A= not $countsB= @$counts != $entries
ABresult
1X1
011
000
343 18 return [ map { $_ + 0.5 } @$counts ];
344 }
345  
346 # Assumed weights for a lattice of $dim dimensions over $m values: a
347 # Laplace distribution about the middle value, for each component.
348 0 ▶sub _laplace ($m, $dim, $lambda) {
Subroutine _laplace: not called CC=3 SCAR=24.8
349 0 my $mid = ($m - 1) / 2;
350 0 my @p = map { exp(-abs($_ - $mid) * $lambda) } 0 .. $m - 1;
351 0 my @w;
352 0 for my $e (0 .. $m**$dim - 1) {
353 0 my ($w, $v) = (1, $e);
354 0 for (1 .. $dim) {
355 0 $w *= $p[ $v % $m ];
356 0 $v = int($v / $m);
357 }
358 0 push @w, $w;
359 }
360 0 return \@w;
361 }
362  
363 # The three header packets: identification, comment and setup.
364 2 ▶sub _headers ($self) {
Subroutine _headers: called CC=1 SCAR=0.0
365 2 return ($self->_id_header, $self->_comment_header, $self->_setup_header);
366 }
367  
368 2 ▶sub _id_header ($self) {
Subroutine _id_header: called CC=3 SCAR=11.0
369 2 my $id = Peta::Vorbis::Encode::Bits->new;
370 2 $id->put(1, 8);
371 2 $id->put(ord, 8) for split //, 'vorbis';
372 2 $id->put(0, 32); # version
373 2 $id->put($self->{channels}, 8);
374 2 $id->put($self->{rate}, 32);
375 2 $id->put(0, 32) for 1 .. 3; # bitrates: not said
376 2 $id->put(8, 4); # short blocks of 2^8
377 2 $id->put(11, 4); # long blocks of 2^11
378 2 $id->put(1, 1); # framing
379 2 return $id->bytes;
380 }
381  
382 2 ▶sub _comment_header ($self) {
Subroutine _comment_header: called CC=3 SCAR=11.0
383 2 my $comment = Peta::Vorbis::Encode::Bits->new;
384 2 $comment->put(3, 8);
385 2 $comment->put(ord, 8) for split //, 'vorbis';
386 2 $comment->put(length VENDOR, 32);
387 2 $comment->put(ord, 8) for split //, VENDOR;
388 2 $comment->put(0, 32); # no comments
389 2 $comment->put(1, 1); # framing
390 2 return $comment->bytes;
391 }
392  
393 2 ▶sub _setup_header ($self) {
Subroutine _setup_header: called CC=4 SCAR=13.9
394 2 my $s = Peta::Vorbis::Encode::Bits->new;
395 2 $s->put(5, 8);
396 2 $s->put(ord, 8) for split //, 'vorbis';
397  
398 2 my @books = map { $self->{books}{$_} } @BOOKS;
399 2 $s->put(@books - 1, 8);
400 2 $_->header($s) for @books;
401  
402 2 $s->put(0, 6); # time: one placeholder
403 2 $s->put(0, 16);
404  
405 2 my @configs = map { $self->{config}{$_} } SHORT, BLOCK;
406 2 $self->_setup_floors($s, @configs);
407 2 $self->_setup_residues($s, @configs);
408 2 $self->_setup_mappings($s, @configs);
409  
410 # Modes: 0 short blocks, 1 long.
411 2 $s->put(@configs - 1, 6);
412 2 for my $m (0 .. $#configs) {
413 4 $s->put($m, 1); # block flag
414 4 $s->put(0, 16); # window type
415 4 $s->put(0, 16); # transform type
416 4 $s->put($m, 8); # mapping
417 }
418  
419 2 $s->put(1, 1); # framing
420 2 return $s->bytes;
421 }
422  
423 # Floors 0 (short blocks) and 1 (long): the points in partitions of four,
424 # one class, one book.
425 2 ▶sub _setup_floors ($self, $s, @configs) {
Subroutine _setup_floors: called CC=4 SCAR=13.9
426 2 $s->put(@configs - 1, 6);
427 2 for my $c (@configs) {
428 4 my @x = @{ $c->{floor_x} };
429 4 my $parts = (@x - 2) / 4;
430  
431 4 $s->put(1, 16); # type 1
432 4 $s->put($parts, 5);
433 4 $s->put(0, 4) for 1 .. $parts;
434 4 $s->put(4 - 1, 3); # class 0: four values
435 4 $s->put(0, 2); # no subclasses
436 4 $s->put($self->{book_number}{floor} + 1, 8);
437 4 $s->put(MULTIPLIER - 1, 2);
438 4 $s->put($c->{rangebits}, 4);
439 4 $s->put($x[$_], $c->{rangebits}) for 2 .. $#x;
440 }
441 2 return;
442 }
443  
444 # Residues 0 and 1, of format 1: the partitions, the classes and their books.
445 2 ▶sub _setup_residues ($self, $s, @configs) {
Subroutine _setup_residues: called CC=7 SCAR=19.5
446 2 $s->put(@configs - 1, 6);
447 2 for my $c (@configs) {
448 4 $s->put(1, 16); # format 1
449 4 $s->put(0, 24); # begin
450 4 $s->put($c->{end}, 24);
451 4 $s->put(PARTITION - 1, 24);
452 4 $s->put(@CLASSES - 1, 6);
453 4 $s->put($self->{book_number}{classes}, 8);
454  
455 4 for my $class (@CLASSES) {
456 32 my $cascade = (1 << @{ $class->[1] }) - 1; # which passes have a book
457 32 $s->put($cascade & 7, 3);
458 32 ▶ $s->put($cascade > 7 ? 1 : 0, 1);
Branch: 1 branch
BranchTrueFalseTotal
$cascade > 7 ? : 4 28 32
459 32 ▶ $s->put($cascade >> 3, 5) if $cascade > 7;
Branch: 1 branch
BranchTrueFalseTotal
if $cascade > 7 4 28 32
460 }
461 4 for my $class (@CLASSES) {
462 32 $s->put($self->{book_number}{$_}, 8) for @{ $class->[1] };
463 }
464 }
465 2 return;
466 }
467  
468 # Mappings 0 and 1: one submap each, its floor and residue those of its
469 # size; two channels coupled.
470 2 ▶sub _setup_mappings ($self, $s, @configs) {
Subroutine _setup_mappings: called CC=3 SCAR=11.0
471 2 $s->put(@configs - 1, 6);
472 2 for my $m (0 .. $#configs) {
473 4 $s->put(0, 16); # type 0
474 4 $s->put(0, 1); # one submap
475 4 ▶ if ($self->{channels} == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'channels'} == 2) { } 4 0 4
476 4 $s->put(1, 1); # coupling: one step,
477 4 $s->put(0, 8);
478 4 $s->put(0, 1); # channel 0 the magnitude,
479 4 $s->put(1, 1); # channel 1 the angle
480 }
481 else {
482 0 $s->put(0, 1); # no coupling
483 }
484 4 $s->put(0, 2); # reserved
485 4 $s->put(0, 8); # time
486 4 $s->put($m, 8); # floor
487 4 $s->put($m, 8); # residue
488 }
489 2 return;
490 }
491  
492 # Takes samples (-1 .. 1), one array reference per channel. Returns the
493 # pages they complete, the headers' with the first call.
494 2 ▶sub encode ($self, @channels) {
Subroutine encode: called CC=4 SCAR=13.9
Pod documented
495 2 ▶ die "expected $self->{channels} channel(s)\n" if @channels != $self->{channels};
Branch: 1 branch
BranchTrueFalseTotal
if @channels != $self->{"channels"} 0 2 2
496  
497 2 my $out = '';
498 2 ▶ if (!$self->{started}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"started"}) 2 0 2
499 2 my ($id, $comment, $setup) = $self->_headers;
500 2 my $ogg = $self->{ogg};
501 2 $out .= $ogg->packet($id, 0) . $ogg->flush;
502 2 $out .= $ogg->packet($comment, 0) . $ogg->packet($setup, 0) . $ogg->flush;
503 2 $self->{started} = 1;
504 }
505  
506 2 push @{ $self->{pcm}[$_] }, @{ $channels[$_] } for 0 .. $#channels;
507 2 $self->{samples} += @{ $channels[0] };
508 2 $out .= $self->_blocks;
509  
510 2 $self->{bytes} += length $out;
511 2 return $out;
512 }
513  
514 # Returns the rest: blocks until one's centre reaches the last sample (the
515 # decoder has every sample then), its page the last, saying how many
516 # samples there are.
517 2 ▶sub finish ($self) {
Subroutine finish: called CC=3 SCAR=11.2
Pod documented
518 0 ▶ my $out = $self->{started} ? '' : $self->encode(map { [] } 1 .. $self->{channels});
Branch: 1 branch
BranchTrueFalseTotal
$self->{'started'} ? : 2 0 2
519 2 push @$_, (0.0) x (2 * BLOCK) for @{ $self->{pcm} };
520 2 $self->{final} = 1;
521 2 $out .= $self->_blocks;
522  
523 2 $self->{bytes} += length $out;
524 2 return $out;
525 }
526  
527 # Codes the blocks there are samples for: each needs its own samples and,
528 # to choose the next one's size, those a long block after it would cover.
529 4 ▶sub _blocks ($self) {
Subroutine _blocks: called CC=12 SCAR=24.9
530 4 my $pcm = $self->{pcm};
531 4 my $out = '';
532  
533 4 ▶ while (!$self->{done}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"done"}) 66 0 66
534 66 my ($c, $n) = @$self{qw(center size)};
535 66 my $long_next = $c + $n / 4 + BLOCK / 4;
536 66 my $have = $self->{base} + @{ $pcm->[0] };
537 66 ▶ last if !$self->{final} && $have < $long_next + HALF + SEGMENT;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"final"} and $have < $long_next + 1024 + 64 2 64 66
Condition100%3/3
not $self->{"final"} and $have < $long_next + 1024 + 64
!ll&&!rl&&r
6582
MC/DC100%2/2
not $self->{"final"} and $have < $long_next + 1024 + 64
not $self->{"final"} $have < $long_next + 1024 + 64
Truth table100%3/3
not(A) and B
A= not $self->{"final"}B= $have < $long_next + 1024 + 64
ABresult
0X0
100
111
538  
539 # The next block is short if an attack lies where a long one would reach.
540 64 $self->_attacks;
541 64 ▶ my $next = (any { $_ >= $c && $_ < $long_next + HALF } @{ $self->{attacks} }) ? SHORT : BLOCK;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
&any(sub { $_ >= $c and $_ < $long_next + 1024; } , @{$$self{'attacks'};}) ? : 2 62 64
$_ >= $c and $_ < $long_next + 1024 2 4 6
542  
543 64 my $from = $c - $n / 2 - $self->{base};
544 64 my @block = map { [ @$_[ $from .. $from + $n - 1 ] ] } @$pcm;
545 64 my $packet = $self->_packet(\@block, $n, $self->{before}, $next);
546 64 ▶ $self->{shorts}++ if $n == SHORT;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 256 2 62 64
547 64 $self->{packets}++;
548  
549 64 my $granule = min($c, $self->{samples});
550 64 ▶ my $last = $self->{final} && $c >= $self->{samples};
Condition100%3/3
$self->{'final'} && $c >= $self->{'samples'}
!ll&&!rl&&r
5842
MC/DC100%2/2
$self->{'final'} && $c >= $self->{'samples'}
$self->{'final'} $c >= $self->{'samples'}
Truth table100%3/3
A && B
A= $self->{'final'}B= $c >= $self->{'samples'}
ABresult
0X0
100
111
551 64 $out .= $self->{ogg}->packet($packet, $granule, $last);
552  
553 # The first audio packet goes on a page of its own: its granule
554 # position, 0, then says nothing is to be cut at the start, and the
555 # last page's can only mean the end (FFmpeg reads a short first page
556 # as a cut start).
557 64 ▶ $out .= $self->{ogg}->flush if $self->{packets} == 1 && !$last;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"packets"} == 1 and not $last 2 62 64
Condition66%2/3
$self->{"packets"} == 1 and not $last
!ll&&!rl&&r
6202
MC/DC50%1/2
$self->{"packets"} == 1 and not $last
$self->{"packets"} == 1 not $last
Truth table66%2/3
A and not(B)
A= $self->{"packets"} == 1B= not $last
ABresult
0X0
100
111
558 64 ▶ if ($last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last) 2 62 64
559 2 $self->{done} = 1;
560 2 last;
561 }
562  
563 # On to the next block; no block reaches further back than half a
564 # long block before its centre.
565 62 $self->{before} = $n;
566 62 $self->{size} = $next;
567 62 $self->{center} = $c + $n / 4 + $next / 4;
568  
569 62 my $drop = $self->{center} - HALF - $self->{base};
570 62 ▶ if ($drop > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($drop > 0) 62 0 62
571 62 splice @$_, 0, $drop for @$pcm;
572 62 $self->{base} += $drop;
573 }
574 62 my $old = $self->{center} - BLOCK;
575 62 @{ $self->{attacks} } = grep { $_ >= $old } @{ $self->{attacks} };
576 }
577 4 return $out;
578 }
579  
580 # Finds the attacks in the samples not yet looked at: in segments of 64
581 # samples, the energy of the changes from sample to sample (what is high in
582 # pitch, where pre-echo is heard), against the loudest of the eight
583 # segments before.
584 64 ▶sub _attacks ($self) {
Subroutine _attacks: called CC=6 SCAR=17.9
585 64 my $pcm = $self->{pcm};
586 64 my $have = $self->{base} + @{ $pcm->[0] };
587 64 my @seen = @{ $self->{energies} };
588  
589 64 ▶ while ($self->{seen} + SEGMENT <= $have) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"seen"} + 64 <= $have) 1064 64 1128
590 1064 my $at = $self->{seen} - $self->{base};
591 1064 my $energy = 0.0;
592 1064 for my $ch (0 .. $#$pcm) {
593 2128 my $x = $pcm->[$ch];
594 2128 my $previous = $self->{tail}[$ch];
595 2128 for my $i ($at .. $at + SEGMENT - 1) {
596 136192 my $d = $x->[$i] - $previous;
597 136192 $energy += $d * $d;
598 136192 $previous = $x->[$i];
599 }
600 2128 $self->{tail}[$ch] = $previous;
601 }
602  
603 1064 ▶ if ($energy > ATTACK * max(@seen) && $energy > AUDIBLE * SEGMENT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($energy > 10 * &max(@seen) and $energy > "6.4e-05") 2 1062 1064
Condition66%2/3
$energy > 10 * &max(@seen) and $energy > "6.4e-05"
!ll&&!rl&&r
106202
MC/DC50%1/2
$energy > 10 * &max(@seen) and $energy > "6.4e-05"
$energy > 10 * &max(@seen) $energy > "6.4e-05"
Truth table66%2/3
A and B
A= $energy > 10 * &max(@seen)B= $energy > "6.4e-05"
ABresult
0X0
100
111
604 2 push @{ $self->{attacks} }, $self->{seen};
605 }
606 1064 shift @seen;
607 1064 push @seen, $energy;
608 1064 $self->{seen} += SEGMENT;
609 }
610 64 $self->{energies} = \@seen;
611 64 return;
612 }
613  
614 # A block's packet: $n samples of each channel, and the sizes of the blocks
615 # before and after it (for a long one's slopes).
616 64 ▶sub _packet ($self, $blocks, $n, $before, $after) {
Subroutine _packet: called CC=8 SCAR=20.9
617 64 my $config = $self->{config}{$n};
618 64 my $out = Peta::Vorbis::Encode::Bits->new;
619 64 $out->put(0, 1); # audio
620  
621 64 my $window;
622 64 ▶ if ($n == SHORT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 256) { } 2 62 64
623 2 $out->put(0, 1); # mode 0: short
624 2 $window = $self->{windows}{short};
625 }
626 else {
627 62 $out->put(1, 1); # mode 1: long
628 62 ▶ $out->put($before == BLOCK ? 1 : 0, 1);
Branch: 1 branch
BranchTrueFalseTotal
$before == 2048 ? : 60 2 62
629 62 ▶ $out->put($after == BLOCK ? 1 : 0, 1);
Branch: 1 branch
BranchTrueFalseTotal
$after == 2048 ? : 60 2 62
630 62 $window = $self->{windows}{"$before/$after"};
631 }
632  
633 64 my (@residue, @used);
634 64 for my $x ($config->{mdct}->transform_all($blocks, $window)) {
635 128 my ($r, $used) = $self->_floor($config, $out, $x);
636 128 push @residue, $r;
637 128 push @used, $used;
638 }
639  
640 64 ▶ if ($self->{channels} == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"channels"} == 2) 64 0 64
641 64 _couple(@residue, $config->{end});
642 64 ▶ $used[0] = $used[1] = 1 if $used[0] || $used[1]; # the decoder decodes both, or neither
Branch: 1 branch
BranchTrueFalseTotal
if $used[0] or $used[1] 64 0 64
Condition33%1/3
$used[0] or $used[1]
l!l&&r!l&&!r
6400
MC/DC0%0/2
$used[0] or $used[1]
$used[0] $used[1]
Truth table33%1/3
A or B
A= $used[0]B= $used[1]
ABresult
1X1
011
000
643 }
644  
645 64 $self->_residue($out, \@residue, \@used, $config->{end});
646 64 return $out->bytes;
647 }
648  
649 # Square polar coupling, the decoder's four cases inverted: the channel with
650 # the larger value is the magnitude, the angle what takes it to the other.
651 # (A silent channel's residue is zeros: the decoder ignores what it makes
652 # of it, as that channel's floor is unused.)
653 689 ▶sub _couple ($l, $r, $end) {
Subroutine _couple: called CC=4 SCAR=13.9
654 689 for my $i (0 .. $end - 1) {
655 52401 my ($a, $b) = ($l->[$i], $r->[$i]);
656 52401 ▶ my $magnitude = abs $a >= abs $b ? $a : $b;
Branch: 1 branch
BranchTrueFalseTotal
abs $a >= abs $b ? : 51441 960 52401
657 52401 $l->[$i] = $magnitude;
658 52401 ▶ $r->[$i] = $magnitude > 0 ? $a - $b : $b - $a;
Branch: 1 branch
BranchTrueFalseTotal
$magnitude > 0 ? : 2094 50307 52401
659 }
660 689 return;
661 }
662  
663 # A channel's floor: fitted below the steps the model allows, coded, and
664 # drawn as the decoder draws it. Returns the residue it leaves (the
665 # spectrum divided by it, rounded), and whether the channel is used at all.
666 128 ▶sub _floor ($self, $config, $out, $x) {
Subroutine _floor: called CC=5 SCAR=16.2
667 128 my $want = $self->_floor_wanted($config, $x);
668 128 my ($final, $values, $flag) = _floor_values($config, $want);
669 128 my $curve = _floor_curve($config, $final, $flag);
670  
671 128 my $end = $config->{end};
672 128 my @r = (0) x $config->{half};
673 128 my $any = 0;
674 128 for my $b (0 .. $end - 1) {
675 103552 my $v = _round($x->[$b] / $DB[ $curve->[$b] ], LARGEST);
676 103552 $r[$b] = $v;
677 103552 ▶ $any ||= $v;
Condition100%3/3
$any ||= $v
l!l&&r!l&&!r
973741286050
MC/DC100%2/2
$any ||= $v
$any $v
Truth table100%3/3
A ||= B
A= $anyB= $v
ABresult
1X1
011
000
678 }
679  
680 128 ▶ if (!$any) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($any) 0 128 128
681 0 $out->put(0, 1); # not used
682 0 return (\@r, 0);
683 }
684  
685 # Used: the first two points as they are; the rest with the floor book.
686 128 $out->put(1, 1);
687 128 $out->put($final->[0], 7);
688 128 $out->put($final->[1], 7);
689 128 my $book = $self->{books}{floor};
690 128 $book->put($out, $values->[$_]) for 2 .. $#$values;
691  
692 128 return (\@r, 1);
693 }
694  
695 # The Y each floor point wants: the finest step of the bins around it.
696 128 ▶sub _floor_wanted ($self, $config, $x) {
Subroutine _floor_wanted: called CC=7 SCAR=19.6
697 128 my $half = $config->{half};
698 128 my $step = $config->{psy}->steps($x);
699 128 my @list = @{ $config->{floor_x} };
700 128 my @sorted = @{ $config->{floor_sorted} };
701 128 my @want = (0) x @list;
702  
703 128 for my $k (0 .. $#sorted) {
704 4256 my $i = $sorted[$k];
705 4256 ▶ my $from = $k > 0 ? int(($list[ $sorted[$k - 1] ] + $list[$i]) / 2) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$k > 0 ? : 4128 128 4256
706 4256 ▶ my $to = $k < $#sorted ? int(($list[$i] + $list[ $sorted[$k + 1] ]) / 2) : $half - 1;
Branch: 1 branch
BranchTrueFalseTotal
$k < $#sorted ? : 4128 128 4256
707 4256 ▶ $to = $half - 1 if $to > $half - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $to > $half - 1 0 4256 4256
708 4256 ▶ $from = $to if $from > $to;
Branch: 1 branch
BranchTrueFalseTotal
if $from > $to 0 4256 4256
709  
710 4256 my $least = min(@$step[ $from .. $to ]);
711 4256 ▶ my $index = 255 + log($least > 1e-30 ? $least : 1e-30) / $DB_STEP;
Branch: 1 branch
BranchTrueFalseTotal
$least > 1e-30 ? : 4256 0 4256
712 4256 $want[$i] = _clamp_to(int($index / MULTIPLIER), 0, RANGE - 1);
713 }
714 128 return \@want;
715 }
716  
717 # The values to code: each point against its prediction from its
718 # neighbours, as the decoder unwraps them. Returns the points' final Y, the
719 # values, and which points the decoder draws the curve through.
720 128 ▶sub _floor_values ($config, $want) {
Subroutine _floor_values: called CC=4 SCAR=13.9
721 128 my @list = @{ $config->{floor_x} };
722 128 my @final = @$want[ 0, 1 ];
723 128 my @value = (0, 0);
724 128 my @flag = (1, 1);
725  
726 128 for my $i (2 .. $#list) {
727 4000 my ($lo, $hi) = ($config->{floor_low}[$i], $config->{floor_high}[$i]);
728 4000 my $predicted = _render_point($list[$lo], $final[$lo], $list[$hi], $final[$hi], $list[$i]);
729 4000 my $v = _wrap($predicted, $want->[$i]);
730 4000 push @value, $v;
731 4000 $final[$i] = $want->[$i];
732  
733 4000 ▶ if ($v) { $flag[$lo] = $flag[$hi] = $flag[$i] = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($v) { } 3484 516 4000
734 516 ▶ else { $flag[$i] //= 0 }
Condition50%1/2
$flag[$i] //= 0
l!l
0516
MC/DC0%0/1
$flag[$i] //= 0
$flag[$i]
Truth table50%1/2
A
A= $flag[$i]
Aresult
11
00
735 }
736 128 return (\@final, \@value, \@flag);
737 }
738  
739 # The floor's curve, as the decoder draws it: indices into @DB, per bin.
740 128 ▶sub _floor_curve ($config, $final, $flag) {
Subroutine _floor_curve: called CC=4 SCAR=13.9
741 128 my $half = $config->{half};
742 128 my @list = @{ $config->{floor_x} };
743 128 my @sorted = @{ $config->{floor_sorted} };
744 128 my @curve = (0) x $half;
745  
746 128 my ($lx, $ly) = (0, $final->[ $sorted[0] ] * MULTIPLIER);
747 128 my ($hx, $hy) = (0, $ly);
748 128 for my $k (1 .. $#sorted) {
749 4128 my $i = $sorted[$k];
750 4128 ▶ next if !$flag->[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless $flag->[$i] 402 3726 4128
751  
752 3726 ($hx, $hy) = ($list[$i], $final->[$i] * MULTIPLIER);
753 3726 _render_line($lx, $ly, $hx, $hy, \@curve);
754 3726 ($lx, $ly) = ($hx, $hy);
755 }
756 128 ▶ _render_line($hx, $hy, $half, $hy, \@curve) if $hx < $half;
Branch: 1 branch
BranchTrueFalseTotal
if $hx < $half 0 128 128
757  
758 128 return \@curve;
759 }
760  
761 # The value a floor point is coded with: what the decoder turns back into
762 # $y from its prediction (the specification's unwrapping, inverted).
763 4000 ▶sub _wrap ($predicted, $y) {
Subroutine _wrap: called CC=7 SCAR=20.6
764 4000 my $highroom = RANGE - $predicted;
765 4000 my $lowroom = $predicted;
766 4000 my $room = 2 * min($highroom, $lowroom);
767 4000 my $d = $y - $predicted;
768  
769 4000 ▶ return 0 if $d == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $d == 0 516 3484 4000
770  
771 3484 ▶ if ($d > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d > 0) { } 1348 2136 3484
772 1348 ▶ return 2 * $d if 2 * $d < $room;
Branch: 1 branch
BranchTrueFalseTotal
if 2 * $d < $room 1348 0 1348
773 0 ▶ return $d + $lowroom if $highroom > $lowroom;
Branch: 1 branch
BranchTrueFalseTotal
if $highroom > $lowroom 0 0 0
774 }
775 else {
776 2136 ▶ return -2 * $d - 1 if -2 * $d - 1 < $room;
Branch: 1 branch
BranchTrueFalseTotal
if -2 * $d - 1 < $room 2028 108 2136
777 108 ▶ return $highroom - 1 - $d if $highroom <= $lowroom;
Branch: 1 branch
BranchTrueFalseTotal
if $highroom <= $lowroom 108 0 108
778 }
779  
780 # Out of what the wrapping reaches from this prediction (cannot happen
781 # for y within the range): code the prediction itself.
782 0 return 0;
783 }
784  
785 # Vorbis' integer line: its Y at $x.
786 4000 ▶sub _render_point ($x0, $y0, $x1, $y1, $x) {
Subroutine _render_point: called CC=2 SCAR=6.9
787 4000 my $dy = $y1 - $y0;
788 4000 my $adx = $x1 - $x0;
789 4000 my $off = int(abs($dy) * ($x - $x0) / $adx);
790 4000 ▶ return $dy < 0 ? $y0 - $off : $y0 + $off;
Branch: 1 branch
BranchTrueFalseTotal
$dy < 0 ? : 1820 2180 4000
791 }
792  
793 # Vorbis' integer line, drawn into @$v from x0 up to (not including) x1.
794 3726 ▶sub _render_line ($x0, $y0, $x1, $y1, $v) {
Subroutine _render_line: called CC=8 SCAR=20.9
795 3726 my $dy = $y1 - $y0;
796 3726 my $adx = $x1 - $x0;
797 3726 ▶ return if $adx <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $adx <= 0 0 3726 3726
798  
799 3726 my $ady = abs $dy;
800 3726 ▶ my $base = $dy < 0 ? -int(-$dy / $adx) : int($dy / $adx);
Branch: 1 branch
BranchTrueFalseTotal
$dy < 0 ? : 1334 2392 3726
801 3726 ▶ my $sy = $dy < 0 ? $base - 1 : $base + 1;
Branch: 1 branch
BranchTrueFalseTotal
$dy < 0 ? : 1334 2392 3726
802 3726 $ady -= abs($base) * $adx;
803  
804 3726 my $n = @$v;
805 3726 my ($y, $err) = ($y0, 0);
806 3726 ▶ $v->[$x0] = $y if $x0 < $n;
Branch: 1 branch
BranchTrueFalseTotal
if $x0 < $n 3726 0 3726
807 3726 for my $x ($x0 + 1 .. $x1 - 1) {
808 123762 $err += $ady;
809 123762 ▶ if ($err >= $adx) { $err -= $adx; $y += $sy }
Branch: 1 branch
BranchTrueFalseTotal
if ($err >= $adx) { } 16042 107720 123762
810 107720 else { $y += $base }
811 123762 ▶ $v->[$x] = $y if $x < $n;
Branch: 1 branch
BranchTrueFalseTotal
if $x < $n 123762 0 123762
812 }
813 3726 return;
814 }
815  
816 # Codes the residue of the used channels (format 1: each channel's vector
817 # on its own): a class word for every two partitions, then pass by pass
818 # the partitions' values with their classes' books.
819 64 ▶sub _residue ($self, $out, $residue, $used, $end) {
Subroutine _residue: called CC=12 SCAR=24.9
820 64 my @channels = grep { $used->[$_] } 0 .. $#$residue;
821 64 ▶ return if !@channels;
Branch: 1 branch
BranchTrueFalseTotal
unless @channels 0 64 64
822  
823 64 my $parts = $end / PARTITION;
824 64 my %class = map { $_ => _classes($residue->[$_], $parts) } @channels;
825  
826 64 my $words = $self->{books}{classes};
827 64 my $per = $words->dimensions;
828 64 my $nclasses = @CLASSES;
829  
830 64 for my $pass (0 .. 3) {
831 256 my $p = 0;
832 256 ▶ while ($p < $parts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p < $parts) 6472 256 6728
833 6472 ▶ if ($pass == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass == 0) 1618 4854 6472
834 1618 for my $ch (@channels) {
835 3236 my $word = 0;
836 3236 ▶ $word = $word * $nclasses + ($class{$ch}[$p + $_] // 0) for 0 .. $per - 1;
Condition50%1/2
$class{$ch}[$p + $_] // 0
l!l
64720
MC/DC0%0/1
$class{$ch}[$p + $_] // 0
$class{$ch}[$p + $_]
Truth table50%1/2
A
A= $class{$ch}[$p + $_]
Aresult
11
00
837 3236 $words->put($out, $word);
838 }
839 }
840 6472 for my $i (0 .. $per - 1) {
841 12944 ▶ last if $p >= $parts;
Branch: 1 branch
BranchTrueFalseTotal
if $p >= $parts 0 12944 12944
842  
843 12944 for my $ch (@channels) {
844 25888 my $c = $class{$ch}[$p];
845 25888 ▶ my $name = $CLASSES[$c][1][$pass] // next;
Condition100%2/2
$CLASSES[$c][1][$pass] // (next)
l!l
114824740
MC/DC100%1/1
$CLASSES[$c][1][$pass] // (next)
$CLASSES[$c][1][$pass]
Truth table100%2/2
A
A= $CLASSES[$c][1][$pass]
Aresult
11
00
846 1148 $self->_partition($out, $self->{books}{$name}, $c, $pass, $residue->[$ch], $p * PARTITION);
847 }
848 12944 $p++;
849 }
850 }
851 }
852 64 return;
853 }
854  
855 # Each partition's class: the first whose largest value holds the
856 # partition's largest.
857 128 ▶sub _classes ($r, $parts) {
Subroutine _classes: called CC=3 SCAR=11.0
858 128 my @classes;
859 128 for my $p (0 .. $parts - 1) {
860 6472 my $top = max(map { abs } @$r[ $p * PARTITION .. ($p + 1) * PARTITION - 1 ]);
861 6472 my $c = 0;
862 6472 ▶ $c++ while $CLASSES[$c][0] < $top;
Branch: 1 branch
BranchTrueFalseTotal
$CLASSES[$c][0] < $top and do { ++$c } 2006 6472 8478
863 6472 push @classes, $c;
864 }
865 128 return \@classes;
866 }
867  
868 # Codes a partition's values in one pass: what is left of each value for
869 # this pass's book, entry by entry of the book's dimension.
870 1148 ▶sub _partition ($self, $out, $book, $class, $pass, $r, $at) {
Subroutine _partition: called CC=2 SCAR=6.9
871 1148 my $dim = $book->dimensions;
872 1148 my @part = map { _share($class, $pass, $r->[$_]) } $at .. $at + PARTITION - 1;
873 1148 ▶ for (my $i = 0; $i < PARTITION; $i += $dim) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 16) 8304 1148 9452
874 8304 $book->put($out, $book->entry(@part[ $i .. $i + $dim - 1 ]));
875 }
876 1148 return;
877 }
878  
879 # What of a value the given pass of a class carries: the cascades split it
880 # into coarse values (each a multiple of its step, as far as its book goes)
881 # and what is left for the last pass.
882 18368 ▶sub _share ($class, $pass, $v) {
Subroutine _share: called CC=4 SCAR=13.9
883 18368 ▶ my $steps = $CASCADE{$class} // return $v;
Condition100%2/2
$CASCADE{$class} // (return $v)
l!l
100168352
MC/DC100%1/1
$CASCADE{$class} // (return $v)
$CASCADE{$class}
Truth table100%2/2
A
A= $CASCADE{$class}
Aresult
11
00
884 10016 for my $p (0 .. $#$steps) {
885 13664 my ($step, $most) = @{ $steps->[$p] };
886 13664 my $part = $step * _round($v / $step, $most);
887 13664 ▶ return $part if $p == $pass;
Branch: 1 branch
BranchTrueFalseTotal
if $p == $pass 5920 7744 13664
888 7744 $v -= $part;
889 }
890 4096 return _clamp_to($v, -2, 2);
891 }
892  
893 # $x rounded half away from zero, within +-$limit.
894 117216 ▶sub _round ($x, $limit) {
Subroutine _round: called CC=2 SCAR=6.9
895 117216 ▶ my $r = $x < 0 ? -int(-$x + 0.5) : int($x + 0.5);
Branch: 1 branch
BranchTrueFalseTotal
$x < 0 ? : 54818 62398 117216
896 117216 return _clamp_to($r, -$limit, $limit);
897 }
898  
899 125568 ▶sub _clamp_to ($x, $low, $high) {
Subroutine _clamp_to: called CC=3 SCAR=11.7
900 125568 ▶ return $low if $x < $low;
Branch: 1 branch
BranchTrueFalseTotal
if $x < $low 0 125568 125568
901 125568 ▶ return $high if $x > $high;
Branch: 1 branch
BranchTrueFalseTotal
if $x > $high 0 125568 125568
902 125568 return $x;
903 }
904  
905 1 1;
906  
907 __END__
« lib/Peta/Synth/Arpeggiator.pm ↑ Summary lib/Peta/MIDI/Encoder.pm »