MidiFrames class

[1]:
import miditapyr
import subprocess

Create an object of class MidiFrames

First define the path where the file is stored. We’ll use the one in the package. But you can also pass your own:

[2]:
test_mid = miditapyr.get_test_midi_file(as_string=True)

Create an object mf of class MidiFrames by reading in the test midi file from the package:

[3]:
mf = miditapyr.MidiFrames(test_mid)

Modify midi data

Define function to set the volume of the notes to the maximum (127):

[4]:
def mod_fun(df):
    df.loc[df['velocity'] >= 0, 'velocity'] = 127
    return df

modify unnested midi frame:

[5]:
midi_frame_unnested_mod = mod_fun(mf.midi_frame_unnested.df)

Update the midi_frame_unnested attribute of mf:

[6]:
mf.midi_frame_unnested.update_unnested_mf(midi_frame_unnested_mod)
mf.midi_frame_unnested.df
[6]:
i_track meta type name time note velocity channel tempo numerator denominator clocks_per_click notated_32nd_notes_per_beat
0 0 True track_name drum-t1-1-t1 0 NaN NaN NaN NaN NaN NaN NaN NaN
1 0 False note_on NaN 0 43.0 127.0 9.0 NaN NaN NaN NaN NaN
2 0 False note_on NaN 0 39.0 127.0 9.0 NaN NaN NaN NaN NaN
3 0 False note_on NaN 0 36.0 127.0 9.0 NaN NaN NaN NaN NaN
4 0 True set_tempo NaN 0 NaN NaN NaN 666666.0 NaN NaN NaN NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ...
263 2 False note_off NaN 31 59.0 127.0 15.0 NaN NaN NaN NaN NaN
264 2 False note_off NaN 9 67.0 127.0 15.0 NaN NaN NaN NaN NaN
265 2 False note_on NaN 266 62.0 127.0 15.0 NaN NaN NaN NaN NaN
266 2 False note_off NaN 5 62.0 127.0 15.0 NaN NaN NaN NaN NaN
267 2 True end_of_track NaN 1 NaN NaN NaN NaN NaN NaN NaN NaN

268 rows × 13 columns

All the velocities were set to 127.

Write back to midi file

By calling write_file() a modified midi file will be created with the passed file name string:

[7]:
mf.write_file('test_out_file.mid')

We’ll transform the file to wav and mp3. By uncommenting the second line of the first command you can use an sf2 soundfont file:

[8]:
%%script false --no-raise-error
# (hack to not run this cell)

# save to wav:
subprocess.run(['fluidsynth',
                # '/path/to/your/sounfont/file.sf2',
                '-F',
                'test_out_file.wav',
                'test_out_file.mid'])
# transform wav to mp3:
subprocess.run(['lame',
                'test_out_file.wav',
                'test_out_file.mp3'])
[10]:
import IPython
IPython.display.Audio(url = 'https://raw.githubusercontent.com/urswilke/miditapyr/master/docs/source/notebooks/test_out_file.mp3')
[10]: