ExMidi.MidiFile (ex_midi v0.2.0)

Copy Markdown View Source

Standard MIDI File (SMF) reader and writer.

Supports Format 0, 1, and 2 MIDI files with full meta message support.

Data structures

  • File: {:midi_file, type, division, tracks}
  • Track: {:track, events}
  • Event: {name, delta_time, values} where values is a list or a string

Examples

iex> midi = ExMidi.MidiFile.read("song.mid")
iex> for track <- ExMidi.MidiFile.tracks(midi) do
...>   for event <- track do
...>     IO.inspect(event)
...>   end
...> end

Inspired by mido's MidiFile with play/1 support:

iex> midi = ExMidi.MidiFile.read("song.mid")
iex> for msg <- ExMidi.MidiFile.play(midi) do
...>   IO.inspect(msg)
...> end

Summary

Functions

Return the time division of a MIDI file.

Return the format type (0, 1, or 2) of a MIDI file.

Iterate through all messages in the file, merging tracks and yielding messages ordered by delta time. Similar to mido's MidiFile.play().

Read a MIDI file from the given path.

Return the length of the longest track in ticks.

Return the tracks of a MIDI file.

Write a MIDI file to the given path.

Types

division()

@type division() :: pos_integer()

event()

@type event() :: {atom(), non_neg_integer(), list()}

file()

@type file() :: {:midi_file, format(), division(), [track()]}

format()

@type format() :: 0 | 1 | 2

track()

@type track() :: {:track, [event()]}

Functions

division(arg)

@spec division(file()) :: division()

Return the time division of a MIDI file.

format(arg)

@spec format(file()) :: format()

Return the format type (0, 1, or 2) of a MIDI file.

play(arg)

@spec play(file()) :: Enumerable.t()

Iterate through all messages in the file, merging tracks and yielding messages ordered by delta time. Similar to mido's MidiFile.play().

Yields {:midi, payload} tuples.

read(path)

@spec read(Path.t()) :: file() | {:error, term()}

Read a MIDI file from the given path.

Returns {:midi_file, format, division, tracks} on success, or {:error, reason} when the file cannot be opened or is malformed.

track_length(arg)

@spec track_length(file()) :: non_neg_integer()

Return the length of the longest track in ticks.

tracks(arg)

@spec tracks(file()) :: [track()]

Return the tracks of a MIDI file.

write(arg, path)

@spec write(file(), Path.t()) :: :ok | {:error, term()}

Write a MIDI file to the given path.