Library reference

Submodules

miditapyr.mido_io module

miditapyr.mido_io.add_abs_time(df)

Add a column of absolute time

Parameters:df (DataFrame) – unnested midi frame
Returns:Dataframe with absolute time column ‘t’ added.
Return type:DataFrame
miditapyr.mido_io.frame_midi(mid)

Function to create a dataframe containing the information parsed by MidiFile

Parameters:mid (MidiFile) – object of class MidiFile
Returns:a dataframe, containing 3 columns:
Return type:DataFrame

The function nest_midi() returns a dataframe of the same format. This dataframe contains these columns:

  • i_track: the track number
  • msg: the (meta) event information in MidiFile in a list of dictionaries
  • meta: whether the event in ‘msg’ is a mido meta event
miditapyr.mido_io.get_test_midi_file(as_string=False)

Get the example midi file included in the

Parameters:as_string (bool, optional) – Should the midi file be returned as a file path or as a MidiFile object, defaults to False
Returns:file path or as a MidiFile
Return type:file path or as a MidiFile
miditapyr.mido_io.merge_midi_frames(df_meta, df_not_notes, df_notes)

Merge dataframes resulting of split_midi_frame().

  • df_meta - (containing all mido meta events),
  • df_not_notes - (non-meta events that are not note_on or note_off) &
  • df_notes - (all note_on or note_off events).
Parameters:
  • df_meta (DataFrame) – Contains all mido meta events.
  • df_not_notes (DataFrame) – Non-meta events that are not note_on or note_off.
  • df_notes (DataFrame) – All note_on or note_off events.
Returns:

unnested midi frame

Return type:

DataFrame

miditapyr.mido_io.nest_midi(df, repair_reticulate_conversion=False)

Function to transform the dataframe returned by unnest_midi() back to a format as in the result of unnest_midi()

Parameters:df – Dataframe returned by unnest_midi()
Returns:a dataframe, containing 3 columns
Return type:DataFrame

The function miditapyr.mido_io.frame_midi() returns a dataframe of the same format. This dataframe contains these columns:

  • i_track: the track number
  • msg: the (meta) event information in MidiFile (see here) in a list of dictionaries
  • meta: whether the event in ‘msg’ is a mido meta event
miditapyr.mido_io.pivot_notes_long(dfw)

Write note_on and note_off events in two lines (wide to long)

Parameters:dfw (DataFrame) – Transforms notes in wide dataframe format to long format.
Returns:Dataframe with twice the number of rows, where the time & velocity columns occur in two rows for all note_on or note_off events.
Return type:DataFrame
miditapyr.mido_io.pivot_notes_wide(df_notes)

Write note_on and note_off events in the same line (long to wide)

Parameters:df_notes (DataFrame) – Notes dataframe from split_midi_frame()
Returns:Dataframe with half the number of rows, but the time & velocity columns occur twice for all note_on or note_off events.
Return type:DataFrame
miditapyr.mido_io.split_df(df)

Function to create a tuple of 2 dataframes containing the information in MidiFile

Parameters:df – dataframe returned by unnest_midi()
Returns:a tuple of 2 dataframes, containing the meta / note information
Return type:tuple
miditapyr.mido_io.split_midi_frame(df_unnested)

Split unnested midi frame into 3 dataframes.

Parameters:df_unnested (DataFrame) – result of unnest_midi()
Returns:(tuple) of 3 dataframes df_meta, df_not_notes & df_notes
  • df_meta - (containing all mido meta events),
  • df_not_notes - (non-meta events that are not note_on or note_off) &
  • df_notes - (all note_on or note_off events).
Return type:tuple of 3 DataFrame
miditapyr.mido_io.unnest_midi(dfc)

Function to transform the dataframe returned by frame_midi() in a tidy format (cf. https://r4ds.had.co.nz/tidy-data.html)

Parameters:dfc (DataFrame) – Dataframe returned by frame_midi()
Returns:a dataframe
Return type:DataFrame

The returned dataframe with the columns i_track and meta of unnest_midi(). The msg column is exploded and each key in the dicts in the msg column of frame_midi() is stored in its own column.

miditapyr.mido_io.write_midi(dfc, ticks_per_beat, filename)

Function to write midi dataframes returned by nest_midi() back to a midi file

Parameters:
  • dfc (DataFrame) – dataframe containing the meta event information returned by unnest_midi()
  • ticks_per_beat (integer) – integer containing the ticks_per_beat information in MidiFile
  • filename (string) – string containing the name of the midi file to be written

miditapyr.midi_frame module

class miditapyr.midi_frame.MidiFrameNested(midi_frame_unnested=None)

Bases: object

Class containing the DataFrame df observing an object of MidiFrameUnnested.

When the observed object midi_frame_unnested is updated with MidiFrameUnnested.update_unnested_mf(), midi_frame_nested is also automatically updated with MidiFrameNested.update_mf_nested().

Parameters:midi_frame_unnestedDataFrame resulting of unnest_midi().
calc_df(midi_frame_unnested)
df
update_mf_nested(midi_frame_unnested, unnested_mf_mod)
class miditapyr.midi_frame.MidiFrameUnnested(midi_frame_raw=None)

Bases: object

Class containing the DataFrame df.

df can be updated with update_unnested_mf(). This also triggers an update of objects of the observing class MidiFrameNested.

Parameters:midi_frame_rawDataFrame resulting of frame_midi().
calc_df(midi_frame_raw)
df
register_observer(observer)
update_unnested_mf(unnested_mf_mod)
class miditapyr.midi_frame.MidiFrames(midi_file_string=None)

Bases: object

Structure that reads in a midi file and has the following attributes:

Variables:

The dataframe midi_frame_unnested.df can be manipulated with the method update_unnested_mf(). This also triggers an update of the dataframe midi_frame_nested.df with the method update_mf_nested().

When update_unnested_mf() was not called, the attribute midi_frame_nested.df should be identical to midi_frame_raw. After calling update_unnested_mf(), midi_frame_nested.df should also contain the changes made to midi_frame_unnested.df.

You can write back the midi data to a midi file by calling the method write_file().

Parameters:midi_file_string – String containing the path to the input midi file.
calc_attributes(midi_file_string)
midi_file
midi_frame_nested
midi_frame_raw
midi_frame_unnested
write_file(out_file_string)

Write midi data back to midi file

Parameters:out_file_string – midi file path where the file should be stored.

Module contents