blib/lib/Math/SegmentedEnvelope.pm

filter: Statementstmt 93.3% 363 / 389 Branchbran 74.4% 64 / 86 Conditioncond 50.4% 63 / 125 MC/DCmcdc 15.9% 11 / 69 Subroutinesub 92.0% 46 / 50 Podpod 100% 31 / 31 totaltotal 77.0% 578 / 750 CC 110 SCAR 20.5
CC
110
Cov
82%
from_pdl
30.0
adsr
15.0
to_svg
14.5
CRAP 7.8
↑ Summary tinyexpr.c »
1 package Math::SegmentedEnvelope;
2 # ABSTRACT: create/manage/evaluate segmented (curved) envelope
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.03';
7  
8 2 require XSLoader;
9 2 XSLoader::load('Math::SegmentedEnvelope', $VERSION);
10  
11 use overload
12 2 ▶ '&{}' => sub {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
13 12 my $self = shift;
14 12 ▶ sub { $self->at(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
15 },
16 3 ▶ '+' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
17 3 my ($a, $b, $swap) = @_;
18 3 ▶ eval { $b->isa(__PACKAGE__) } ? $a->add($b) : $a->offset($b + 0);
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $b->isa(__PACKAGE__) } } ? : 1 2 3
19 },
20 3 ▶ '-' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
21 3 my ($a, $b, $swap) = @_;
22 $swap ? $a->scale(-1)->offset($b + 0)
23 3 ▶ : (eval { $b->isa(__PACKAGE__) } ? $a->add($b->scale(-1)) : $a->offset(-($b + 0)));
Branch: 2 branches
BranchTrueFalseTotal
$swap ? : 1 2 3
eval { do { $b->isa(__PACKAGE__) } } ? : 1 1 2
24 },
25 3 ▶ '*' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
26 3 my ($a, $b, $swap) = @_;
27 3 ▶ eval { $b->isa(__PACKAGE__) } ? $a->multiply($b) : $a->scale($b + 0);
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $b->isa(__PACKAGE__) } } ? : 1 2 3
28 },
29 1 ▶ 'neg' => sub { $_[0]->scale(-1) },
Subroutine __ANON__: called CC=1 SCAR=0.0
30 65 ▶ '""' => sub { sprintf("Math::SegmentedEnvelope(segments=%d, duration=%.4gs)", $_[0]->segments, $_[0]->duration) },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 2 fallback => 1;
32  
33 2 *copy = \&clone;
34 2 *integral = \&area;
35  
36 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 2 our @EXPORT_OK = qw(env adsr asr perc concat morpher_formulas spline from_samples);
38  
39 0 ▶sub env { __PACKAGE__->new(@_) }
Subroutine env: not called CC=1 SCAR=6.9
Pod documented
40  
41 12 ▶sub adsr {
Subroutine adsr: called CC=13 SCAR=27.1
Pod documented
42 12 my $class = __PACKAGE__;
43 12 ▶ if (@_ && eval { $_[0]->isa(__PACKAGE__) }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and eval { do { $_[0]->isa(__PACKAGE__) } }) 2 10 12
Condition66%2/3
@_ and eval { do { $_[0]->isa(__PACKAGE__) } }
!ll&&!rl&&r
0102
MC/DC50%1/2
@_ and eval { do { $_[0]->isa(__PACKAGE__) } }
@_ eval { do { $_[0]->isa(__PACKAGE__) } }
Truth table66%2/3
A and B
A= @_B= eval { do { $_[0]->isa(__PACKAGE__) } }
ABresult
0X0
100
111
44 2 my $inv = shift;
45 2 ▶ $class = ref($inv) || $inv;
Condition33%1/3
ref $inv || $inv
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $inv || $inv
ref $inv $inv
Truth table33%1/3
A || B
A= ref $invB= $inv
ABresult
1X1
011
000
46 }
47 12 my ($attack, $decay, $sustain, $release, %opts) = @_;
48  
49 12 ▶ $attack //= 0.1;
Condition50%1/2
$attack //= "0.1"
l!l
120
MC/DC0%0/1
$attack //= "0.1"
$attack
Truth table50%1/2
A
A= $attack
Aresult
11
00
50 12 ▶ $decay //= 0.1;
Condition50%1/2
$decay //= "0.1"
l!l
120
MC/DC0%0/1
$decay //= "0.1"
$decay
Truth table50%1/2
A
A= $decay
Aresult
11
00
51 12 ▶ $sustain //= 0.7;
Condition50%1/2
$sustain //= "0.7"
l!l
120
MC/DC0%0/1
$sustain //= "0.7"
$sustain
Truth table50%1/2
A
A= $sustain
Aresult
11
00
52 12 ▶ $release //= 0.3;
Condition50%1/2
$release //= "0.3"
l!l
120
MC/DC0%0/1
$release //= "0.3"
$release
Truth table50%1/2
A
A= $release
Aresult
11
00
53  
54 12 ▶ my $peak = $opts{peak} // 1.0;
Condition100%2/2
$opts{"peak"} // 1
l!l
111
MC/DC100%1/1
$opts{"peak"} // 1
$opts{"peak"}
Truth table100%2/2
A
A= $opts{"peak"}
Aresult
11
00
55 12 ▶ my $sustain_time = $opts{sustain_time} // 0.5;
Condition100%2/2
$opts{"sustain_time"} // "0.5"
l!l
111
MC/DC100%1/1
$opts{"sustain_time"} // "0.5"
$opts{"sustain_time"}
Truth table100%2/2
A
A= $opts{"sustain_time"}
Aresult
11
00
56 12 ▶ my $attack_curve = $opts{attack_curve} // 2;
Condition50%1/2
$opts{"attack_curve"} // 2
l!l
012
MC/DC0%0/1
$opts{"attack_curve"} // 2
$opts{"attack_curve"}
Truth table50%1/2
A
A= $opts{"attack_curve"}
Aresult
11
00
57 12 ▶ my $decay_curve = $opts{decay_curve} // -2;
Condition50%1/2
$opts{"decay_curve"} // -2
l!l
012
MC/DC0%0/1
$opts{"decay_curve"} // -2
$opts{"decay_curve"}
Truth table50%1/2
A
A= $opts{"decay_curve"}
Aresult
11
00
58 12 ▶ my $release_curve = $opts{release_curve} // -2;
Condition50%1/2
$opts{"release_curve"} // -2
l!l
012
MC/DC0%0/1
$opts{"release_curve"} // -2
$opts{"release_curve"}
Truth table50%1/2
A
A= $opts{"release_curve"}
Aresult
11
00
59  
60 12 my $sustain_level = $peak * $sustain;
61  
62 12 my $def = [
63 [0, $peak, $sustain_level, $sustain_level, 0],
64 [$attack, $decay, $sustain_time, $release],
65 [$attack_curve, $decay_curve, 1, $release_curve],
66 ];
67  
68 12 $class->new($def, %opts);
69 }
70  
71 21 ▶sub perc {
Subroutine perc: called CC=9 SCAR=23.0
Pod documented
72 21 my $class = __PACKAGE__;
73 21 ▶ if (@_ && eval { $_[0]->isa(__PACKAGE__) }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and eval { do { $_[0]->isa(__PACKAGE__) } }) 2 19 21
Condition66%2/3
@_ and eval { do { $_[0]->isa(__PACKAGE__) } }
!ll&&!rl&&r
0192
MC/DC50%1/2
@_ and eval { do { $_[0]->isa(__PACKAGE__) } }
@_ eval { do { $_[0]->isa(__PACKAGE__) } }
Truth table66%2/3
A and B
A= @_B= eval { do { $_[0]->isa(__PACKAGE__) } }
ABresult
0X0
100
111
74 2 my $inv = shift;
75 2 ▶ $class = ref($inv) || $inv;
Condition33%1/3
ref $inv || $inv
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $inv || $inv
ref $inv $inv
Truth table33%1/3
A || B
A= ref $invB= $inv
ABresult
1X1
011
000
76 }
77 21 my ($attack, $decay, %opts) = @_;
78  
79 21 ▶ $attack //= 0.01;
Condition50%1/2
$attack //= "0.01"
l!l
210
MC/DC0%0/1
$attack //= "0.01"
$attack
Truth table50%1/2
A
A= $attack
Aresult
11
00
80 21 ▶ $decay //= 0.5;
Condition50%1/2
$decay //= "0.5"
l!l
210
MC/DC0%0/1
$decay //= "0.5"
$decay
Truth table50%1/2
A
A= $decay
Aresult
11
00
81  
82 21 ▶ my $peak = $opts{peak} // 1.0;
Condition100%2/2
$opts{"peak"} // 1
l!l
516
MC/DC100%1/1
$opts{"peak"} // 1
$opts{"peak"}
Truth table100%2/2
A
A= $opts{"peak"}
Aresult
11
00
83 21 ▶ my $attack_curve = $opts{attack_curve} // 2;
Condition50%1/2
$opts{"attack_curve"} // 2
l!l
021
MC/DC0%0/1
$opts{"attack_curve"} // 2
$opts{"attack_curve"}
Truth table50%1/2
A
A= $opts{"attack_curve"}
Aresult
11
00
84 21 ▶ my $decay_curve = $opts{decay_curve} // -3;
Condition50%1/2
$opts{"decay_curve"} // -3
l!l
021
MC/DC0%0/1
$opts{"decay_curve"} // -3
$opts{"decay_curve"}
Truth table50%1/2
A
A= $opts{"decay_curve"}
Aresult
11
00
85  
86 21 my $def = [
87 [0, $peak, 0],
88 [$attack, $decay],
89 [$attack_curve, $decay_curve],
90 ];
91  
92 21 $class->new($def, %opts);
93 }
94  
95 6 ▶sub asr {
Subroutine asr: called CC=10 SCAR=24.3
Pod documented
96 6 my $class = __PACKAGE__;
97 6 ▶ if (@_ && eval { $_[0]->isa(__PACKAGE__) }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and eval { do { $_[0]->isa(__PACKAGE__) } }) 1 5 6
Condition66%2/3
@_ and eval { do { $_[0]->isa(__PACKAGE__) } }
!ll&&!rl&&r
051
MC/DC50%1/2
@_ and eval { do { $_[0]->isa(__PACKAGE__) } }
@_ eval { do { $_[0]->isa(__PACKAGE__) } }
Truth table66%2/3
A and B
A= @_B= eval { do { $_[0]->isa(__PACKAGE__) } }
ABresult
0X0
100
111
98 1 my $inv = shift;
99 1 ▶ $class = ref($inv) || $inv;
Condition33%1/3
ref $inv || $inv
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $inv || $inv
ref $inv $inv
Truth table33%1/3
A || B
A= ref $invB= $inv
ABresult
1X1
011
000
100 }
101 6 my ($attack, $sustain_time, $release, %opts) = @_;
102  
103 6 ▶ $attack //= 0.1;
Condition50%1/2
$attack //= "0.1"
l!l
60
MC/DC0%0/1
$attack //= "0.1"
$attack
Truth table50%1/2
A
A= $attack
Aresult
11
00
104 6 ▶ $sustain_time //= 0.5;
Condition50%1/2
$sustain_time //= "0.5"
l!l
60
MC/DC0%0/1
$sustain_time //= "0.5"
$sustain_time
Truth table50%1/2
A
A= $sustain_time
Aresult
11
00
105 6 ▶ $release //= 0.3;
Condition50%1/2
$release //= "0.3"
l!l
60
MC/DC0%0/1
$release //= "0.3"
$release
Truth table50%1/2
A
A= $release
Aresult
11
00
106  
107 6 ▶ my $peak = $opts{peak} // 1.0;
Condition100%2/2
$opts{"peak"} // 1
l!l
24
MC/DC100%1/1
$opts{"peak"} // 1
$opts{"peak"}
Truth table100%2/2
A
A= $opts{"peak"}
Aresult
11
00
108 6 ▶ my $attack_curve = $opts{attack_curve} // 2;
Condition50%1/2
$opts{"attack_curve"} // 2
l!l
06
MC/DC0%0/1
$opts{"attack_curve"} // 2
$opts{"attack_curve"}
Truth table50%1/2
A
A= $opts{"attack_curve"}
Aresult
11
00
109 6 ▶ my $release_curve = $opts{release_curve} // -2;
Condition50%1/2
$opts{"release_curve"} // -2
l!l
06
MC/DC0%0/1
$opts{"release_curve"} // -2
$opts{"release_curve"}
Truth table50%1/2
A
A= $opts{"release_curve"}
Aresult
11
00
110  
111 6 my $def = [
112 [0, $peak, $peak, 0],
113 [$attack, $sustain_time, $release],
114 [$attack_curve, 1, $release_curve],
115 ];
116  
117 6 $class->new($def, %opts);
118 }
119  
120 9 ▶sub concat {
Subroutine concat: called CC=8 SCAR=20.8
Pod documented
121 9 my $class = __PACKAGE__;
122 9 ▶ if (@_ && eval { $_[0]->isa(__PACKAGE__) }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and eval { do { $_[0]->isa(__PACKAGE__) } }) 8 1 9
Condition66%2/3
@_ and eval { do { $_[0]->isa(__PACKAGE__) } }
!ll&&!rl&&r
108
MC/DC50%1/2
@_ and eval { do { $_[0]->isa(__PACKAGE__) } }
@_ eval { do { $_[0]->isa(__PACKAGE__) } }
Truth table66%2/3
A and B
A= @_B= eval { do { $_[0]->isa(__PACKAGE__) } }
ABresult
0X0
100
111
123 8 ▶ if (!ref($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0]) { } 4 4 8
124 4 $class = shift;
125 } else {
126 4 $class = ref($_[0]);
127 }
128 }
129 9 my @envelopes = @_;
130  
131 9 ▶ return $class->new() unless @envelopes;
Branch: 1 branch
BranchTrueFalseTotal
unless @envelopes 1 8 9
132 8 ▶ return $envelopes[0] if @envelopes == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @envelopes == 1 1 7 8
133  
134 7 my (@levels, @durs, @curves);
135  
136 7 for my $i (0 .. $#envelopes) {
137 15 my $e = $envelopes[$i];
138 15 my $def = $e->def;
139  
140 15 my @e_levels = @{$def->[0]};
141 15 my @e_durs = @{$def->[1]};
142 15 my @e_curves = @{$def->[2]};
143  
144 15 ▶ if ($i == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0) { } 7 8 15
145 # First envelope: take all levels
146 7 push @levels, @e_levels;
147 } else {
148 # Subsequent envelopes: skip first level (use last from previous)
149 8 push @levels, @e_levels[1 .. $#e_levels];
150 }
151  
152 15 push @durs, @e_durs;
153 15 push @curves, @e_curves;
154 }
155  
156 7 my %opts = eval { %{$envelopes[0]->_options} };
157 7 $class->new([[@levels], [@durs], [@curves]], %opts);
158 }
159  
160 50 ▶sub _options {
Subroutine _options: called CC=7 SCAR=19.7
161 50 my ($self) = @_;
162 50 ▶ my %opts = (
Branch: 4 branches
BranchTrueFalseTotal
$self->is_morph ? : 0 50 50
$self->is_hold ? : 12 38 50
$self->is_fold_over ? : 12 38 50
$self->is_wrap_neg ? : 12 38 50
163 is_morph => $self->is_morph ? 1 : 0,
164 is_hold => $self->is_hold ? 1 : 0,
165 is_fold_over => $self->is_fold_over ? 1 : 0,
166 is_wrap_neg => $self->is_wrap_neg ? 1 : 0,
167 );
168 50 my $formula = $self->morpher_formula;
169 50 ▶ $opts{morpher_formula} = $formula if defined $formula;
Branch: 1 branch
BranchTrueFalseTotal
if defined $formula 0 50 50
170 50 my $cv = $self->morpher;
171 50 ▶ $opts{morpher} = $cv if defined $cv;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cv 0 50 50
172 50 return \%opts;
173 }
174  
175 8 ▶sub scale {
Subroutine scale: called CC=2 SCAR=6.9
Pod documented
176 8 my ($self, $factor) = @_;
177 8 ▶ $factor //= 1.0;
Condition50%1/2
$factor //= 1
l!l
80
MC/DC0%0/1
$factor //= 1
$factor
Truth table50%1/2
A
A= $factor
Aresult
11
00
178  
179 8 my $def = $self->def;
180 8 my @levels = map { $_ * $factor } @{$def->[0]};
181  
182 8 ref($self)->new([[@levels], [@{$def->[1]}], [@{$def->[2]}]], %{$self->_options});
183 }
184  
185 3 ▶sub reverse {
Subroutine reverse: called CC=1 SCAR=0.0
Pod documented
186 3 my ($self) = @_;
187  
188 3 my $def = $self->def;
189 3 my @levels = reverse @{$def->[0]};
190 3 my @durs = reverse @{$def->[1]};
191 3 my @curves = map { -$_ } reverse @{$def->[2]};
192  
193 3 ref($self)->new([[@levels], [@durs], [@curves]], %{$self->_options});
194 }
195  
196 4 ▶sub delay {
Subroutine delay: called CC=3 SCAR=11.0
Pod documented
197 4 my ($self, $time) = @_;
198 4 ▶ $time //= 0;
Condition50%1/2
$time //= 0
l!l
40
MC/DC0%0/1
$time //= 0
$time
Truth table50%1/2
A
A= $time
Aresult
11
00
199 4 ▶ return $self if $time <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $time <= 0 2 2 4
200  
201 2 my $def = $self->def;
202 2 my $start_level = $def->[0][0];
203  
204 2 my @levels = ($start_level, @{$def->[0]});
205 2 my @durs = ($time, @{$def->[1]});
206 2 my @curves = (1, @{$def->[2]});
207  
208 2 ref($self)->new([[@levels], [@durs], [@curves]], %{$self->_options});
209 }
210  
211 4 ▶sub stretch {
Subroutine stretch: called CC=2 SCAR=6.9
Pod documented
212 4 my ($self, $factor) = @_;
213 4 ▶ $factor //= 1.0;
Condition50%1/2
$factor //= 1
l!l
40
MC/DC0%0/1
$factor //= 1
$factor
Truth table50%1/2
A
A= $factor
Aresult
11
00
214  
215 4 my $def = $self->def;
216 4 my @durs = map { $_ * $factor } @{$def->[1]};
217  
218 4 ref($self)->new([[@{$def->[0]}], [@durs], [@{$def->[2]}]], %{$self->_options});
219 }
220  
221 1 ▶sub with_duration {
Subroutine with_duration: called CC=4 SCAR=15.2
Pod documented
222 1 my ($self, $new_dur) = @_;
223 1 ▶ die "with_duration: duration must be positive\n" unless defined $new_dur && $new_dur > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new_dur and $new_dur > 0 0 1 1
Condition33%1/3
defined $new_dur and $new_dur > 0
!ll&&!rl&&r
001
MC/DC0%0/2
defined $new_dur and $new_dur > 0
defined $new_dur $new_dur > 0
Truth table33%1/3
A and B
A= defined $new_durB= $new_dur > 0
ABresult
0X0
100
111
224 1 my $cur = $self->duration;
225 1 ▶ return $self if $cur <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $cur <= 0 0 1 1
226 1 return $self->stretch($new_dur / $cur);
227 }
228  
229 7 ▶sub offset {
Subroutine offset: called CC=2 SCAR=6.9
Pod documented
230 7 my ($self, $value) = @_;
231 7 ▶ $value //= 0;
Condition50%1/2
$value //= 0
l!l
70
MC/DC0%0/1
$value //= 0
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
232  
233 7 my $def = $self->def;
234 7 my @levels = map { $_ + $value } @{$def->[0]};
235  
236 7 ref($self)->new([[@levels], [@{$def->[1]}], [@{$def->[2]}]], %{$self->_options});
237 }
238  
239 2 ▶sub invert {
Subroutine invert: called CC=1 SCAR=0.0
Pod documented
240 2 my ($self) = @_;
241  
242 2 my $def = $self->def;
243 2 my @levels = map { 1 - $_ } @{$def->[0]};
244  
245 2 ref($self)->new([[@levels], [@{$def->[1]}], [@{$def->[2]}]], %{$self->_options});
246 }
247  
248 3 ▶sub normalize {
Subroutine normalize: called CC=4 SCAR=13.9
Pod documented
249 3 my ($self, $lo, $hi) = @_;
250 3 ▶ $lo //= 0;
Condition100%2/2
$lo //= 0
l!l
21
MC/DC100%1/1
$lo //= 0
$lo
Truth table100%2/2
A
A= $lo
Aresult
11
00
251 3 ▶ $hi //= 1;
Condition100%2/2
$hi //= 1
l!l
21
MC/DC100%1/1
$hi //= 1
$hi
Truth table100%2/2
A
A= $hi
Aresult
11
00
252  
253 3 my $min = $self->min_value;
254 3 my $max = $self->max_value;
255 3 my $range = $max - $min;
256 3 ▶ return $self->scale(0)->offset($lo) if $range == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $range == 0 0 3 3
257  
258 3 my $def = $self->def;
259 3 my $target = $hi - $lo;
260 3 my @levels = map { $lo + ($_ - $min) / $range * $target } @{$def->[0]};
261  
262 3 ref($self)->new([[@levels], [@{$def->[1]}], [@{$def->[2]}]], %{$self->_options});
263 }
264  
265 2 ▶sub loop {
Subroutine loop: called CC=4 SCAR=14.0
Pod documented
266 2 my ($self, $n) = @_;
267 2 ▶ $n //= 2;
Condition50%1/2
$n //= 2
l!l
20
MC/DC0%0/1
$n //= 2
$n
Truth table50%1/2
A
A= $n
Aresult
11
00
268 2 ▶ $n = 1 if $n < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n < 1 0 2 2
269 2 ▶ return $self if $n == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 1 1 1 2
270  
271 1 my $class = ref($self);
272 1 my @copies = ($self) x $n;
273 1 $class->concat(@copies);
274 }
275  
276 2 ▶sub map_levels {
Subroutine map_levels: called CC=1 SCAR=0.0
Pod documented
277 2 my ($self, $fn) = @_;
278  
279 2 my $def = $self->def;
280 2 my @levels = map { $fn->($_) } @{$def->[0]};
281  
282 2 ref($self)->new([[@levels], [@{$def->[1]}], [@{$def->[2]}]], %{$self->_options});
283 }
284  
285 5 ▶sub add {
Subroutine add: called CC=1 SCAR=0.0
Pod documented
286 5 my ($self, $other, %opts) = @_;
287 5 ▶ $self->_combine($other, sub { $_[0] + $_[1] }, %opts);
Subroutine __ANON__: called CC=1 SCAR=0.0
288 }
289  
290 3 ▶sub multiply {
Subroutine multiply: called CC=1 SCAR=0.0
Pod documented
291 3 my ($self, $other, %opts) = @_;
292 3 ▶ $self->_combine($other, sub { $_[0] * $_[1] }, %opts);
Subroutine __ANON__: called CC=1 SCAR=0.0
293 }
294  
295 8 ▶sub min_value {
Subroutine min_value: called CC=4 SCAR=14.1
Pod documented
296 8 my ($self, $samples) = @_;
297 8 ▶ $samples //= 1024;
Condition50%1/2
$samples //= 1024
l!l
08
MC/DC0%0/1
$samples //= 1024
$samples
Truth table50%1/2
A
A= $samples
Aresult
11
00
298 8 my @tbl = $self->table($samples);
299 8 my $min = $tbl[0];
300 8 ▶ for (@tbl) { $min = $_ if $_ < $min }
Branch: 1 branch
BranchTrueFalseTotal
if $_ < $min 0 8192 8192
301 8 return $min;
302 }
303  
304 8 ▶sub max_value {
Subroutine max_value: called CC=4 SCAR=13.9
Pod documented
305 8 my ($self, $samples) = @_;
306 8 ▶ $samples //= 1024;
Condition50%1/2
$samples //= 1024
l!l
08
MC/DC0%0/1
$samples //= 1024
$samples
Truth table50%1/2
A
A= $samples
Aresult
11
00
307 8 my @tbl = $self->table($samples);
308 8 my $max = $tbl[0];
309 8 ▶ for (@tbl) { $max = $_ if $_ > $max }
Branch: 1 branch
BranchTrueFalseTotal
if $_ > $max 3004 5188 8192
310 8 return $max;
311 }
312  
313 3 ▶sub to_hash {
Subroutine to_hash: called CC=6 SCAR=18.8
Pod documented
314 3 my ($self) = @_;
315 3 my $def = $self->def;
316 3 ▶ my %h = (
Branch: 4 branches
BranchTrueFalseTotal
$self->is_morph ? : 3 0 3
$self->is_hold ? : 3 0 3
$self->is_fold_over ? : 2 1 3
$self->is_wrap_neg ? : 0 3 3
317 def => $def,
318 is_morph => $self->is_morph ? 1 : 0,
319 is_hold => $self->is_hold ? 1 : 0,
320 is_fold_over => $self->is_fold_over ? 1 : 0,
321 is_wrap_neg => $self->is_wrap_neg ? 1 : 0,
322 );
323 3 my $formula = $self->morpher_formula;
324 3 ▶ $h{morpher_formula} = $formula if defined $formula;
Branch: 1 branch
BranchTrueFalseTotal
if defined $formula 3 0 3
325 3 return \%h;
326 }
327  
328 4 ▶sub from_hash {
Subroutine from_hash: called CC=7 SCAR=20.6
Pod documented
329 4 my $class = __PACKAGE__;
330 4 ▶ if (@_ && eval { $_[0]->isa(__PACKAGE__) }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and eval { do { $_[0]->isa(__PACKAGE__) } }) 4 0 4
Condition33%1/3
@_ and eval { do { $_[0]->isa(__PACKAGE__) } }
!ll&&!rl&&r
004
MC/DC0%0/2
@_ and eval { do { $_[0]->isa(__PACKAGE__) } }
@_ eval { do { $_[0]->isa(__PACKAGE__) } }
Truth table33%1/3
A and B
A= @_B= eval { do { $_[0]->isa(__PACKAGE__) } }
ABresult
0X0
100
111
331 4 my $inv = shift;
332 4 ▶ $class = ref($inv) || $inv;
Condition33%1/3
ref $inv || $inv
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $inv || $inv
ref $inv $inv
Truth table33%1/3
A || B
A= ref $invB= $inv
ABresult
1X1
011
000
333 }
334 4 my ($h) = @_;
335 4 ▶ die "from_hash: expected a hash reference\n" unless ref($h) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $h eq "HASH" 1 3 4
336 3 my %opts;
337 3 for my $k (qw(is_morph is_hold is_fold_over is_wrap_neg morpher_formula)) {
338 15 ▶ $opts{$k} = $h->{$k} if exists $h->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $h->{$k} 15 0 15
339 }
340 3 $class->new($h->{def}, %opts);
341 }
342  
343 2 ▶sub STORABLE_freeze {
Subroutine STORABLE_freeze: called CC=1 SCAR=0.0
Pod documented
344 2 my ($self, $cloning) = @_;
345 2 require Storable;
346 2 return Storable::freeze($self->to_hash);
347 }
348  
349 2 ▶sub STORABLE_thaw {
Subroutine STORABLE_thaw: called CC=1 SCAR=0.0
Pod documented
350 2 my ($self, $cloning, $data) = @_;
351 2 require Storable;
352 2 my $h = Storable::thaw($data);
353 2 my $obj = Math::SegmentedEnvelope->from_hash($h);
354 2 ${$self} = ${$obj};
355 2 ${$obj} = 0;
356 2 return $self;
357 }
358  
359 1 ▶sub to_svg {
Subroutine to_svg: called CC=13 SCAR=26.8
Pod documented
360 1 my ($self, %opts) = @_;
361 1 ▶ my $w = $opts{width} // 400;
Condition50%1/2
$opts{"width"} // 400
l!l
01
MC/DC0%0/1
$opts{"width"} // 400
$opts{"width"}
Truth table50%1/2
A
A= $opts{"width"}
Aresult
11
00
362 1 ▶ my $h = $opts{height} // 150;
Condition50%1/2
$opts{"height"} // 150
l!l
01
MC/DC0%0/1
$opts{"height"} // 150
$opts{"height"}
Truth table50%1/2
A
A= $opts{"height"}
Aresult
11
00
363 1 ▶ my $pad = $opts{padding} // 10;
Condition50%1/2
$opts{"padding"} // 10
l!l
01
MC/DC0%0/1
$opts{"padding"} // 10
$opts{"padding"}
Truth table50%1/2
A
A= $opts{"padding"}
Aresult
11
00
364 1 ▶ my $stroke = $opts{stroke} // '#2563eb';
Condition50%1/2
$opts{"stroke"} // "#2563eb"
l!l
01
MC/DC0%0/1
$opts{"stroke"} // "#2563eb"
$opts{"stroke"}
Truth table50%1/2
A
A= $opts{"stroke"}
Aresult
11
00
365 1 ▶ my $fill = $opts{fill} // '#2563eb22';
Condition50%1/2
$opts{"fill"} // "#2563eb22"
l!l
01
MC/DC0%0/1
$opts{"fill"} // "#2563eb22"
$opts{"fill"}
Truth table50%1/2
A
A= $opts{"fill"}
Aresult
11
00
366 1 ▶ my $samples = $opts{samples} // ($w - 2 * $pad);
Condition50%1/2
$opts{"samples"} // $w - 2 * $pad
l!l
01
MC/DC0%0/1
$opts{"samples"} // $w - 2 * $pad
$opts{"samples"}
Truth table50%1/2
A
A= $opts{"samples"}
Aresult
11
00
367 1 ▶ $samples = 2 if $samples < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $samples < 2 0 1 1
368  
369 1 my @vals = $self->table($samples);
370 1 my ($min, $max) = ($vals[0], $vals[0]);
371 1 ▶ for (@vals) { $min = $_ if $_ < $min; $max = $_ if $_ > $max }
Branch: 2 branches
BranchTrueFalseTotal
if $_ < $min 0 380 380
if $_ > $max 38 342 380
372 1 ▶ my $range = $max - $min || 1;
Condition50%1/2
$max - $min || 1
l!l
10
MC/DC0%0/1
$max - $min || 1
$max - $min
Truth table50%1/2
A
A= $max - $min
Aresult
11
00
373  
374 1 my $pw = ($w - 2 * $pad) / ($samples - 1);
375 1 my @points;
376 1 for my $i (0 .. $#vals) {
377 380 my $x = $pad + $i * $pw;
378 380 my $y = $pad + (1 - ($vals[$i] - $min) / $range) * ($h - 2 * $pad);
379 380 push @points, sprintf("%.1f,%.1f", $x, $y);
380 }
381  
382 1 my $polyline = join(' ', @points);
383 1 my $fill_points = join(' ', @points,
384 sprintf("%.1f,%.1f", $pad + ($samples - 1) * $pw, $h - $pad),
385 sprintf("%.1f,%.1f", $pad, $h - $pad));
386  
387 1 return qq{<svg xmlns="http://www.w3.org/2000/svg" width="$w" height="$h" viewBox="0 0 $w $h">}
388 . qq{<polygon points="$fill_points" fill="$fill" stroke="none"/>}
389 . qq{<polyline points="$polyline" fill="none" stroke="$stroke" stroke-width="1.5"/>}
390 . qq{</svg>\n};
391 }
392  
393 1 ▶sub to_supercollider {
Subroutine to_supercollider: called CC=2 SCAR=6.9
Pod documented
394 1 my ($self, %opts) = @_;
395 1 ▶ my $varname = $opts{name} // 'env';
Condition50%1/2
$opts{"name"} // "env"
l!l
01
MC/DC0%0/1
$opts{"name"} // "env"
$opts{"name"}
Truth table50%1/2
A
A= $opts{"name"}
Aresult
11
00
396 1 my $def = $self->def;
397 1 my @levels = @{$def->[0]};
398 1 my @durs = @{$def->[1]};
399 1 my @curves = @{$def->[2]};
400  
401 # SuperCollider Env format: Env([levels], [times], [curves])
402 1 my $l = join(', ', map { sprintf '%.4g', $_ } @levels);
403 1 my $t = join(', ', map { sprintf '%.4g', $_ } @durs);
404 1 my $c = join(', ', map { sprintf '%.4g', $_ } @curves);
405  
406 1 return "var $varname = Env([$l], [$t], [$c]);\n";
407 }
408  
409 1 ▶sub to_csound {
Subroutine to_csound: called CC=2 SCAR=6.9
Pod documented
410 1 my ($self, %opts) = @_;
411 1 my $def = $self->def;
412 1 my @levels = @{$def->[0]};
413 1 my @durs = @{$def->[1]};
414  
415 # Csound linseg/expseg format: p-fields
416 # linseg istart, idur1, ival1, idur2, ival2, ...
417 1 my @args = (sprintf('%.4g', $levels[0]));
418 1 for my $i (0 .. $#durs) {
419 4 push @args, sprintf('%.4g', $durs[$i]);
420 4 push @args, sprintf('%.4g', $levels[$i + 1]);
421 }
422  
423 1 return "linseg " . join(', ', @args) . "\n";
424 }
425  
426 1 ▶sub to_glsl {
Subroutine to_glsl: called CC=4 SCAR=13.9
Pod documented
427 1 my ($self, %opts) = @_;
428 1 ▶ my $name = $opts{name} // 'envelope';
Condition50%1/2
$opts{"name"} // "envelope"
l!l
10
MC/DC0%0/1
$opts{"name"} // "envelope"
$opts{"name"}
Truth table50%1/2
A
A= $opts{"name"}
Aresult
11
00
429 1 ▶ my $samples = $opts{samples} // 64;
Condition50%1/2
$opts{"samples"} // 64
l!l
10
MC/DC0%0/1
$opts{"samples"} // 64
$opts{"samples"}
Truth table50%1/2
A
A= $opts{"samples"}
Aresult
11
00
430 1 ▶ $samples = 2 if $samples < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $samples < 2 0 1 1
431  
432 1 my @vals = $self->table($samples);
433 1 my $dur = $self->duration;
434  
435 1 my $code = "float ${name}(float t) {\n";
436 1 $code .= " float dur = " . sprintf('%.6f', $dur) . ";\n";
437 1 $code .= " float n = clamp(t / dur, 0.0, 1.0) * " . ($samples - 1) . ".0;\n";
438 1 $code .= " int i = int(n);\n";
439 1 $code .= " float f = fract(n);\n";
440 1 $code .= " float data[$samples] = float[$samples](";
441 1 $code .= join(', ', map { sprintf '%.5f', $_ } @vals);
442 1 $code .= ");\n";
443 1 $code .= " if (i >= " . ($samples - 1) . ") return data[" . ($samples - 1) . "];\n";
444 1 $code .= " return mix(data[i], data[i + 1], f);\n";
445 1 $code .= "}\n";
446  
447 1 return $code;
448 }
449  
450 0 ▶sub as_pdl {
Subroutine as_pdl: not called CC=1 SCAR=6.9
Pod documented
451 0 my ($self) = @_;
452 0 require PDL::Core;
453 0 my $n = $self->segments + 1;
454 0 my $p = PDL::Core::zeroes(PDL::Core::double(), $n);
455 0 my $ref = $p->get_dataref;
456 0 $$ref = $self->_raw_levels;
457 0 $p->upd_data;
458 0 return $p;
459 }
460  
461 0 ▶sub to_pdl {
Subroutine to_pdl: not called CC=2 SCAR=17.9
Pod documented
462 0 my ($self, $samples) = @_;
463 0 ▶ $samples //= 1024;
Condition0%0/2
$samples //= 1024
l!l
00
MC/DC0%0/1
$samples //= 1024
$samples
Truth table0%0/2
A
A= $samples
Aresult
11
00
464 0 require PDL::Core;
465 0 my $p = PDL::Core::zeroes(PDL::Core::double(), $samples);
466 0 my $ref = $p->get_dataref;
467 0 $$ref = $self->_raw_table($samples);
468 0 $p->upd_data;
469 0 return $p;
470 }
471  
472 0 ▶sub from_pdl {
Subroutine from_pdl: not called CC=5 SCAR=34.0
Pod documented
473 0 my $class = __PACKAGE__;
474 0 ▶ if (@_ && eval { $_[0]->isa(__PACKAGE__) }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and eval { do { $_[0]->isa(__PACKAGE__) } }) 0 0 0
Condition0%0/3
@_ and eval { do { $_[0]->isa(__PACKAGE__) } }
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and eval { do { $_[0]->isa(__PACKAGE__) } }
@_ eval { do { $_[0]->isa(__PACKAGE__) } }
Truth table0%0/3
A and B
A= @_B= eval { do { $_[0]->isa(__PACKAGE__) } }
ABresult
0X0
100
111
475 0 my $inv = shift;
476 0 ▶ $class = ref($inv) || $inv;
Condition0%0/3
ref $inv || $inv
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $inv || $inv
ref $inv $inv
Truth table0%0/3
A || B
A= ref $invB= $inv
ABresult
1X1
011
000
477 }
478 0 my ($pdl, $duration) = @_;
479 0 ▶ $duration //= 1.0;
Condition0%0/2
$duration //= 1
l!l
00
MC/DC0%0/1
$duration //= 1
$duration
Truth table0%0/2
A
A= $duration
Aresult
11
00
480 0 my @vals = $pdl->list;
481 0 return $class->from_samples(\@vals, $duration);
482 }
483  
484 12 ▶sub _combine {
Subroutine _combine: called CC=10 SCAR=23.3
485 12 my ($self, $other, $op, %opts) = @_;
486 12 ▶ my $segments = delete $opts{segments} // 32;
Condition100%2/2
delete $opts{"segments"} // 32
l!l
66
MC/DC100%1/1
delete $opts{"segments"} // 32
delete $opts{"segments"}
Truth table100%2/2
A
A= delete $opts{"segments"}
Aresult
11
00
487 12 ▶ $segments = 32 if $segments < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $segments < 1 1 11 12
488 12 ▶ my $duration = delete $opts{duration} // $self->duration;
Condition33%1/3
delete $opts{"duration"} // $self->duration
l!l&&r!l&&!r
0120
MC/DC0%0/2
delete $opts{"duration"} // $self->duration
delete $opts{"duration"} $self->duration
Truth table33%1/3
A // B
A= delete $opts{"duration"}B= $self->duration
ABresult
1X1
011
000
489 12 ▶ $duration = 1.0 if $duration <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $duration <= 0 0 12 12
490  
491 12 my $s1 = $self->static;
492 12 my $s2 = $other->static;
493 12 my $d1 = $self->duration;
494 12 my $d2 = $other->duration;
495  
496 12 my (@levels, @durs, @curves);
497 12 my $seg_dur = $duration / $segments;
498 12 for my $i (0 .. $segments) {
499 274 my $t = $i * $seg_dur;
500 274 ▶ my $t1 = $d1 > 0 ? ($t / $duration) * $d1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$d1 > 0 ? : 274 0 274
501 274 ▶ my $t2 = $d2 > 0 ? ($t / $duration) * $d2 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$d2 > 0 ? : 274 0 274
502 274 push @levels, $op->($s1->($t1), $s2->($t2));
503 }
504 12 push @durs, $seg_dur for 1 .. $segments;
505 12 push @curves, 1 for 1 .. $segments;
506  
507 12 my %combined_opts = (%{$self->_options}, %opts);
508 12 ref($self)->new([[@levels], [@durs], [@curves]], %combined_opts);
509 }
510  
511 4 ▶sub blend {
Subroutine blend: called CC=2 SCAR=7.0
Pod documented
512 4 my ($self, $other, $mix, %opts) = @_;
513 4 ▶ $mix //= 0.5;
Condition50%1/2
$mix //= "0.5"
l!l
40
MC/DC0%0/1
$mix //= "0.5"
$mix
Truth table50%1/2
A
A= $mix
Aresult
11
00
514 4 ▶ $self->_combine($other, sub { $_[0] * (1 - $mix) + $_[1] * $mix }, %opts);
Subroutine __ANON__: called CC=1 SCAR=0.0
515 }
516  
517 package Math::SegmentedEnvelope::Static;
518  
519 2 ▶use overload '&{}' => sub {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
520 585 my $self = shift;
521 585 ▶ return sub { $self->call(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
522 2 }, fallback => 1;
523  
524 2 1;
525  
526 __END__
↑ Summary tinyexpr.c »