| 1 | package Peta::Synth::MIDI::Stream; | ||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: MIDI bytes as a device sends them, into events | ||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||
| 4 | # A stream of MIDI bytes, as a device sends them, into events: running | ||||||||||||||||||||||||||||||||||
| 5 | # status, system exclusive between F0 and F7, real-time bytes (clock, | ||||||||||||||||||||||||||||||||||
| 6 | # active sensing) anywhere and ignored but System Reset (FF, a reset as GM | ||||||||||||||||||||||||||||||||||
| 7 | # System On is one), system common messages skipped. | ||||||||||||||||||||||||||||||||||
| 8 | # Bytes may come in any pieces; a message cut between two is completed by | ||||||||||||||||||||||||||||||||||
| 9 | # the next. | ||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use v5.36; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Peta::Synth::MIDI; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | 1 | our $VERSION = '0.2610180'; | |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | sub new ($class) { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 17 | 1 | return bless { | |||||||||||||||||||||||||||||||||
| 18 | status => 0, # the running status | ||||||||||||||||||||||||||||||||||
| 19 | data => '', # its data bytes so far | ||||||||||||||||||||||||||||||||||
| 20 | need => 0, # and how many it takes | ||||||||||||||||||||||||||||||||||
| 21 | skip => 0, # data bytes of a system common message still to come | ||||||||||||||||||||||||||||||||||
| 22 | sysex => undef, # a system exclusive message so far, while in one | ||||||||||||||||||||||||||||||||||
| 23 | }, $class; | ||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||
| 26 | # The events the bytes complete. | ||||||||||||||||||||||||||||||||||
| 27 | 6 | ▶ | sub feed ($self, $bytes) { | ||||||||||||||||||||||||||||||||
|
Subroutine
feed: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 28 | 6 | my @events; | |||||||||||||||||||||||||||||||||
| 29 | 6 | for my $byte (unpack 'C*', $bytes) { | |||||||||||||||||||||||||||||||||
| 30 | 21 | ▶ | if ($byte >= 0xf8) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 31 | # Real time: ignored, but a reset. | ||||||||||||||||||||||||||||||||||
| 32 | 1 | ▶ | push @events, { type => 'sysex', data => "\x7e\x7f\x09\x01\xf7" } if $byte == 0xff; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 33 | 1 | next; | |||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||
| 36 | 20 | ▶ | if ($byte == 0xf0) { | ||||||||||||||||||||||||||||||||
|
Branch: 7 branches
| |||||||||||||||||||||||||||||||||||
| 37 | # System exclusive begins; it ends running status. | ||||||||||||||||||||||||||||||||||
| 38 | 1 | @$self{qw(sysex status skip)} = ('', 0, 0); | |||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||
| 40 | elsif ($byte == 0xf7) { | ||||||||||||||||||||||||||||||||||
| 41 | 1 | ▶ | push @events, Peta::Synth::MIDI::message("\xf0$self->{sysex}\xf7") if defined $self->{sysex}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 42 | 1 | $self->{sysex} = undef; | |||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||
| 44 | elsif ($byte >= 0xf1) { | ||||||||||||||||||||||||||||||||||
| 45 | # System common: its data bytes are skipped. | ||||||||||||||||||||||||||||||||||
| 46 | 1 | @$self{qw(sysex status skip)} = (undef, 0, Peta::Synth::MIDI::data_bytes($byte)); | |||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||
| 48 | elsif ($byte >= 0x80) { | ||||||||||||||||||||||||||||||||||
| 49 | # A channel message's status. | ||||||||||||||||||||||||||||||||||
| 50 | 3 | @$self{qw(sysex status data skip)} = (undef, $byte, '', 0); | |||||||||||||||||||||||||||||||||
| 51 | 3 | $self->{need} = Peta::Synth::MIDI::data_bytes($byte); | |||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||
| 53 | 4 | elsif (defined $self->{sysex}) { $self->{sysex} .= chr $byte } | |||||||||||||||||||||||||||||||||
| 54 | 2 | elsif ($self->{skip}) { $self->{skip}-- } | |||||||||||||||||||||||||||||||||
| 55 | elsif ($self->{status}) { | ||||||||||||||||||||||||||||||||||
| 56 | 7 | $self->{data} .= chr $byte; | |||||||||||||||||||||||||||||||||
| 57 | 7 | ▶ | next if length $self->{data} < $self->{need}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 58 | 4 | my $event = Peta::Synth::MIDI::message(chr($self->{status}) . $self->{data}); | |||||||||||||||||||||||||||||||||
| 59 | 4 | ▶ | push @events, $event if $event; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 60 | 4 | $self->{data} = ''; # running status: the next data bytes are a message of the same status | |||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||
| 63 | 6 | return @events; | |||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||
| 66 | 1 | 1; | |||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||
| 68 | __END__ | ||||||||||||||||||||||||||||||||||