lib/Peta/MIDI/Encoder.pm

filter: Statementstmt 92.0% 58 / 63 Branchbran 88.8% 32 / 36 Conditioncond 78.1% 25 / 32 MC/DCmcdc 56.2% 9 / 16 Subroutinesub 90.0% 9 / 10 Podpod 100% 4 / 4 totaltotal 85.0% 137 / 161 CC 32 SCAR 20.8
CC
32
Cov
88%
encode
12.2
_channel_message
11.1
new
5.3
CRAP 8.0
« lib/Peta/Vorbis/Encode.pm ↑ Summary lib/Peta/Vorbis/Encode/Book.pm »
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
l!l
13
$arg{"enable_running_status"} // 0
l!l
22
$arg{"running_status_retransmit"} // 10
l!l
04
$arg{"concat"} // 0
l!l
04
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"}
Aresult
11
00
Truth table100%2/2
A
A= $arg{"enable_running_status"}
Aresult
11
00
Truth table50%1/2
A
A= $arg{"running_status_retransmit"}
Aresult
11
00
Truth table50%1/2
A
A= $arg{"concat"}
Aresult
11
00
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
BranchTrueFalseTotal
unless (defined $status) 0 32 32
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
BranchTrueFalseTotal
if ($name eq 'sysex') { } 3 29 32
elsif ($status < 240) { } 17 12 29
49 3 ▶ my $msg = ref $event[0] eq 'ARRAY' ? pack('C*', @{ $event[0] }) : $event[0] // '';
Branch: 1 branch
BranchTrueFalseTotal
ref $event[0] eq 'ARRAY' ? : 1 2 3
Condition50%1/2
$event[0] // ""
l!l
20
MC/DC0%0/1
$event[0] // ""
$event[0]
Truth table50%1/2
A
A= $event[0]
Aresult
11
00
50 3 $msg =~ s/\A\xf0//;
51 3 ▶ $msg .= "\xf7" if $msg !~ /\xf7\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg =~ /\xf7\z/u 2 1 3
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
BranchTrueFalseTotal
if $name eq "pitch_bend" 2 15 17
59 17 ▶ while (@event) {
Branch: 1 branch
BranchTrueFalseTotal
if (@event) 18 17 35
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
BranchTrueFalseTotal
if $name eq "song_position" 1 11 12
66 12 ▶ $self->{running} = 0 if $status < 0xf8; # real time leaves running status as it is
Branch: 1 branch
BranchTrueFalseTotal
if $status < 248 4 8 12
67 12 @messages = (pack 'C*', $status, @event[ 0 .. message_length($status) - 2 ]);
68 }
69 32 ▶ return $self->{concat} ? join('', @messages) : @messages;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'concat'} ? : 0 32 32
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
BranchTrueFalseTotal
if eval { do { $event->isa("Peta::MIDI::Event") } } 0 32 32
75 32 ▶ return @$event if ref $event eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $event eq "ARRAY" 31 1 32
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
BranchTrueFalseTotal
if ($self->{"enable_14bit_cc"} and $status == 176 and $values[0] < 32) 2 16 18
Condition66%4/6
$self->{"enable_14bit_cc"} and $status == 176 and $values[0] < 32
!ll&&!rl&&r
1602
$self->{"enable_14bit_cc"} and $status == 176
!ll&&!rl&&r
1602
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
ABCresult
0XX0
10X0
1100
1111
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] //= []
l!l
11
MC/DC100%1/1
$self->{"coarse"}[$channel] //= []
$self->{"coarse"}[$channel]
Truth table100%2/2
A
A= $self->{"coarse"}[$channel]
Aresult
11
00
86 2 my @parts = ([ $number | 0x20, $fine ]);
87 2 ▶ unshift @parts, [ $number, $coarse ] if ($sent->[$number] // -1) != $coarse;
Branch: 1 branch
BranchTrueFalseTotal
if ($sent->[$number] // -1) != $coarse 1 1 2
Condition100%2/2
$sent->[$number] // -1
l!l
11
MC/DC100%1/1
$sent->[$number] // -1
$sent->[$number]
Truth table100%2/2
A
A= $sent->[$number]
Aresult
11
00
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
BranchTrueFalseTotal
if $self->{"enable_running_status"} and $status == 128 and not $values[1] and ($self->{"running"} & 240) != 128 1 15 16
Condition88%8/9
$self->{"enable_running_status"} and $status == 128 and not $values[1] and ($self->{"running"} & 240) != 128
!ll&&!rl&&r
1501
$self->{"enable_running_status"} and $status == 128 and not $values[1]
!ll&&!rl&&r
1411
$self->{"enable_running_status"} and $status == 128
!ll&&!rl&&r
592
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
ABCDresult
0XXX0
10XX0
110X0
11100
11111
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
BranchTrueFalseTotal
if ($self->{"enable_running_status"}) 11 8 19
97 11 ▶ if ($status == $self->{running} && $self->{repeats}++ < $self->{running_status_retransmit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == $self->{"running"} and $self->{"repeats"}++ < $self->{"running_status_retransmit"}) 2 9 11
Condition66%2/3
$status == $self->{"running"} and $self->{"repeats"}++ < $self->{"running_status_retransmit"}
!ll&&!rl&&r
902
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"}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
$self->{'concat'} ? : 0 2 2
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__
« lib/Peta/Vorbis/Encode.pm ↑ Summary lib/Peta/Vorbis/Encode/Book.pm »