Python Forum
How do I manipulate musical notes using Python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I manipulate musical notes using Python?
#1
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.
Reply
#2
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
Reply
#3
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.
Reply
#4
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to code a musical slur in python tkinter? philipbergwerf 0 550 Jul-27-2023, 04:21 PM
Last Post: philipbergwerf
  Openpyxl manipulate excel write formula SamLiu 0 1,062 Nov-04-2022, 03:00 PM
Last Post: SamLiu
  using fitz to manipulate pdf Pedroski55 1 4,778 Mar-11-2020, 07:18 AM
Last Post: Gribouillis
  DB_ methods to calculate/manipulate data issac_n 0 2,270 Dec-26-2017, 07:03 AM
Last Post: issac_n
  How to manipulate csv 2 Prince_Bhatia 4 3,703 Nov-24-2017, 10:16 AM
Last Post: Prince_Bhatia
  Simplest way to create a musical note and play it insearchofanswers87 3 13,613 Nov-21-2017, 07:02 PM
Last Post: insearchofanswers87
  How to manipulate csv Prince_Bhatia 4 3,335 Nov-06-2017, 09:11 AM
Last Post: Prince_Bhatia
  Python 3.5 Instantiate and manipulate object with multiprocessing jipete 1 4,864 Dec-28-2016, 12:46 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020