| 1 | package Peta::Synth::EQ; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: the master equalizer | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | # The master equalizer: four bands of biquad filters (R. Bristow-Johnson's | ||||||||||||||||||||||||||||
| 5 | # cookbook) on both channels. | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # low a low shelf (100 Hz) | ||||||||||||||||||||||||||||
| 8 | # mid1 a peaking band (400 Hz) | ||||||||||||||||||||||||||||
| 9 | # mid2 a peaking band (2.5 kHz) | ||||||||||||||||||||||||||||
| 10 | # high a high shelf (8 kHz) | ||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||
| 12 | # Each band has a frequency, a gain in dB (-12 .. 12) and, for the peaking | ||||||||||||||||||||||||||||
| 13 | # bands, a Q. A band at 0 dB is left out; all at 0 dB, the equalizer | ||||||||||||||||||||||||||||
| 14 | # passes the sound untouched. Linear and time-invariant, so it may run on | ||||||||||||||||||||||||||||
| 15 | # parts of a mix that are added up afterwards. | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 16 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 16 | our $VERSION = '0.2610180'; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 16 | ▶ | use constant PI => 3.141592653589793; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 16 | my @BANDS = ( | |||||||||||||||||||||||||||
| 24 | [ low => 'lowshelf', 100, 0.71 ], | ||||||||||||||||||||||||||||
| 25 | [ mid1 => 'peak', 400, 1.0 ], | ||||||||||||||||||||||||||||
| 26 | [ mid2 => 'peak', 2500, 1.0 ], | ||||||||||||||||||||||||||||
| 27 | [ high => 'highshelf', 8000, 0.71 ], | ||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||
| 29 | 16 | my %RANGE = (freq => [ 20, 20000 ], gain => [ -12, 12 ], q => [ 0.3, 8 ]); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 8736 | ▶ | sub new ($class, %arg) { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 8736 | ▶ | my $self = bless { rate => $arg{rate} // 48000, bands => {} }, $class; | ||||||||||||||||||||||||||
|
Condition50%1/2
$arg{"rate"} // 48000
MC/DC0%0/1
$arg{"rate"} // 48000
$arg{"rate"}
Truth table50%1/2
A
A= $arg{"rate"}
| |||||||||||||||||||||||||||||
| 33 | 8736 | for my $b (@BANDS) { | |||||||||||||||||||||||||||
| 34 | 34944 | my ($name, $type, $freq, $q) = @$b; | |||||||||||||||||||||||||||
| 35 | 34944 | my $band = $self->{bands}{$name} = { | |||||||||||||||||||||||||||
| 36 | type => $type, | ||||||||||||||||||||||||||||
| 37 | freq => $freq, | ||||||||||||||||||||||||||||
| 38 | gain => 0, | ||||||||||||||||||||||||||||
| 39 | q => $q, | ||||||||||||||||||||||||||||
| 40 | state => [ 0.0, 0.0, 0.0, 0.0 ], # the filter's, left and right | ||||||||||||||||||||||||||||
| 41 | }; | ||||||||||||||||||||||||||||
| 42 | 34944 | $self->_coefficients($band); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 8736 | ▶ | $self->set(%{ $arg{bands} // {} }); | ||||||||||||||||||||||||||
|
Condition100%2/2
$arg{"bands"} // {}
MC/DC100%1/1
$arg{"bands"} // {}
$arg{"bands"}
Truth table100%2/2
A
A= $arg{"bands"}
| |||||||||||||||||||||||||||||
| 45 | 8736 | return $self; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 290 | ▶ | sub names () { return map { $_->[0] } @BANDS } | ||||||||||||||||||||||||||
|
Subroutine
names: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub range ($what) { return @{ $RANGE{$what} } } | ||||||||||||||||||||||||||
|
Subroutine
range: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # set(low => { gain => 3 }, mid2 => { freq => 3000, q => 2 }, ...) | ||||||||||||||||||||||||||||
| 52 | 8819 | ▶ | sub set ($self, %bands) { | ||||||||||||||||||||||||||
|
Subroutine
set: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 8819 | for my $name (keys %bands) { | |||||||||||||||||||||||||||
| 54 | 40 | ▶ | my $band = $self->{bands}{$name} // die "the equalizer has no band '$name'\n"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"bands"}{$name} // die("the equalizer has no band '${name}'\n")
MC/DC0%0/1
$self->{"bands"}{$name} // die("the equalizer has no band '${name}'\n")
$self->{"bands"}{$name}
Truth table50%1/2
A
A= $self->{"bands"}{$name}
| |||||||||||||||||||||||||||||
| 55 | 40 | for my $what (keys %{ $bands{$name} }) { | |||||||||||||||||||||||||||
| 56 | 86 | ▶ | my $range = $RANGE{$what} // die "an equalizer band has no '$what'\n"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$RANGE{$what} // die("an equalizer band has no '${what}'\n")
MC/DC0%0/1
$RANGE{$what} // die("an equalizer band has no '${what}'\n")
$RANGE{$what}
Truth table50%1/2
A
A= $RANGE{$what}
| |||||||||||||||||||||||||||||
| 57 | 86 | my $v = 0 + $bands{$name}{$what}; | |||||||||||||||||||||||||||
| 58 | 86 | ▶ | $v = $range->[0] if $v < $range->[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 86 | ▶ | $v = $range->[1] if $v > $range->[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 86 | $band->{$what} = $v; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 40 | ▶ | $band->{state} = [ 0.0, 0.0, 0.0, 0.0 ] if $band->{gain} != 0 && !$band->{on}; # back from 0 dB: not what it held then | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$band->{"gain"} != 0 and not $band->{"on"}
MC/DC100%2/2
$band->{"gain"} != 0 and not $band->{"on"}
$band->{"gain"} != 0
not $band->{"on"}
Truth table100%3/3
A and not(B)
A= $band->{"gain"} != 0B= not $band->{"on"}
| |||||||||||||||||||||||||||||
| 63 | 40 | $band->{on} = $band->{gain} != 0; | |||||||||||||||||||||||||||
| 64 | 40 | $self->_coefficients($band); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 8819 | return $self; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # { band => { freq, gain, q } } | ||||||||||||||||||||||||||||
| 70 | 104 | ▶ | sub params ($self) { | ||||||||||||||||||||||||||
|
Subroutine
params: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 104 | my %params; | |||||||||||||||||||||||||||
| 72 | 104 | for my $name (names()) { | |||||||||||||||||||||||||||
| 73 | 416 | my $band = $self->{bands}{$name}; | |||||||||||||||||||||||||||
| 74 | 416 | $params{$name} = { freq => $band->{freq}, gain => $band->{gain}, q => $band->{q} }; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 104 | return \%params; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # Whether it changes anything. | ||||||||||||||||||||||||||||
| 80 | 15905 | ▶ | sub active ($self) { return scalar grep { $_->{gain} != 0 } values %{ $self->{bands} } } | ||||||||||||||||||||||||||
|
Subroutine
active: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 1013 | ▶ | sub clear ($self) { | ||||||||||||||||||||||||||
|
Subroutine
clear: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 1013 | $_->{state} = [ 0.0, 0.0, 0.0, 0.0 ] for values %{ $self->{bands} }; | |||||||||||||||||||||||||||
| 84 | 1013 | return; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # Whether the filters have rung out: what they hold is below -200 dB. | ||||||||||||||||||||||||||||
| 88 | 499 | ▶ | sub quiet ($self) { | ||||||||||||||||||||||||||
|
Subroutine
quiet: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | 499 | for my $band (values %{ $self->{bands} }) { | |||||||||||||||||||||||||||
| 90 | 1996 | ▶ | return 0 if grep { abs $_ > 1e-10 } @{ $band->{state} }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 499 | return 1; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # The cookbook's coefficients, normalized by a0: [b0, b1, b2, a1, a2]. | ||||||||||||||||||||||||||||
| 96 | 34984 | ▶ | sub _coefficients ($self, $band) { | ||||||||||||||||||||||||||
|
Subroutine
_coefficients: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 97 | 34984 | my $A = 10**($band->{gain} / 40); | |||||||||||||||||||||||||||
| 98 | 34984 | my $w0 = 2 * PI * $band->{freq} / $self->{rate}; | |||||||||||||||||||||||||||
| 99 | 34984 | ▶ | $w0 = PI * 0.99 if $w0 > PI * 0.99; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 34984 | my ($cos, $sin) = (cos $w0, sin $w0); | |||||||||||||||||||||||||||
| 101 | 34984 | my $alpha = $sin / (2 * $band->{q}); | |||||||||||||||||||||||||||
| 102 | 34984 | my $s = 2 * sqrt($A) * $alpha; # the shelves' | |||||||||||||||||||||||||||
| 103 | 34984 | my ($b0, $b1, $b2, $a0, $a1, $a2); | |||||||||||||||||||||||||||
| 104 | 34984 | ▶ | if ($band->{type} eq 'peak') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 105 | 17488 | $b0 = 1 + $alpha * $A; | |||||||||||||||||||||||||||
| 106 | 17488 | $b1 = -2 * $cos; | |||||||||||||||||||||||||||
| 107 | 17488 | $b2 = 1 - $alpha * $A; | |||||||||||||||||||||||||||
| 108 | 17488 | $a0 = 1 + $alpha / $A; | |||||||||||||||||||||||||||
| 109 | 17488 | $a1 = -2 * $cos; | |||||||||||||||||||||||||||
| 110 | 17488 | $a2 = 1 - $alpha / $A; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | elsif ($band->{type} eq 'lowshelf') { | ||||||||||||||||||||||||||||
| 113 | 8751 | $b0 = $A * (($A + 1) - ($A - 1) * $cos + $s); | |||||||||||||||||||||||||||
| 114 | 8751 | $b1 = 2 * $A * (($A - 1) - ($A + 1) * $cos); | |||||||||||||||||||||||||||
| 115 | 8751 | $b2 = $A * (($A + 1) - ($A - 1) * $cos - $s); | |||||||||||||||||||||||||||
| 116 | 8751 | $a0 = ($A + 1) + ($A - 1) * $cos + $s; | |||||||||||||||||||||||||||
| 117 | 8751 | $a1 = -2 * (($A - 1) + ($A + 1) * $cos); | |||||||||||||||||||||||||||
| 118 | 8751 | $a2 = ($A + 1) + ($A - 1) * $cos - $s; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | else { | ||||||||||||||||||||||||||||
| 121 | 8745 | $b0 = $A * (($A + 1) + ($A - 1) * $cos + $s); | |||||||||||||||||||||||||||
| 122 | 8745 | $b1 = -2 * $A * (($A - 1) + ($A + 1) * $cos); | |||||||||||||||||||||||||||
| 123 | 8745 | $b2 = $A * (($A + 1) + ($A - 1) * $cos - $s); | |||||||||||||||||||||||||||
| 124 | 8745 | $a0 = ($A + 1) - ($A - 1) * $cos + $s; | |||||||||||||||||||||||||||
| 125 | 8745 | $a1 = 2 * (($A - 1) - ($A + 1) * $cos); | |||||||||||||||||||||||||||
| 126 | 8745 | $a2 = ($A + 1) - ($A - 1) * $cos - $s; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 34984 | $band->{coef} = [ $b0 / $a0, $b1 / $a0, $b2 / $a0, $a1 / $a0, $a2 / $a0 ]; | |||||||||||||||||||||||||||
| 129 | 34984 | return; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # The response in dB at a frequency, all bands together: for drawing it. | ||||||||||||||||||||||||||||
| 133 | 188 | ▶ | sub response ($self, $freq) { | ||||||||||||||||||||||||||
|
Subroutine
response: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | 188 | my $w = 2 * PI * $freq / $self->{rate}; | |||||||||||||||||||||||||||
| 135 | 188 | my $db = 0; | |||||||||||||||||||||||||||
| 136 | 188 | for my $band (values %{ $self->{bands} }) { | |||||||||||||||||||||||||||
| 137 | 752 | ▶ | next if $band->{gain} == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 161 | my ($b0, $b1, $b2, $a1, $a2) = @{ $band->{coef} }; | |||||||||||||||||||||||||||
| 139 | 161 | my ($c1, $s1, $c2, $s2) = (cos $w, sin $w, cos(2 * $w), sin(2 * $w)); | |||||||||||||||||||||||||||
| 140 | 161 | my ($nr, $ni) = ($b0 + $b1 * $c1 + $b2 * $c2, -$b1 * $s1 - $b2 * $s2); | |||||||||||||||||||||||||||
| 141 | 161 | my ($dr, $di) = (1 + $a1 * $c1 + $a2 * $c2, -$a1 * $s1 - $a2 * $s2); | |||||||||||||||||||||||||||
| 142 | 161 | $db += 10 * log(($nr * $nr + $ni * $ni) / ($dr * $dr + $di * $di)) / log(10); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 188 | return $db; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | # Both channels through the bands that are not at 0 dB: fresh arrays. | ||||||||||||||||||||||||||||
| 148 | 184 | ▶ | sub process ($self, $left, $right) { | ||||||||||||||||||||||||||
|
Subroutine
process: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 149 | 184 | my @l = @$left; | |||||||||||||||||||||||||||
| 150 | 184 | my @r = @$right; | |||||||||||||||||||||||||||
| 151 | 184 | for my $name (names()) { | |||||||||||||||||||||||||||
| 152 | 736 | my $band = $self->{bands}{$name}; | |||||||||||||||||||||||||||
| 153 | 736 | ▶ | next if $band->{gain} == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 187 | my ($b0, $b1, $b2, $a1, $a2) = @{ $band->{coef} }; | |||||||||||||||||||||||||||
| 155 | 187 | my ($l1, $l2, $r1, $r2) = @{ $band->{state} }; # transposed direct form II | |||||||||||||||||||||||||||
| 156 | 187 | for my $i (0 .. $#l) { | |||||||||||||||||||||||||||
| 157 | 238208 | my $x = $l[$i]; | |||||||||||||||||||||||||||
| 158 | 238208 | my $y = $b0 * $x + $l1; | |||||||||||||||||||||||||||
| 159 | 238208 | $l1 = $b1 * $x - $a1 * $y + $l2; | |||||||||||||||||||||||||||
| 160 | 238208 | $l2 = $b2 * $x - $a2 * $y; | |||||||||||||||||||||||||||
| 161 | 238208 | $l[$i] = $y; | |||||||||||||||||||||||||||
| 162 | 238208 | $x = $r[$i]; | |||||||||||||||||||||||||||
| 163 | 238208 | $y = $b0 * $x + $r1; | |||||||||||||||||||||||||||
| 164 | 238208 | $r1 = $b1 * $x - $a1 * $y + $r2; | |||||||||||||||||||||||||||
| 165 | 238208 | $r2 = $b2 * $x - $a2 * $y; | |||||||||||||||||||||||||||
| 166 | 238208 | $r[$i] = $y; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | 187 | $band->{state} = [ $l1, $l2, $r1, $r2 ]; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | 184 | return (\@l, \@r); | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 16 | 1; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | __END__ | ||||||||||||||||||||||||||||