| 1 | package Peta::Synth::MIDI; | ||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Standard MIDI Files, read and written | ||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # Standard MIDI Files (SMF, formats 0, 1 and 2): read into tracks of events | ||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | # at their tick, and written back from them; and single messages, as a | ||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # device sends them. What the events mean (notes, controllers, system | ||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # exclusive) is Peta::Synth::Synth's business; this module only knows the | ||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # bytes. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | # An event is a hash: | ||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # { tick => 960, track => 1, type => 'note_on', channel => 0, key => 60, velocity => 100 } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # note_off, note_on channel key velocity | ||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # key_pressure channel key value | ||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # controller channel number value | ||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # program channel program | ||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # channel_pressure channel value | ||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | # pitch_bend channel value (-8192 .. 8191, 0 is centre) | ||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # meta meta (the type byte) data (the bytes) | ||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # tempo usec (microseconds per quarter note) | ||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # sysex data (the bytes after F0 or F7) | ||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # | ||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # Tempo is a meta event that is common enough to be its own type; every other | ||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # meta event (text, time signature, end of track, ...) stays 'meta'. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 7 | ▶ | use v5.36; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 7 | our $VERSION = '0.2610180'; | |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # The channel messages, 0x80 .. 0xEF by the status' high nibble: their | ||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # type and how many data bytes they take. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 7 | my @CHANNEL_TYPE = ( | |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | [ note_off => 2 ], | ||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | [ note_on => 2 ], | ||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | [ key_pressure => 2 ], | ||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | [ controller => 2 ], | ||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | [ program => 1 ], | ||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | [ channel_pressure => 1 ], | ||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | [ pitch_bend => 2 ], | ||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # The data bytes of the system common messages F1 .. F6. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 7 | my %COMMON_SIZE = (0xf1 => 1, 0xf2 => 2, 0xf3 => 1, 0xf4 => 0, 0xf5 => 0, 0xf6 => 0); | |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | # How many data bytes follow a status byte (0 for those that have none). | ||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 1251 | ▶ | sub data_bytes ($status) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
data_bytes: called CC=4 SCAR=14.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 1251 | ▶ | return $CHANNEL_TYPE[ ($status >> 4) - 8 ][1] if $status >= 0x80 && $status < 0xf0; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$status >= 128 and $status < 240
MC/DC50%1/2
$status >= 128 and $status < 240
$status >= 128
$status < 240
Truth table66%2/3
A and B
A= $status >= 128B= $status < 240
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | ▶ | return $COMMON_SIZE{$status} // 0; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$COMMON_SIZE{$status} // 0
MC/DC0%0/1
$COMMON_SIZE{$status} // 0
$COMMON_SIZE{$status}
Truth table50%1/2
A
A= $COMMON_SIZE{$status}
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 19 | ▶ | sub new ($class, %arg) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | return bless { | ||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | format => $arg{format} // 1, | ||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | division => $arg{division} // 480, | ||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 19 | ▶ | tracks => $arg{tracks} // [], | ||||||||||||||||||||||||||||||||||||||||||||||
|
Condition83%5/6
$arg{"format"} // 1
$arg{"division"} // 480
$arg{"tracks"} // []
MC/DC100%1/1
$arg{"format"} // 1
$arg{"format"}
MC/DC0%0/1
$arg{"division"} // 480
$arg{"division"}
MC/DC100%1/1
$arg{"tracks"} // []
$arg{"tracks"}
Truth table100%2/2
A
A= $arg{"format"}
Truth table50%1/2
A
A= $arg{"division"}
Truth table100%2/2
A
A= $arg{"tracks"}
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 4 | ▶ | sub read ($class, $file) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 4 | ▶ | open my $fh, '<:raw', $file or die "cannot read '$file': $!\n"; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 4 | local $/; | |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 4 | my $bytes = <$fh>; | |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 4 | close $fh; | |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 4 | return $class->parse($bytes, $file); | |||||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 13 | ▶ | sub parse ($class, $bytes, $name = 'the data') { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=11 SCAR=26.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 13 | my $pos = 0; | |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | # A RIFF wrapper (.rmi) holds the SMF in its 'data' chunk. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 13 | ▶ | if (substr($bytes, 0, 4) eq 'RIFF' && substr($bytes, 8, 4) eq 'RMID') { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
substr($bytes, 0, 4) eq "RIFF" and substr($bytes, 8, 4) eq "RMID"
MC/DC0%0/2
substr($bytes, 0, 4) eq "RIFF" and substr($bytes, 8, 4) eq "RMID"
substr($bytes, 0, 4) eq "RIFF"
substr($bytes, 8, 4) eq "RMID"
Truth table33%1/3
A and B
A= substr($bytes, 0, 4) eq "RIFF"B= substr($bytes, 8, 4) eq "RMID"
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | my $at = index($bytes, 'MThd'); | |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | die "$name: a RIFF MIDI file without an SMF in it\n" if $at < 0; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | $pos = $at; | |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 13 | ▶ | die "$name: not a Standard MIDI File\n" unless substr($bytes, $pos, 4) eq 'MThd'; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 11 | my ($length, $format, $count, $division) = unpack 'N n n n', substr($bytes, $pos + 4, 10); | |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 11 | ▶ | die "$name: header too short\n" if $length < 6; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 11 | $pos += 8 + $length; | |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 11 | my $self = $class->new(format => $format, division => $division); | |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 11 | ▶ | while ($pos + 8 <= length $bytes) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 12 | my ($id, $size) = unpack 'a4 N', substr($bytes, $pos, 8); | |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 12 | $pos += 8; | |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 12 | ▶ | $size = length($bytes) - $pos if $pos + $size > length $bytes; # a cut-off last track | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 12 | ▶ | push @{ $self->{tracks} }, _track(substr($bytes, $pos, $size), scalar @{ $self->{tracks} }, $name) | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | if $id eq 'MTrk'; | ||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 12 | $pos += $size; | |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | warn "$name: header says $count tracks, the file has " . @{ $self->{tracks} } . "\n" | |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 11 | ▶ | if @{ $self->{tracks} } != $count; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 11 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 1266 | ▶ | sub _vlq ($bytes, $pos) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_vlq: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 1266 | my $value = 0; | |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 1266 | for (1 .. 4) { | |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 1273 | my $byte = ord substr($$bytes, $$pos++, 1); | |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 1273 | $value = ($value << 7) | ($byte & 0x7f); | |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 1273 | ▶ | return $value if $byte < 0x80; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | return $value; | |||||||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 12 | ▶ | sub _track ($bytes, $track, $name) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_track: called CC=13 SCAR=26.9
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 12 | my ($pos, $tick, $status, @events) = (0, 0, 0); | |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 12 | my $end = length $bytes; | |||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 12 | ▶ | while ($pos < $end) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 1251 | $tick += _vlq(\$bytes, \$pos); | |||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 1251 | ▶ | last if $pos >= $end; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 1251 | my $byte = ord substr($bytes, $pos, 1); | |||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 1251 | ▶ | if ($byte >= 0x80) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 1248 | $pos++; | |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 1248 | ▶ | $status = $byte if $byte < 0xf0; # running status survives meta and sysex (as most readers have it) | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 3 | ▶ | die "$name: track $track: data byte without a status at offset $pos\n" unless $status; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 3 | $byte = $status; | |||||||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 1251 | ▶ | if ($byte == 0xff) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 15 | my $meta = ord substr($bytes, $pos++, 1); | |||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 15 | my $len = _vlq(\$bytes, \$pos); | |||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 15 | my $data = substr($bytes, $pos, $len); | |||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 15 | $pos += $len; | |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 15 | ▶ | if ($meta == 0x51 && $len == 3) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$meta == 81 and $len == 3
MC/DC50%1/2
$meta == 81 and $len == 3
$meta == 81
$len == 3
Truth table66%2/3
A and B
A= $meta == 81B= $len == 3
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 3 | push @events, { tick => $tick, track => $track, type => 'tempo', usec => unpack('N', "\0$data") }; | |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 12 | push @events, { tick => $tick, track => $track, type => 'meta', meta => $meta, data => $data }; | |||||||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 15 | ▶ | last if $meta == 0x2f; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | ▶ | elsif ($byte == 0xf0 || $byte == 0xf7) { | |||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$byte == 240 or $byte == 247
MC/DC0%0/2
$byte == 240 or $byte == 247
$byte == 240
$byte == 247
Truth table33%1/3
A or B
A= $byte == 240B= $byte == 247
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | my $len = _vlq(\$bytes, \$pos); | |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | push @events, { tick => $tick, track => $track, type => 'sysex', data => substr($bytes, $pos, $len) }; | |||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | $pos += $len; | |||||||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | elsif ($byte >= 0xf1) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | # System common and real-time bytes do not belong in a file; skip their data. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | $pos += data_bytes($byte); | |||||||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 1236 | my $size = data_bytes($byte); | |||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 1236 | my ($one, $two) = unpack 'C2', substr($bytes, $pos, $size) . "\0"; | |||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 1236 | $pos += $size; | |||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 1236 | push @events, { tick => $tick, track => $track, %{ _channel_event($byte, $one, $two) } }; | |||||||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 12 | return \@events; | |||||||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | # A channel message's event from its status and data bytes. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 1246 | ▶ | sub _channel_event ($status, $one, $two) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_channel_event: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 1246 | my $type = $CHANNEL_TYPE[ ($status >> 4) - 8 ][0]; | |||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 1246 | my %event = (type => $type, channel => $status & 0x0f); | |||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 1246 | ▶ | if ($type eq 'note_on' && $two == 0) { @event{qw(type key velocity)} = ('note_off', $one, 64) } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
Condition100%3/3
$type eq "note_on" and $two == 0
MC/DC100%2/2
$type eq "note_on" and $two == 0
$type eq "note_on"
$two == 0
Truth table100%3/3
A and B
A= $type eq "note_on"B= $two == 0
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 1226 | ▶ | elsif ($type eq 'note_on' || $type eq 'note_off') { @event{qw(key velocity)} = ($one, $two) } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$type eq "note_on" or $type eq "note_off"
MC/DC100%2/2
$type eq "note_on" or $type eq "note_off"
$type eq "note_on"
$type eq "note_off"
Truth table100%3/3
A or B
A= $type eq "note_on"B= $type eq "note_off"
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | elsif ($type eq 'key_pressure') { @event{qw(key value)} = ($one, $two) } | |||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 10 | elsif ($type eq 'controller') { @event{qw(number value)} = ($one, $two) } | |||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 4 | elsif ($type eq 'program') { $event{program} = $one } | |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 1 | elsif ($type eq 'channel_pressure') { $event{value} = $one } | |||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 2 | else { $event{value} = (($two << 7) | $one) - 8192 } | |||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 1246 | return \%event; | |||||||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | # One message as a device sends it (its bytes, status first, a system | ||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | # exclusive one whole from F0 to F7): its event, or undef for what is not | ||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | # a channel's or system exclusive (clock, active sensing, ...) or is cut short. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 15 | ▶ | sub message ($bytes) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
message: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 15 | my ($status, @data) = unpack 'C*', $bytes; | |||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 15 | ▶ | return unless defined $status; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 15 | ▶ | return { type => 'sysex', data => substr($bytes, 1) } if $status == 0xf0; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 12 | ▶ | return 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
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 11 | my $size = data_bytes($status); | |||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 11 | ▶ | return if @data < $size || grep { $_ > 0x7f } @data[ 0 .. $size - 1 ]; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@data < $size or grep {$_ > 127;} @data[0 .. $size - 1]
MC/DC50%1/2
@data < $size or grep {$_ > 127;} @data[0 .. $size - 1]
@data < $size
grep {$_ > 127;} @data[0 .. $size - 1]
Truth table66%2/3
A or B
A= @data < $sizeB= grep {$_ > 127;} @data[0 .. $size - 1]
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 10 | ▶ | return _channel_event($status, $data[0], $data[1] // 0); | ||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$data[1] // 0
MC/DC100%1/1
$data[1] // 0
$data[1]
Truth table100%2/2
A
A= $data[1]
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 1 | ▶ | sub format ($self) { return $self->{format} } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
format: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 1 | ▶ | sub division ($self) { return $self->{division} } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
division: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 3 | ▶ | sub tracks ($self) { return $self->{tracks} } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tracks: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | # All events of all tracks in the order they take effect, each with its time | ||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | # in seconds ('time'), the tempo map applied; copies, the tracks are left as | ||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | # they are. Events at the same tick keep the order of the file: track by | ||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | # track, and within a track as they were written (a note off written before | ||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | # a pedal at the same tick ends the note; written after, the pedal holds it). | ||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | # Format 2 files hold independent sequences; they are played one after another. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 12 | ▶ | sub timeline ($self) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
timeline: called CC=13 SCAR=26.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 12 | my $tracks = $self->{tracks}; | |||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 12 | my $offset = 0; # in format 2, where this track's sequence begins | |||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 12 | my @keyed; # [ tick, track, index in the track, event ] | |||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 12 | for my $t (0 .. $#$tracks) { | |||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 14 | my $last = 0; | |||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 14 | my $index = 0; | |||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 14 | for my $event (@{ $tracks->[$t] }) { | |||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 715 | my $tick = $event->{tick} + $offset; | |||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 715 | push @keyed, [ $tick, $t, $index++, { %$event, tick => $tick, track => $t } ]; | |||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 715 | ▶ | $last = $event->{tick} if $event->{tick} > $last; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 14 | ▶ | $offset += $last if $self->{format} == 2; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 12 | ▶ | my @sorted = sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] || $a->[2] <=> $b->[2] } @keyed; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Condition83%5/6
$Peta::Synth::MIDI::a->[0] <=> $Peta::Synth::MIDI::b->[0] || $Peta::Synth::MIDI::a->[1] <=> $Peta::Synth::MIDI::b->[1] || $Peta::Synth::MIDI::a->[2] <=> $Peta::Synth::MIDI::b->[2]
$Peta::Synth::MIDI::a->[0] <=> $Peta::Synth::MIDI::b->[0] or $Peta::Synth::MIDI::a->[1] <=> $Peta::Synth::MIDI::b->[1]
MC/DC0%0/3
$Peta::Synth::MIDI::a->[0] <=> $Peta::Synth::MIDI::b->[0] || $Peta::Synth::MIDI::a->[1] <=> $Peta::Synth::MIDI::b->[1] || $Peta::Synth::MIDI::a->[2] <=> $Peta::Synth::MIDI::b->[2]
$Peta::Synth::MIDI::a->[0] <=> $Peta::Synth::MIDI::b->[0]
$Peta::Synth::MIDI::a->[1] <=> $Peta::Synth::MIDI::b->[1]
$Peta::Synth::MIDI::a->[2] <=> $Peta::Synth::MIDI::b->[2]
Truth table75%3/4
A or B || C
A= $Peta::Synth::MIDI::a->[0] <=> $Peta::Synth::MIDI::b->[0]B= $Peta::Synth::MIDI::a->[1] <=> $Peta::Synth::MIDI::b->[1]C= $Peta::Synth::MIDI::a->[2] <=> $Peta::Synth::MIDI::b->[2]
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 12 | my @events = map { $_->[3] } @sorted; | |||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 12 | my $division = $self->{division}; | |||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 12 | ▶ | if ($division & 0x8000) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | # SMPTE: frames per second (as a negative byte) and ticks per frame; no tempo. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | my $fps = 256 - ($division >> 8); | |||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | $fps = 29.97 if $fps == 29; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | my $per_tick = 1 / ($fps * ($division & 0xff)); | |||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | $_->{time} = $_->{tick} * $per_tick for @events; | |||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | return \@events; | |||||||||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 12 | my ($tick, $time, $per_tick) = (0, 0, 0.5 / $division); # 120 bpm until said otherwise | |||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 12 | for my $event (@events) { | |||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 715 | $time += ($event->{tick} - $tick) * $per_tick; | |||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 715 | $tick = $event->{tick}; | |||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 715 | $event->{time} = $time; | |||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 715 | ▶ | $per_tick = $event->{usec} / 1e6 / $division if $event->{type} eq 'tempo' && $event->{usec}; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$event->{"type"} eq "tempo" and $event->{"usec"}
MC/DC50%1/2
$event->{"type"} eq "tempo" and $event->{"usec"}
$event->{"type"} eq "tempo"
$event->{"usec"}
Truth table66%2/3
A and B
A= $event->{"type"} eq "tempo"B= $event->{"usec"}
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 12 | return \@events; | |||||||||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | # Writing: the counterpart of read, for what composes in Perl and for the tests. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | # A variable-length quantity: seven bits a byte, the high bit set on all | ||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | # but the last. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 629 | ▶ | sub _vlq_bytes ($value) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_vlq_bytes: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 629 | my $bytes = chr($value & 0x7f); | |||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 629 | ▶ | $bytes = chr(0x80 | ($value & 0x7f)) . $bytes while ($value >>= 7); | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 629 | return $bytes; | |||||||||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | # An event as the bytes of a track (running status not used). | ||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 629 | ▶ | sub _event_bytes ($event) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_event_bytes: called CC=13 SCAR=26.1
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 629 | my $type = $event->{type}; | |||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 629 | ▶ | my $ch = $event->{channel} // 0; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$event->{"channel"} // 0
MC/DC100%1/1
$event->{"channel"} // 0
$event->{"channel"}
Truth table100%2/2
A
A= $event->{"channel"}
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 629 | ▶ | return "\xff\x51\x03" . substr(pack('N', $event->{usec}), 1) if $type eq 'tempo'; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 626 | ▶ | return "\xff" . chr($event->{meta}) . _vlq_bytes(length $event->{data}) . $event->{data} if $type eq 'meta'; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 626 | ▶ | return "\xf0" . _vlq_bytes(length $event->{data}) . $event->{data} if $type eq 'sysex'; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 626 | ▶ | return pack 'C3', 0x80 | $ch, $event->{key}, $event->{velocity} // 64 if $type eq 'note_off'; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$event->{"velocity"} // 64
MC/DC100%1/1
$event->{"velocity"} // 64
$event->{"velocity"}
Truth table100%2/2
A
A= $event->{"velocity"}
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 319 | ▶ | return pack 'C3', 0x90 | $ch, $event->{key}, $event->{velocity} if $type eq 'note_on'; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 12 | ▶ | return pack 'C3', 0xa0 | $ch, $event->{key}, $event->{value} if $type eq 'key_pressure'; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 12 | ▶ | return pack 'C3', 0xb0 | $ch, $event->{number}, $event->{value} if $type eq 'controller'; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 4 | ▶ | return pack 'C2', 0xc0 | $ch, $event->{program} if $type eq 'program'; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 1 | ▶ | return pack 'C2', 0xd0 | $ch, $event->{value} if $type eq 'channel_pressure'; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 1 | ▶ | if ($type eq 'pitch_bend') { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 1 | my $value = $event->{value} + 8192; | |||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 1 | return pack 'C3', 0xe0 | $ch, $value & 0x7f, $value >> 7; | |||||||||||||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | die "cannot write an event of type '$type'\n"; | |||||||||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | # The file as bytes. Each track gets one end of track, at its last event. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 6 | ▶ | sub bytes ($self) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bytes: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 6 | my $out = pack 'a4 N n n n', 'MThd', 6, $self->{format}, scalar @{ $self->{tracks} }, $self->{division}; | |||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 6 | for my $track (@{ $self->{tracks} }) { | |||||||||||||||||||||||||||||||||||||||||||||||
| 261 | # In tick order; events at the same tick in the order they are given. | ||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 7 | my @indexed = map { [ $_, $track->[$_] ] } 0 .. $#$track; | |||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 7 | ▶ | my @events = map { $_->[1] } sort { $a->[1]{tick} <=> $b->[1]{tick} || $a->[0] <=> $b->[0] } @indexed; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$Peta::Synth::MIDI::a->[1]{'tick'} <=> $Peta::Synth::MIDI::b->[1]{'tick'} || $Peta::Synth::MIDI::a->[0] <=> $Peta::Synth::MIDI::b->[0]
MC/DC0%0/2
$Peta::Synth::MIDI::a->[1]{'tick'} <=> $Peta::Synth::MIDI::b->[1]{'tick'} || $Peta::Synth::MIDI::a->[0] <=> $Peta::Synth::MIDI::b->[0]
$Peta::Synth::MIDI::a->[1]{'tick'} <=> $Peta::Synth::MIDI::b->[1]{'tick'}
$Peta::Synth::MIDI::a->[0] <=> $Peta::Synth::MIDI::b->[0]
Truth table66%2/3
A || B
A= $Peta::Synth::MIDI::a->[1]{'tick'} <=> $Peta::Synth::MIDI::b->[1]{'tick'}B= $Peta::Synth::MIDI::a->[0] <=> $Peta::Synth::MIDI::b->[0]
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 7 | my ($tick, $body) = (0, ''); | |||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 7 | for my $event (@events) { | |||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 629 | ▶ | next if $event->{type} eq 'meta' && $event->{meta} == 0x2f; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$event->{"type"} eq "meta" and $event->{"meta"} == 47
MC/DC0%0/2
$event->{"type"} eq "meta" and $event->{"meta"} == 47
$event->{"type"} eq "meta"
$event->{"meta"} == 47
Truth table33%1/3
A and B
A= $event->{"type"} eq "meta"B= $event->{"meta"} == 47
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 629 | $body .= _vlq_bytes($event->{tick} - $tick) . _event_bytes($event); | |||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 629 | $tick = $event->{tick}; | |||||||||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 7 | $body .= "\0\xff\x2f\0"; | |||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 7 | $out .= pack('a4 N', 'MTrk', length $body) . $body; | |||||||||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 6 | return $out; | |||||||||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 1 | ▶ | sub write ($self, $file) { | ||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 1 | ▶ | open my $fh, '>:raw', $file or die "cannot write '$file': $!\n"; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 1 | print {$fh} $self->bytes; | |||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 1 | ▶ | close $fh or die "cannot write '$file': $!\n"; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 1 | return $file; | |||||||||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||