madmom.utils.midi

This module contains MIDI functionality.

Almost all code is taken from Giles Hall’s python-midi package: https://github.com/vishnubob/python-midi

It combines the complete package in a single file, to make it easier to distribute. Most notable changes are MIDITrack and MIDIFile classes which handle all data i/o and provide a interface which allows to read/display all notes as simple numpy arrays. Also, the EventRegistry is handled differently.

The last merged commit is 3053fefe.

Since then the following commits have been added functionality-wise:

  • 0964c0b (prevent multiple tick conversions)
  • c43bf37 (add pitch and value properties to AfterTouchEvent)
  • 40111c6 (add 0x08 MetaEvent: ProgramNameEvent)
  • 43de818 (handle unknown MIDI meta events gracefully)

Additionally, the module has been updated to work with Python3.

The MIT License (MIT) Copyright (c) 2013 Giles F. Hall

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

madmom.utils.midi.byte2int(byte)[source]

Convert a byte-character to an integer.

madmom.utils.midi.read_variable_length(data)[source]

Read a variable length variable from the given data.

Parameters:

data : bytearray

Data of variable length.

Returns:

length : int

Length in bytes.

madmom.utils.midi.write_variable_length(value)[source]

Write a variable length variable.

Parameters:

value : bytearray

Value to be encoded as a variable of variable length.

Returns:

bytearray

Variable with variable length.

class madmom.utils.midi.EventRegistry[source]

Class for registering Events.

Event classes should be registered manually by calling EventRegistry.register_event(EventClass) after the class definition.

classmethod register_event(event)[source]

Registers an event in the registry.

Parameters:

event : Event instance

Event to be registered.

Raises:

ValueError

For unknown events.

class madmom.utils.midi.AbstractEvent(**kwargs)[source]

Abstract Event.

class madmom.utils.midi.Event(**kwargs)[source]

Event.

classmethod is_event(status_msg)[source]

Indicates whether the given status message belongs to this event.

Parameters:

status_msg : int

Status message.

Returns:

bool

True if the given status message belongs to this event.

class madmom.utils.midi.MetaEvent(**kwargs)[source]

MetaEvent is a special subclass of Event that is not meant to be used as a concrete class. It defines a subset of Events known as the Meta events.

classmethod is_event(status_msg)[source]

Indicates whether the given status message belongs to this event.

Parameters:

status_msg : int

Status message.

Returns:

bool

True if the given status message belongs to this event.

class madmom.utils.midi.MetaEventWithText(**kwargs)[source]

Meta Event With Text.

class madmom.utils.midi.NoteEvent(**kwargs)[source]

NoteEvent is a special subclass of Event that is not meant to be used as a concrete class. It defines the generalities of NoteOn and NoteOff events.

pitch

Pitch of the note event.

velocity

Velocity of the note event.

class madmom.utils.midi.NoteOnEvent(**kwargs)[source]

Note On Event.

class madmom.utils.midi.NoteOffEvent(**kwargs)[source]

Note Off Event.

class madmom.utils.midi.AfterTouchEvent(**kwargs)[source]

After Touch Event.

pitch

Pitch of the after touch event.

value

Value of the after touch event.

class madmom.utils.midi.ControlChangeEvent(**kwargs)[source]

Control Change Event.

control

Control ID.

value

Value of the controller.

class madmom.utils.midi.ProgramChangeEvent(**kwargs)[source]

Program Change Event.

value

Value of the Program Change Event.

class madmom.utils.midi.ChannelAfterTouchEvent(**kwargs)[source]

Channel After Touch Event.

value

Value of the Channel After Touch Event.

class madmom.utils.midi.PitchWheelEvent(**kwargs)[source]

Pitch Wheel Event.

pitch

Pitch of the Pitch Wheel Event.

class madmom.utils.midi.SysExEvent(**kwargs)[source]

System Exclusive Event.

classmethod is_event(status_msg)[source]

Indicates whether the given status message belongs to this event.

Parameters:

status_msg : int

Status message.

Returns:

bool

True if the given status message belongs to this event.

class madmom.utils.midi.SequenceNumberMetaEvent(**kwargs)[source]

Sequence Number Meta Event.

class madmom.utils.midi.TextMetaEvent(**kwargs)[source]

Text Meta Event.

class madmom.utils.midi.CopyrightMetaEvent(**kwargs)[source]

Copyright Meta Event.

class madmom.utils.midi.TrackNameEvent(**kwargs)[source]

Track Name Event.

class madmom.utils.midi.InstrumentNameEvent(**kwargs)[source]

Instrument Name Event.

class madmom.utils.midi.LyricsEvent(**kwargs)[source]

Lyrics Event.

class madmom.utils.midi.MarkerEvent(**kwargs)[source]

Marker Event.

class madmom.utils.midi.CuePointEvent(**kwargs)[source]

