MIDIFile class
A class that encapsulates a full, well-formed MIDI file object.
This is a container object that contains a _header (:class:MIDIHeader
),
one or more _tracks (class:MIDITrack
), and the data associated with a
proper and well-formed MIDI file.
Properties
Methods
-
addKeySignature(
{required int track, required num time, required int no_of_accidentals, required int accidental_type, required int accidental_mode, dynamic insertion_order = 0}) → dynamic - Add a Key Signature to a track :param track: The track to which this should be added :param time: The time at which the signature should be placed :param accidentals: The number of accidentals in the key signature :param accidental_type: The type of accidental :param mode: The mode of the scale The easiest way to use this function is to make sure that the symbolic constants for accidental_type and mode are imported. By doing this: .. code:: from midiutil.MidiFile import * one gets the following constants defined:
-
addNote(
{required int track, required int channel, required int pitch, required num time, required num duration, int volume = 100, dynamic annotation = ''}) → dynamic -
Add notes to the MIDIFile object
:param track: The track to which the note is added.
:param channel: the MIDI channel to assign to the note.
Integer, 0-15
:param pitch: the MIDI pitch numberInteger, 0-127
. :param time: the time at which the note sounds. The value can be either quarter notes Float, or ticksInteger
. Ticks may be specified by passing eventtime_is_ticks=True to the MIDIFile constructor. The default is quarter notes. :param duration: the duration of the note. Like the time argument, the value can be either quarter notes Float, or ticksInteger
. :param volume: the volume (velocity) of the note.Integer, 0-127
. :param annotation: Arbitrary data to attach to the note. Theannotation
parameter attaches arbitrary data to the note. This is not used in the code, but can be useful anyway. As an example, I have created a project that uses MIDIFile to writecsound <http:///csound.github.io/>
orchestra files directly from the classEventList
. -
addProgramChange(
{required int tracknum, required int channel, required num time, required int program}) → dynamic -
Add a MIDI program change event.
:param tracknum: The zero-based track number to which program change event is added.
:param channel: the MIDI channel to assign to the event.
Integer, 0-15
:param time: The time (in beats) at which the program change event is placed double. :param program: the program number.Integer, 0-127
. -
addTempo(
{required int track, required num time, required int tempo}) → dynamic -
Add notes to the MIDIFile object
:param track: The track to which the tempo event is added. Note that
in a format 1 file this parameter is ignored and the tempo is
written to the tempo track
:param time: The time (in beats) at which tempo event is placed
:param tempo: The tempo, in Beats per Minute.
Integer
-
addTimeSignature(
{required int track, required num time, required int numerator, required int denominator, required dynamic clocks_per_tick, dynamic notes_per_quarter = 8}) → dynamic -
Add a time signature event.
:param track: The track to which the signature is assigned. Note that
in a format 1 file this parameter is ignored and the event is
written to the tempo track
:param time: The time (in beats) at which the event is placed.
In general this should probably be time 0 (the beginning of the
track).
:param numerator: The numerator of the time signature. Int
:param denominator: The denominator of the time signature, expressed as
a power of two (see below). Int
:param clocks_per_tick: The number of MIDI clock ticks per metronome
click (see below).
:param notes_per_quarter: The number of annotated 32nd notes in a MIDI
quarter note. This is almost always 8 (the default), but some
sequencers allow this value to be changed. Unless you know that
your sequencing software supports it, this should be left at its
default value.
The data format for this event is a little obscure.
The
denominator
should be specified as a power of 2, with a half note being one, a quarter note being two, and eight note being three, etc. Thus, for example, a 4/4 time signature would have anumerator
of 4 and adenominator
of 2. A 7/8 time signature would be anumerator
of 7 and adenominator
of 3. Theclocks_per_tick
argument specifies the number of clock ticks per metronome click. By definition there are 24 ticks in a quarter note, so a metronome click per quarter note would be 24. A click every third eighth note would be 3 * 12 = 36. Thenotes_per_quarter
value is also a little confusing. It specifies the number of 32nd notes in a MIDI quarter note. Usually there are 8 32nd notes in a quarter note (8/32 = 1/4), so the default value is 8. However, one can change this value if needed. Setting it to 16, for example, would cause the music to play at double speed, as there would be 16/32 (or what could be considered two quarter notes for every one MIDI quarter note. Note that both theclocks_per_tick
and thenotes_per_quarter
are specified in terms of quarter notes, even is the score is not a quarter-note based score (i.e., even if the denominator is not4
). So if you're working with a time signature of, say, 6/8, one still needs to specify the clocks per quarter note. -
close(
) → dynamic - Close the MIDIFile for further writing. To close the File for events, we must close the _tracks, adjust the time to be zero-origined, and have the _tracks write to their MIDI Stream data structure.
-
findOrigin(
) → dynamic - Find the earliest time in the file's _tracks.append.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
writeFile(
File fileHandle) → dynamic - Write the MIDI File. param fileHandle: A file handle that has been opened for binary writing.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited