| 1 | package Peta::Synth::Input::Raw; | ||
| 2 | # ABSTRACT: MIDI bytes from a device or a pipe | ||
| 3 | |||
| 4 | # MIDI read as bytes from a path: a raw MIDI device (/dev/snd/midiC*D*, | ||
| 5 | # one per hardware port), a named pipe, a file. The same interface as | ||
| 6 | # Peta::Synth::Input. | ||
| 7 | |||
| 8 | use v5.36; | ||
| 9 | use Peta::Synth::MIDI::Stream; | ||
| 10 | |||
| 11 | our $VERSION = '0.2610180'; | ||
| 12 | |||
| 13 | sub new ($class, $path) { | ||
| 14 | open my $fh, '<:raw', $path or die "MIDI from '$path': $!\n"; | ||
| 15 | return bless { fh => $fh, path => $path, stream => Peta::Synth::MIDI::Stream->new }, $class; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub handle ($self) { return $self->{fh} } | ||
| 19 | sub address ($self) { return $self->{path} } | ||
| 20 | sub connected ($self) { return $self->{path} } | ||
| 21 | |||
| 22 | # What has come in; waits at most $wait seconds for something (0: not at all). | ||
| 23 | sub events ($self, $wait = 0) { | ||
| 24 | my @events; | ||
| 25 | my $bits = ''; | ||
| 26 | vec($bits, fileno $self->{fh}, 1) = 1; | ||
| 27 | while (select(my $ready = $bits, undef, undef, $wait)) { | ||
| 28 | $wait = 0; | ||
| 29 | my $got = sysread $self->{fh}, my $buf, 4096; | ||
| 30 | last if !$got; | ||
| 31 | |||
| 32 | push @events, $self->{stream}->feed($buf); | ||
| 33 | } | ||
| 34 | return @events; | ||
| 35 | } | ||
| 36 | |||
| 37 | 1; | ||
| 38 | |||
| 39 | __END__ |