| 1 | package Peta::MIDI::Device::In; | ||
| 2 | # ABSTRACT: a MIDI input as MIDI::RtMidi::FFI::Device::In has it | ||
| 3 | |||
| 4 | # A MIDI input as MIDI::RtMidi::FFI::Device::In has it: messages come in | ||
| 5 | # whole (system exclusive too), each with the seconds since the one before; | ||
| 6 | # system exclusive, timing (clock, MIDI time code) and active sensing are | ||
| 7 | # ignored unless told otherwise. They are queued (get_message), handed to a | ||
| 8 | # callback, or written to a handle (get_fh) as they come. | ||
| 9 | # | ||
| 10 | # RtMidi calls the callback from a thread of its own. Here a callback runs | ||
| 11 | # whenever the program's event loop does (AnyEvent's, and through it EV, | ||
| 12 | # Event, IO::Async, Coro: a program that waits in Coro sees its callbacks | ||
| 13 | # run meanwhile), or while the program waits in dispatch(). | ||
| 14 | |||
| 15 | use v5.36; | ||
| 16 | use parent 'Peta::MIDI::Device'; | ||
| 17 | use Carp qw(croak); | ||
| 18 | use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK); | ||
| 19 | use Scalar::Util qw(weaken); | ||
| 20 | use Time::HiRes qw(time); | ||
| 21 | use Peta::MIDI::Decoder; | ||
| 22 | |||
| 23 | our $VERSION = '0.2610180'; | ||
| 24 | |||
| 25 | sub _direction ($self) { return 'in' } | ||
| 26 | |||
| 27 | sub _build ($self, %arg) { | ||
| 28 | $self->{ignore} = [ map { $arg{$_} // 1 } qw(ignore_sysex ignore_timing ignore_sensing) ]; | ||
| 29 | $self->{remap_event_names} = $arg{remap_event_names} // 1; | ||
| 30 | $self->{bufsize} = $arg{bufsize} // $arg{queue_size_limit} // 4096; | ||
| 31 | $self->{decoder} = Peta::MIDI::Decoder->new( | ||
| 32 | enable_14bit_cc => $arg{enable_14bit_cc} // 0, | ||
| 33 | retain_events => $arg{retain_events} // 1, | ||
| 34 | ); | ||
| 35 | $self->{framer} = Peta::MIDI::Decoder->new; # the bytes as they come into whole messages | ||
| 36 | $self->{queue} = []; | ||
| 37 | return; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub bufsize ($self) { return $self->{bufsize} } | ||
| 41 | sub decoder ($self) { return $self->{decoder} } | ||
| 42 | |||
| 43 | # What is ignored: system exclusive, timing (clock, MIDI time code), active sensing. | ||
| 44 | sub ignore_types ($self, $sysex, $timing, $sensing) { | ||
| 45 | $self->{ignore} = [ $sysex, $timing, $sensing ]; | ||
| 46 | return; | ||
| 47 | } | ||
| 48 | sub ignore_sysex ($self, $on) { return $self->ignore_types($on, @{ $self->{ignore} }[ 1, 2 ]) } | ||
| 49 | sub ignore_timing ($self, $on) { return $self->ignore_types($self->{ignore}[0], $on, $self->{ignore}[2]) } | ||
| 50 | sub ignore_sensing ($self, $on) { return $self->ignore_types(@{ $self->{ignore} }[ 0, 1 ], $on) } | ||
| 51 | |||
| 52 | # $callback->($dt, $message) for each message as it comes. | ||
| 53 | sub set_callback ($self, $callback) { | ||
| 54 | $self->cancel_callback; | ||
| 55 | $self->{callback} = $callback; | ||
| 56 | $self->_watch; | ||
| 57 | return; | ||
| 58 | } | ||
| 59 | |||
| 60 | # $callback->($dt, $message, $event): the event decoded ([ name, fields ... ]). | ||
| 61 | sub set_callback_decoded ($self, $callback) { | ||
| 62 | return $self->set_callback(sub ($dt, $message) { | ||
| 63 | my $event = $self->decode_message($message) // return; | ||
| 64 | $callback->($dt, $message, $event); | ||
| 65 | }); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub cancel_callback ($self) { | ||
| 69 | delete @$self{qw(callback watcher fh writer)}; | ||
| 70 | return; | ||
| 71 | } | ||
| 72 | |||
| 73 | # A non-blocking handle the messages' bytes are written to as they come; | ||
| 74 | # needs an event loop (AnyEvent) to pump it. | ||
| 75 | sub get_fh ($self) { | ||
| 76 | $self->cancel_callback; | ||
| 77 | eval { require AnyEvent; 1 } or croak 'get_fh needs AnyEvent, to pump the handle as messages come'; | ||
| 78 | pipe(my $reader, my $writer) or croak "a pipe: $!"; | ||
| 79 | for my $fh ($reader, $writer) { | ||
| 80 | binmode $fh; | ||
| 81 | fcntl($fh, F_SETFL, fcntl($fh, F_GETFL, 0) | O_NONBLOCK); | ||
| 82 | } | ||
| 83 | @$self{qw(fh writer)} = ($reader, $writer); | ||
| 84 | $self->_watch; | ||
| 85 | return $reader; | ||
| 86 | } | ||
| 87 | |||
| 88 | # The sequencer's handle watched in the event loop, if there is AnyEvent. | ||
| 89 | sub _watch ($self) { | ||
| 90 | return if !eval { require AnyEvent; 1 }; | ||
| 91 | my $weak = $self; | ||
| 92 | weaken $weak; | ||
| 93 | $self->{watcher} = AnyEvent->io(fh => $self->{alsa}->handle, poll => 'r', cb => sub { $weak->_pump(0) if $weak }); | ||
| 94 | return; | ||
| 95 | } | ||
| 96 | |||
| 97 | # Waits up to $seconds for messages and hands them out (to the callback, | ||
| 98 | # the handle or the queue) as they come; how many there were. For a | ||
| 99 | # program without an event loop. | ||
| 100 | sub dispatch ($self, $seconds = 0) { | ||
| 101 | my $until = time + $seconds; | ||
| 102 | my $count = 0; | ||
| 103 | while (1) { | ||
| 104 | $count += $self->_pump($until - time > 0 ? $until - time : 0); | ||
| 105 | last if time >= $until; | ||
| 106 | } | ||
| 107 | return $count; | ||
| 108 | } | ||
| 109 | |||
| 110 | # What has come, as whole messages, handed out; how many. | ||
| 111 | sub _pump ($self, $wait) { | ||
| 112 | my $framer = $self->{framer}; | ||
| 113 | for my $message ($self->{alsa}->read($wait)) { | ||
| 114 | my ($bytes, undef, $port) = @$message; | ||
| 115 | $framer->decode($bytes) if defined $self->{port} && $port == $self->{port}; | ||
| 116 | } | ||
| 117 | my $count = 0; | ||
| 118 | for my $event ($framer->events) { | ||
| 119 | my $status = $event->status; | ||
| 120 | next if $status == 0xf0 && $self->{ignore}[0]; | ||
| 121 | next if ($status == 0xf8 || $status == 0xf1) && $self->{ignore}[1]; | ||
| 122 | next if $status == 0xfe && $self->{ignore}[2]; | ||
| 123 | |||
| 124 | my $now = time; | ||
| 125 | my $dt = defined $self->{last} ? $now - $self->{last} : 0; | ||
| 126 | $self->{last} = $now; | ||
| 127 | $count++; | ||
| 128 | if ($self->{callback}) { $self->{callback}->($dt, $event->bytes) } | ||
| 129 | elsif ($self->{writer}) { syswrite $self->{writer}, $event->bytes } | ||
| 130 | else { | ||
| 131 | push @{ $self->{queue} }, $event->bytes; | ||
| 132 | shift @{ $self->{queue} } while @{ $self->{queue} } > $self->{bufsize}; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | return $count; | ||
| 136 | } | ||
| 137 | |||
| 138 | # The next message (its bytes), or undef. | ||
| 139 | sub get_message ($self) { | ||
| 140 | $self->_pump(0) if !@{ $self->{queue} }; | ||
| 141 | return shift @{ $self->{queue} }; | ||
| 142 | } | ||
| 143 | |||
| 144 | sub get_message_decoded ($self) { | ||
| 145 | my $message = $self->get_message // return undef; | ||
| 146 | return $self->decode_message($message); | ||
| 147 | } | ||
| 148 | sub get_event ($self) { return $self->get_message_decoded } | ||
| 149 | |||
| 150 | # A message's event, [ name, fields ... ], MIDI::Event's names unless told | ||
| 151 | # otherwise (remap_event_names); undef where the message is not whole (or | ||
| 152 | # is a coarse controller part kept back, with enable_14bit_cc). | ||
| 153 | sub decode_message ($self, $message) { | ||
| 154 | my $decoder = $self->{decoder}; | ||
| 155 | return undef if !$decoder->decode($message); | ||
| 156 | my $event = $decoder->fetch_one_event->as_arrayref; | ||
| 157 | $event->[0] = $self->name_to_midi_event($event->[0]) if $self->{remap_event_names}; | ||
| 158 | return $event; | ||
| 159 | } | ||
| 160 | sub decode ($self, $message) { return $self->decode_message($message) } | ||
| 161 | |||
| 162 | 1; | ||
| 163 | |||
| 164 | __END__ |