| 1 | package Peta::Synth::Output; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: sound to WAV, FLAC, MP3, Ogg Vorbis and Opus files | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | # Where rendered sound goes: a writer takes it in pieces (left and right as | ||||||||||||||||||||||||||||
| 5 | # arrays of floats in -1 .. 1) and is closed at the end. | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # wav written here: 16 bits (dithered), 24 bits, or 32-bit floats | ||||||||||||||||||||||||||||
| 8 | # flac encoded here (Peta::Synth::Output::FLAC): 16 or 24 bits | ||||||||||||||||||||||||||||
| 9 | # mp3 encoded here (Peta::MP3::Encode): constant or variable bitrate | ||||||||||||||||||||||||||||
| 10 | # ogg encoded here (Peta::Vorbis::Encode): Ogg Vorbis, by quality | ||||||||||||||||||||||||||||
| 11 | # opus encoded by ffmpeg fed WAV through a pipe, until there is an | ||||||||||||||||||||||||||||
| 12 | # encoder here | ||||||||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||||||||
| 14 | # Every writer has write(\@left, \@right) and close. The class methods open | ||||||||||||||||||||||||||||
| 15 | # one for a file by its extension. | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 9 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 9 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 9 | our $VERSION = '0.2610180'; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # The format of each file name extension, and the class that writes it. | ||||||||||||||||||||||||||||
| 23 | 9 | my %FORMAT_OF = (wav => 'wav', flac => 'flac', mp3 => 'mp3', ogg => 'ogg', oga => 'ogg', opus => 'opus'); | |||||||||||||||||||||||||||
| 24 | 9 | my %WRITER = ( | |||||||||||||||||||||||||||
| 25 | wav => 'Peta::Synth::Output::WAV', | ||||||||||||||||||||||||||||
| 26 | flac => 'Peta::Synth::Output::FLAC', | ||||||||||||||||||||||||||||
| 27 | mp3 => 'Peta::Synth::Output::MP3', | ||||||||||||||||||||||||||||
| 28 | ogg => 'Peta::Synth::Output::Vorbis', | ||||||||||||||||||||||||||||
| 29 | opus => 'Peta::Synth::Output::Pipe', | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # The format a file's name says. | ||||||||||||||||||||||||||||
| 33 | 22 | ▶ | sub format_of ($file) { | ||||||||||||||||||||||||||
|
Subroutine
format_of: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 22 | my ($ext) = $file =~ /\.(\w+)\z/; | |||||||||||||||||||||||||||
| 35 | 22 | ▶ | my $format = $FORMAT_OF{ lc($ext // '') }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$ext // ""
MC/DC0%0/1
$ext // ""
$ext
Truth table50%1/2
A
A= $ext
| |||||||||||||||||||||||||||||
| 36 | 22 | ▶ | return $format if $format; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | my $have = join ' ', sort keys %FORMAT_OF; | |||||||||||||||||||||||||||
| 39 | 1 | die "'$file': cannot tell the format from the name (have: $have)\n"; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # Where a program is on the PATH, if it is. | ||||||||||||||||||||||||||||
| 43 | 7 | ▶ | sub which ($program) { | ||||||||||||||||||||||||||
|
Subroutine
which: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 7 | for my $dir (File::Spec->path) { | |||||||||||||||||||||||||||
| 45 | 70 | my $path = File::Spec->catfile($dir, $program); | |||||||||||||||||||||||||||
| 46 | 70 | ▶ | return $path if -f $path && -x _; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-f $path and -x _
MC/DC0%0/2
-f $path and -x _
-f $path
-x _
Truth table33%1/3
A and B
A= -f $pathB= -x _
| |||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | 7 | return; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # The formats this machine can write, and whether each is written here. | ||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub formats () { | ||||||||||||||||||||||||||
|
Subroutine
formats: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 0 | my $ffmpeg = which('ffmpeg'); | |||||||||||||||||||||||||||
| 54 | 0 | my %have = (wav => 'native', flac => 'native', mp3 => 'native', ogg => 'native'); | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | $have{opus} = 'external' if $ffmpeg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | return \%have; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # open($file, rate => 48000, bits => 16, quality => ...): a writer for the | ||||||||||||||||||||||||||||
| 60 | # format the file's name says. | ||||||||||||||||||||||||||||
| 61 | 20 | ▶ | sub open ($class, $file, %opt) { | ||||||||||||||||||||||||||
|
Subroutine
open: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 20 | my $format = format_of($file); | |||||||||||||||||||||||||||
| 63 | 20 | ▶ | require Peta::Synth::Output::FLAC if $format eq 'flac'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 20 | return $WRITER{$format}->new($file, format => $format, %opt); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # Convenience: a whole sound at once. | ||||||||||||||||||||||||||||
| 68 | 15 | ▶ | sub write_file ($class, $file, $left, $right, %opt) { | ||||||||||||||||||||||||||
|
Subroutine
write_file: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 15 | my $out = $class->open($file, %opt); | |||||||||||||||||||||||||||
| 70 | 15 | my $step = 65536; | |||||||||||||||||||||||||||
| 71 | 15 | ▶ | for (my $at = 0; $at < @$left; $at += $step) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 15 | my $end = $at + $step - 1; | |||||||||||||||||||||||||||
| 73 | 15 | ▶ | $end = $#$left if $end > $#$left; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 15 | $out->write([ @$left[ $at .. $end ] ], [ @$right[ $at .. $end ] ]); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 15 | return $out->close; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # The two channels' samples, interleaved: left, right, left, ... | ||||||||||||||||||||||||||||
| 80 | 279 | ▶ | sub interleave ($left, $right) { | ||||||||||||||||||||||||||
|
Subroutine
interleave: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 279 | my $n = @$left; | |||||||||||||||||||||||||||
| 82 | 279 | my @inter = (0) x (2 * $n); | |||||||||||||||||||||||||||
| 83 | 279 | for my $i (0 .. $n - 1) { | |||||||||||||||||||||||||||
| 84 | 401416 | $inter[2 * $i] = $left->[$i]; | |||||||||||||||||||||||||||
| 85 | 401416 | $inter[2 * $i + 1] = $right->[$i]; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 279 | return \@inter; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # Integer samples from floats, interleaved: clipped, and with triangular | ||||||||||||||||||||||||||||
| 91 | # dither of one step (from a fixed stream: the same file every time) when | ||||||||||||||||||||||||||||
| 92 | # there are 16 bits. The state of the dither is kept in $state. | ||||||||||||||||||||||||||||
| 93 | 9 | ▶ | sub to_integers ($left, $right, $bits, $state) { | ||||||||||||||||||||||||||
|
Subroutine
to_integers: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | 9 | my $n = scalar @$left; | |||||||||||||||||||||||||||
| 95 | 9 | my @out = (0) x (2 * $n); | |||||||||||||||||||||||||||
| 96 | 9 | ▶ | my $scale = $bits == 16 ? 32767.0 : 8388607.0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 9 | my $top = $scale; | |||||||||||||||||||||||||||
| 98 | 9 | my $low = -$scale - 1.0; | |||||||||||||||||||||||||||
| 99 | 9 | ▶ | my $dither = $bits == 16 ? 1.0 : 0.0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 9 | my $s = $$state; | |||||||||||||||||||||||||||
| 101 | 9 | for my $i (0 .. $n - 1) { | |||||||||||||||||||||||||||
| 102 | 62002 | $s = ($s * 1103515245 + 12345) % 2147483648; | |||||||||||||||||||||||||||
| 103 | 62002 | my $r = $s / 2147483648.0; | |||||||||||||||||||||||||||
| 104 | 62002 | $s = ($s * 1103515245 + 12345) % 2147483648; | |||||||||||||||||||||||||||
| 105 | 62002 | $r -= $s / 2147483648.0; | |||||||||||||||||||||||||||
| 106 | 62002 | my $v = $left->[$i] * $scale + $dither * $r; | |||||||||||||||||||||||||||
| 107 | 62002 | ▶ | $v = $top if $v > $top; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 62002 | ▶ | $v = $low if $v < $low; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 62002 | ▶ | $out[2 * $i] = int($v + ($v < 0.0 ? -0.5 : 0.5)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 62002 | $v = $right->[$i] * $scale + $dither * $r * -1.0; | |||||||||||||||||||||||||||
| 112 | 62002 | ▶ | $v = $top if $v > $top; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 62002 | ▶ | $v = $low if $v < $low; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 62002 | ▶ | $out[2 * $i + 1] = int($v + ($v < 0.0 ? -0.5 : 0.5)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 9 | $$state = $s; | |||||||||||||||||||||||||||
| 117 | 9 | return \@out; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | package Peta::Synth::Output::WAV; | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # RIFF WAVE: the header is written with the sizes unknown and set at close, | ||||||||||||||||||||||||||||
| 123 | # or, when the output cannot seek (a pipe), with the largest sizes, which | ||||||||||||||||||||||||||||
| 124 | # readers take as "until the end". | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 9 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # new($file, rate => 48000, bits => 16, fh => ...): with fh, onto that handle. | ||||||||||||||||||||||||||||
| 129 | 10 | ▶ | sub new ($class, $file, %opt) { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.4
| |||||||||||||||||||||||||||||
| 130 | 10 | ▶ | my $bits = $opt{bits} // 16; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opt{"bits"} // 16
MC/DC100%1/1
$opt{"bits"} // 16
$opt{"bits"}
Truth table100%2/2
A
A= $opt{"bits"}
| |||||||||||||||||||||||||||||
| 131 | 10 | ▶ | die "WAV is written with 16, 24 or 32 (float) bits, not $bits\n" if $bits !~ /\A(?:16|24|32)\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 10 | my $fh = $opt{fh}; | |||||||||||||||||||||||||||
| 134 | 10 | ▶ | if (!$fh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 10 | ▶ | open $fh, '>:raw', $file or die "cannot write '$file': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 10 | binmode $fh; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | my $self = bless { | ||||||||||||||||||||||||||||
| 140 | file => $file, | ||||||||||||||||||||||||||||
| 141 | fh => $fh, | ||||||||||||||||||||||||||||
| 142 | 10 | ▶ | rate => $opt{rate} // 48000, | ||||||||||||||||||||||||||
|
Condition50%1/2
$opt{"rate"} // 48000
MC/DC0%0/1
$opt{"rate"} // 48000
$opt{"rate"}
Truth table50%1/2
A
A= $opt{"rate"}
| |||||||||||||||||||||||||||||
| 143 | bits => $bits, | ||||||||||||||||||||||||||||
| 144 | frames => 0, | ||||||||||||||||||||||||||||
| 145 | dither => 1, # Peta::Synth::Output::to_integers' state | ||||||||||||||||||||||||||||
| 146 | }, $class; | ||||||||||||||||||||||||||||
| 147 | 10 | print {$fh} $self->_header(0xffffffff - 36); | |||||||||||||||||||||||||||
| 148 | 10 | return $self; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | # The header, for $bytes of samples. | ||||||||||||||||||||||||||||
| 152 | 20 | ▶ | sub _header ($self, $bytes) { | ||||||||||||||||||||||||||
|
Subroutine
_header: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 153 | 20 | my $bits = $self->{bits}; | |||||||||||||||||||||||||||
| 154 | 20 | my $block = 2 * $bits / 8; # bytes per frame | |||||||||||||||||||||||||||
| 155 | 20 | ▶ | my $type = $bits == 32 ? 3 : 1; # IEEE float, or PCM | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | return join '', | ||||||||||||||||||||||||||||
| 158 | pack('a4 V a4', 'RIFF', 36 + $bytes, 'WAVE'), | ||||||||||||||||||||||||||||
| 159 | 20 | pack('a4 V v v V V v v', 'fmt ', 16, $type, 2, $self->{rate}, $self->{rate} * $block, $block, $bits), | |||||||||||||||||||||||||||
| 160 | pack('a4 V', 'data', $bytes); | ||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 10 | ▶ | sub write ($self, $left, $right) { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 164 | 10 | my $bits = $self->{bits}; | |||||||||||||||||||||||||||
| 165 | 10 | my $data; | |||||||||||||||||||||||||||
| 166 | 10 | ▶ | if ($bits == 32) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 6 | $data = pack 'f<*', @{ Peta::Synth::Output::interleave($left, $right) }; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | else { | ||||||||||||||||||||||||||||
| 170 | 4 | my $samples = Peta::Synth::Output::to_integers($left, $right, $bits, \$self->{dither}); | |||||||||||||||||||||||||||
| 171 | 4 | ▶ | $data = $bits == 16 ? pack('s<*', @$samples) : join '', map { substr pack('l<', $_), 0, 3 } @$samples; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 10 | print { $self->{fh} } $data; | |||||||||||||||||||||||||||
| 174 | 10 | $self->{frames} += @$left; | |||||||||||||||||||||||||||
| 175 | 10 | return $self; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 10 | ▶ | sub close ($self) { | ||||||||||||||||||||||||||
|
Subroutine
close: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 179 | 10 | my $fh = $self->{fh}; | |||||||||||||||||||||||||||
| 180 | 10 | my $bytes = $self->{frames} * 2 * $self->{bits} / 8; | |||||||||||||||||||||||||||
| 181 | 10 | ▶ | if (seek $fh, 0, 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 10 | print {$fh} $self->_header($bytes); | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | 10 | ▶ | close $fh or die "cannot write '$self->{file}': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 10 | return $self->{file}; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | package Peta::Synth::Output::Pipe; | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | # A format encoded by an external program, fed WAV through a pipe. | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 9 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 0 | ▶ | sub _command ($format, $file, $quality) { | ||||||||||||||||||||||||||
|
Subroutine
_command: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 195 | 0 | my $ffmpeg = Peta::Synth::Output::which('ffmpeg'); | |||||||||||||||||||||||||||
| 196 | 0 | my @ff = ($ffmpeg, qw(-hide_banner -loglevel error -y -f wav -i pipe:0)); | |||||||||||||||||||||||||||
| 197 | 0 | ▶ | if ($format eq 'opus') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 0 | ▶ | return [ @ff, qw(-codec:a libopus -b:a), ($quality // 160) . 'k', $file ] if $ffmpeg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$quality // 160
MC/DC0%0/1
$quality // 160
$quality
Truth table0%0/2
A
A= $quality
| |||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | 0 | die "cannot write $format here: it is encoded by ffmpeg, and it is not on the PATH\n"; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 0 | ▶ | sub new ($class, $file, %opt) { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 204 | 0 | my $command = _command($opt{format}, $file, $opt{quality}); | |||||||||||||||||||||||||||
| 205 | 0 | ▶ | open my $fh, '|-', @$command or die "cannot run $command->[0]: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | 0 | binmode $fh; | |||||||||||||||||||||||||||
| 207 | 0 | my $wav = Peta::Synth::Output::WAV->new(undef, %opt, bits => 16, fh => $fh); | |||||||||||||||||||||||||||
| 208 | 0 | return bless { file => $file, wav => $wav, fh => $fh, program => $command->[0] }, $class; | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 0 | ▶ | sub write ($self, $left, $right) { | ||||||||||||||||||||||||||
|
Subroutine
write: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 212 | 0 | $self->{wav}->write($left, $right); | |||||||||||||||||||||||||||
| 213 | 0 | return $self; | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 0 | ▶ | sub close ($self) { | ||||||||||||||||||||||||||
|
Subroutine
close: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 217 | 0 | ▶ | close $self->{fh} or die "$self->{program} could not write '$self->{file}' (exit " . ($? >> 8) . ")\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 0 | return $self->{file}; | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | package Peta::Synth::Output::MP3; | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | # Layer III (MPEG-1, or MPEG-2 at 16-24 kHz), encoded here: the quality in | ||||||||||||||||||||||||||||
| 224 | # kbit/s (a constant bitrate), or V0 .. V9 (a variable one, as LAME's -V). | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 9 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 3 | ▶ | sub new ($class, $file, %opt) { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 229 | 3 | require Peta::MP3::Encode; | |||||||||||||||||||||||||||
| 230 | 3 | ▶ | my $rate = $opt{rate} // 48000; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opt{"rate"} // 48000
MC/DC0%0/1
$opt{"rate"} // 48000
$opt{"rate"}
Truth table50%1/2
A
A= $opt{"rate"}
| |||||||||||||||||||||||||||||
| 231 | 3 | ▶ | die "MP3 is written at 32000, 44100 or 48000 Hz (MPEG-1), 16000, 22050 or 24000 Hz (MPEG-2), not $rate\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | if $rate !~ /\A(?:32000|44100|48000|16000|22050|24000)\z/; | ||||||||||||||||||||||||||||
| 233 | 3 | my $quality = $opt{quality}; | |||||||||||||||||||||||||||
| 234 | 3 | my %bitrate; | |||||||||||||||||||||||||||
| 235 | 3 | ▶ | if (!defined $quality) { %bitrate = () } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 236 | 1 | elsif ($quality =~ /\A[Vv]([0-9])\z/) { %bitrate = (vbr => $1) } | |||||||||||||||||||||||||||
| 237 | 1 | else { %bitrate = (bitrate => $quality) } | |||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 3 | my $encoder = Peta::MP3::Encode->new(rate => $rate, channels => 2, %bitrate, ffmpeg_gapless => $opt{ffmpeg_gapless}); | |||||||||||||||||||||||||||
| 240 | 3 | ▶ | open my $fh, '>:raw', $file or die "cannot write '$file': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | 3 | print {$fh} $encoder->info_frame; # its place; written again at the end | |||||||||||||||||||||||||||
| 242 | 3 | return bless { file => $file, fh => $fh, encoder => $encoder }, $class; | |||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 3 | ▶ | sub write ($self, $left, $right) { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 246 | 3 | print { $self->{fh} } $self->{encoder}->encode($left, $right); | |||||||||||||||||||||||||||
| 247 | 3 | return $self; | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 3 | ▶ | sub close ($self) { | ||||||||||||||||||||||||||
|
Subroutine
close: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 251 | 3 | my $fh = $self->{fh}; | |||||||||||||||||||||||||||
| 252 | 3 | print {$fh} $self->{encoder}->finish; | |||||||||||||||||||||||||||
| 253 | 3 | ▶ | print {$fh} $self->{encoder}->info_frame if seek $fh, 0, 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | 3 | ▶ | close $fh or die "cannot write '$self->{file}': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 255 | 3 | return $self->{file}; | |||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | package Peta::Synth::Output::Vorbis; | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | # Ogg Vorbis, encoded here: variable bitrate, the quality 0 to 10. | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | 9 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 1 | ▶ | sub new ($class, $file, %opt) { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.9
| |||||||||||||||||||||||||||||
| 265 | 1 | require Peta::Vorbis::Encode; | |||||||||||||||||||||||||||
| 266 | 1 | ▶ | my $encoder = Peta::Vorbis::Encode->new(rate => $opt{rate} // 48000, channels => 2, quality => $opt{quality} // 6); | ||||||||||||||||||||||||||
|
Condition50%2/4
$opt{"rate"} // 48000
$opt{"quality"} // 6
MC/DC0%0/1
$opt{"rate"} // 48000
$opt{"rate"}
MC/DC0%0/1
$opt{"quality"} // 6
$opt{"quality"}
Truth table50%1/2
A
A= $opt{"rate"}
Truth table50%1/2
A
A= $opt{"quality"}
| |||||||||||||||||||||||||||||
| 267 | 1 | ▶ | open my $fh, '>:raw', $file or die "cannot write '$file': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | 1 | return bless { file => $file, fh => $fh, encoder => $encoder }, $class; | |||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 1 | ▶ | sub write ($self, $left, $right) { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 272 | 1 | print { $self->{fh} } $self->{encoder}->encode($left, $right); | |||||||||||||||||||||||||||
| 273 | 1 | return $self; | |||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 1 | ▶ | sub close ($self) { | ||||||||||||||||||||||||||
|
Subroutine
close: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 277 | 1 | my $fh = $self->{fh}; | |||||||||||||||||||||||||||
| 278 | 1 | print {$fh} $self->{encoder}->finish; | |||||||||||||||||||||||||||
| 279 | 1 | ▶ | close $fh or die "cannot write '$self->{file}': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 280 | 1 | return $self->{file}; | |||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | package Peta::Synth::Output; | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 9 | 1; | |||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | __END__ | ||||||||||||||||||||||||||||