Package jazzparser :: Package utils :: Module midi
[hide private]
[frames] | no frames]

Module midi

source code


Author: Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>

Functions [hide private]
(float,float) pair
note_on_similarity(midi0, midi1)
Given two EventStreams, returns the similarity, between 0 and 1, of the note on events.
source code
 
trim_intro(mid)
Many MIDI files begin with a count-in on a drum.
source code
midi.sequencer_pygame.Sequencer
play_stream(stream, block=False)
Plays an event stream.
source code
 
get_midi_text(stream)
Extracts descriptive text from a midi.EventStream.
source code
 
first_note_tick(stream)
Returns the tick time of the first note-on event in an EventStream.
source code
 
simplify(stream, remove_drums=False, remove_pc=False, remove_all_text=False, one_track=False, remove_tempo=False, remove_control=False, one_channel=False, remove_misc_control=False, real_note_offs=False, remove_duplicates=False)
Filters a midi midi.EventStream to simplify it.
source code
 
remove_duplicate_notes(stream, replay=False)
Some processing operations, like simplify, can leave a midi file with the same note being played twice at the same time.
source code
 
remove_channels(stream, channels=[])
Modifies a stream in place to remove all events played on certain channels.
source code
 
note_ons(stream)
Filters the events in an event stream to return only note-on events with a non-zero velocity.
source code
Variables [hide private]
  __package__ = None
hash(x)
Function Details [hide private]

note_on_similarity(midi0, midi1)

source code 

Given two EventStreams, returns the similarity, between 0 and 1, of the note on events.

This is useful for detecting duplicate MIDI files. Exact mirrors of files can easily be found by checking for equality of the raw MIDI data, but often files are redistributed with different instruments, or simply different meta data.

Note that the absolute time of two MIDI events must be equal for them to match, so an offset version of the same file will not match.

This measure is not symmetric. midi0 is compared to midi1 and vice versa and both results are returned. If you want a symmetric measure, average the two.

Returns: (float,float) pair
a pair of similarities, the first from testing for how much of midi0 is found in midi1 and the second from the opposite.

trim_intro(mid)

source code 

Many MIDI files begin with a count-in on a drum. Some might even begin with some silence.

Given a midi.EventStream, returns a version with any drum intro or silence trimmed from the beginning, so that the first thing heard is a non-drum note.

It is assumed that this is General MIDI data, so channel 10 is necessarily a drum channel.

play_stream(stream, block=False)

source code 

Plays an event stream.

Various methods for playing midi data are provided in the midi library. At the time of writing, the only one that works is Timidity via SDL via PyGame and this is what this function uses.

Whatever happens with the midi library, this function should continue to provide some convenient way to play a midi.EventStream.

If a keyboard interrupt is received, the playing will stop. The interrupt will still be raised.

Returns: midi.sequencer_pygame.Sequencer
the sequencer that's been instantiated to do the playing.

get_midi_text(stream)

source code 

Extracts descriptive text from a midi.EventStream. This is often stored in the form of copyright notices, text events, track names, etc. This tries to pull out everything it can and return it all in a multiline string.

Midi data can use non-ASCII characters. We assume the latin1 encoding is intended for these. This certainly covers the most common case: the copyright symbol.

Returns a unicode string.

simplify(stream, remove_drums=False, remove_pc=False, remove_all_text=False, one_track=False, remove_tempo=False, remove_control=False, one_channel=False, remove_misc_control=False, real_note_offs=False, remove_duplicates=False)

source code 

Filters a midi midi.EventStream to simplify it. This is useful as a preprocessing step before taking midi input to an algorithm, for example, to make it clearer what the algorithm is using.

Use kwargs to determine what filters will be applied. Without any kwargs, the stream will just be left as it was.

Returns a filtered copy of the stream.

Parameters:
  • remove_drums (bool) - filter out all channel 10 events
  • remove_pc (bool) - filter out all program change events
  • remove_all_text (bool) - filter out any text events. This includes copyright, text, track name, lyrics.
  • one_track (bool) - reduce everything to just one track
  • remove_tempo (bool) - filter out all tempo events
  • remove_control (bool) - filter out all control change events
  • one_channel (bool) - use only one channel: set the channel of every event to 0
  • remove_misc_control (bool) - filters a miscellany of device control events: aftertouch, channel aftertouch, pitch wheel, sysex, port
  • real_note_offs (bool) - replace 0-velocity note-ons with actual note-offs. Some midi files use one, some the other

remove_duplicate_notes(stream, replay=False)

source code 

Some processing operations, like simplify, can leave a midi file with the same note being played twice at the same time.

To avoid the confusion this leads to, it's best to remove these. This function will remove multiple instances of the same note being played simultaneously (in the same track and channel) and insert note-off events before a note is replayed that's already being played.

This can lead to some strange effects if multiple instruments have been reduced to one, as in the case of simplify. You may wish to keep seperate instruments on separate channels to avoid this.

Parameters:
  • replay (bool) - if True, notes that are played while they're already sounding while be replayed - taken off and put back on. Otherwise, such notes will be ignored.