| 1 | package Peta::Synth; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: a software synthesizer in Perl | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | # A software synthesizer in Perl: MIDI files to sound, every instrument | ||||||||||||||||||||||||||||
| 5 | # synthesized (no samples), on whatever this perl and this machine have: | ||||||||||||||||||||||||||||
| 6 | # plain Perl, pperl's JIT, PDL, a graphics card through pperl's WebGPU. | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # This module is the front: rendering a file offline, with the voices | ||||||||||||||||||||||||||||
| 9 | # shared among processes when asked; playing it as it renders; playing | ||||||||||||||||||||||||||||
| 10 | # MIDI as it comes in. The parts: | ||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||
| 12 | # Peta::Synth::MIDI Standard MIDI Files, single messages (::Stream: a device's bytes) | ||||||||||||||||||||||||||||
| 13 | # Peta::Synth::Input MIDI as it is played: the ALSA sequencer, raw devices | ||||||||||||||||||||||||||||
| 14 | # Peta::Synth::Synth the running synthesizer: channels, MIDI's messages, voices, the mix | ||||||||||||||||||||||||||||
| 15 | # Peta::Synth::Patch the fields of a patch, the General MIDI instruments, the drum kit | ||||||||||||||||||||||||||||
| 16 | # Peta::Synth::Voice the voice model: partials, string, FM, noise, filter, modulation | ||||||||||||||||||||||||||||
| 17 | # Peta::Synth::Engine the engines that render voices | ||||||||||||||||||||||||||||
| 18 | # Peta::Synth::Reverb the reverb; Peta::Synth::EQ the equalizers | ||||||||||||||||||||||||||||
| 19 | # Peta::Synth::Output WAV, FLAC, MP3 and Ogg Vorbis written here, Opus through ffmpeg; | ||||||||||||||||||||||||||||
| 20 | # the sound server (Output::PipeWire, Output::Pulse) | ||||||||||||||||||||||||||||
| 21 | # | ||||||||||||||||||||||||||||
| 22 | # What each does is told in doc/: midi.md, synthesis.md, rendering.md, backends.md. | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 1 | ▶ | use File::Temp qw(tempfile); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use List::Util qw(max sum0); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use POSIX (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use Time::HiRes (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | use Peta::Synth::MIDI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | use Peta::Synth::Synth; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 1 | ▶ | use Peta::Synth::Engine; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | use Peta::Synth::Output; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | our $VERSION = '0.2610180'; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | use constant QUANTUM => 1024; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | my %DEFAULT = ( | |||||||||||||||||||||||||||
| 39 | rate => 48000, | ||||||||||||||||||||||||||||
| 40 | partials => 48, | ||||||||||||||||||||||||||||
| 41 | fidelity => 'high', | ||||||||||||||||||||||||||||
| 42 | engine => 'auto', | ||||||||||||||||||||||||||||
| 43 | workers => 1, | ||||||||||||||||||||||||||||
| 44 | bits => 16, | ||||||||||||||||||||||||||||
| 45 | normalize => -1.0, | ||||||||||||||||||||||||||||
| 46 | tail => 30, | ||||||||||||||||||||||||||||
| 47 | quality => undef, | ||||||||||||||||||||||||||||
| 48 | ffmpeg_gapless => 0, | ||||||||||||||||||||||||||||
| 49 | waves => undef, | ||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # Renders a MIDI file into a sound file. | ||||||||||||||||||||||||||||
| 53 | # | ||||||||||||||||||||||||||||
| 54 | # rate sample rate (48000) | ||||||||||||||||||||||||||||
| 55 | # partials the most partials a voice has (48): the main measure of quality and cost | ||||||||||||||||||||||||||||
| 56 | # fidelity low, mid or high (high): what may be left out that is hardly heard | ||||||||||||||||||||||||||||
| 57 | # (Peta::Synth::Synth: voices end sooner as they fade, fewer at once) | ||||||||||||||||||||||||||||
| 58 | # engine perl, pdl, gpu or auto | ||||||||||||||||||||||||||||
| 59 | # workers processes the voices are shared among (1) | ||||||||||||||||||||||||||||
| 60 | # bits 16, 24 or 32 (float, WAV only) | ||||||||||||||||||||||||||||
| 61 | # normalize the peak in dBFS the result is scaled to (-1); undef: as rendered | ||||||||||||||||||||||||||||
| 62 | # tail the most seconds rendered after the last event, for the release and reverb (30) | ||||||||||||||||||||||||||||
| 63 | # length only the file's first seconds | ||||||||||||||||||||||||||||
| 64 | # bank changes to the patches, as Peta::Synth::Synth's edits() gives them | ||||||||||||||||||||||||||||
| 65 | # waves harmonic partials read from wave sequences (Peta::Synth::Wavetable): 1, 0, or | ||||||||||||||||||||||||||||
| 66 | # undef: as the engine fares better (Peta::Synth::Synth) | ||||||||||||||||||||||||||||
| 67 | # progress a sub called with (seconds rendered, seconds in all) | ||||||||||||||||||||||||||||
| 68 | # | ||||||||||||||||||||||||||||
| 69 | # Returns a hash of what was done: seconds of sound, seconds it took, notes, | ||||||||||||||||||||||||||||
| 70 | # peak voices, the engine. | ||||||||||||||||||||||||||||
| 71 | 4 | ▶ | sub render_file ($class, $in, $out, %opt) { | ||||||||||||||||||||||||||
|
Subroutine
render_file: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 4 | my %o = (%DEFAULT, %opt); | |||||||||||||||||||||||||||
| 73 | 4 | my ($timeline, $length) = _timeline($in, $o{length}); | |||||||||||||||||||||||||||
| 74 | 4 | my $started = Time::HiRes::time(); | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # Measured once, here, not by every worker. | ||||||||||||||||||||||||||||
| 77 | 4 | ▶ | $o{engine} = Peta::Synth::Engine::choose(rate => $o{rate}, partials => $o{partials}) if $o{engine} eq 'auto'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 4 | ▶ | my $workers = $o{workers} > 1 ? $o{workers} : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | my @parts = $workers == 1 | ||||||||||||||||||||||||||||
| 81 | 4 | ▶ | ? _render_part($timeline, $length, \%o, undef, $o{progress}, _temp_file('part')) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | : _render_parts($timeline, $length, \%o, $workers); | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 4 | my $result = _mix_down(\@parts, $out, \%o); | |||||||||||||||||||||||||||
| 85 | 4 | unlink $_->{file} for @parts; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 4 | $result->{took} = Time::HiRes::time() - $started; | |||||||||||||||||||||||||||
| 88 | 4 | $result->{notes} = sum0 map { $_->{notes} } @parts; | |||||||||||||||||||||||||||
| 89 | 4 | $result->{peak_voices} = sum0 map { $_->{peak_voices} } @parts; | |||||||||||||||||||||||||||
| 90 | 4 | $result->{engine} = $parts[0]{engine}; | |||||||||||||||||||||||||||
| 91 | 4 | $result->{workers} = $workers; | |||||||||||||||||||||||||||
| 92 | 4 | return $result; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # What rendering a MIDI file will cost, before it is rendered: its | ||||||||||||||||||||||||||||
| 96 | # Peta::Synth::Profile. rate, partials, bank, length, tail, waves as for render_file. | ||||||||||||||||||||||||||||
| 97 | 0 | ▶ | sub profile_file ($class, $in, %opt) { | ||||||||||||||||||||||||||
|
Subroutine
profile_file: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 0 | my %o = (%DEFAULT, %opt); | |||||||||||||||||||||||||||
| 99 | 0 | my ($timeline, $length) = _timeline($in, $o{length}); | |||||||||||||||||||||||||||
| 100 | 0 | require Peta::Synth::Profile; | |||||||||||||||||||||||||||
| 101 | return Peta::Synth::Profile->new( | ||||||||||||||||||||||||||||
| 102 | timeline => $timeline, | ||||||||||||||||||||||||||||
| 103 | length => $length, | ||||||||||||||||||||||||||||
| 104 | tail => $o{tail}, | ||||||||||||||||||||||||||||
| 105 | rate => $o{rate}, | ||||||||||||||||||||||||||||
| 106 | partials => $o{partials}, | ||||||||||||||||||||||||||||
| 107 | bank => $o{bank}, | ||||||||||||||||||||||||||||
| 108 | waves => $o{waves}, | ||||||||||||||||||||||||||||
| 109 | 0 | ); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # A MIDI file's events, as far as $length seconds if that is given (what | ||||||||||||||||||||||||||||
| 113 | # still sounds there is let go), and the time of the last. | ||||||||||||||||||||||||||||
| 114 | 4 | ▶ | sub _timeline ($in, $length) { | ||||||||||||||||||||||||||
|
Subroutine
_timeline: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 115 | 4 | ▶ | my $midi = ref $in ? $in : Peta::Synth::MIDI->read($in); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 4 | my $timeline = $midi->timeline; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 4 | ▶ | if (defined $length) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 1 | my @all_sound_off = map { { time => $length, type => 'controller', channel => $_, number => 123, value => 0 } } 0 .. 15; | |||||||||||||||||||||||||||
| 120 | 1 | $timeline = [ (grep { $_->{time} < $length } @$timeline), @all_sound_off ]; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | 4 | ▶ | return ($timeline, @$timeline ? $timeline->[-1]{time} : 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # Plays a MIDI file on the sound server as it renders. | ||||||||||||||||||||||||||||
| 126 | # | ||||||||||||||||||||||||||||
| 127 | # rate, partials, engine, bank, length, tail as for render_file | ||||||||||||||||||||||||||||
| 128 | # fidelity as for render_file, but mid by default | ||||||||||||||||||||||||||||
| 129 | # latency seconds the server holds ahead of what it plays (0.15) | ||||||||||||||||||||||||||||
| 130 | # output pipewire (pperl: libpipewire through Peta::FFI), pulse (the | ||||||||||||||||||||||||||||
| 131 | # PulseAudio protocol, which PipeWire serves too), or auto (pipewire | ||||||||||||||||||||||||||||
| 132 | # where it can be, pulse otherwise) | ||||||||||||||||||||||||||||
| 133 | # auto new notes get fewer partials when the machine cannot keep up (1) | ||||||||||||||||||||||||||||
| 134 | # prepare the wave sequences the file needs made before it plays (1), so | ||||||||||||||||||||||||||||
| 135 | # that no note waits for its; prepared, a sub called with (the | ||||||||||||||||||||||||||||
| 136 | # sequences, the seconds it took) once they are | ||||||||||||||||||||||||||||
| 137 | # stop a sub: when it says so, playing stops at once | ||||||||||||||||||||||||||||
| 138 | # progress a sub called with (seconds played, seconds in all, the synthesizer) | ||||||||||||||||||||||||||||
| 139 | # | ||||||||||||||||||||||||||||
| 140 | # The sound is not normalized (its peak is not known before it is played): | ||||||||||||||||||||||||||||
| 141 | # the synthesizer's limiter keeps it from clipping. | ||||||||||||||||||||||||||||
| 142 | 0 | ▶ | sub play_file ($class, $in, %opt) { | ||||||||||||||||||||||||||
|
Subroutine
play_file: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||||||||||
| 143 | 0 | my %o = (%DEFAULT, fidelity => 'mid', latency => 0.15, auto => 1, output => 'auto', prepare => 1, %opt); | |||||||||||||||||||||||||||
| 144 | 0 | my ($timeline, $length) = _timeline($in, $o{length}); | |||||||||||||||||||||||||||
| 145 | 0 | my $engine = Peta::Synth::Engine::create($o{engine}, rate => $o{rate}, partials => $o{partials}); | |||||||||||||||||||||||||||
| 146 | 0 | ▶ | my $waves = $o{waves} // ($engine->can('reads_waves') ? $engine->reads_waves : 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$o{"waves"} // ($engine->can("reads_waves") ? $engine->reads_waves : 1)
MC/DC0%0/2
$o{"waves"} // ($engine->can("reads_waves") ? $engine->reads_waves : 1)
$o{"waves"}
($engine->can("reads_waves") ? $engine->reads_waves : 1)
Truth table0%0/3
A // B
A= $o{"waves"}B= ($engine->can("reads_waves") ? $engine->reads_waves : 1)
| |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | $waves = _prepared_waves($timeline, $length, \%o) if $waves && $o{prepare}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$waves and $o{"prepare"}
MC/DC0%0/2
$waves and $o{"prepare"}
$waves
$o{"prepare"}
Truth table0%0/3
A and B
A= $wavesB= $o{"prepare"}
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | my $synth = Peta::Synth::Synth->new(rate => $o{rate}, partials => $o{partials}, fidelity => $o{fidelity}, engine => $engine, waves => $waves); | |||||||||||||||||||||||||||
| 150 | 0 | ▶ | $synth->apply_edits($o{bank}) if $o{bank}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 0 | $synth->schedule($timeline); | |||||||||||||||||||||||||||
| 152 | 0 | ▶ | my $title = ref $in ? 'Peta::Synth' : $in =~ s{.*/}{}r; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | my $out = player($o{output}, rate => $o{rate}, latency => $o{latency}, title => $title); | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | my $limit = int(($length + $o{tail}) * $o{rate}); | |||||||||||||||||||||||||||
| 156 | 0 | my $stopped = 0; | |||||||||||||||||||||||||||
| 157 | 0 | ▶ | while ($synth->busy && $synth->now < $limit) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$synth->busy and $synth->now < $limit
MC/DC0%0/2
$synth->busy and $synth->now < $limit
$synth->busy
$synth->now < $limit
Truth table0%0/3
A and B
A= $synth->busyB= $synth->now < $limit
| |||||||||||||||||||||||||||||
| 158 | 0 | ▶ | if ($o{stop} && $o{stop}->()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$o{"stop"} and $o{"stop"}->()
MC/DC0%0/2
$o{"stop"} and $o{"stop"}->()
$o{"stop"}
$o{"stop"}->()
Truth table0%0/3
A and B
A= $o{"stop"}B= $o{"stop"}->()
| |||||||||||||||||||||||||||||
| 159 | 0 | $stopped = 1; | |||||||||||||||||||||||||||
| 160 | 0 | last; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | 0 | _play_quantum($synth, $out, QUANTUM, \%o); | |||||||||||||||||||||||||||
| 163 | 0 | ▶ | $o{progress}->($synth->now / $o{rate}, $length, $synth) if $o{progress}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | ▶ | if ($stopped) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 0 | $out->stop; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | else { | ||||||||||||||||||||||||||||
| 170 | 0 | $out->close; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | 0 | my $stats = $synth->stats; | |||||||||||||||||||||||||||
| 173 | return { | ||||||||||||||||||||||||||||
| 174 | seconds => $synth->now / $o{rate}, | ||||||||||||||||||||||||||||
| 175 | notes => $stats->{notes}, | ||||||||||||||||||||||||||||
| 176 | peak_voices => $stats->{peak_voices}, | ||||||||||||||||||||||||||||
| 177 | engine => $engine->name, | ||||||||||||||||||||||||||||
| 178 | stopped => $stopped, | ||||||||||||||||||||||||||||
| 179 | partials => $synth->{partials}, | ||||||||||||||||||||||||||||
| 180 | 0 | output => ref($out) =~ s/.*:://r, | |||||||||||||||||||||||||||
| 181 | }; | ||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | # The wave sequences a file's notes read, made before it plays: every note | ||||||||||||||||||||||||||||
| 185 | # foreseen (by a Peta::Synth::Profile) makes its own. | ||||||||||||||||||||||||||||
| 186 | 0 | ▶ | sub _prepared_waves ($timeline, $length, $o) { | ||||||||||||||||||||||||||
|
Subroutine
_prepared_waves: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 187 | 0 | require Peta::Synth::Profile; | |||||||||||||||||||||||||||
| 188 | 0 | my $started = Time::HiRes::time(); | |||||||||||||||||||||||||||
| 189 | 0 | my $waves = Peta::Synth::Wavetable->new; | |||||||||||||||||||||||||||
| 190 | Peta::Synth::Profile->new( | ||||||||||||||||||||||||||||
| 191 | timeline => $timeline, | ||||||||||||||||||||||||||||
| 192 | length => $length, | ||||||||||||||||||||||||||||
| 193 | tail => $o->{tail}, | ||||||||||||||||||||||||||||
| 194 | rate => $o->{rate}, | ||||||||||||||||||||||||||||
| 195 | partials => $o->{partials}, | ||||||||||||||||||||||||||||
| 196 | bank => $o->{bank}, | ||||||||||||||||||||||||||||
| 197 | 0 | waves => $waves, | |||||||||||||||||||||||||||
| 198 | ); | ||||||||||||||||||||||||||||
| 199 | 0 | ▶ | $o->{prepared}->($waves->stats, Time::HiRes::time() - $started) if $o->{prepared}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 0 | return $waves; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | # Plays MIDI as it comes in (Peta::Synth::Input): from a keyboard, a | ||||||||||||||||||||||||||||
| 204 | # sequencer, a pipe, until stop says so. | ||||||||||||||||||||||||||||
| 205 | # | ||||||||||||||||||||||||||||
| 206 | # rate, partials, engine, bank as for render_file | ||||||||||||||||||||||||||||
| 207 | # fidelity as for render_file, but mid by default | ||||||||||||||||||||||||||||
| 208 | # in what to read: all (every source, and those plugged in later), | ||||||||||||||||||||||||||||
| 209 | # none (a port for others to connect), sources by address or | ||||||||||||||||||||||||||||
| 210 | # name, or a path (a raw MIDI device, a pipe); see Peta::Synth::Input | ||||||||||||||||||||||||||||
| 211 | # latency seconds the server holds ahead (0.03) | ||||||||||||||||||||||||||||
| 212 | # quantum samples rendered at a time (256): with the latency, how late a | ||||||||||||||||||||||||||||
| 213 | # key is heard | ||||||||||||||||||||||||||||
| 214 | # output as for play_file, or an object that writes (write, stop) | ||||||||||||||||||||||||||||
| 215 | # auto, stop as for play_file | ||||||||||||||||||||||||||||
| 216 | # progress a sub called with (the synthesizer, the input) after each quantum | ||||||||||||||||||||||||||||
| 217 | 0 | ▶ | sub play_live ($class, %opt) { | ||||||||||||||||||||||||||
|
Subroutine
play_live: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||
| 218 | 0 | my %o = (%DEFAULT, fidelity => 'mid', in => 'all', latency => 0.03, quantum => 256, auto => 1, output => 'auto', %opt); | |||||||||||||||||||||||||||
| 219 | 0 | ▶ | die "a quantum is a multiple of 64 samples\n" if $o{quantum} % 64 || $o{quantum} < 64; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$o{"quantum"} % 64 or $o{"quantum"} < 64
MC/DC0%0/2
$o{"quantum"} % 64 or $o{"quantum"} < 64
$o{"quantum"} % 64
$o{"quantum"} < 64
Truth table0%0/3
A or B
A= $o{"quantum"} % 64B= $o{"quantum"} < 64
| |||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 0 | require Peta::Synth::Input; | |||||||||||||||||||||||||||
| 222 | 0 | my $in = Peta::Synth::Input->open($o{in}); | |||||||||||||||||||||||||||
| 223 | 0 | my $engine = Peta::Synth::Engine::create($o{engine}, rate => $o{rate}, partials => $o{partials}); | |||||||||||||||||||||||||||
| 224 | 0 | my $synth = Peta::Synth::Synth->new(rate => $o{rate}, partials => $o{partials}, fidelity => $o{fidelity}, engine => $engine, waves => $o{waves}); | |||||||||||||||||||||||||||
| 225 | 0 | ▶ | $synth->apply_edits($o{bank}) if $o{bank}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | my $out = ref $o{output} | ||||||||||||||||||||||||||||
| 227 | ? $o{output} | ||||||||||||||||||||||||||||
| 228 | 0 | ▶ | : player($o{output}, rate => $o{rate}, latency => $o{latency}, title => 'Peta::Synth'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 0 | my $notes = 0; | |||||||||||||||||||||||||||
| 231 | 0 | ▶ | until ($o{stop} && $o{stop}->()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$o{"stop"} and $o{"stop"}->()
MC/DC0%0/2
$o{"stop"} and $o{"stop"}->()
$o{"stop"}
$o{"stop"}->()
Truth table0%0/3
A and B
A= $o{"stop"}B= $o{"stop"}->()
| |||||||||||||||||||||||||||||
| 232 | 0 | for my $event ($in->events) { | |||||||||||||||||||||||||||
| 233 | 0 | ▶ | $notes++ if $event->{type} eq 'note_on'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | 0 | $synth->play($event); | |||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | 0 | _play_quantum($synth, $out, $o{quantum}, \%o); | |||||||||||||||||||||||||||
| 237 | 0 | ▶ | $o{progress}->($synth, $in) if $o{progress}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 0 | $out->stop; | |||||||||||||||||||||||||||
| 241 | return { | ||||||||||||||||||||||||||||
| 242 | seconds => $synth->now / $o{rate}, | ||||||||||||||||||||||||||||
| 243 | notes => $notes, | ||||||||||||||||||||||||||||
| 244 | engine => $engine->name, | ||||||||||||||||||||||||||||
| 245 | partials => $synth->{partials}, | ||||||||||||||||||||||||||||
| 246 | 0 | output => ref($out) =~ s/.*:://r, | |||||||||||||||||||||||||||
| 247 | input => $in->address, | ||||||||||||||||||||||||||||
| 248 | }; | ||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | # One quantum rendered and written; the time it took told to the | ||||||||||||||||||||||||||||
| 252 | # synthesizer, which (with auto) gives new notes fewer partials if it falls behind. | ||||||||||||||||||||||||||||
| 253 | 0 | ▶ | sub _play_quantum ($synth, $out, $quantum, $o) { | ||||||||||||||||||||||||||
|
Subroutine
_play_quantum: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 254 | 0 | my $started = Time::HiRes::time(); | |||||||||||||||||||||||||||
| 255 | 0 | my ($left, $right) = $synth->render_arrays($quantum); | |||||||||||||||||||||||||||
| 256 | 0 | $synth->account(Time::HiRes::time() - $started, $quantum); | |||||||||||||||||||||||||||
| 257 | 0 | ▶ | $synth->adapt($o->{partials}) if $o->{auto}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | 0 | $out->write($left, $right); | |||||||||||||||||||||||||||
| 259 | 0 | return; | |||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | # A writer that plays: 'pipewire', 'pulse', or 'auto' (PipeWire where this | ||||||||||||||||||||||||||||
| 263 | # perl can call libpipewire, the PulseAudio protocol otherwise). | ||||||||||||||||||||||||||||
| 264 | 0 | ▶ | sub player ($name, %opt) { | ||||||||||||||||||||||||||
|
Subroutine
player: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 265 | 0 | ▶ | if ($name eq 'pipewire' || $name eq 'auto') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$name eq "pipewire" or $name eq "auto"
MC/DC0%0/2
$name eq "pipewire" or $name eq "auto"
$name eq "pipewire"
$name eq "auto"
Truth table0%0/3
A or B
A= $name eq "pipewire"B= $name eq "auto"
| |||||||||||||||||||||||||||||
| 266 | 0 | require Peta::Synth::Output::PipeWire; | |||||||||||||||||||||||||||
| 267 | 0 | ▶ | my $out = Peta::Synth::Output::PipeWire::available() && eval { Peta::Synth::Output::PipeWire->new(%opt) }; | ||||||||||||||||||||||||||
|
Condition0%0/3
Peta::Synth::Output::PipeWire::available() && eval {
do {
'Peta::Synth::Output::PipeWire'->new(%opt)
}
}
MC/DC0%0/2
Peta::Synth::Output::PipeWire::available() && eval {
do {
'Peta::Synth::Output::PipeWire'->new(%opt)
}
}
Peta::Synth::Output::PipeWire::available()
eval {
do {
'Peta::Synth::Output::PipeWire'->new(%opt)
}
}
Truth table0%0/3
A && B
A= Peta::Synth::Output::PipeWire::available()B= eval {
do {
'Peta::Synth::Output::PipeWire'->new(%opt)
}
}
| |||||||||||||||||||||||||||||
| 268 | 0 | ▶ | return $out if $out; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | 0 | ▶ | die $@ || "PipeWire cannot be played to from here\n" if $name eq 'pipewire'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$@ || "PipeWire cannot be played to from here\n"
MC/DC0%0/1
$@ || "PipeWire cannot be played to from here\n"
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | 0 | ▶ | die "no such output: $name (pipewire, pulse, auto)\n" if $name ne 'pulse' && $name ne 'auto'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$name ne "pulse" and $name ne "auto"
MC/DC0%0/2
$name ne "pulse" and $name ne "auto"
$name ne "pulse"
$name ne "auto"
Truth table0%0/3
A and B
A= $name ne "pulse"B= $name ne "auto"
| |||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 0 | require Peta::Synth::Output::Pulse; | |||||||||||||||||||||||||||
| 274 | 0 | return Peta::Synth::Output::Pulse->new(%opt); | |||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | # The voices shared among $workers processes, each rendering its part into a | ||||||||||||||||||||||||||||
| 278 | # file of its own; what each did is passed back in a file beside it. | ||||||||||||||||||||||||||||
| 279 | 1 | ▶ | sub _render_parts ($timeline, $length, $o, $workers) { | ||||||||||||||||||||||||||
|
Subroutine
_render_parts: called CC=9 SCAR=25.6
| |||||||||||||||||||||||||||||
| 280 | 1 | my @children; | |||||||||||||||||||||||||||
| 281 | 1 | for my $w (0 .. $workers - 1) { | |||||||||||||||||||||||||||
| 282 | 2 | my $file = _temp_file('part'); | |||||||||||||||||||||||||||
| 283 | 2 | ▶ | my $progress = $w == 0 ? $o->{progress} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 2 | ▶ | my $pid = fork // die "cannot fork: $!\n"; | ||||||||||||||||||||||||||
|
Condition50%1/2
fork // die("cannot fork: $!\n")
MC/DC0%0/1
fork // die("cannot fork: $!\n")
fork
Truth table50%1/2
A
A= fork
| |||||||||||||||||||||||||||||
| 286 | 2 | ▶ | if (!$pid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | 0 | my $done = eval { | |||||||||||||||||||||||||||
| 288 | 0 | _write_stats("$file.stats", _render_part($timeline, $length, $o, [ $w, $workers ], $progress, $file)); | |||||||||||||||||||||||||||
| 289 | 0 | 1; | |||||||||||||||||||||||||||
| 290 | }; | ||||||||||||||||||||||||||||
| 291 | 0 | ▶ | warn $@ unless $done; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | 0 | ▶ | POSIX::_exit($done ? 0 : 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | 2 | push @children, [ $pid, $file ]; | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 1 | my @parts; | |||||||||||||||||||||||||||
| 298 | 1 | for my $child (@children) { | |||||||||||||||||||||||||||
| 299 | 2 | my ($pid, $file) = @$child; | |||||||||||||||||||||||||||
| 300 | 2 | waitpid $pid, 0; | |||||||||||||||||||||||||||
| 301 | 2 | ▶ | die "a worker failed\n" if $?; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 302 | 2 | push @parts, { file => $file, %{ _read_stats("$file.stats") } }; | |||||||||||||||||||||||||||
| 303 | 2 | unlink "$file.stats"; | |||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | 1 | return @parts; | |||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | # A temporary file's name, the file made (and left to the caller to remove). | ||||||||||||||||||||||||||||
| 309 | 5 | ▶ | sub _temp_file ($what) { | ||||||||||||||||||||||||||
|
Subroutine
_temp_file: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 310 | 5 | my ($fh, $file) = tempfile("peta-synth-$what-XXXXXX", TMPDIR => 1, UNLINK => 0); | |||||||||||||||||||||||||||
| 311 | 5 | close $fh; | |||||||||||||||||||||||||||
| 312 | 5 | return $file; | |||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | # One process's share: the notes whose number modulo the workers is its own | ||||||||||||||||||||||||||||
| 316 | # (or all), rendered into $file as packed float frames (L R L R ...). | ||||||||||||||||||||||||||||
| 317 | 3 | ▶ | sub _render_part ($timeline, $length, $o, $share, $progress, $file) { | ||||||||||||||||||||||||||
|
Subroutine
_render_part: called CC=11 SCAR=24.4
| |||||||||||||||||||||||||||||
| 318 | 3 | my $engine = Peta::Synth::Engine::create($o->{engine}); | |||||||||||||||||||||||||||
| 319 | my $synth = Peta::Synth::Synth->new( | ||||||||||||||||||||||||||||
| 320 | rate => $o->{rate}, | ||||||||||||||||||||||||||||
| 321 | partials => $o->{partials}, | ||||||||||||||||||||||||||||
| 322 | fidelity => $o->{fidelity}, | ||||||||||||||||||||||||||||
| 323 | engine => $engine, | ||||||||||||||||||||||||||||
| 324 | limiter => 0, | ||||||||||||||||||||||||||||
| 325 | gain => 0.3, | ||||||||||||||||||||||||||||
| 326 | waves => $o->{waves}, | ||||||||||||||||||||||||||||
| 327 | 0 | ▶ | $share ? (filter => sub ($id) { $id % $share->[1] == $share->[0] }) : (), | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 328 | ); | ||||||||||||||||||||||||||||
| 329 | 3 | ▶ | $synth->apply_edits($o->{bank}) if $o->{bank}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 330 | 3 | $synth->schedule($timeline); | |||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 3 | ▶ | open my $fh, '>:raw', $file or die "cannot write '$file': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 333 | 3 | my $rate = $o->{rate}; | |||||||||||||||||||||||||||
| 334 | 3 | my $limit = int(($length + $o->{tail}) * $rate); | |||||||||||||||||||||||||||
| 335 | 3 | my $peak = 0; | |||||||||||||||||||||||||||
| 336 | 3 | ▶ | while ($synth->busy && $synth->now < $limit) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$synth->busy and $synth->now < $limit
MC/DC50%1/2
$synth->busy and $synth->now < $limit
$synth->busy
$synth->now < $limit
Truth table66%2/3
A and B
A= $synth->busyB= $synth->now < $limit
| |||||||||||||||||||||||||||||
| 337 | 134 | my ($left, $right) = $synth->render_arrays(QUANTUM); | |||||||||||||||||||||||||||
| 338 | 134 | my @frames = (0.0) x (2 * QUANTUM); | |||||||||||||||||||||||||||
| 339 | 134 | for my $i (0 .. QUANTUM - 1) { | |||||||||||||||||||||||||||
| 340 | 137216 | my ($l, $r) = ($left->[$i], $right->[$i]); | |||||||||||||||||||||||||||
| 341 | 137216 | $frames[ 2 * $i ] = $l; | |||||||||||||||||||||||||||
| 342 | 137216 | $frames[ 2 * $i + 1 ] = $r; | |||||||||||||||||||||||||||
| 343 | 137216 | my $x = abs $l; | |||||||||||||||||||||||||||
| 344 | 137216 | ▶ | $peak = $x if $x > $peak; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 345 | 137216 | $x = abs $r; | |||||||||||||||||||||||||||
| 346 | 137216 | ▶ | $peak = $x if $x > $peak; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | 134 | print {$fh} pack 'f*', @frames; | |||||||||||||||||||||||||||
| 349 | 134 | ▶ | $progress->($synth->now / $rate, $length) if $progress; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||
| 351 | 3 | ▶ | close $fh or die "cannot write '$file': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 3 | my $stats = $synth->stats; | |||||||||||||||||||||||||||
| 354 | return { | ||||||||||||||||||||||||||||
| 355 | file => $file, | ||||||||||||||||||||||||||||
| 356 | frames => $synth->now, | ||||||||||||||||||||||||||||
| 357 | notes => $stats->{notes}, | ||||||||||||||||||||||||||||
| 358 | peak_voices => $stats->{peak_voices}, | ||||||||||||||||||||||||||||
| 359 | 3 | engine => $engine->name, | |||||||||||||||||||||||||||
| 360 | # The peak as the file holds it: rounding to 32 bits keeps the order | ||||||||||||||||||||||||||||
| 361 | # of magnitudes, so the largest rounded is the largest, rounded. | ||||||||||||||||||||||||||||
| 362 | peak => unpack('f', pack 'f', $peak), | ||||||||||||||||||||||||||||
| 363 | }; | ||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 0 | ▶ | sub _write_stats ($file, $part) { | ||||||||||||||||||||||||||
|
Subroutine
_write_stats: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 367 | 0 | ▶ | open my $fh, '>', $file or die "cannot write '$file': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 368 | 0 | print {$fh} map { "$_\t$part->{$_}\n" } grep { $_ ne 'file' } sort keys %$part; | |||||||||||||||||||||||||||
| 369 | 0 | close $fh; | |||||||||||||||||||||||||||
| 370 | 0 | return; | |||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | 2 | ▶ | sub _read_stats ($file) { | ||||||||||||||||||||||||||
|
Subroutine
_read_stats: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 374 | 2 | ▶ | open my $fh, '<', $file or die "a worker left no account of its work\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 375 | 2 | my %stats = map { chomp; split /\t/, $_, 2 } <$fh>; | |||||||||||||||||||||||||||
| 376 | 2 | return \%stats; | |||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | 1 | ▶ | use constant CHUNK => 65536; # frames read, summed and written at a time | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | # The parts summed, the sum scaled to the wanted peak and written. | ||||||||||||||||||||||||||||
| 382 | 4 | ▶ | sub _mix_down ($parts, $out, $o) { | ||||||||||||||||||||||||||
|
Subroutine
_mix_down: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 383 | 4 | my $frames = max map { $_->{frames} } @$parts; | |||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | # One part: its peak is known, and it is the sum. | ||||||||||||||||||||||||||||
| 386 | 4 | ▶ | my ($sum_file, $peak) = @$parts == 1 ? ($parts->[0]{file}, $parts->[0]{peak}) : _sum($parts, $frames); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 387 | 4 | ▶ | my $scale = defined $o->{normalize} && $peak > 0 ? 10**($o->{normalize} / 20) / $peak : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $o->{"normalize"} and $peak > 0
MC/DC50%1/2
defined $o->{"normalize"} and $peak > 0
defined $o->{"normalize"}
$peak > 0
Truth table66%2/3
A and B
A= defined $o->{"normalize"}B= $peak > 0
| |||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | my $writer = Peta::Synth::Output->open( | ||||||||||||||||||||||||||||
| 390 | $out, | ||||||||||||||||||||||||||||
| 391 | rate => $o->{rate}, | ||||||||||||||||||||||||||||
| 392 | bits => $o->{bits}, | ||||||||||||||||||||||||||||
| 393 | quality => $o->{quality}, | ||||||||||||||||||||||||||||
| 394 | ffmpeg_gapless => $o->{ffmpeg_gapless}, | ||||||||||||||||||||||||||||
| 395 | 4 | ); | |||||||||||||||||||||||||||
| 396 | 4 | ▶ | open my $in, '<:raw', $sum_file or die "cannot read '$sum_file': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 397 | 4 | ▶ | for (my $at = 0; $at < $frames; $at += CHUNK) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 398 | 4 | ▶ | my $count = $frames - $at < CHUNK ? $frames - $at : CHUNK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 399 | 4 | read($in, my $bytes, 8 * $count); | |||||||||||||||||||||||||||
| 400 | 4 | my @inter = unpack 'f*', $bytes; | |||||||||||||||||||||||||||
| 401 | 4 | my @left = (0.0) x $count; | |||||||||||||||||||||||||||
| 402 | 4 | my @right = (0.0) x $count; | |||||||||||||||||||||||||||
| 403 | 4 | for my $i (0 .. $count - 1) { | |||||||||||||||||||||||||||
| 404 | 185344 | $left[$i] = $inter[ 2 * $i ] * $scale; | |||||||||||||||||||||||||||
| 405 | 185344 | $right[$i] = $inter[ 2 * $i + 1 ] * $scale; | |||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||
| 407 | 4 | $writer->write(\@left, \@right); | |||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||
| 409 | 4 | close $in; | |||||||||||||||||||||||||||
| 410 | 4 | ▶ | unlink $sum_file if @$parts > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 411 | 4 | $writer->close; | |||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | 4 | return { seconds => $frames / $o->{rate}, peak => $peak, scale => $scale, file => $out }; | |||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | # Several parts added up, chunk by chunk, into a temporary file: its name, | ||||||||||||||||||||||||||||
| 417 | # and the peak found on the way. | ||||||||||||||||||||||||||||
| 418 | 1 | ▶ | sub _sum ($parts, $frames) { | ||||||||||||||||||||||||||
|
Subroutine
_sum: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 419 | 1 | ▶ | my @in = map { open my $fh, '<:raw', $_->{file} or die "cannot read '$_->{file}': $!\n"; $fh } @$parts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 420 | 1 | my ($out, $file) = tempfile('peta-synth-sum-XXXXXX', TMPDIR => 1, UNLINK => 1); | |||||||||||||||||||||||||||
| 421 | 1 | binmode $out; | |||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | 1 | my $peak = 0; | |||||||||||||||||||||||||||
| 424 | 1 | ▶ | for (my $at = 0; $at < $frames; $at += CHUNK) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 425 | 1 | ▶ | my $count = $frames - $at < CHUNK ? $frames - $at : CHUNK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 426 | 1 | my @sum = (0.0) x (2 * $count); | |||||||||||||||||||||||||||
| 427 | 1 | for my $fh (@in) { | |||||||||||||||||||||||||||
| 428 | 2 | read($fh, my $bytes, 8 * $count); | |||||||||||||||||||||||||||
| 429 | 2 | my @part = unpack 'f*', $bytes; | |||||||||||||||||||||||||||
| 430 | 2 | for my $i (0 .. $#part) { | |||||||||||||||||||||||||||
| 431 | 192512 | $sum[$i] += $part[$i]; | |||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||
| 434 | 1 | for my $i (0 .. $#sum) { | |||||||||||||||||||||||||||
| 435 | 96256 | my $a = abs $sum[$i]; | |||||||||||||||||||||||||||
| 436 | 96256 | ▶ | $peak = $a if $a > $peak; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||
| 438 | 1 | print {$out} pack 'f*', @sum; | |||||||||||||||||||||||||||
| 439 | } | ||||||||||||||||||||||||||||
| 440 | 1 | close $_ for @in; | |||||||||||||||||||||||||||
| 441 | 1 | close $out; | |||||||||||||||||||||||||||
| 442 | 1 | return ($file, $peak); | |||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | 1 | 1; | |||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | __END__ | ||||||||||||||||||||||||||||