| 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"
MC/DC0%0/1
$spec //= "all"
$spec
Truth table50%1/2
A
A= $spec
| |||||||||||||||||||||
| 27 | 1 | ▶ | if ($spec =~ m{\A/}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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"
MC/DC0%0/1
$arg{"name"} // "Peta::Synth"
$arg{"name"}
Truth table50%1/2
A
A= $arg{"name"}
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 65 | 0 | ▶ | if (!defined $address) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 90 | 0 | delete $self->{connected}{$address}; | |||||||||||||||||||
| 91 | 0 | return; | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | 0 | ▶ | return if !$self->{follow}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 94 | 0 | ▶ | eval { $self->connect($address); 1 } if grep { $_->{address} eq $address } $self->sources; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
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
| |||||||||||||||||||||
| 104 | 0 | } $self->{alsa}->read($wait); | |||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 1 | 1; | |||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | __END__ | ||||||||||||||||||||