Cue Point Event.

class madmom.utils.midi.ProgramNameEvent(**kwargs)[source]

Program Name Event.

class madmom.utils.midi.UnknownMetaEvent(**kwargs)[source]

Unknown Meta Event.

The meta_command class variable must be set by the constructor of inherited classes.

Parameters:

meta_command : int

Value of the meta command.

class madmom.utils.midi.ChannelPrefixEvent(**kwargs)[source]

Channel Prefix Event.

class madmom.utils.midi.PortEvent(**kwargs)[source]

Port Event.

class madmom.utils.midi.TrackLoopEvent(**kwargs)[source]

Track Loop Event.

class madmom.utils.midi.EndOfTrackEvent(**kwargs)[source]

End Of Track Event.

class madmom.utils.midi.SetTempoEvent(**kwargs)[source]

Set Tempo Event.

microseconds_per_quarter_note

Microseconds per quarter note.

class madmom.utils.midi.SmpteOffsetEvent(**kwargs)[source]

SMPTE Offset Event.

class madmom.utils.midi.TimeSignatureEvent(**kwargs)[source]

Time Signature Event.

numerator

Numerator of the time signature.

denominator

Denominator of the time signature.

metronome

Metronome.

thirty_seconds

Thirty-seconds of the time signature.

class madmom.utils.midi.KeySignatureEvent(**kwargs)[source]

Key Signature Event.

alternatives

Alternatives of the key signature.

minor

Major / minor.

class madmom.utils.midi.SequencerSpecificEvent(**kwargs)[source]

Sequencer Specific Event.

class madmom.utils.midi.MIDITrack(events=None)[source]

MIDI Track.

Parameters:

events : list

MIDI events.

data_stream

MIDI data stream representation of the track.

classmethod from_file(midi_stream)[source]

Create a MIDI track by reading the data from a stream.

Parameters:

midi_stream : open file handle

MIDI file stream (e.g. open MIDI file handle)

Returns:

MIDITrack instance

MIDITrack instance

classmethod from_notes(notes, resolution=480)[source]

Create a MIDI track from the given notes.

Parameters:

notes : numpy array

Array with the notes, one per row. The columns must be: (onset time, pitch, duration, velocity).

resolution : int

Resolution (i.e. microseconds per quarter note) of the MIDI track.

Returns:

MIDITrack instance

MIDITrack instance

class madmom.utils.midi.MIDIFile(tracks=None, resolution=480, file_format=0)[source]

MIDI File.

Parameters:

tracks : list

List of MIDITrack instances.

resolution : int, optional

Resolution (i.e. microseconds per quarter note).

file_format : int, optional

Format of the MIDI file.

ticks_per_quarter_note

Number of ticks per quarter note.

tempi()[source]

Tempi of the MIDI file.

Returns:

tempi : numpy array

Array with tempi (tick, seconds per tick, cumulative time).

time_signatures()[source]

Time signatures of the MIDI file.

Returns:

time_signatures : numpy array

Array with time signatures (tick, numerator, denominator).

notes(note_time_unit='s')[source]

Notes of the MIDI file.

Parameters:

note_time_unit : {‘s’, ‘b’}

Time unit for notes, seconds (‘s’) or beats (‘b’).

Returns:

notes : numpy array

Array with notes (onset time, pitch, duration, velocity).

data_stream

MIDI data stream representation of the MIDI file.

write(midi_file)[source]

Write a MIDI file.

Parameters:

midi_file : str

The MIDI file name.

classmethod from_file(midi_file)[source]

Create a MIDI file instance from a .mid file.

Parameters:

midi_file : str

Name of the .mid file to load.

Returns:

MIDIFile instance

MIDIFile instance

classmethod from_notes(notes)[source]

Create a MIDIFile instance from a numpy array with notes.

Parameters:

notes : numpy array or list of tuples

Notes (onset, pitch, offset, velocity).

Returns:

MIDIFile instance

MIDIFile instance with all notes collected in one track.

static add_arguments(parser, length=None, velocity=None)[source]

Add MIDI related arguments to an existing parser object.

Parameters:

parser : argparse parser instance

Existing argparse parser object.

length : float, optional

Default length of the notes [seconds].

velocity : int, optional

Default velocity of the notes.

Returns:

argparse argument group

MIDI argument parser group object.

madmom.utils.midi.process_notes(data, output=None)[source]

This is a simple processing function. It either loads the notes from a MIDI file and or writes the notes to a file.

The behaviour depends on the presence of the output argument, if ‘None’ is given, the notes are read, otherwise the notes are written to file.

Parameters:

data : str or numpy array

MIDI file to be loaded (if output is ‘None’) / notes to be written.

output : str, optional

Output file name. If set, the notes given by data are written.

Returns:

notes : numpy array

Notes read/written.