Python Forum

Full Version: How do I manipulate musical notes using Python?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to manipulate a midi file using python. For now, I would be happy if I could simply increase one note by one increment.

I know this is a very basic python problem but I do not know exactly how to search for an answer here. I can import a midi file, I can print a midi file, I can create a midi file from scratch but I cannot seem to make changes to the file. There are many online tutorials that show how to change variables but I cannot seem to parse apart the midi file in order to edit it. I am currently using Google Colab notebooks.

I am using mido to get the file into python

from mido import MidiFile

        mid = MidiFile('test.mid')
Print it:

 for i, track in enumerate(mid.tracks):
            print('Track {}: {}'.format(i, track.name))
            for msg in track:
                print(msg)
I get something like this:

Output:
Track 0: � <meta message track_name name=u'\x00' time=0> <meta message time_signature numerator=4 denominator=4 clocks_per_click=36 notated_32nd_notes_per_beat=8 time=0> <meta message time_signature numerator=4 denominator=4 clocks_per_click=36 notated_32nd_notes_per_beat=8 time=0> note_on channel=0 note=36 velocity=100 time=0 note_off channel=0 note=36 velocity=64 time=96 note_on channel=0 note=40 velocity=100 time=0 note_off channel=0 note=40 velocity=64 time=96 note_on channel=0 note=43 velocity=100 time=0 note_off channel=0 note=43 velocity=64 time=96 note_on channel=0 note=45 velocity=100 time=0 note_off channel=0 note=45 velocity=64 time=96 <meta message end_of_track time=0>
I would love to know how to manipulate this. Initially to simply to increase one of the notes by 1 increment.
Hi,

Quote:I know this is a very basic python problem but I do not know exactly how to search for an answer here.
No it's not. To be precise, it has nothing to do with Python in a strict sense. What you are looking for is a Python module which cannot only print tracks - as shown in your example, but can access the actual MIDI date and manipulate it. The mido module you are using seems to be able to do so.

After a quick read through https://mido.readthedocs.io/en/latest/intro.html I think it's fairly easy: iterate of the messages of a MIDI file, copy the current message and alter the note at the same time, write the message to a new file. Done - I guess ;-)

Gruß, noisefloor
Hey have you heard of sonic pi: home page
You can run your input with the program and capture outputs and control tones
with their module psonic. (Use pip to install) The main program needs to be running
to use python. Lots of examples to get you started.
(Jul-07-2019, 12:04 AM)joe_momma Wrote: [ -> ]Hey have you heard of sonic pi: home page You can run your input with the program and capture outputs and control tones with their module psonic. (Use pip to install) The main program needs to be running to use python. Lots of examples to get you started.
looks awesome! thanks!

(Jul-06-2019, 12:31 PM)noisefloor Wrote: [ -> ]Hi,
Quote:I know this is a very basic python problem but I do not know exactly how to search for an answer here.
No it's not. To be precise, it has nothing to do with Python in a strict sense. What you are looking for is a Python module which cannot only print tracks - as shown in your example, but can access the actual MIDI date and manipulate it. The mido module you are using seems to be able to do so. After a quick read through https://mido.readthedocs.io/en/latest/intro.html I think it's fairly easy: iterate of the messages of a MIDI file, copy the current message and alter the note at the same time, write the message to a new file. Done - I guess ;-) Gruß, noisefloor
Ah yes... I think you nailed the root of my problem. I was thinking the midifile command I was using was already pulling it into a list but I still need more formatting before I can manipulate. Thanks!