lib/Peta/Synth/Fluid.pm

filter: Statementstmt 35.0% 20 / 57 Branchbran 6.2% 1 / 16 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 62.5% 5 / 8 Podpod 100% 4 / 4 totaltotal 28.0% 30 / 107 CC 18 SCAR 37.0
CC
18
Cov
24%
render_file
72.0
render
56.0
unavailable
7.9
CRAP 40.6
« lib/Peta/Synth/Profile.pm ↑ Summary blib/lib/Peta/MIDI/Device/In.pm »
1 package Peta::Synth::Fluid;
2 # ABSTRACT: FluidSynth as a reference: the same events, played from a SoundFont
3  
4 # FluidSynth as a reference: the same MIDI events played by it from a
5 # General MIDI SoundFont (recorded instruments), to hear and measure this
6 # synthesizer's instruments against. Needs the program fluidsynth and a
7 # SoundFont; FluidR3, where the distributions put it, by default.
8  
9 1 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Peta::Synth::Analysis;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Peta::Synth::MIDI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our $VERSION = '0.2610180';
15  
16 1 our $SOUNDFONT = '/usr/share/soundfonts/FluidR3_GM.sf2';
17  
18 # Whether FluidSynth can play here: '' if it can, why not if not.
19 1 ▶sub unavailable ($soundfont = $SOUNDFONT) {
Subroutine unavailable: called CC=4 SCAR=20.7
Pod documented
20 1 ▶ return "no SoundFont at $soundfont" if !-e $soundfont;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $soundfont 1 0 1
21 0 ▶ return 'fluidsynth is not installed' if system('fluidsynth --version >/dev/null 2>&1') != 0;
Branch: 1 branch
BranchTrueFalseTotal
if system("fluidsynth --version >/dev/null 2>&1") != 0 0 0 0
22 0 return '';
23 }
24  
25 # The events (as Peta::Synth::Synth's schedule takes them: time in seconds)
26 # played by FluidSynth: (\@left, \@right). Options: rate (48000),
27 # soundfont, effects (its reverb and chorus; off by default), length (in
28 # samples: cut or padded to it; FluidSynth stops at the last event).
29 0 ▶sub render ($events, %opt) {
Subroutine render: not called CC=7 SCAR=40.3
Pod documented
30 0 ▶ my $rate = $opt{rate} // 48000;
Condition0%0/2
$opt{"rate"} // 48000
l!l
00
MC/DC0%0/1
$opt{"rate"} // 48000
$opt{"rate"}
Truth table0%0/2
A
A= $opt{"rate"}
Aresult
11
00
31 0 my $dir = tempdir(CLEANUP => 1);
32  
33 # A crotchet of 480 ticks, 120 of them a minute: 960 ticks a second.
34 0 my $tpq = 480;
35 0 my $second = 2 * $tpq;
36 0 my @track = ({ tick => 0, type => 'tempo', usec => 500000 });
37 0 for my $event (sort { $a->{time} <=> $b->{time} } @$events) {
38 0 my %e = %$event;
39 0 $e{tick} = int(delete($e{time}) * $second + 0.5);
40 0 push @track, \%e;
41 }
42 0 ▶ if ($opt{length}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"length"}) 0 0 0
43 # An event at the end, which FluidSynth plays to.
44 0 push @track, { tick => int($opt{length} / $rate * $second + 0.5), type => 'meta', meta => 0x01, data => 'end' };
45 }
46 0 Peta::Synth::MIDI->new(format => 0, division => $tpq, tracks => [ \@track ])->write("$dir/in.mid");
47  
48 0 render_file("$dir/in.mid", "$dir/out.wav", %opt);
49 0 my $wav = Peta::Synth::Analysis::read_wav("$dir/out.wav");
50 0 my ($left, $right) = @{ $wav->{channels} };
51 0 ▶ if (my $length = $opt{length}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $length = $opt{"length"}) 0 0 0
52 0 for my $ch ($left, $right) {
53 0 ▶ push @$ch, (0.0) x ($length - @$ch) if @$ch < $length;
Branch: 1 branch
BranchTrueFalseTotal
if @$ch < $length 0 0 0
54 0 $#$ch = $length - 1;
55 }
56 }
57 0 return ($left, $right);
58 }
59  
60 # Where a program is compared, the lowest key its phrase or notes start
61 # from (two octaves above it they end): low instruments lower, high ones
62 # higher, the rest at 48.
63 #
64 # 36 the basses (32-39), cello, contrabass, tuba, baritone sax, bassoon,
65 # bass and lead (87)
66 # 72 celesta, glockenspiel, music box, xylophone, piccolo, recorder,
67 # whistle, ocarina, tinkle bell
68 # 55 violin, trumpet, muted trumpet, soprano sax, oboe, flute, pan flute,
69 # fiddle, shanai
70 my %HOME = (
71 14 (map { $_ => 36 } 32 .. 39, 42, 43, 58, 67, 70, 87),
72 9 (map { $_ => 72 } 8, 9, 10, 13, 72, 74, 78, 79, 112),
73 1 (map { $_ => 55 } 40, 56, 59, 64, 68, 73, 75, 110, 111),
74 );
75 0 ▶sub home ($program) { return $HOME{$program} // 48 }
Subroutine home: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$HOME{$program} // 48
l!l
00
MC/DC0%0/1
$HOME{$program} // 48
$HOME{$program}
Truth table0%0/2
A
A= $HOME{$program}
Aresult
11
00
76  
77 # A MIDI file played by FluidSynth into a WAV file (16 bits). Options as render's.
78 0 ▶sub render_file ($midi, $wav, %opt) {
Subroutine render_file: not called CC=8 SCAR=42.8
Pod documented
79 0 ▶ my $rate = $opt{rate} // 48000;
Condition0%0/2
$opt{"rate"} // 48000
l!l
00
MC/DC0%0/1
$opt{"rate"} // 48000
$opt{"rate"}
Truth table0%0/2
A
A= $opt{"rate"}
Aresult
11
00
80 0 ▶ my $soundfont = $opt{soundfont} // $SOUNDFONT;
Condition0%0/3
$opt{"soundfont"} // $Peta::Synth::Fluid::SOUNDFONT
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{"soundfont"} // $Peta::Synth::Fluid::SOUNDFONT
$opt{"soundfont"} $Peta::Synth::Fluid::SOUNDFONT
Truth table0%0/3
A // B
A= $opt{"soundfont"}B= $Peta::Synth::Fluid::SOUNDFONT
ABresult
1X1
011
000
81 0 ▶ my @effects = $opt{effects} ? () : ('-R', 0, '-C', 0);
Branch: 1 branch
BranchTrueFalseTotal
$opt{'effects'} ? : 0 0 0
82 0 ▶ my $pid = fork // die "cannot fork: $!\n";
Condition0%0/2
fork // die("cannot fork: $!\n")
l!l
00
MC/DC0%0/1
fork // die("cannot fork: $!\n")
fork
Truth table0%0/2
A
A= fork
Aresult
11
00
83 0 ▶ if (!$pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 0 0
84 0 open STDOUT, '>', '/dev/null';
85 0 open STDERR, '>', '/dev/null';
86 0 exec 'fluidsynth', '-ni', @effects, '-r', $rate, '-O', 's16', '-T', 'wav', '-F', $wav, $soundfont, $midi;
87 0 exit 127;
88 }
89 0 waitpid $pid, 0;
90 0 ▶ die "fluidsynth failed on $midi\n" if $? != 0 || !-s $wav;
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 or not -s $wav 0 0 0
Condition0%0/3
$? != 0 or not -s $wav
l!l&&r!l&&!r
000
MC/DC0%0/2
$? != 0 or not -s $wav
$? != 0 not -s $wav
Truth table0%0/3
A or not(B)
A= $? != 0B= not -s $wav
ABresult
1X1
011
000
91 0 return $wav;
92 }
93  
94 1 1;
95  
96 __END__
« lib/Peta/Synth/Profile.pm ↑ Summary blib/lib/Peta/MIDI/Device/In.pm »