NAME

Peta::Synth::MIDI - Standard MIDI Files, read and written

VERSION

version 0.2610180

SYNOPSIS

my $midi = Peta::Synth::MIDI->read('song.mid');
for my $event (@{ $midi->timeline }) {
    printf "%8.3f %s\n", $event->{time}, $event->{type};
}

my $out = Peta::Synth::MIDI->new(division => 480, tracks => [[
    { tick => 0,   type => 'note_on',  channel => 0, key => 60, velocity => 100 },
    { tick => 480, type => 'note_off', channel => 0, key => 60 },
]]);
$out->write('c.mid');

DESCRIPTION

Reads and writes Standard MIDI Files of formats 0, 1 and 2, and RIFF MIDI (.rmi) files. A file is a list of tracks, a track a list of events at their tick. The event types and their fields are listed at the top of the source; a note_on with velocity 0 is read as a note_off.

METHODS

new

new(format => 1, division => 480, tracks => [...]): a file in memory.

read

read($file): the file, parsed. Dies if it is not a MIDI file.

parse

parse($bytes, $name): the same from the bytes of a file.

message

Peta::Synth::MIDI::message($bytes): one message as a device sends it, as an event (sysex with what follows F0), or undef where it is no channel or system exclusive message.

data_bytes

data_bytes($status): how many data bytes follow a status byte: those of a channel message (1 or 2) or of a system common one (0 to 2).

format, division, tracks

The SMF format, the division (ticks per quarter note, or SMPTE timing when the top bit is set) and the tracks.

timeline

All events of all tracks in the order they take effect, each with its time in seconds in time, the tempo map applied (copies: the tracks are not changed). Events at the same tick keep the order of the file, track by track: what a file says first happens first. The sequences of a format 2 file are played one after another.

bytes, write

The file as bytes, or written to $file. Events at the same tick are written in the order they are given.

AUTHOR

PetaMem s.r.o. <info@petamem.com>

COPYRIGHT

Copyright (c) 2026 PetaMem s.r.o.

LICENSE

This package is free software, dual-licensed under the Artistic License 2.0 and the BSD 2-Clause License. See the LICENSE file of the distribution.