| 1 | package Peta::MP3::Encode::Psy; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: how much noise a granule's bands can take | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | # How much noise each scalefactor band of a granule can take unheard: the | ||||||||||||||||||||||||||||
| 5 | # psychoacoustic model, a simple one. From the granule's own frequency | ||||||||||||||||||||||||||||
| 6 | # lines (no separate FFT): | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # energy per band (and, in a short block, per window) | ||||||||||||||||||||||||||||
| 9 | # spreading every band's energy masks its neighbours, by Schroeder's | ||||||||||||||||||||||||||||
| 10 | # spreading function over the Bark scale | ||||||||||||||||||||||||||||
| 11 | # offset the noise may come that far below the spread energy: for a | ||||||||||||||||||||||||||||
| 12 | # tone masking noise 14.5 dB plus the Bark, at most 25 dB; for | ||||||||||||||||||||||||||||
| 13 | # noise masking noise 8 dB (NOISE); between them by the | ||||||||||||||||||||||||||||
| 14 | # band's tonality | ||||||||||||||||||||||||||||
| 15 | # tonality by the band's spectral flatness (the geometric mean of its | ||||||||||||||||||||||||||||
| 16 | # lines' energies against their mean): noise is at about -5 | ||||||||||||||||||||||||||||
| 17 | # dB, a band holding a tone at -13 dB or less. Flatter than -6 | ||||||||||||||||||||||||||||
| 18 | # dB is noise, below -16 dB a tone, linear between. A band of | ||||||||||||||||||||||||||||
| 19 | # fewer than eight lines cannot tell, and counts as a tone. | ||||||||||||||||||||||||||||
| 20 | # quiet never below the threshold of hearing in quiet (Terhardt) | ||||||||||||||||||||||||||||
| 21 | # | ||||||||||||||||||||||||||||
| 22 | # In a short block each window is masked by its own energy only: the quiet | ||||||||||||||||||||||||||||
| 23 | # windows before an attack keep their noise low, which is what short | ||||||||||||||||||||||||||||
| 24 | # blocks are for. | ||||||||||||||||||||||||||||
| 25 | # | ||||||||||||||||||||||||||||
| 26 | # Also here: whether a granule has an attack, for choosing short blocks. | ||||||||||||||||||||||||||||
| 27 | # | ||||||||||||||||||||||||||||
| 28 | # A sine of full scale has an energy of about 1 in a long granule's lines, | ||||||||||||||||||||||||||||
| 29 | # and is taken to sound at 92 dB SPL. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | use List::Util qw(max); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 1 | ▶ | use Peta::Hearing; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 1 | ▶ | use Peta::MP3::Encode::Tables; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | our $VERSION = '0.2610180'; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | ▶ | use constant FULL_SCALE_SPL => 92; # dB SPL of a full-scale sine | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 1 | ▶ | use constant ATTACK => 10; # a segment this many times louder than those before it | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 1 | ▶ | use constant AUDIBLE => 1e-6; # and at least this loud (energy per sample) | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 1 | ▶ | use constant NOISE => Peta::Hearing::NOISE_OFFSET; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 1 | ▶ | use constant SHORT => 2; # the block type of short blocks | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 15 | ▶ | sub new ($class, $rate) { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 15 | ▶ | my $bands = $Peta::MP3::Encode::Tables::BANDS{$rate} // die "no scalefactor bands for $rate Hz\n"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$Peta::MP3::Encode::Tables::BANDS{$rate} // die("no scalefactor bands for $rate Hz\n")
MC/DC0%0/1
$Peta::MP3::Encode::Tables::BANDS{$rate} // die("no scalefactor bands for $rate Hz\n")
$Peta::MP3::Encode::Tables::BANDS{$rate}
Truth table50%1/2
A
A= $Peta::MP3::Encode::Tables::BANDS{$rate}
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Long blocks have 576 lines; short ones 192 a window, each three times | ||||||||||||||||||||||||||||
| 49 | # as wide, where a sine's line has a third of the energy. | ||||||||||||||||||||||||||||
| 50 | return bless { | ||||||||||||||||||||||||||||
| 51 | long => _bands($bands->{long}, $rate / 2 / 576, 1), | ||||||||||||||||||||||||||||
| 52 | 15 | short => _bands($bands->{short}, $rate / 2 / 192, 1 / 3), | |||||||||||||||||||||||||||
| 53 | energies => [ (0.0) x 8 ], # the last eight segments', for attacks | ||||||||||||||||||||||||||||
| 54 | tail => [], # each channel's last sample | ||||||||||||||||||||||||||||
| 55 | }, $class; | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # What the model needs of a set of scalefactor bands: their edges (in | ||||||||||||||||||||||||||||
| 59 | # lines $width Hz wide), the threshold in quiet as an energy in the lines, | ||||||||||||||||||||||||||||
| 60 | # the masking offset of a tone, and the spreading from every band to every | ||||||||||||||||||||||||||||
| 61 | # other. | ||||||||||||||||||||||||||||
| 62 | 30 | ▶ | sub _bands ($edges, $width, $scale) { | ||||||||||||||||||||||||||
|
Subroutine
_bands: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 63 | 30 | my (@bark, @quiet, @tone); | |||||||||||||||||||||||||||
| 64 | 30 | for my $b (0 .. $#$edges - 1) { | |||||||||||||||||||||||||||
| 65 | 525 | my $lo = $edges->[$b] * $width; | |||||||||||||||||||||||||||
| 66 | 525 | my $hi = $edges->[$b + 1] * $width; | |||||||||||||||||||||||||||
| 67 | 525 | push @bark, Peta::Hearing::bark(($lo + $hi) / 2); | |||||||||||||||||||||||||||
| 68 | 525 | push @tone, Peta::Hearing::tone_offset($bark[-1]); | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # The quietest audible across the band. | ||||||||||||||||||||||||||||
| 71 | 525 | my $least = 1e9; | |||||||||||||||||||||||||||
| 72 | 525 | ▶ | for (my $f = $lo + $width / 2; $f < $hi; $f += $width) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 11520 | my $db = Peta::Hearing::quiet($f); | |||||||||||||||||||||||||||
| 74 | 11520 | ▶ | $least = $db if $db < $least; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 525 | push @quiet, $scale * 10**(($least - FULL_SCALE_SPL) / 10); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # The spreading from every band to every other: SPREAD[count to + from]. | ||||||||||||||||||||||||||||
| 80 | 30 | my @spread; | |||||||||||||||||||||||||||
| 81 | 30 | for my $to (0 .. $#bark) { | |||||||||||||||||||||||||||
| 82 | 525 | push @spread, map { Peta::Hearing::spreading($bark[$to] - $_) } @bark; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | return { | ||||||||||||||||||||||||||||
| 86 | 30 | edges => $edges, | |||||||||||||||||||||||||||
| 87 | count => scalar @bark, | ||||||||||||||||||||||||||||
| 88 | quiet => \@quiet, | ||||||||||||||||||||||||||||
| 89 | tone => \@tone, | ||||||||||||||||||||||||||||
| 90 | spread => \@spread, | ||||||||||||||||||||||||||||
| 91 | }; | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 15297 | ▶ | sub _clamp ($x, $low, $high) { | ||||||||||||||||||||||||||
|
Subroutine
_clamp: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 95 | 15297 | ▶ | return $low if $x < $low; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 12334 | ▶ | return $high if $x > $high; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 2104 | return $x; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # The noise each band may take: an array reference, one per long band; in a | ||||||||||||||||||||||||||||
| 101 | # short block one per band and window, 3 b + w. | ||||||||||||||||||||||||||||
| 102 | 1220 | ▶ | sub allowed ($self, $xr, $type) { | ||||||||||||||||||||||||||
|
Subroutine
allowed: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 1220 | ▶ | return _masked($self->{long}, $xr, 0, 1) if $type != SHORT; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 37 | my @allowed; | |||||||||||||||||||||||||||
| 106 | 37 | for my $w (0 .. 2) { | |||||||||||||||||||||||||||
| 107 | 111 | my $one = _masked($self->{short}, $xr, $w, 3); | |||||||||||||||||||||||||||
| 108 | 111 | $allowed[ 3 * $_ + $w ] = $one->[$_] for 0 .. $#$one; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 37 | return \@allowed; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # The noise allowed in each band of one window: $windows 1 for a long | ||||||||||||||||||||||||||||
| 114 | # block; for a short one 3, its windows' lines side by side, window $w of | ||||||||||||||||||||||||||||
| 115 | # band b being lines 3 edge[b] + w width onwards. | ||||||||||||||||||||||||||||
| 116 | 1294 | ▶ | sub _masked ($set, $xr, $w, $windows) { | ||||||||||||||||||||||||||
|
Subroutine
_masked: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 117 | 1294 | my @edges = @{ $set->{edges} }; | |||||||||||||||||||||||||||
| 118 | 1294 | my $count = $set->{count}; | |||||||||||||||||||||||||||
| 119 | 1294 | my @spread = @{ $set->{spread} }; | |||||||||||||||||||||||||||
| 120 | 1294 | my @tone = @{ $set->{tone} }; | |||||||||||||||||||||||||||
| 121 | 1294 | my @energy = (0.0) x $count; | |||||||||||||||||||||||||||
| 122 | 1294 | my @offset = (0.0) x $count; | |||||||||||||||||||||||||||
| 123 | 1294 | for my $b (0 .. $count - 1) { | |||||||||||||||||||||||||||
| 124 | 27469 | my $width = $edges[$b + 1] - $edges[$b]; | |||||||||||||||||||||||||||
| 125 | 27469 | my $lo = $windows * $edges[$b] + $w * $width; | |||||||||||||||||||||||||||
| 126 | 27469 | my ($sum, $logs) = (0.0, 0.0); | |||||||||||||||||||||||||||
| 127 | 27469 | for my $i ($lo .. $lo + $width - 1) { | |||||||||||||||||||||||||||
| 128 | 702720 | my $e = $xr->[$i] * $xr->[$i]; | |||||||||||||||||||||||||||
| 129 | 702720 | $sum += $e; | |||||||||||||||||||||||||||
| 130 | 702720 | ▶ | $logs += log($e + 1e-30) if $width >= 8; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 27469 | $energy[$b] = $sum; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # How tonal the band is, 0 (noise) to 1, from its flatness in dB. | ||||||||||||||||||||||||||||
| 135 | # A band too narrow to tell counts as a tone. | ||||||||||||||||||||||||||||
| 136 | 27469 | my $tonal = 1.0; | |||||||||||||||||||||||||||
| 137 | 27469 | ▶ | if ($width >= 8 && $sum > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$width >= 8 and $sum > 0
MC/DC100%2/2
$width >= 8 and $sum > 0
$width >= 8
$sum > 0
Truth table100%3/3
A and B
A= $width >= 8B= $sum > 0
| |||||||||||||||||||||||||||||
| 138 | 15297 | my $flat = 4.342944819032518 * ($logs / $width - log($sum / $width)); | |||||||||||||||||||||||||||
| 139 | 15297 | $tonal = _clamp((-6 - $flat) / 10, 0.0, 1.0); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 27469 | $offset[$b] = 10**(-($tonal * $tone[$b] + (1 - $tonal) * NOISE) / 10); | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # Each band masked by the spread energy of all, never below quiet. | ||||||||||||||||||||||||||||
| 145 | 1294 | my @quiet = @{ $set->{quiet} }; | |||||||||||||||||||||||||||
| 146 | 1294 | my @allowed = (0.0) x $count; | |||||||||||||||||||||||||||
| 147 | 1294 | for my $to (0 .. $count - 1) { | |||||||||||||||||||||||||||
| 148 | 27469 | my $masked = 0.0; | |||||||||||||||||||||||||||
| 149 | 27469 | my $row = $to * $count; | |||||||||||||||||||||||||||
| 150 | 27469 | for my $from (0 .. $count - 1) { $masked += $energy[$from] * $spread[ $row + $from ] } | |||||||||||||||||||||||||||
| 151 | 27469 | $masked *= $offset[$to]; | |||||||||||||||||||||||||||
| 152 | 27469 | ▶ | $allowed[$to] = $masked > $quiet[$to] ? $masked : $quiet[$to]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | 1294 | return \@allowed; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | # Whether a granule's samples (one array reference per channel) have an | ||||||||||||||||||||||||||||
| 158 | # attack: in segments of 64 samples, the energy of the changes from sample | ||||||||||||||||||||||||||||
| 159 | # to sample (what is high in pitch, where pre-echo is heard), against the | ||||||||||||||||||||||||||||
| 160 | # loudest of the eight segments before. Called once per granule, in order. | ||||||||||||||||||||||||||||
| 161 | # | ||||||||||||||||||||||||||||
| 162 | # Two answers: an attack early in the granule (its first 192 samples), and | ||||||||||||||||||||||||||||
| 163 | # one later. The filterbank delays the sound by about 240 samples, and the | ||||||||||||||||||||||||||||
| 164 | # short windows of the MDCT of granule k cover the samples from 624 before | ||||||||||||||||||||||||||||
| 165 | # its start to 144 after it: an early attack is short granule k's own, a | ||||||||||||||||||||||||||||
| 166 | # later one the next granule's. | ||||||||||||||||||||||||||||
| 167 | 705 | ▶ | sub attack ($self, @channels) { | ||||||||||||||||||||||||||
|
Subroutine
attack: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||
| 168 | 705 | my @seen = @{ $self->{energies} }; | |||||||||||||||||||||||||||
| 169 | 705 | my $tail = $self->{tail}; | |||||||||||||||||||||||||||
| 170 | 705 | my ($early, $late) = (0, 0); | |||||||||||||||||||||||||||
| 171 | 705 | for my $s (0 .. 8) { | |||||||||||||||||||||||||||
| 172 | 6345 | my $energy = 0.0; | |||||||||||||||||||||||||||
| 173 | 6345 | for my $c (0 .. $#channels) { | |||||||||||||||||||||||||||
| 174 | 11187 | my $samples = $channels[$c]; | |||||||||||||||||||||||||||
| 175 | 11187 | ▶ | my $previous = $s == 0 ? ($tail->[$c] // 0.0) : $samples->[ 64 * $s - 1 ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$tail->[$c] // 0
MC/DC100%1/1
$tail->[$c] // 0
$tail->[$c]
Truth table100%2/2
A
A= $tail->[$c]
| |||||||||||||||||||||||||||||
| 176 | 11187 | for my $i (64 * $s .. 64 * $s + 63) { | |||||||||||||||||||||||||||
| 177 | 715968 | my $d = $samples->[$i] - $previous; | |||||||||||||||||||||||||||
| 178 | 715968 | $energy += $d * $d; | |||||||||||||||||||||||||||
| 179 | 715968 | $previous = $samples->[$i]; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | 6345 | ▶ | if ($energy > ATTACK * max(@seen) && $energy > AUDIBLE * 64) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$energy > 10 * &max(@seen) and $energy > "6.4e-05"
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"
| |||||||||||||||||||||||||||||
| 183 | 28 | ▶ | if ($s < 3) { $early = 1 } else { $late = 1 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | 6345 | shift @seen; | |||||||||||||||||||||||||||
| 186 | 6345 | push @seen, $energy; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | 705 | $self->{energies} = \@seen; | |||||||||||||||||||||||||||
| 189 | 705 | $tail->[$_] = $channels[$_][-1] for 0 .. $#channels; | |||||||||||||||||||||||||||
| 190 | 705 | return ($early, $late); | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 1 | 1; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | __END__ | ||||||||||||||||||||||||||||