| 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.0808'; | ||
| 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 | 0 | ▶ | 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 | has _midi_routine => ( | ||
| 69 | is => 'rw', | ||
| 70 | ); | ||
| 71 | |||
| 72 | |||
| 73 | has silent => ( | ||
| 74 | is => 'ro', | ||
| 75 | default => 0, | ||
| 76 | ); | ||
| 77 | |||
| 78 | |||
| 79 | sub BUILD { | ||
| 80 | my ($self, $args) = @_; | ||
| 81 | |||
| 82 | my $midi_rtn = IO::Async::Routine->new( | ||
| 83 | channels_in => [ $self->_msg_channel ], | ||
| 84 | channels_out => [ $self->_midi_channel ], | ||
| 85 | model => 'spawn', | ||
| 86 | module => __PACKAGE__, | ||
| 87 | func => '_rtmidi_loop', | ||
| 88 | ); | ||
| 89 | $self->loop->add($midi_rtn); | ||
| 90 | $self->_midi_routine($midi_rtn); | ||
| 91 | $self->_midi_channel->configure( | ||
| 92 | on_recv => sub ($channel, $event) { | ||
| 93 | my $dt = shift @$event; | ||
| 94 | my $ev = shift @$event; | ||
| 95 | my $port = shift @$event; | ||
| 96 | print "Delta time: $dt, MIDI port: $port\n" if $self->verbose; | ||
| 97 | $self->_filter_and_forward($port, $dt, $ev); | ||
| 98 | } | ||
| 99 | ); | ||
| 100 | |||
| 101 | my $input_name = $self->input; | ||
| 102 | $self->_msg_channel->send(\$input_name); | ||
| 103 | |||
| 104 | unless ($args->{midi_out}) { | ||
| 105 | $self->midi_out->open_virtual_port('RtController'); | ||
| 106 | # use Data::Dumper::Compact 'ddc'; | ||
| 107 | # print ddc $self->midi_out; | ||
| 108 | |||
| 109 | _log(sprintf 'Opening out port %s...', $self->output) | ||
| 110 | if $self->verbose; | ||
| 111 | _open_port($self->midi_out, $self->output); | ||
| 112 | _log(sprintf 'Opened out port %s', $self->output) | ||
| 113 | if $self->verbose; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | sub _log { | ||
| 118 | print join("\n", @_), "\n"; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub _open_port($device, $name) { | ||
| 122 | $device->open_port_by_name(qr/\Q$name/i) | ||
| 123 | || croak "Failed to open port $name"; | ||
| 124 | return $name; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub _rtmidi_loop ($msg_ch, $midi_ch) { | ||
| 128 | my $midi_in = MIDI::RtMidi::FFI::Device->new(type => 'in'); | ||
| 129 | my $name = _open_port($midi_in, ${ $msg_ch->recv }); | ||
| 130 | $midi_in->set_callback_decoded( | ||
| 131 | sub { | ||
| 132 | my (@event) = @_; | ||
| 133 | my @e = $event[0] eq 'clock' || $event[0] eq 'start' || $event[0] eq 'stop' | ||
| 134 | ? ($event[0], 0) : @event[0, 2]; | ||
| 135 | $midi_ch->send([ @e, $name ]) | ||
| 136 | } | ||
| 137 | ); # delta-time, event, midi port | ||
| 138 | sleep; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub _filter_and_forward ($self, $port, $dt, $event) { | ||
| 142 | my $event_filters = $self->filters->{all} // []; | ||
| 143 | push @$event_filters, @{ $self->filters->{ $event->[0] } // [] }; | ||
| 144 | |||
| 145 | for my $filter (@$event_filters) { | ||
| 146 | return if $filter->($port, $dt, $event); | ||
| 147 | } | ||
| 148 | |||
| 149 | $self->send_it($event) unless $self->silent; | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | sub add_filter ($self, $name, $event_type, $action) { | ||
| 154 | if ( ref $event_type eq 'ARRAY' ) { | ||
| 155 | $self->add_filter( $name, $_, $action ) for @$event_type; | ||
| 156 | return; | ||
| 157 | } | ||
| 158 | _log("Add $name filter for $event_type") | ||
| 159 | if $self->verbose; | ||
| 160 | push @{ $self->filters->{$event_type} }, $action; | ||
| 161 | } | ||
| 162 | |||
| 163 | |||
| 164 | sub send_it ($self, $event) { | ||
| 165 | _log("Event: @$event") if $self->verbose; | ||
| 166 | $self->midi_out->send_event(@$event); | ||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | sub delay_send ($self, $delay_time, $event) { | ||
| 171 | $self->loop->add( | ||
| 172 | IO::Async::Timer::Countdown->new( | ||
| 173 | delay => $delay_time, | ||
| 174 | on_expire => sub { $self->send_it($event) } | ||
| 175 | )->start | ||
| 176 | ) | ||
| 177 | } | ||
| 178 | |||
| 179 | |||
| 180 | sub run ($self) { | ||
| 181 | $self->loop->run; | ||
| 182 | } | ||
| 183 | |||
| 184 | |||
| 185 | sub stop ($self) { | ||
| 186 | my $rtn = $self->_midi_routine or return; | ||
| 187 | $rtn->kill('TERM'); | ||
| 188 | $self->loop->remove($rtn); | ||
| 189 | $self->_midi_routine(undef); | ||
| 190 | $self->midi_out->close_port() if $self->midi_out; | ||
| 191 | } | ||
| 192 | |||
| 193 | |||
| 194 | sub open_controllers ($inputs, $output, $verbose) { | ||
| 195 | my %controllers; | ||
| 196 | my $name = $inputs->[0]; | ||
| 197 | my $control = __PACKAGE__->new( | ||
| 198 | input => $name, | ||
| 199 | output => $output, | ||
| 200 | verbose => $verbose, | ||
| 201 | ); | ||
| 202 | $controllers{$name} = $control; | ||
| 203 | for my $i (@$inputs[1 .. $#$inputs]) { | ||
| 204 | $controllers{$i} = __PACKAGE__->new( | ||
| 205 | input => $i, | ||
| 206 | loop => $control->loop, | ||
| 207 | midi_out => $control->midi_out, | ||
| 208 | verbose => 1, | ||
| 209 | ); | ||
| 210 | } | ||
| 211 | return \%controllers; | ||
| 212 | } | ||
| 213 | |||
| 214 | 1; | ||
| 215 | |||
| 216 | __END__ | ||