| 1 | package Peta::Vorbis::Encode::Psy; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: how coarsely each MDCT bin may be quantized | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | # How much noise each MDCT bin of a block can take unheard, as a | ||||||||||||||||||||||||||||
| 5 | # quantization step: the step s whose noise (s^2 / 12) is what the bin may | ||||||||||||||||||||||||||||
| 6 | # take. The model of Peta::MP3::Encode::Psy, on Vorbis' bins: | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # bands the bins grouped by Bark, one band per Bark | ||||||||||||||||||||||||||||
| 9 | # spreading every band's energy masks its neighbours (Schroeder) | ||||||||||||||||||||||||||||
| 10 | # offset the noise may come that far below the spread energy: for a | ||||||||||||||||||||||||||||
| 11 | # tone masking noise 14.5 dB plus the Bark, at most 25 dB; for | ||||||||||||||||||||||||||||
| 12 | # noise masking noise 8 dB; between them by the band's | ||||||||||||||||||||||||||||
| 13 | # tonality; plus 2 dB per step of quality above 5 (less below) | ||||||||||||||||||||||||||||
| 14 | # tonality by the band's spectral flatness (the geometric mean of its | ||||||||||||||||||||||||||||
| 15 | # bins' energies against their mean): noise is at about -5 dB, | ||||||||||||||||||||||||||||
| 16 | # a band holding a tone at -15 dB or less. Flatter than -6 dB | ||||||||||||||||||||||||||||
| 17 | # is noise, below -16 dB a tone, linear between. A band of | ||||||||||||||||||||||||||||
| 18 | # fewer than eight bins cannot tell (a tone in four bins often | ||||||||||||||||||||||||||||
| 19 | # looks as flat as noise), and counts as a tone. | ||||||||||||||||||||||||||||
| 20 | # quiet never below the threshold of hearing in quiet (Terhardt) | ||||||||||||||||||||||||||||
| 21 | # | ||||||||||||||||||||||||||||
| 22 | # The level of a full-scale sine in this transform's units is measured by | ||||||||||||||||||||||||||||
| 23 | # the encoder, not assumed, and taken to sound at 92 dB SPL. | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use Peta::Hearing; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1 | our $VERSION = '0.2610180'; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | use constant FULL_SCALE_SPL => 92; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 1 | ▶ | use constant NOISE => Peta::Hearing::NOISE_OFFSET; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # $bins bins of a block at $rate; $full the energy of a full-scale sine in them. | ||||||||||||||||||||||||||||
| 34 | 4 | ▶ | sub new ($class, $rate, $bins, $quality, $full) { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 4 | my $width = $rate / 2 / $bins; | |||||||||||||||||||||||||||
| 36 | 4 | my @band = map { int Peta::Hearing::bark(($_ + 0.5) * $width) } 0 .. $bins - 1; | |||||||||||||||||||||||||||
| 37 | 4 | my $count = $band[-1] + 1; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # Each band's bins, lo .. hi - 1. A band no bin falls in (none at these | ||||||||||||||||||||||||||||
| 40 | # rates) is kept, empty. | ||||||||||||||||||||||||||||
| 41 | 4 | my (@lo, @hi); | |||||||||||||||||||||||||||
| 42 | 4 | for my $i (0 .. $bins - 1) { | |||||||||||||||||||||||||||
| 43 | 2304 | ▶ | $lo[ $band[$i] ] //= $i; | ||||||||||||||||||||||||||
|
Condition66%2/3
$lo[$band[$i]] //= $i
MC/DC0%0/2
$lo[$band[$i]] //= $i
$lo[$band[$i]]
$i
Truth table66%2/3
A //= B
A= $lo[$band[$i]]B= $i
| |||||||||||||||||||||||||||||
| 44 | 2304 | $hi[ $band[$i] ] = $i + 1; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 4 | for my $b (0 .. $count - 1) { | |||||||||||||||||||||||||||
| 47 | 100 | ▶ | $lo[$b] //= $hi[$b - 1] // 0; | ||||||||||||||||||||||||||
|
Condition75%3/4
$lo[$b] //= $hi[$b - 1] // 0
$hi[$b - 1] // 0
MC/DC100%1/1
$lo[$b] //= $hi[$b - 1] // 0
$lo[$b]
Truth table100%2/2
A
A= $lo[$b]
| |||||||||||||||||||||||||||||
| 48 | 100 | ▶ | $hi[$b] //= $lo[$b]; | ||||||||||||||||||||||||||
|
Condition66%2/3
$hi[$b] //= $lo[$b]
MC/DC0%0/2
$hi[$b] //= $lo[$b]
$hi[$b]
$lo[$b]
Truth table66%2/3
A //= B
A= $hi[$b]B= $lo[$b]
| |||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # The quietest audible across each band, as an energy in the bins; a | ||||||||||||||||||||||||||||
| 52 | # tone's masking offset at its middle; the spreading from every band to | ||||||||||||||||||||||||||||
| 53 | # every other, SPREAD[count to + from]. | ||||||||||||||||||||||||||||
| 54 | 4 | my (@quiet, @tone); | |||||||||||||||||||||||||||
| 55 | 4 | for my $b (0 .. $count - 1) { | |||||||||||||||||||||||||||
| 56 | 100 | my $least = 1e9; | |||||||||||||||||||||||||||
| 57 | 100 | for my $i ($lo[$b] .. $hi[$b] - 1) { | |||||||||||||||||||||||||||
| 58 | 2304 | my $db = Peta::Hearing::quiet(($i + 0.5) * $width); | |||||||||||||||||||||||||||
| 59 | 2304 | ▶ | $least = $db if $db < $least; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 100 | push @quiet, $full * 10**(($least - FULL_SCALE_SPL) / 10); | |||||||||||||||||||||||||||
| 62 | 100 | push @tone, Peta::Hearing::tone_offset($b + 0.5); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 4 | my @spread; | |||||||||||||||||||||||||||
| 65 | 4 | for my $to (0 .. $count - 1) { | |||||||||||||||||||||||||||
| 66 | 100 | push @spread, map { Peta::Hearing::spreading($to - $_) } 0 .. $count - 1; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 4 | return bless { | |||||||||||||||||||||||||||
| 70 | bins => $bins, | ||||||||||||||||||||||||||||
| 71 | count => $count, | ||||||||||||||||||||||||||||
| 72 | lo => \@lo, | ||||||||||||||||||||||||||||
| 73 | hi => \@hi, | ||||||||||||||||||||||||||||
| 74 | quiet => \@quiet, | ||||||||||||||||||||||||||||
| 75 | tone => \@tone, | ||||||||||||||||||||||||||||
| 76 | spread => \@spread, | ||||||||||||||||||||||||||||
| 77 | adjust => 2 * ($quality - 5), # dB more or less below the masking | ||||||||||||||||||||||||||||
| 78 | }, $class; | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # The step each bin may be quantized with: an array reference. | ||||||||||||||||||||||||||||
| 82 | 128 | ▶ | sub steps ($self, $x) { | ||||||||||||||||||||||||||
|
Subroutine
steps: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 128 | my $count = $self->{count}; | |||||||||||||||||||||||||||
| 84 | 128 | my $adjust = $self->{adjust}; | |||||||||||||||||||||||||||
| 85 | 128 | my @lo = @{ $self->{lo} }; | |||||||||||||||||||||||||||
| 86 | 128 | my @hi = @{ $self->{hi} }; | |||||||||||||||||||||||||||
| 87 | 128 | my @tone = @{ $self->{tone} }; | |||||||||||||||||||||||||||
| 88 | 128 | my @energy = (0.0) x $count; | |||||||||||||||||||||||||||
| 89 | 128 | my @offset = (0.0) x $count; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 128 | for my $b (0 .. $count - 1) { | |||||||||||||||||||||||||||
| 92 | 3200 | my ($sum, $logs) = (0.0, 0.0); | |||||||||||||||||||||||||||
| 93 | 3200 | my $k = $hi[$b] - $lo[$b]; | |||||||||||||||||||||||||||
| 94 | 3200 | for my $i ($lo[$b] .. $hi[$b] - 1) { | |||||||||||||||||||||||||||
| 95 | 127488 | my $e = $x->[$i] * $x->[$i]; | |||||||||||||||||||||||||||
| 96 | 127488 | $sum += $e; | |||||||||||||||||||||||||||
| 97 | 127488 | ▶ | $logs += log($e + 1e-30) if $k >= 8; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 3200 | $energy[$b] = $sum; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # How tonal the band is, 0 (noise) to 1, from its flatness in dB. | ||||||||||||||||||||||||||||
| 102 | 3200 | my $tonal = 1.0; | |||||||||||||||||||||||||||
| 103 | 3200 | ▶ | if ($k >= 8 && $sum > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$k >= 8 and $sum > 0
MC/DC50%1/2
$k >= 8 and $sum > 0
$k >= 8
$sum > 0
Truth table66%2/3
A and B
A= $k >= 8B= $sum > 0
| |||||||||||||||||||||||||||||
| 104 | 2000 | my $flat = 4.342944819032518 * ($logs / $k - log($sum / $k)); | |||||||||||||||||||||||||||
| 105 | 2000 | $tonal = (-6 - $flat) / 10; | |||||||||||||||||||||||||||
| 106 | 2000 | ▶ | $tonal = 0.0 if $tonal < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 2000 | ▶ | $tonal = 1.0 if $tonal > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 3200 | my $db = $tonal * $tone[$b] + (1 - $tonal) * NOISE + $adjust; | |||||||||||||||||||||||||||
| 111 | 3200 | ▶ | $db = 0 if $db < 0; # never noise louder than what masks it | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 3200 | $offset[$b] = 10**(-$db / 10); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # Each band masked by the spread energy of all, never below quiet; its | ||||||||||||||||||||||||||||
| 116 | # step shared by its bins. | ||||||||||||||||||||||||||||
| 117 | 128 | my @spread = @{ $self->{spread} }; | |||||||||||||||||||||||||||
| 118 | 128 | my @quiet = @{ $self->{quiet} }; | |||||||||||||||||||||||||||
| 119 | 128 | my @step = (0.0) x $self->{bins}; | |||||||||||||||||||||||||||
| 120 | 128 | for my $b (0 .. $count - 1) { | |||||||||||||||||||||||||||
| 121 | 3200 | my $masked = 0.0; | |||||||||||||||||||||||||||
| 122 | 3200 | my $row = $b * $count; | |||||||||||||||||||||||||||
| 123 | 3200 | for my $from (0 .. $count - 1) { $masked += $energy[$from] * $spread[ $row + $from ] } | |||||||||||||||||||||||||||
| 124 | 3200 | $masked *= $offset[$b]; | |||||||||||||||||||||||||||
| 125 | 3200 | ▶ | $masked = $quiet[$b] if $masked < $quiet[$b]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 3200 | ▶ | my $width = $hi[$b] - $lo[$b] || 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
$hi[$b] - $lo[$b] || 1
MC/DC100%1/1
$hi[$b] - $lo[$b] || 1
$hi[$b] - $lo[$b]
Truth table100%2/2
A
A= $hi[$b] - $lo[$b]
| |||||||||||||||||||||||||||||
| 128 | 3200 | my $s = sqrt(12 * $masked / $width); | |||||||||||||||||||||||||||
| 129 | 3200 | $step[$_] = $s for $lo[$b] .. $hi[$b] - 1; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 128 | return \@step; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 1 | 1; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | __END__ | ||||||||||||||||||||||||||||