blib/lib/MIDI/RtController.pm

filter: Statementstmt 92.3% 24 / 26 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 94.2% 33 / 35 CC 1 SCAR 0.3
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package MIDI::RtController;
2 our $AUTHORITY = 'cpan:GENE';
3  
4 # ABSTRACT: Control your MIDI controller
5  
6 2 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 our $VERSION = '0.0804';
9  
10 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use IO::Async::Channel ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use IO::Async::Loop ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use IO::Async::Routine ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use IO::Async::Timer::Countdown ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use MIDI::RtMidi::FFI::Device ();
Subroutine BEGIN: called CC=1 SCAR=2.6
18 use namespace::clean;
19  
20  
21 has verbose => (
22 is => 'lazy',
23 );
24 sub _build_verbose {
25 my ($self) = @_;
26 return $ENV{PERL_FUTURE_DEBUG} ? 1 : 0;
27 }
28  
29  
30 has input => (
31 is => 'ro',
32 required => 1,
33 );
34  
35  
36 has output => (
37 is => 'ro',
38 );
39  
40  
41 has loop => (
42 is => 'ro',
43 default => sub { IO::Async::Loop->new },
44 );
45  
46  
47 has filters => (
48 is => 'rw',
49 default => sub { {} },
50 );
51  
52 has _msg_channel => (
53 is => 'ro',
54 default => sub { IO::Async::Channel->new },
55 );
56  
57 has _midi_channel => (
58 is => 'ro',
59 default => sub { IO::Async::Channel->new },
60 );
61  
62  
63 has midi_out => (
64 is => 'ro',
65 default => sub { RtMidiOut->new },
66 );
67  
68  
69 sub BUILD {
70 my ($self, $args) = @_;
71  
72 my $midi_rtn = IO::Async::Routine->new(
73 channels_in => [ $self->_msg_channel ],
74 channels_out => [ $self->_midi_channel ],
75 model => 'spawn',
76 module => __PACKAGE__,
77 func => '_rtmidi_loop',
78 );
79 $self->loop->add($midi_rtn);
80 $self->_midi_channel->configure(
81 on_recv => sub ($channel, $event) {
82 my $dt = shift @$event;
83 my $ev = shift @$event;
84 my $port = shift @$event;
85 print "Delta time: $dt, MIDI port: $port\n" if $self->verbose;
86 $self->_filter_and_forward($port, $dt, $ev);
87 }
88 );
89  
90 my $input_name = $self->input;
91 $self->_msg_channel->send(\$input_name);
92  
93 unless ($args->{midi_out}) {
94 $self->midi_out->open_virtual_port('RtController');
95 # use Data::Dumper::Compact 'ddc';
96 # print ddc $self->midi_out;
97  
98 _log(sprintf 'Opening out port %s...', $self->output)
99 if $self->verbose;
100 _open_port($self->midi_out, $self->output);
101 _log(sprintf 'Opened out port %s', $self->output)
102 if $self->verbose;
103 }
104 }
105  
106 sub _log {
107 print join("\n", @_), "\n";
108 }
109  
110 sub _open_port($device, $name) {
111 $device->open_port_by_name(qr/\Q$name/i)
112 || croak "Failed to open port $name";
113 return $name;
114 }
115  
116 sub _rtmidi_loop ($msg_ch, $midi_ch) {
117 my $midi_in = MIDI::RtMidi::FFI::Device->new(type => 'in');
118 my $name = _open_port($midi_in, ${ $msg_ch->recv });
119 $midi_in->set_callback_decoded(
120 sub {
121 my (@event) = @_;
122 my @e = $event[0] eq 'clock' || $event[0] eq 'start' || $event[0] eq 'stop'
123 ? ($event[0], 0) : @event[0, 2];
124 $midi_ch->send([ @e, $name ])
125 }
126 ); # delta-time, event, midi port
127 sleep;
128 }
129  
130 sub _filter_and_forward ($self, $port, $dt, $event) {
131 my $event_filters = $self->filters->{all} // [];
132 push @$event_filters, @{ $self->filters->{ $event->[0] } // [] };
133  
134 for my $filter (@$event_filters) {
135 return if $filter->($port, $dt, $event);
136 }
137  
138 $self->send_it($event);
139 }
140  
141  
142 sub add_filter ($self, $name, $event_type, $action) {
143 if ( ref $event_type eq 'ARRAY' ) {
144 $self->add_filter( $name, $_, $action ) for @$event_type;
145 return;
146 }
147 _log("Add $name filter for $event_type")
148 if $self->verbose;
149 push @{ $self->filters->{$event_type} }, $action;
150 }
151  
152  
153 sub send_it ($self, $event) {
154 _log("Event: @$event") if $self->verbose;
155 $self->midi_out->send_event(@$event);
156 }
157  
158  
159 sub delay_send ($self, $delay_time, $event) {
160 $self->loop->add(
161 IO::Async::Timer::Countdown->new(
162 delay => $delay_time,
163 on_expire => sub { $self->send_it($event) }
164 )->start
165 )
166 }
167  
168  
169 sub run ($self) {
170 $self->loop->run;
171 }
172  
173  
174 sub open_controllers ($inputs, $output, $verbose) {
175 my %controllers;
176 my $name = $inputs->[0];
177 my $control = __PACKAGE__->new(
178 input => $name,
179 output => $output,
180 verbose => $verbose,
181 );
182 $controllers{$name} = $control;
183 for my $i (@$inputs[1 .. $#$inputs]) {
184 $controllers{$i} = __PACKAGE__->new(
185 input => $i,
186 loop => $control->loop,
187 midi_out => $control->midi_out,
188 verbose => 1,
189 );
190 }
191 return \%controllers;
192 }
193  
194 1;
195  
196 __END__
↑ Summary