lib/Peta/Synth/Input.pm

filter: Statementstmt 22.7% 15 / 66 Branchbran 4.5% 1 / 22 Conditioncond 28.5% 2 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 35.7% 5 / 14 Podpod 100% 9 / 9 totaltotal 26.2% 32 / 122 CC 20 SCAR 25.8
CC
20
Cov
19%
open
21.5
connect
20.0
_announced
20.0
CRAP 13.2
« lib/Peta/Synth/Engine/PDL.pm ↑ Summary lib/Peta/Synth/MIDI/Stream.pm »
1 package Peta::Synth::Input;
2 # ABSTRACT: MIDI as it is played: the ALSA sequencer, raw devices
3  
4 # MIDI coming in as it is played: a port of a client of the ALSA sequencer
5 # (Peta::MIDI::ALSA: the kernel's MIDI router, no library). Keyboards on
6 # USB, MIDI interfaces, software keyboards and sequencers are its clients;
7 # PipeWire bridges its own MIDI to it. The port is connected to the sources
8 # it is told (all of them, by default, and those that appear later); what
9 # comes from each is read as MIDI bytes (Peta::Synth::MIDI::Stream), a
10 # controller of 14 bits, an RPN or NRPN as their controllers.
11 #
12 # A raw MIDI device or a pipe (a path) is read as bytes instead:
13 # Peta::Synth::Input::Raw.
14  
15 1 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Peta::MIDI::ALSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Peta::Synth::MIDI::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 our $VERSION = '0.2610180';
20  
21 # What to read MIDI from: undef or 'all' (every source there is and comes),
22 # 'none' (a port others connect to), a path (a raw device or a pipe), or
23 # sources: 'client:port' or a part of a client's or port's name, several
24 # joined by commas.
25 1 ▶sub open ($class, $spec = undef) {
Subroutine open: called CC=7 SCAR=30.7
Pod documented
26 1 ▶ $spec //= 'all';
Condition50%1/2
$spec //= "all"
l!l
10
MC/DC0%0/1
$spec //= "all"
$spec
Truth table50%1/2
A
A= $spec
Aresult
11
00
27 1 ▶ if ($spec =~ m{\A/}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ m[\A/]u) 0 1 1
28 0 require Peta::Synth::Input::Raw;
29 0 return Peta::Synth::Input::Raw->new($spec);
30 }
31 1 my $self = $class->new;
32 0 ▶ if ($spec eq 'all') { $self->follow }
Branch: 2 branches
BranchTrueFalseTotal
if ($spec eq 'all') { } 0 0 0
elsif ($spec ne 'none') { } 0 0 0
33 0 elsif ($spec ne 'none') { $self->connect($_) for split /\s*,\s*/, $spec }
34 0 return $self;
35 }
36  
37 2 ▶sub new ($class, %arg) {
Subroutine new: called CC=2 SCAR=12.4
Pod documented
38 2 ▶ my $alsa = Peta::MIDI::ALSA->new(name => $arg{name} // 'Peta::Synth');
Condition50%1/2
$arg{"name"} // "Peta::Synth"
l!l
02
MC/DC0%0/1
$arg{"name"} // "Peta::Synth"
$arg{"name"}
Truth table50%1/2
A
A= $arg{"name"}
Aresult
11
00
39 0 my $self = bless {
40 alsa => $alsa,
41 port => $alsa->create_port('MIDI in', 'in'),
42 streams => {}, # each source's bytes, by address
43 connected => {}, # the sources, by address
44 }, $class;
45 0 ▶ $alsa->on_announce(sub ($what, $address) { $self->_announced($what, $address) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 0 return $self;
47 }
48  
49 0 ▶sub handle ($self) { return $self->{alsa}->handle }
Subroutine handle: not called CC=1 SCAR=6.9
Pod documented
50 0 ▶sub address ($self) { return $self->{alsa}->client . ":$self->{port}" }
Subroutine address: not called CC=1 SCAR=6.9
Pod documented
51 0 ▶sub connected ($self) { return sort keys %{ $self->{connected} } }
Subroutine connected: not called CC=1 SCAR=6.9
Pod documented
52  
53 # Every port others can read MIDI from (not the system's, not this
54 # client's): { address, client, port } by name.
55 0 ▶sub sources ($self = undef) {
Subroutine sources: not called CC=3 SCAR=24.8
Pod documented
56 0 ▶ my @ports = ref $self ? $self->{alsa}->ports : Peta::MIDI::ALSA::ports();
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 0 0
57 0 return map { +{ address => $_->{address}, client => $_->{client_name}, port => $_->{port_name} } }
58 0 grep { Peta::MIDI::ALSA::readable($_->{cap}) } @ports;
59 }
60  
61 # Connects a source to this client's port: 'client:port', or the first
62 # source whose client or port name holds the text.
63 0 ▶sub connect ($self, $source) {
Subroutine connect: not called CC=4 SCAR=30.0
Pod documented
64 0 ▶ my $address = $source =~ /\A\d+:\d+\z/ ? $source : undef;
Branch: 1 branch
BranchTrueFalseTotal
$source =~ /\A\d+:\d+\z/u ? : 0 0 0
65 0 ▶ if (!defined $address) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $address) 0 0 0
66 0 my ($found) = grep { index(lc "$_->{client} $_->{port}", lc $source) >= 0 } $self->sources;
67 0 ▶ die "no MIDI source '$source' (peta-synth inputs lists them)\n" if !$found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 0 0 0
68 0 $address = $found->{address};
69 }
70 0 $self->{alsa}->subscribe($address, $self->address);
71 0 $self->{connected}{$address} = 1;
72 0 return $self;
73 }
74  
75 # Every source, and those that come later (announced by the system's
76 # port 0:1); not the kernel's loopback (Midi Through).
77 0 ▶sub follow ($self) {
Subroutine follow: not called CC=3 SCAR=24.8
Pod documented
78 0 $self->{follow} = 1;
79 0 for my $source ($self->sources) {
80 0 ▶ next if $source->{client} =~ /\AMidi Through/;
Branch: 1 branch
BranchTrueFalseTotal
if $source->{"client"} =~ /\AMidi Through/u 0 0 0
81 0 eval { $self->connect($source->{address}); 1 };
82 }
83 0 eval { $self->{alsa}->subscribe('0:1', $self->address); 1 };
84 0 return $self;
85 }
86  
87 # A port come (connected, following, if it is a source) or gone.
88 0 ▶sub _announced ($self, $what, $address) {
Subroutine _announced: not called CC=4 SCAR=30.0
89 0 ▶ if ($what eq 'exit') {
Branch: 1 branch
BranchTrueFalseTotal
if ($what eq "exit") 0 0 0
90 0 delete $self->{connected}{$address};
91 0 return;
92 }
93 0 ▶ return if !$self->{follow};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"follow"} 0 0 0
94 0 ▶ eval { $self->connect($address); 1 } if grep { $_->{address} eq $address } $self->sources;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->{"address"} eq $address;} $self->sources 0 0 0
95 0 return;
96 }
97  
98 # What has come in, as Peta::Synth::MIDI's events; waits at most $wait
99 # seconds for something (0: not at all).
100 0 ▶sub events ($self, $wait = 0) {
Subroutine events: not called CC=3 SCAR=24.8
Pod documented
101 return map {
102 0 my ($bytes, $source) = @$_;
103 0 ▶ ($self->{streams}{$source} //= Peta::Synth::MIDI::Stream->new)->feed($bytes);
Condition0%0/3
$self->{"streams"}{$source} //= "Peta::Synth::MIDI::Stream"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"streams"}{$source} //= "Peta::Synth::MIDI::Stream"->new
$self->{"streams"}{$source} "Peta::Synth::MIDI::Stream"->new
Truth table0%0/3
A //= B
A= $self->{"streams"}{$source}B= "Peta::Synth::MIDI::Stream"->new
ABresult
1X1
011
000
104 0 } $self->{alsa}->read($wait);
105 }
106  
107 1 1;
108  
109 __END__
« lib/Peta/Synth/Engine/PDL.pm ↑ Summary lib/Peta/Synth/MIDI/Stream.pm »