lib/Peta/Synth/Wavetable.pm

filter: Statementstmt 100% 100 / 100 Branchbran 89.2% 25 / 28 Conditioncond 81.8% 18 / 22 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 100% 15 / 15 Podpod 100% 4 / 4 totaltotal 93.9% 170 / 181 CC 27 SCAR 17.3
CC
27
Cov
95%
harmonics
10.0
_make
9.0
_rate
4.1
CRAP 5.6
« lib/Peta/MP3/Encode/Psy.pm ↑ Summary lib/Peta/Synth/Output.pm »
1 package Peta::Synth::Wavetable;
2 # ABSTRACT: partials compiled into wave sequences, made once and kept
3  
4 # Partials compiled into wave sequences: a voice whose partials are
5 # harmonics (each copy of its unison: harmonics of the copy's own
6 # fundamental) sounds the same read from tables of one period each, one
7 # table for every moment where its spectrum has moved by some 3 dB (each
8 # partial's decay and fade-in), crossfaded from one to the next. Reading
9 # two tables costs the same whatever the number of partials; the partials,
10 # turned one by one, cost each its own.
11 #
12 # A sequence is made once for what its partials are (their harmonics,
13 # amplitudes, decays and fade-ins: whatever the patch, key, velocity,
14 # channel and budget made them) and kept: every voice with the same
15 # partials reads it, each from its own place in the period (so that two
16 # alike are not one, louder). Its partials' phases follow from what they
17 # are, so a sequence is the same whoever made it first.
18 #
19 # A sequence: { size (samples a period), times [ seconds since the voice
20 # began ], tables [ [ size + 1 samples (the last the first again) ] ] }.
21 # After the last time, the last table holds (the spectrum is still, or
22 # silent).
23  
24 17 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 17 ▶use Digest::MD5 qw(md5);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 17 ▶use List::Util qw(max min);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 17 ▶use Peta::Synth::FFT;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 17 our $VERSION = '0.2610180';
30  
31 17 ▶use constant TWO_PI => 6.283185307179586;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 17 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
33 17 STEP => 0.3466, # ln(sqrt(2)): a partial moves 3 dB between tables at most
34 SHORTEST => 0.002, # seconds between tables at the least, at most
35 LONGEST => 2,
36 QUIET => 1e-3, # a partial under this much of the loudest one does not set the steps
37 SILENT => 1e-5, # partials under this much of the start: the sequence ends
38 TABLES => 256, # tables in a sequence at most
39 PER_CYCLE => 32, # samples a period of the highest harmonic, at the least
40 17 };
41  
42 508 ▶sub new ($class) {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
43 508 return bless {
44 sequences => {}, # by the MD5 of their partials
45 made => 0,
46 used => 0,
47 samples => 0,
48 seconds => 0,
49 }, $class;
50 }
51  
52 # The sequence for partials [harmonic, amplitude, decay (1/s), fade-in (s)]:
53 # made, or found made.
54 364 ▶sub sequence ($self, $partials) {
Subroutine sequence: called CC=2 SCAR=6.9
Pod documented
55 364 my $key = md5(pack 'd*', map { @$_ } @$partials);
56 364 $self->{used}++;
57 364 ▶ return $self->{sequences}{$key} if $self->{sequences}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"sequences"}{$key} 74 290 364
58  
59 290 require Time::HiRes;
60 290 my $started = Time::HiRes::time();
61 290 my $sequence = _make($partials, $key);
62 290 $self->{made}++;
63 290 $self->{samples} += @{ $sequence->{tables} } * ($sequence->{size} + 1);
64 290 $self->{seconds} += Time::HiRes::time() - $started;
65  
66 290 return $self->{sequences}{$key} = $sequence;
67 }
68  
69 # How much was made and found: { sequences, made, used (made or found),
70 # samples (in all the tables), seconds (spent making them) }.
71 1 ▶sub stats ($self) {
Subroutine stats: called CC=1 SCAR=0.0
Pod documented
72 return {
73 1 sequences => scalar keys %{ $self->{sequences} },
74 1 map { $_ => $self->{$_} } qw(made used samples seconds),
75 };
76 }
77  
78 # A voice's partials as copies of harmonics: [ [shift, [ [harmonic, amp,
79 # decay, attack] ... ]] ... ], one for each copy of the unison, $most at
80 # most (its frequency over the voice's fundamental, and the harmonics of
81 # that), or undef if they are not harmonics so (a stiff string's, a bell's).
82 604 ▶sub harmonics ($ratio, $amp, $decay, $attack, $most = 1) {
Subroutine harmonics: called CC=10 SCAR=23.0
Pod documented
83 604 my @copies; # [shift, partials]
84 my %copy; # shift => its copy
85 604 for my $i (0 .. $#$ratio) {
86 6450 my $r = $ratio->[$i];
87 # A copy's fundamental is its first partial's (the copies carry the strongest first).
88 6450 my $copy;
89 6450 for my $c (@copies) {
90 7660 my $h = $r / $c->[0];
91 7660 ▶ if (abs($h - int($h + 0.5)) < 1e-7 * $h && int($h + 0.5) >= 1) { $copy = $c; last }
Branch: 1 branch
BranchTrueFalseTotal
if (abs $h - int($h + "0.5") < "1e-07" * $h and int $h + "0.5" >= 1) 5146 2514 7660
Condition66%2/3
abs $h - int($h + "0.5") < "1e-07" * $h and int $h + "0.5" >= 1
!ll&&!rl&&r
251405146
MC/DC50%1/2
abs $h - int($h + "0.5") < "1e-07" * $h and int $h + "0.5" >= 1
abs $h - int($h + "0.5") < "1e-07" * $h int $h + "0.5" >= 1
Truth table66%2/3
A and B
A= abs $h - int($h + "0.5") < "1e-07" * $hB= int $h + "0.5" >= 1
ABresult
0X0
100
111
92 }
93 6450 ▶ if (!$copy) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($copy) 1304 5146 6450
94 # A new copy: the shift that makes this partial a whole harmonic of it.
95 1304 ▶ my $h = int($r + 0.5) || 1;
Condition100%2/2
int $r + 0.5 || 1
l!l
12995
MC/DC100%1/1
int $r + 0.5 || 1
int $r + 0.5
Truth table100%2/2
A
A= int $r + 0.5
Aresult
11
00
96 1304 my $shift = $r / $h;
97 1304 ▶ return undef if abs($shift - 1) > 0.03; # not a detuned copy: not harmonic
Branch: 1 branch
BranchTrueFalseTotal
if abs $shift - 1 > "0.03" 213 1091 1304
98 1091 ▶ return undef if @copies >= $most; # more copies than the unison has: a stiff string's partials
Branch: 1 branch
BranchTrueFalseTotal
if @copies >= $most 183 908 1091
99 908 $copy = [ $shift, [] ];
100 908 push @copies, $copy;
101 }
102 6054 push @{ $copy->[1] }, [ int($r / $copy->[0] + 0.5), $amp->[$i], $decay->[$i], $attack->[$i] ];
103 }
104 208 return \@copies;
105 }
106  
107 # A partial's amplitude at a time.
108 83335 ▶sub _level ($p, $t) {
Subroutine _level: called CC=2 SCAR=6.9
109 83335 my ($h, $a, $d, $att) = @$p;
110 83335 my $level = $a * exp(-$d * $t);
111 83335 ▶ return $att > 0 ? $level * (1 - exp(-$t / $att)) : $level;
Branch: 1 branch
BranchTrueFalseTotal
$att > 0 ? : 9208 74127 83335
112 }
113  
114 # Whether a partial sets the steps between tables at time $t: one not much
115 # quieter than the loudest, or one still fading in.
116 82544 ▶sub _counts ($p, $level, $loudest, $t) {
Subroutine _counts: called CC=3 SCAR=11.0
117 82544 my $attack = $p->[3];
118 82544 ▶ return $level >= QUIET * $loudest || $attack > 0 && $t < 5 * $attack;
Condition100%6/6
$level >= 0.001 * $loudest || $attack > 0 && $t < 5 * $attack
l!l&&r!l&&!r
33475165847411
$attack > 0 and $t < 5 * $attack
!ll&&!rl&&r
47365461658
MC/DC100%3/3
$level >= 0.001 * $loudest || $attack > 0 && $t < 5 * $attack
$level >= 0.001 * $loudest $attack > 0 $t < 5 * $attack
Truth table100%4/4
A || B and C
A= $level >= 0.001 * $loudestB= $attack > 0C= $t < 5 * $attack
ABCresult
1XX1
0111
00X0
0100
119 }
120  
121 # How fast (1/s) a partial's level moves at a time, as a logarithm.
122 35133 ▶sub _rate ($p, $t) {
Subroutine _rate: called CC=4 SCAR=14.0
123 35133 my ($h, $a, $d, $att) = @$p;
124 35133 my $rate = $d;
125 35133 ▶ if ($att > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($att > 0) 9161 25972 35133
126 9161 my $e = exp(-$t / $att);
127 9161 ▶ $rate += $e / $att / (1 - $e) if $e < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $e < 1 9161 0 9161
128 9161 ▶ $rate = 1e9 if $e >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $e >= 1 0 9161 9161
129 }
130 35133 return $rate;
131 }
132  
133 290 ▶sub _make ($partials, $key) {
Subroutine _make: called CC=9 SCAR=22.0
134 290 my $top = max(map { $_->[0] } @$partials);
135 290 my $size = 64;
136 290 ▶ $size *= 2 while $size < PER_CYCLE * $top && $size < 16384;
Branch: 1 branch
BranchTrueFalseTotal
$size < 32 * $top and $size < 16384 and do { $size *= 2 } 834 290 1124
Condition66%2/3
$size < 32 * $top and $size < 16384
!ll&&!rl&&r
2900834
MC/DC50%1/2
$size < 32 * $top and $size < 16384
$size < 32 * $top $size < 16384
Truth table66%2/3
A and B
A= $size < 32 * $topB= $size < 16384
ABresult
0X0
100
111
137 # The phases, from what the partials are: the same sequence whoever makes it.
138 290 my @phase = map { TWO_PI * unpack('N', md5($key . pack 'N', $_)) / 4294967296 } 0 .. $#$partials;
139 290 ▶ my $start = max(map { $_->[1] } @$partials) || 1;
Condition50%1/2
&max(map({$_->[1];} @$partials)) || 1
l!l
2900
MC/DC0%0/1
&max(map({$_->[1];} @$partials)) || 1
&max(map({$_->[1];} @$partials))
Truth table50%1/2
A
A= &max(map({$_->[1];} @$partials))
Aresult
11
00
140 290 my (@times, @tables);
141 290 my $t = 0;
142 290 while (1) {
143 4749 my @level = map { _level($_, $t) } @$partials;
144 4749 push @times, $t;
145 4749 push @tables, _table($size, $partials, \@level, \@phase);
146 4749 my $loudest = max(@level);
147 4749 ▶ last if @tables >= TABLES || $t > 0 && $loudest < SILENT * $start;
Branch: 1 branch
BranchTrueFalseTotal
if @tables >= 256 or $t > 0 and $loudest < "1e-05" * $start 56 4693 4749
Condition83%5/6
@tables >= 256 or $t > 0 and $loudest < "1e-05" * $start
l!l&&r!l&&!r
0564693
$t > 0 and $loudest < "1e-05" * $start
!ll&&!rl&&r
290440356
MC/DC66%2/3
@tables >= 256 or $t > 0 and $loudest < "1e-05" * $start
@tables >= 256 $t > 0 $loudest < "1e-05" * $start
Truth table75%3/4
A or B and C
A= @tables >= 256B= $t > 0C= $loudest < "1e-05" * $start
ABCresult
1XX1
0111
00X0
0100
148 # The next table where the fastest partial that counts has moved 3 dB.
149 4693 ▶ my @rates = map { _counts($partials->[$_], $level[$_], $loudest, $t) ? _rate($partials->[$_], max($t, SHORTEST)) : 0 }
Branch: 1 branch
BranchTrueFalseTotal
_counts($partials->[$_], $level[$_], $loudest, $t) ? : 35133 47411 82544
150 0 .. $#$partials;
151 4693 my $fastest = max(0, @rates);
152 4693 ▶ last if $fastest < 1e-6; # still: the last table holds
Branch: 1 branch
BranchTrueFalseTotal
if $fastest < "1e-06" 234 4459 4693
153 4459 $t += min(LONGEST, max(SHORTEST, STEP / $fastest));
154 }
155 290 return { size => $size, times => \@times, tables => \@tables };
156 }
157  
158 # One period of the harmonics at their levels: by an inverse FFT, the
159 # period's samples the real parts.
160 4749 ▶sub _table ($size, $partials, $level, $phase) {
Subroutine _table: called CC=3 SCAR=11.0
161 4749 my @re = ((0.0) x $size);
162 4749 my @im = ((0.0) x $size);
163 4749 for my $i (0 .. $#$partials) {
164 83335 my $h = $partials->[$i][0];
165 83335 ▶ next if $h >= $size / 2;
Branch: 1 branch
BranchTrueFalseTotal
if $h >= $size / 2 0 83335 83335
166 # level sin(theta h + phase): the bin's conjugate, so that the forward transform inverts.
167 83335 $re[$h] += $level->[$i] * sin($phase->[$i]);
168 83335 $im[$h] += $level->[$i] * cos($phase->[$i]);
169 }
170 4749 Peta::Synth::FFT::fft(\@re, \@im);
171 4749 push @re, $re[0];
172 4749 return \@re;
173 }
174  
175 17 1;
176  
177 __END__
« lib/Peta/MP3/Encode/Psy.pm ↑ Summary lib/Peta/Synth/Output.pm »