| 1 | package Peta::MIDI::Event; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: a MIDI message as an event: its name and fields | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # A MIDI message as an event: its name, its fields in their order, the | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | # seconds since the message before it (dt), and its bytes. The names and | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # fields are those of MIDI::Stream (the names MIDI::Event has besides, by | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # name_to_midi_event and name_from_midi_event): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # note_off, note_on channel note velocity (a note on of velocity 0 is a note off) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | # polytouch channel note pressure key_after_touch | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # control_change channel control value | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # program_change channel program patch_change | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # aftertouch channel pressure channel_after_touch | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # pitch_bend channel value pitch_wheel_change (-8192 .. 8191) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # sysex msg sysex_f0 (the bytes between F0 and F7) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # timecode byte | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # song_position position (0 .. 16383) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # song_select song | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | # tune_request, eox (sysex_f7), clock, start, continue, stop, active_sensing, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # system_reset (no fields) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 3 | ▶ | use v5.36; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 3 | ▶ | use Exporter qw(import); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 3 | our $VERSION = '0.2610180'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 3 | our @EXPORT_OK = qw(status_byte status_name keys_for message_length split_bytes combine_bytes); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 3 | my %STATUS = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | note_off => 0x80, note_on => 0x90, polytouch => 0xa0, control_change => 0xb0, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | program_change => 0xc0, aftertouch => 0xd0, pitch_bend => 0xe0, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | sysex => 0xf0, timecode => 0xf1, song_position => 0xf2, song_select => 0xf3, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | tune_request => 0xf6, eox => 0xf7, clock => 0xf8, start => 0xfa, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | continue => 0xfb, stop => 0xfc, active_sensing => 0xfe, system_reset => 0xff, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 3 | my %NAME = reverse %STATUS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 3 | my %KEYS = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | note_off => [qw(channel note velocity)], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | note_on => [qw(channel note velocity)], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | polytouch => [qw(channel note pressure)], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | control_change => [qw(channel control value)], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | program_change => [qw(channel program)], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | aftertouch => [qw(channel pressure)], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | pitch_bend => [qw(channel value)], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | sysex => [qw(msg)], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | timecode => [qw(byte)], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | song_position => [qw(position)], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | song_select => [qw(song)], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 3 | my %MIDI_EVENT = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | key_after_touch => 'polytouch', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | patch_change => 'program_change', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | channel_after_touch => 'aftertouch', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | pitch_wheel_change => 'pitch_bend', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | sysex_f0 => 'sysex', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | sysex_f7 => 'eox', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 3 | my %TO_MIDI_EVENT = reverse %MIDI_EVENT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # A name's status byte (channel 0), and a status byte's name. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 32 | ▶ | sub status_byte ($name) { return $STATUS{$name} } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
status_byte: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 50 | ▶ | sub status_name ($status) { return $status < 0xf0 ? $NAME{ $status & 0xf0 } : $NAME{$status} } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
status_name: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # An event's fields, in order. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 36 | ▶ | sub keys_for ($name) { return $KEYS{$name} // [] } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
keys_for: called CC=2 SCAR=6.9
Pod
documented
Condition100%2/2
$KEYS{$name} // []
MC/DC100%1/1
$KEYS{$name} // []
$KEYS{$name}
Truth table100%2/2
A
A= $KEYS{$name}
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | # MIDI::Event's names and back. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 1 | ▶ | sub name_to_midi_event ($name) { return $TO_MIDI_EVENT{$name} // $name } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
name_to_midi_event: called CC=2 SCAR=9.2
Pod
documented
Condition33%1/3
$TO_MIDI_EVENT{$name} // $name
MC/DC0%0/2
$TO_MIDI_EVENT{$name} // $name
$TO_MIDI_EVENT{$name}
$name
Truth table33%1/3
A // B
A= $TO_MIDI_EVENT{$name}B= $name
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 33 | ▶ | sub name_from_midi_event ($name) { return $MIDI_EVENT{$name} // $name } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
name_from_midi_event: called CC=2 SCAR=7.2
Pod
documented
Condition66%2/3
$MIDI_EVENT{$name} // $name
MC/DC0%0/2
$MIDI_EVENT{$name} // $name
$MIDI_EVENT{$name}
$name
Truth table66%2/3
A // B
A= $MIDI_EVENT{$name}B= $name
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | # A message's length with its status byte (0: as long as it is, system exclusive). | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 103 | ▶ | sub message_length ($status) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
message_length: called CC=10 SCAR=23.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 103 | ▶ | return 0 if $status < 0x80 || $status == 0xf0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$status < 128 or $status == 240
MC/DC50%1/2
$status < 128 or $status == 240
$status < 128
$status == 240
Truth table66%2/3
A or B
A= $status < 128B= $status == 240
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 97 | ▶ | return 3 if $status < 0xc0 || ($status >= 0xe0 && $status < 0xf0) || $status == 0xf2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
$status < 192 or $status >= 224 and $status < 240 or $status == 242
$status < 192 or $status >= 224 and $status < 240
$status >= 224 and $status < 240
MC/DC100%4/4
$status < 192 or $status >= 224 and $status < 240 or $status == 242
$status < 192
$status >= 224
$status < 240
$status == 242
Truth table83%5/6
A or B and C or D
A= $status < 192B= $status >= 224C= $status < 240D= $status == 242
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 29 | ▶ | return 2 if $status < 0xe0 || $status == 0xf1 || $status == 0xf3; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$status < 224 or $status == 241 or $status == 243
$status < 224 or $status == 241
MC/DC100%3/3
$status < 224 or $status == 241 or $status == 243
$status < 224
$status == 241
$status == 243
Truth table100%4/4
A or B or C
A= $status < 224B= $status == 241C= $status == 243
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 11 | return 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | # A value of 14 bits as (lsb, msb), and back. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 5 | ▶ | sub split_bytes ($value) { return ($value & 0x7f, ($value >> 7) & 0x7f) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
split_bytes: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 6 | ▶ | sub combine_bytes ($lsb, $msb) { return (($msb & 0x7f) << 7) | ($lsb & 0x7f) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
combine_bytes: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | # An event from a whole message (its bytes as numbers) and the seconds | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | # since the one before. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 50 | ▶ | sub new ($class, $message, $dt = 0) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=11 SCAR=24.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 50 | my ($status, @data) = @$message; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 50 | ▶ | my $name = status_name($status) // 'unknown'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
status_name($status) // "unknown"
MC/DC0%0/1
status_name($status) // "unknown"
status_name($status)
Truth table50%1/2
A
A= status_name($status)
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 50 | ▶ | $name = 'note_off' if $name eq 'note_on' && !$data[1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$name eq "note_on" and not $data[1]
MC/DC100%2/2
$name eq "note_on" and not $data[1]
$name eq "note_on"
not $data[1]
Truth table100%3/3
A and not(B)
A= $name eq "note_on"B= not $data[1]
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 50 | my %field; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 50 | ▶ | if ($status < 0xf0) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 19 | my @keys = @{ $KEYS{$name} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 19 | @field{@keys} = ($status & 0x0f, @data); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 19 | ▶ | $field{value} = combine_bytes(@data) - 8192 if $name eq 'pitch_bend'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 3 | elsif ($name eq 'sysex') { $field{msg} = pack 'C*', grep { $_ != 0xf7 } @data } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 2 | elsif ($name eq 'song_position') { $field{position} = combine_bytes(@data) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | elsif ($name eq 'song_select') { $field{song} = $data[0] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 1 | elsif ($name eq 'timecode') { $field{byte} = $data[0] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 50 | return bless { name => $name, status => $status, message => [@$message], dt => $dt, %field }, $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 54 | ▶ | sub name ($self) { return $self->{name} } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
name: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub status ($self) { return $self->{status} } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
status: not called CC=1 SCAR=6.9
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | sub message ($self) { return $self->{message} } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
message: not called CC=1 SCAR=6.9
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | sub dt ($self) { return $self->{dt} } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dt: not called CC=1 SCAR=6.9
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 1 | ▶ | sub bytes ($self) { return pack 'C*', @{ $self->{message} } } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bytes: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | # The fields by name: channel, note, velocity, pressure, control, value, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | # program, msg, byte, position, song (undef where an event has none). | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 3 | for my $field (qw(channel note velocity pressure control value program msg byte position song)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 3 | ▶ | no strict 'refs'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 1 | ▶ | *$field = sub ($self) { return $self->{$field} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 34 | ▶ | sub as_arrayref ($self) { return [ $self->{name}, @$self{ @{ keys_for($self->{name}) } } ] } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_arrayref: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 1 | ▶ | sub as_hashref ($self) { return { name => $self->{name}, map { $_ => $self->{$_} } @{ keys_for($self->{name}) } } } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_hashref: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | sub TO_JSON ($self) { return $self->as_hashref } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
TO_JSON: not called CC=1 SCAR=6.9
Pod
documented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 3 | 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | __END__ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||