| 1 | package Peta::Synth::Engine; | ||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: the engines that render voices | ||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||
| 4 | # Where the voices become samples. Every engine renders the same voice model | ||||||||||||||||||||||||||||||||||||
| 5 | # (Peta::Synth::Voice) and gives the same sound, within the precision of its | ||||||||||||||||||||||||||||||||||||
| 6 | # arithmetic; they differ in speed, and so in how many partials and voices | ||||||||||||||||||||||||||||||||||||
| 7 | # a machine affords. | ||||||||||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||||||||||
| 9 | # perl plain Perl: everywhere; on pperl compiled by the JIT | ||||||||||||||||||||||||||||||||||||
| 10 | # pdl PDL: the partials of many voices as whole-array operations | ||||||||||||||||||||||||||||||||||||
| 11 | # gpu WebGPU compute shaders: every partial of every voice at once (pperl) | ||||||||||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||||||||||
| 13 | # An engine implements | ||||||||||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||||||||||
| 15 | # new(rate => ..., partials => ...) dies when its engine is missing | ||||||||||||||||||||||||||||||||||||
| 16 | # name | ||||||||||||||||||||||||||||||||||||
| 17 | # render(\@voices, $n, $rate) (\@left, \@right, \@send): $n samples | ||||||||||||||||||||||||||||||||||||
| 18 | # | ||||||||||||||||||||||||||||||||||||
| 19 | # and may implement | ||||||||||||||||||||||||||||||||||||
| 20 | # | ||||||||||||||||||||||||||||||||||||
| 21 | # render_reverb(\@voices, $n, $rate, $reverb) | ||||||||||||||||||||||||||||||||||||
| 22 | # the same and the reverb on it, (\@left, \@right, | ||||||||||||||||||||||||||||||||||||
| 23 | # \@send, \@wet_left, \@wet_right) | ||||||||||||||||||||||||||||||||||||
| 24 | # reads_waves whether it fares better reading harmonic partials | ||||||||||||||||||||||||||||||||||||
| 25 | # from wave sequences (Peta::Synth::Wavetable) | ||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||
| 27 | 14 | ▶ | use v5.36; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||
| 29 | 14 | our $VERSION = '0.2610180'; | |||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||
| 31 | 14 | my %CLASS = ( | |||||||||||||||||||||||||||||||||||
| 32 | perl => 'Peta::Synth::Engine::Perl', | ||||||||||||||||||||||||||||||||||||
| 33 | pdl => 'Peta::Synth::Engine::PDL', | ||||||||||||||||||||||||||||||||||||
| 34 | gpu => 'Peta::Synth::Engine::GPU', | ||||||||||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||
| 37 | 3 | ▶ | sub names () { return sort keys %CLASS } | ||||||||||||||||||||||||||||||||||
|
Subroutine
names: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||
| 39 | # An engine, or undef when it is not there; $@ then says why. Why an | ||||||||||||||||||||||||||||||||||||
| 40 | # engine's module did not load is kept: perl does not load it again, and | ||||||||||||||||||||||||||||||||||||
| 41 | # would only say so. | ||||||||||||||||||||||||||||||||||||
| 42 | 14 | my %MISSING; | |||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||
| 44 | 46 | ▶ | sub try ($name, %arg) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
try: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | my $class = $CLASS{$name} // die "unknown engine '$name' (have: @{[ names() ]}, auto)\n"; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$CLASS{$name} // die("unknown engine '${name}' (have: @{[names()];}, auto)\n")
MC/DC0%0/1
$CLASS{$name} // die("unknown engine '${name}' (have: @{[names()];}, auto)\n")
$CLASS{$name}
Truth table50%1/2
A
A= $CLASS{$name}
| |||||||||||||||||||||||||||||||||||||
| 46 | 46 | ▶ | if ($MISSING{$class}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 47 | 6 | $@ = $MISSING{$class}; | |||||||||||||||||||||||||||||||||||
| 48 | 6 | return undef; | |||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||
| 51 | 40 | (my $file = "$class.pm") =~ s{::}{/}g; | |||||||||||||||||||||||||||||||||||
| 52 | 40 | ▶ | if (!eval { require $file; 1 }) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 53 | 2 | $MISSING{$class} = $@; | |||||||||||||||||||||||||||||||||||
| 54 | 2 | return undef; | |||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||
| 56 | 38 | return eval { $class->new(%arg) }; | |||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||
| 59 | 2 | ▶ | sub available () { return grep { defined try($_) } names() } | ||||||||||||||||||||||||||||||||||
|
Subroutine
available: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||
| 61 | # An engine by name, or for 'auto' the fastest here (see choose). | ||||||||||||||||||||||||||||||||||||
| 62 | 29 | ▶ | sub create ($name = 'auto', %arg) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
create: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 63 | 29 | ▶ | $name = choose(%arg) if $name eq 'auto'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 64 | 29 | ▶ | return try($name, %arg) // die "engine '$name' is not available here: $@"; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
try($name, %arg) // die("engine '${name}' is not available here: $@")
MC/DC0%0/1
try($name, %arg) // die("engine '${name}' is not available here: $@")
try($name, %arg)
Truth table50%1/2
A
A= try($name, %arg)
| |||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||
| 67 | # The name of the engine 'auto' takes: $ENV{PETA_SYNTH_ENGINE} when set, | ||||||||||||||||||||||||||||||||||||
| 68 | # or the fastest of those here, measured; rate and partials as the | ||||||||||||||||||||||||||||||||||||
| 69 | # synthesizer will have them. | ||||||||||||||||||||||||||||||||||||
| 70 | 1 | ▶ | sub choose (%arg) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
choose: called CC=3 SCAR=16.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 71 | 1 | ▶ | return $ENV{PETA_SYNTH_ENGINE} if $ENV{PETA_SYNTH_ENGINE} && $ENV{PETA_SYNTH_ENGINE} ne 'auto'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ENV{"PETA_SYNTH_ENGINE"} and $ENV{"PETA_SYNTH_ENGINE"} ne "auto"
MC/DC0%0/2
$ENV{"PETA_SYNTH_ENGINE"} and $ENV{"PETA_SYNTH_ENGINE"} ne "auto"
$ENV{"PETA_SYNTH_ENGINE"}
$ENV{"PETA_SYNTH_ENGINE"} ne "auto"
Truth table33%1/3
A and B
A= $ENV{"PETA_SYNTH_ENGINE"}B= $ENV{"PETA_SYNTH_ENGINE"} ne "auto"
| |||||||||||||||||||||||||||||||||||||
| 72 | 0 | my ($fastest) = measure(%arg); | |||||||||||||||||||||||||||||||||||
| 73 | 0 | return $fastest->{name}; | |||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||
| 76 | # How fast each engine here renders a test: a chord on four instruments and | ||||||||||||||||||||||||||||||||||||
| 77 | # three drums, with the reverb, as a synthesizer plays them. Fastest first: | ||||||||||||||||||||||||||||||||||||
| 78 | # [ { name, speed } ], speed in seconds of sound per second. | ||||||||||||||||||||||||||||||||||||
| 79 | # | ||||||||||||||||||||||||||||||||||||
| 80 | # What is fastest depends on the perl (pperl's JIT makes the Perl engine | ||||||||||||||||||||||||||||||||||||
| 81 | # fast), on the card, and on how much there is to render: a GPU costs a | ||||||||||||||||||||||||||||||||||||
| 82 | # fixed time per quantum, which pays from some number of partials on. The | ||||||||||||||||||||||||||||||||||||
| 83 | # test is a moderate load; the first quanta are not timed, so that pperl's | ||||||||||||||||||||||||||||||||||||
| 84 | # JIT has compiled the loops. | ||||||||||||||||||||||||||||||||||||
| 85 | 14 | my $QUANTUM = 1024; | |||||||||||||||||||||||||||||||||||
| 86 | 14 | my $WARM = 2; # quanta not timed | |||||||||||||||||||||||||||||||||||
| 87 | 14 | my $TIMED = 4; # quanta timed at most | |||||||||||||||||||||||||||||||||||
| 88 | 14 | my $BUDGET = 0.25; # seconds an engine is timed for at most, after one quantum | |||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||
| 90 | 1 | ▶ | sub measure (%arg) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
measure: called CC=13 SCAR=25.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 91 | 1 | require Peta::Synth::Synth; | |||||||||||||||||||||||||||||||||||
| 92 | 1 | require Time::HiRes; | |||||||||||||||||||||||||||||||||||
| 93 | 1 | ▶ | my $rate = $arg{rate} // 48000; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$arg{"rate"} // 48000
MC/DC0%0/1
$arg{"rate"} // 48000
$arg{"rate"}
Truth table50%1/2
A
A= $arg{"rate"}
| |||||||||||||||||||||||||||||||||||||
| 94 | 1 | my @speed; | |||||||||||||||||||||||||||||||||||
| 95 | 1 | for my $name (names()) { | |||||||||||||||||||||||||||||||||||
| 96 | 3 | ▶ | my $engine = try($name) or next; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 97 | 2 | ▶ | my $synth = Peta::Synth::Synth->new(rate => $rate, partials => $arg{partials} // 48, engine => $engine, limiter => 0); | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$arg{"partials"} // 48
MC/DC0%0/1
$arg{"partials"} // 48
$arg{"partials"}
Truth table50%1/2
A
A= $arg{"partials"}
| |||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||
| 99 | # Piano, strings, brass and a pad, four notes each; kick, snare, hi-hat. | ||||||||||||||||||||||||||||||||||||
| 100 | 2 | my @programs = (0, 48, 61, 89); | |||||||||||||||||||||||||||||||||||
| 101 | 2 | for my $channel (0 .. $#programs) { | |||||||||||||||||||||||||||||||||||
| 102 | 8 | $synth->play({ type => 'program', channel => $channel, program => $programs[$channel] }); | |||||||||||||||||||||||||||||||||||
| 103 | 8 | for my $key (48 + $channel, 55, 60 + $channel, 64) { | |||||||||||||||||||||||||||||||||||
| 104 | 32 | $synth->play({ type => 'note_on', channel => $channel, key => $key, velocity => 90 }); | |||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||
| 107 | 2 | $synth->play({ type => 'note_on', channel => 9, key => $_, velocity => 100 }) for 36, 38, 42; | |||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||
| 109 | 2 | $synth->render_arrays($QUANTUM) for 1 .. $WARM; | |||||||||||||||||||||||||||||||||||
| 110 | 2 | my ($best, $spent) = (9**9**9, 0); | |||||||||||||||||||||||||||||||||||
| 111 | 2 | for (1 .. $TIMED) { | |||||||||||||||||||||||||||||||||||
| 112 | 5 | my $started = Time::HiRes::time(); | |||||||||||||||||||||||||||||||||||
| 113 | 5 | $synth->render_arrays($QUANTUM); | |||||||||||||||||||||||||||||||||||
| 114 | 5 | my $took = Time::HiRes::time() - $started; | |||||||||||||||||||||||||||||||||||
| 115 | 5 | ▶ | $best = $took if $took < $best; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 116 | 5 | ▶ | last if ($spent += $took) > $BUDGET; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||
| 118 | 2 | ▶ | push @speed, { name => $name, speed => $QUANTUM / $rate / ($best || 1e-9) }; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$best || 1e-09
MC/DC0%0/1
$best || 1e-09
$best
Truth table50%1/2
A
A= $best
| |||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||
| 120 | 1 | return sort { $b->{speed} <=> $a->{speed} } @speed; | |||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||
| 123 | # Which perl this is, and what it has: for choosing what a machine can afford. | ||||||||||||||||||||||||||||||||||||
| 124 | 4 | ▶ | sub perl_info () { | ||||||||||||||||||||||||||||||||||
|
Subroutine
perl_info: called CC=8 SCAR=29.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 125 | 4 | my $pperl = $^V =~ /\.99\z/; # pperl's version ends so | |||||||||||||||||||||||||||||||||||
| 126 | return { | ||||||||||||||||||||||||||||||||||||
| 127 | perl => $pperl ? 'pperl' : 'perl5', | ||||||||||||||||||||||||||||||||||||
| 128 | version => $pperl ? $ENV{PPERL} : sprintf('%vd', $^V), # pperl sets PPERL to its own | ||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | jit => $pperl && do { require Config; my @counts = eval { Config::jit_counts() }; @counts ? 1 : 0 } ? 1 : 0, | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 130 | 4 | ▶ | pdl => eval { require PDL::Lite; 1 } ? 1 : 0, | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | gpu => eval { require Peta::WebGPU; Peta::WebGPU::have_gpu() } ? 1 : 0, | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition33%1/3
$pperl and do {
require Config;
my(@counts) = eval {
do {
Config::jit_counts()
}
};
@counts ? 1 : 0
}
MC/DC0%0/2
$pperl and do {
require Config;
my(@counts) = eval {
do {
Config::jit_counts()
}
};
@counts ? 1 : 0
}
$pperl
do {
require Config;
my(@counts) = eval {
do {
Config::jit_counts()
}
};
@counts ? 1 : 0
}
Truth table33%1/3
A and B
A= $pperlB= do {
require Config;
my(@counts) = eval {
do {
Config::jit_counts()
}
};
@counts ? 1 : 0
}
| |||||||||||||||||||||||||||||||||||||
| 132 | cpus => cpus(), | ||||||||||||||||||||||||||||||||||||
| 133 | }; | ||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||
| 136 | 4 | ▶ | sub cpus () { | ||||||||||||||||||||||||||||||||||
|
Subroutine
cpus: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 137 | 4 | ▶ | open my $fh, '<', '/proc/cpuinfo' or return 1; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 138 | 4 | my $count = grep { /^processor\s*:/ } <$fh>; | |||||||||||||||||||||||||||||||||||
| 139 | 4 | ▶ | return $count || 1; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$count || 1
MC/DC0%0/1
$count || 1
$count
Truth table50%1/2
A
A= $count
| |||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||
| 142 | 14 | 1; | |||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||
| 144 | __END__ | ||||||||||||||||||||||||||||||||||||