| 1 | package Peta::MIDI::Encoder; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: events into MIDI bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # Events into MIDI bytes: an event is [ name, fields ... ] (as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | # Peta::MIDI::Event has them, MIDI::Event's names too), { name => ..., | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # field => ... }, or a Peta::MIDI::Event. A channel message given more | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # values than one message holds is several: control_change(0, 7, 100, 10, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # 64) is two. Pitch bends are -8192 .. 8191; a system exclusive message's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # msg is its bytes (a string, or a list of numbers), F0 and F7 added. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # enable_running_status leaves out a channel message's status byte when it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # is the last one sent (and sends it again every running_status_retransmit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # messages; real-time messages leave running status as it is, others end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # it); a note off of velocity 0 is then sent as a note on, which keeps it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # enable_14bit_cc sends controllers 0 .. 31 as values of 14 bits: the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # coarse part when it changes, the fine part (the controller plus 32) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # always. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use v5.36; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use Carp qw(carp); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use Peta::MIDI::Event qw(status_byte keys_for message_length split_bytes); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 2 | our $VERSION = '0.2610180'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 4 | ▶ | sub new ($class, %arg) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | return bless { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | enable_14bit_cc => $arg{enable_14bit_cc} // 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | enable_running_status => $arg{enable_running_status} // 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | running_status_retransmit => $arg{running_status_retransmit} // 10, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 4 | ▶ | concat => $arg{concat} // 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition75%6/8
$arg{"enable_14bit_cc"} // 0
$arg{"enable_running_status"} // 0
$arg{"running_status_retransmit"} // 10
$arg{"concat"} // 0
MC/DC100%1/1
$arg{"enable_14bit_cc"} // 0
$arg{"enable_14bit_cc"}
MC/DC100%1/1
$arg{"enable_running_status"} // 0
$arg{"enable_running_status"}
MC/DC0%0/1
$arg{"running_status_retransmit"} // 10
$arg{"running_status_retransmit"}
MC/DC0%0/1
$arg{"concat"} // 0
$arg{"concat"}
Truth table100%2/2
A
A= $arg{"enable_14bit_cc"}
Truth table100%2/2
A
A= $arg{"enable_running_status"}
Truth table50%1/2
A
A= $arg{"running_status_retransmit"}
Truth table50%1/2
A
A= $arg{"concat"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | running => 0, # the status last sent, for running status | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | repeats => 0, # how many times it has run on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | coarse => [], # controllers' coarse parts last sent, by channel | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | # An event's bytes: a message each (one string, with concat). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 32 | ▶ | sub encode ($self, $event) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode: called CC=12 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 32 | my @event = _fields($event); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 32 | my $name = Peta::MIDI::Event::name_from_midi_event(shift @event); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 32 | my $status = status_byte($name); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 32 | ▶ | if (!defined $status) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | carp "an unknown MIDI event: $name"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 32 | my @messages; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 32 | ▶ | if ($name eq 'sysex') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 3 | ▶ | my $msg = ref $event[0] eq 'ARRAY' ? pack('C*', @{ $event[0] }) : $event[0] // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$event[0] // ""
MC/DC0%0/1
$event[0] // ""
$event[0]
Truth table50%1/2
A
A= $event[0]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 3 | $msg =~ s/\A\xf0//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 3 | ▶ | $msg .= "\xf7" if $msg !~ /\xf7\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 3 | $self->{running} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 3 | @messages = ("\xf0$msg"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | elsif ($status < 0xf0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 17 | my $channel = shift(@event) & 0x0f; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 17 | my $data = message_length($status) - 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 17 | ▶ | @event = map { split_bytes($_ + 8192) } @event if $name eq 'pitch_bend'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 17 | ▶ | while (@event) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 18 | my @values = splice @event, 0, $data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 18 | push @messages, $self->_channel_message($status, $channel, @values); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 12 | ▶ | @event = split_bytes($event[0]) if $name eq 'song_position'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 12 | ▶ | $self->{running} = 0 if $status < 0xf8; # real time leaves running status as it is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 12 | @messages = (pack 'C*', $status, @event[ 0 .. message_length($status) - 2 ]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 32 | ▶ | return $self->{concat} ? join('', @messages) : @messages; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # An event's name and fields, however it is given. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 32 | ▶ | sub _fields ($event) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fields: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | return @{ $event->as_arrayref } if eval { $event->isa('Peta::MIDI::Event') }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 32 | ▶ | return @$event if ref $event eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 1 | my $keys = keys_for(Peta::MIDI::Event::name_from_midi_event($event->{name})); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | return ($event->{name}, @$event{@$keys}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | # A channel message (two, for a controller of 14 bits). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 18 | ▶ | sub _channel_message ($self, $status, $channel, @values) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_channel_message: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 18 | ▶ | if ($self->{enable_14bit_cc} && $status == 0xb0 && $values[0] < 0x20) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$self->{"enable_14bit_cc"} and $status == 176 and $values[0] < 32
$self->{"enable_14bit_cc"} and $status == 176
MC/DC33%1/3
$self->{"enable_14bit_cc"} and $status == 176 and $values[0] < 32
$self->{"enable_14bit_cc"}
$status == 176
$values[0] < 32
Truth table50%2/4
A and B and C
A= $self->{"enable_14bit_cc"}B= $status == 176C= $values[0] < 32
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 2 | my ($number, $value) = @values; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 2 | my ($fine, $coarse) = split_bytes($value); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 2 | ▶ | my $sent = $self->{coarse}[$channel] //= []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"coarse"}[$channel] //= []
MC/DC100%1/1
$self->{"coarse"}[$channel] //= []
$self->{"coarse"}[$channel]
Truth table100%2/2
A
A= $self->{"coarse"}[$channel]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 2 | my @parts = ([ $number | 0x20, $fine ]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 2 | ▶ | unshift @parts, [ $number, $coarse ] if ($sent->[$number] // -1) != $coarse; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$sent->[$number] // -1
MC/DC100%1/1
$sent->[$number] // -1
$sent->[$number]
Truth table100%2/2
A
A= $sent->[$number]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 2 | $sent->[$number] = $coarse; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 2 | return map { $self->_bytes($status | $channel, @$_) } @parts; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 16 | ▶ | $status = 0x90 if $self->{enable_running_status} && $status == 0x80 && !$values[1] && ($self->{running} & 0xf0) != 0x80; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition88%8/9
$self->{"enable_running_status"} and $status == 128 and not $values[1] and ($self->{"running"} & 240) != 128
$self->{"enable_running_status"} and $status == 128 and not $values[1]
$self->{"enable_running_status"} and $status == 128
MC/DC75%3/4
$self->{"enable_running_status"} and $status == 128 and not $values[1] and ($self->{"running"} & 240) != 128
$self->{"enable_running_status"}
$status == 128
not $values[1]
($self->{"running"} & 240) != 128
Truth table80%4/5
A and B and not(C) and D
A= $self->{"enable_running_status"}B= $status == 128C= not $values[1]D= ($self->{"running"} & 240) != 128
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 16 | return $self->_bytes($status | $channel, @values); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 19 | ▶ | sub _bytes ($self, $status, @data) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_bytes: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 19 | ▶ | if ($self->{enable_running_status}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 11 | ▶ | if ($status == $self->{running} && $self->{repeats}++ < $self->{running_status_retransmit}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$status == $self->{"running"} and $self->{"repeats"}++ < $self->{"running_status_retransmit"}
MC/DC50%1/2
$status == $self->{"running"} and $self->{"repeats"}++ < $self->{"running_status_retransmit"}
$status == $self->{"running"}
$self->{"repeats"}++ < $self->{"running_status_retransmit"}
Truth table66%2/3
A and B
A= $status == $self->{"running"}B= $self->{"repeats"}++ < $self->{"running_status_retransmit"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 2 | return pack 'C*', @data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 9 | @$self{qw(running repeats)} = ($status, 0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 17 | return pack 'C*', $status, @data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | # Several events' bytes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 2 | ▶ | sub encode_events ($self, @events) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_events: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 2 | my @bytes = map { $self->encode($_) } @events; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 2 | ▶ | return $self->{concat} ? join('', @bytes) : @bytes; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub clear_running_status ($self) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clear_running_status: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | @$self{qw(running repeats)} = (0, 0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||