lib/Peta/MIDI/Device.pm

filter: Statementstmt 30.5% 29 / 95 Branchbran 26.9% 7 / 26 Conditioncond 27.2% 6 / 22 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 23.3% 7 / 30 Podpod 100% 21 / 21 totaltotal 33.9% 70 / 206 CC 28 SCAR 22.9
CC
28
Cov
29%
new
20.2
open_port
20.0
_ports
12.0
CRAP 9.9
« lib/Peta/Synth/Engine.pm ↑ Summary lib/Peta/MIDI/Decoder.pm »
1 package Peta::MIDI::Device;
2 # ABSTRACT: MIDI devices as MIDI::RtMidi::FFI::Device has them, on the ALSA sequencer
3  
4 # MIDI devices as MIDI::RtMidi::FFI::Device has them, on the ALSA
5 # sequencer (Peta::MIDI::ALSA; no library): a device is an input
6 # (Peta::MIDI::Device::In) or an output (::Out) with one port of its own,
7 # opened onto another client's port (by number, name, pattern or a list of
8 # them) or open to all as a virtual port. Ports are numbered as the
9 # sequencer lists them, and named 'client:port client_number:port_number',
10 # as RtMidi names them.
11  
12 1 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Peta::MIDI::ALSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Peta::MIDI::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 our $VERSION = '0.2610180';
19  
20 # A device: new(type => 'in' or 'out' (the default), name => ...), or
21 # Peta::MIDI::Device::In->new(...) and ::Out->new(...).
22 2 ▶sub new ($class, %arg) {
Subroutine new: called CC=12 SCAR=30.1
Pod documented
23 2 ▶ if ($class eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq __PACKAGE__) 1 1 2
24 1 ▶ my $type = delete($arg{type}) // 'out';
Condition50%1/2
delete $arg{"type"} // "out"
l!l
10
MC/DC0%0/1
delete $arg{"type"} // "out"
delete $arg{"type"}
Truth table50%1/2
A
A= delete $arg{"type"}
Aresult
11
00
25 1 ▶ croak "a device is 'in' or 'out', not '$type'" if $type ne 'in' && $type ne 'out';
Branch: 1 branch
BranchTrueFalseTotal
if $type ne "in" and $type ne "out" 0 1 1
Condition33%1/3
$type ne "in" and $type ne "out"
!ll&&!rl&&r
010
MC/DC0%0/2
$type ne "in" and $type ne "out"
$type ne "in" $type ne "out"
Truth table33%1/3
A and B
A= $type ne "in"B= $type ne "out"
ABresult
0X0
100
111
26 1 ▶ my $kind = $type eq 'in' ? 'Peta::MIDI::Device::In' : 'Peta::MIDI::Device::Out';
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'in' ? : 0 1 1
27 1 (my $file = "$kind.pm") =~ s{::}{/}g;
28 1 require $file;
29 1 return $kind->new(%arg);
30 }
31 1 ▶ croak "'type' is for Peta::MIDI::Device->new only" if exists $arg{type};
Branch: 1 branch
BranchTrueFalseTotal
if exists $arg{"type"} 0 1 1
32 1 ▶ my $api = $arg{api_name} // $arg{api} // 'alsa';
Condition40%2/5
($arg{"api_name"} // $arg{"api"}) // "alsa"
l!l
01
$arg{"api_name"} // $arg{"api"}
l!l&&r!l&&!r
001
MC/DC0%0/2
($arg{"api_name"} // $arg{"api"}) // "alsa"
$arg{"api_name"} $arg{"api"}
Truth table33%1/3
A // B
A= $arg{"api_name"}B= $arg{"api"}
ABresult
1X1
011
000
33 1 ▶ croak "the MIDI API here is ALSA, not '$api'" if $api ne 'alsa' && $api ne 'unspecified';
Branch: 1 branch
BranchTrueFalseTotal
if $api ne "alsa" and $api ne "unspecified" 0 1 1
Condition33%1/3
$api ne "alsa" and $api ne "unspecified"
!ll&&!rl&&r
100
MC/DC0%0/2
$api ne "alsa" and $api ne "unspecified"
$api ne "alsa" $api ne "unspecified"
Truth table33%1/3
A and B
A= $api ne "alsa"B= $api ne "unspecified"
ABresult
0X0
100
111
34  
35 1 ▶ my $self = bless { name => $arg{name} // "Peta::MIDI Client $class" }, $class;
Condition50%1/2
$arg{"name"} // "Peta::MIDI Client $class"
l!l
10
MC/DC0%0/1
$arg{"name"} // "Peta::MIDI Client $class"
$arg{"name"}
Truth table50%1/2
A
A= $arg{"name"}
Aresult
11
00
36 1 $self->{alsa} = Peta::MIDI::ALSA->new(name => $self->{name});
37 0 $self->_build(%arg);
38 0 return $self;
39 }
40  
41 0 ▶sub _build ($self, %arg) { return }
Subroutine _build: not called CC=1 SCAR=6.9
42  
43 0 ▶sub name ($self) { return $self->{name} }
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
44 0 ▶sub port_name ($self) { return $self->{port_name} }
Subroutine port_name: not called CC=1 SCAR=6.9
Pod documented
45 0 ▶sub connected_to ($self) { return $self->{connected_to} }
Subroutine connected_to: not called CC=1 SCAR=6.9
Pod documented
46 0 ▶sub connected_to_name ($self) { return $self->{connected_to_name} }
Subroutine connected_to_name: not called CC=1 SCAR=6.9
Pod documented
47 0 ▶sub ok ($self, $ok = undef) { return 1 }
Subroutine ok: not called CC=2 SCAR=17.9
Pod documented
48 0 ▶sub msg ($self) { return '' }
Subroutine msg: not called CC=1 SCAR=6.9
Pod documented
49 0 ▶sub get_current_api ($self) { return 'alsa' }
Subroutine get_current_api: not called CC=1 SCAR=6.9
Pod documented
50 0 ▶sub get_compiled_api ($self) { return ('alsa') }
Subroutine get_compiled_api: not called CC=1 SCAR=6.9
Pod documented
51  
52 0 ▶sub name_to_midi_event ($self, $name) { return Peta::MIDI::Event::name_to_midi_event($name) }
Subroutine name_to_midi_event: not called CC=1 SCAR=6.9
Pod documented
53 0 ▶sub name_from_midi_event ($self, $name) { return Peta::MIDI::Event::name_from_midi_event($name) }
Subroutine name_from_midi_event: not called CC=1 SCAR=6.9
Pod documented
54  
55 # The ports a device of this kind can open (an input those that can be
56 # read, an output those that can be written), in the sequencer's order.
57 0 ▶sub _ports ($self) {
Subroutine _ports: not called CC=3 SCAR=24.8
58 0 ▶ my $can = $self->_direction eq 'in' ? \&Peta::MIDI::ALSA::readable : \&Peta::MIDI::ALSA::writable;
Branch: 1 branch
BranchTrueFalseTotal
$self->_direction eq 'in' ? : 0 0 0
59 0 ▶ return sort { $a->{client} <=> $b->{client} || $a->{port} <=> $b->{port} } grep { $can->($_->{cap}) } $self->{alsa}->ports;
Condition0%0/3
$Peta::MIDI::Device::a->{'client'} <=> $Peta::MIDI::Device::b->{'client'} || $Peta::MIDI::Device::a->{'port'} <=> $Peta::MIDI::Device::b->{'port'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$Peta::MIDI::Device::a->{'client'} <=> $Peta::MIDI::Device::b->{'client'} || $Peta::MIDI::Device::a->{'port'} <=> $Peta::MIDI::Device::b->{'port'}
$Peta::MIDI::Device::a->{'client'} <=> $Peta::MIDI::Device::b->{'client'} $Peta::MIDI::Device::a->{'port'} <=> $Peta::MIDI::Device::b->{'port'}
Truth table0%0/3
A || B
A= $Peta::MIDI::Device::a->{'client'} <=> $Peta::MIDI::Device::b->{'client'}B= $Peta::MIDI::Device::a->{'port'} <=> $Peta::MIDI::Device::b->{'port'}
ABresult
1X1
011
000
60 }
61  
62 0 ▶sub _port_name ($port) { return "$port->{client_name}:$port->{port_name} $port->{address}" }
Subroutine _port_name: not called CC=1 SCAR=6.9
63  
64 0 ▶sub get_port_count ($self) {
Subroutine get_port_count: not called CC=1 SCAR=6.9
Pod documented
65 0 my @ports = $self->_ports;
66 0 return scalar @ports;
67 }
68  
69 0 ▶sub get_port_name ($self, $number) {
Subroutine get_port_name: not called CC=2 SCAR=17.9
Pod documented
70 0 ▶ my $port = ($self->_ports)[$number] // return undef;
Condition0%0/2
($self->_ports)[$number] // (return undef)
l!l
00
MC/DC0%0/1
($self->_ports)[$number] // (return undef)
($self->_ports)[$number]
Truth table0%0/2
A
A= ($self->_ports)[$number]
Aresult
11
00
71 0 return _port_name($port);
72 }
73  
74 0 ▶sub get_all_port_nums ($self) {
Subroutine get_all_port_nums: not called CC=1 SCAR=6.9
Pod documented
75 0 my @ports = $self->_ports;
76 0 return { map { $_ => _port_name($ports[$_]) } 0 .. $#ports };
77 }
78  
79 0 ▶sub get_all_port_names ($self) { return { reverse %{ $self->get_all_port_nums } } }
Subroutine get_all_port_names: not called CC=1 SCAR=6.9
Pod documented
80  
81 # The ports' numbers whose names are the name, match the pattern, or (a
82 # list) any of them, in that order.
83 0 ▶sub get_ports_by_name ($self, $which) {
Subroutine get_ports_by_name: not called CC=3 SCAR=24.8
Pod documented
84 0 ▶ return map { $self->get_ports_by_name($_) } @$which if ref $which eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $which eq "ARRAY" 0 0 0
85 0 my $names = $self->get_all_port_nums;
86 return grep {
87 0 ▶ ref $which eq 'Regexp' ? $names->{$_} =~ $which : $names->{$_} eq $which
Branch: 1 branch
BranchTrueFalseTotal
ref $which eq 'Regexp' ? : 0 0 0
88 0 } sort { $a <=> $b } keys %$names;
89 }
90  
91 0 ▶sub print_ports ($self, $handle = \*STDOUT) {
Subroutine print_ports: not called CC=3 SCAR=24.8
Pod documented
92 0 my $names = $self->get_all_port_nums;
93 0 print {$handle} "$_: $names->{$_}\n" for sort { $a <=> $b } keys %$names;
94 0 return;
95 }
96  
97 # A port of this device's own, connected to port $number.
98 0 ▶sub open_port ($self, $number, $name = 'Peta::MIDI') {
Subroutine open_port: not called CC=4 SCAR=30.0
Pod documented
99 0 ▶ my $port = ($self->_ports)[$number] // croak "Invalid port number ($number)";
Condition0%0/2
($self->_ports)[$number] // croak("Invalid port number ($number)")
l!l
00
MC/DC0%0/1
($self->_ports)[$number] // croak("Invalid port number ($number)")
($self->_ports)[$number]
Truth table0%0/2
A
A= ($self->_ports)[$number]
Aresult
11
00
100 0 $self->close_port;
101 0 my $mine = $self->{alsa}->create_port($name, $self->_direction);
102 0 my $address = $self->{alsa}->client . ":$mine";
103 0 ▶ my ($sender, $dest) = $self->_direction eq 'in' ? ($port->{address}, $address) : ($address, $port->{address});
Branch: 1 branch
BranchTrueFalseTotal
$self->_direction eq 'in' ? : 0 0 0
104 0 $self->{alsa}->subscribe($sender, $dest);
105 0 @$self{qw(port port_name connected_to connected_to_name subscription)} = ($mine, $name, $number, _port_name($port), [ $sender, $dest ]);
106 0 return !!1;
107 }
108  
109 0 ▶sub open_port_by_name ($self, $which, $name = Time::HiRes::time()) {
Subroutine open_port_by_name: not called CC=3 SCAR=24.8
Pod documented
110 0 my ($number) = $self->get_ports_by_name($which);
111 0 ▶ croak 'No available device found matching supplied criteria' if !defined $number;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $number 0 0 0
112 0 return $self->open_port($number, $name);
113 }
114  
115 # A port of this device's own that others connect to.
116 0 ▶sub open_virtual_port ($self, $name) {
Subroutine open_virtual_port: not called CC=1 SCAR=6.9
Pod documented
117 0 $self->close_port;
118 0 @$self{qw(port port_name)} = ($self->{alsa}->create_port($name, $self->_direction), $name);
119 0 return !!1;
120 }
121  
122 0 ▶sub close_port ($self) {
Subroutine close_port: not called CC=3 SCAR=24.8
Pod documented
123 0 ▶ return if !defined $self->{port};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"port"} 0 0 0
124 0 ▶ eval { $self->{alsa}->unsubscribe(@{ $self->{subscription} }); 1 } if $self->{subscription};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"subscription"} 0 0 0
125 0 eval { $self->{alsa}->delete_port($self->{port}); 1 };
126 0 delete @$self{qw(port subscription connected_to connected_to_name)};
127 0 return !!1;
128 }
129  
130 1 ▶sub DESTROY ($self) { $self->close_port if $self->{alsa} }
Subroutine DESTROY: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"alsa"} 0 1 1
131  
132 1 1;
133  
134 __END__
« lib/Peta/Synth/Engine.pm ↑ Summary lib/Peta/MIDI/Decoder.pm »