| 1 | package Peta::Vorbis::Encode::MDCT::PDL; | ||||||||||||
| 2 | # ABSTRACT: Vorbis' MDCT with PDL | ||||||||||||
| 3 | |||||||||||||
| 4 | # The MDCT of Peta::Vorbis::Encode::MDCT in PDL: the same steps, as | ||||||||||||
| 5 | # whole-array operations on all channels of a block at once. | ||||||||||||
| 6 | # | ||||||||||||
| 7 | # fold the windowed block's quarters, reversed where they must be, | ||||||||||||
| 8 | # added into the DCT-IV's n/2 inputs | ||||||||||||
| 9 | # rotate those as n/4 complex values, times the pre-rotation | ||||||||||||
| 10 | # FFT PDL::FFT's, of n/4 points | ||||||||||||
| 11 | # rotate times the post-rotation, even outputs forward, odd ones back | ||||||||||||
| 12 | |||||||||||||
| 13 | 1 | ▶ | use v5.36; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | # PDL exports dozens of functions into a package; they go to one of their | ||||||||||||
| 16 | # own, and this one calls PDL by name. | ||||||||||||
| 17 | { | ||||||||||||
| 18 | package Peta::Vorbis::Encode::MDCT::PDL::Imports; | ||||||||||||
| 19 | 1 | ▶ | use PDL::LiteF; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | 1 | ▶ | use PDL::FFT (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | } | ||||||||||||
| 22 | |||||||||||||
| 23 | 1 | ▶ | use Peta::Vorbis::Encode::MDCT; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 24 | |||||||||||||
| 25 | 1 | our $VERSION = '0.2610180'; | |||||||||||
| 26 | |||||||||||||
| 27 | 5 | ▶ | sub new ($class, $n, $scale = 1.0) { | ||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 28 | 5 | my @pre = Peta::Vorbis::Encode::MDCT::pre_angles($n); | |||||||||||
| 29 | 5 | my @post = Peta::Vorbis::Encode::MDCT::post_angles($n); | |||||||||||
| 30 | 5 | ▶ | my $pdl = sub (@list) { PDL->pdl(PDL::double(), \@list) }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 31 | |||||||||||||
| 32 | return bless { | ||||||||||||
| 33 | n => $n, | ||||||||||||
| 34 | scale => $scale, | ||||||||||||
| 35 | 1168 | pre_c => $pdl->(map { cos $_ } @pre), | |||||||||||
| 36 | 1168 | pre_s => $pdl->(map { sin $_ } @pre), | |||||||||||
| 37 | 1168 | post_c => $pdl->(map { cos($_) * $scale } @post), | |||||||||||
| 38 | 5 | post_s => $pdl->(map { sin($_) * $scale } @post), | |||||||||||
| 39 | windows => {}, # each window's piddle, by its array | ||||||||||||
| 40 | }, $class; | ||||||||||||
| 41 | } | ||||||||||||
| 42 | |||||||||||||
| 43 | # The coefficients of a block of n samples, windowed with $w. | ||||||||||||
| 44 | 4 | ▶ | sub transform ($self, $x, $w) { return ($self->transform_all([$x], $w))[0] } | ||||||||||
|
Subroutine
transform: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 45 | |||||||||||||
| 46 | # The coefficients of each channel's block of n samples, all windowed with | ||||||||||||
| 47 | # $w: one array reference per channel. | ||||||||||||
| 48 | 69 | ▶ | sub transform_all ($self, $channels, $w) { | ||||||||||
|
Subroutine
transform_all: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 49 | 69 | my $n = $self->{n}; | |||||||||||
| 50 | 69 | my $m = $n / 2; | |||||||||||
| 51 | 69 | my $h = $m / 2; | |||||||||||
| 52 | # Windows kept by the array they came from (held, so its address is not | ||||||||||||
| 53 | # another's later). | ||||||||||||
| 54 | 69 | ▶ | my $window = ($self->{windows}{$w} //= [ $w, PDL->pdl(PDL::double(), $w) ])->[1]; | ||||||||||
|
Condition100%2/2
$self->{"windows"}{$w} //= [$w, "PDL"->pdl(PDL::double(), $w)]
MC/DC100%1/1
$self->{"windows"}{$w} //= [$w, "PDL"->pdl(PDL::double(), $w)]
$self->{"windows"}{$w}
Truth table100%2/2
A
A= $self->{"windows"}{$w}
| |||||||||||||
| 55 | 69 | my $y = PDL->pdl(PDL::double(), $channels) * $window; # (n, channels) | |||||||||||
| 56 | |||||||||||||
| 57 | 69 | my $v = PDL->zeroes(PDL::double(), $m, scalar @$channels); | |||||||||||
| 58 | 69 | $v->slice('0:' . ($h - 1)) .= -$y->slice((3 * $h - 1) . ':' . (2 * $h)) - $y->slice((3 * $h) . ':' . ($n - 1)); | |||||||||||
| 59 | 69 | $v->slice("$h:" . ($m - 1)) .= $y->slice('0:' . ($h - 1)) - $y->slice((2 * $h - 1) . ":$h"); | |||||||||||
| 60 | |||||||||||||
| 61 | 69 | my $a = $v->slice('0:' . ($m - 2) . ':2'); | |||||||||||
| 62 | 69 | my $b = $v->slice(($m - 1) . ':1:-2'); | |||||||||||
| 63 | 69 | my $re = $a * $self->{pre_c} - $b * $self->{pre_s}; | |||||||||||
| 64 | 69 | my $im = $a * $self->{pre_s} + $b * $self->{pre_c}; | |||||||||||
| 65 | 69 | PDL::FFT::fft($re, $im); | |||||||||||
| 66 | |||||||||||||
| 67 | 69 | my $out = PDL->zeroes(PDL::double(), $m, scalar @$channels); | |||||||||||
| 68 | 69 | $out->slice('0:' . ($m - 2) . ':2') .= $re * $self->{post_c} - $im * $self->{post_s}; | |||||||||||
| 69 | 69 | $out->slice(($m - 1) . ':1:-2') .= -($re * $self->{post_s} + $im * $self->{post_c}); | |||||||||||
| 70 | 69 | my @all = $out->list; | |||||||||||
| 71 | 69 | return map { [ @all[ $_ * $m .. ($_ + 1) * $m - 1 ] ] } 0 .. $#$channels; | |||||||||||
| 72 | } | ||||||||||||
| 73 | |||||||||||||
| 74 | 1 | 1; | |||||||||||
| 75 | |||||||||||||
| 76 | __END__ | ||||||||||||