Python Forum
How to set a sequence of midi note on and note off values in ticks
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to set a sequence of midi note on and note off values in ticks
#1
Hello,

I am currently writing an application where i need to write midi files using the tick value for each notes duration and onset. I have decided to use the library mido for this and need some assistance understanding how to create midi messages.

For now to understand how midi note messages work i have aimed to create a quarter note at the start of each bar for a total of 4 bars in 4/4.
This was my initial approach:

 
    midi_file = MidiFile(type=0)
    midi_track = MidiTrack()
    midi_file.tracks.append(midi_track)

    beat_tick_length = midi_file.ticks_per_beat
    bar_tick_length = 4 * midi_file.ticks_per_beat # 4/4 time signature

    for i in range(4):
        midi_track.append(Message('note_on', note=60, velocity=127, time=i*bar_tick_length))
        midi_track.append(Message('note_off', note=60, velocity=127, time=480))
My expectation was that each quarter note would start at the beginning of each bar at the midi tick positions (0, 1920, 3840, 5760). These midi tick positions are calculated by finding the bar length for a 4/4 measure in ticks which is the beat length (ppqn * 4). Instead the notes appear at the following positions.

0
2400 (1920 + (1*480)) bar 2 add one quarter note
6720 (5760 + (2*480)) bar 4 add two quarter notes
13440(14880(3*480)) bar 7 add three quarter notes

I now see that the append() function appends the note on to the sequence, rather than just adding another note on message anywhere in the sequence. One option is to work with this function and instead of writing the onset at its point in the whole file, instead to write its onset taking into account the notes that have came before it. I see the clue is in the function name 'append'. Is there an alternative function to add midi notes?

Just to be clear the below code produces the desired output but by data will not be formatted in such a way that i can write midi files like this.

    midi_track.append(Message('note_on', note=60, velocity=127, time=0))
    midi_track.append(Message('note_off', note=60, velocity=127, time=480))

    for i in range(3):
        midi_track.append(Message('note_on', note=60, velocity=127, time=1440))
        midi_track.append(Message('note_off', note=60, velocity=127, time=480))
So i am wondering if it is at all possible to set the durations and onsets as times relative to the whole file rather than appending them. I usually use the c++ framework JUCE when working with midi and it works in such a way that if a message is created at the onset specified it will write a note at that onset, instead of the previous message + the onset.

Does anyone know if what im after is possible using mido, or will i have to work with just the append() function?

Thank you!
Reply
#2
you may be trying to re-invent the wheel (or perhaps not), but check the packages available for midi here:

https://pypi.org/search/?q=midi

Just might find one that fits the bill.
Reply
#3
Hello,

I also looked into python_midi but that worked in a similar way. I have now decided to go back to using c++ as this is what im more familiar with and i can get the results that i want a lot easier. Later on in this project it may be more straight forward to be in python, if thats the case i'll learn to work with these tools and go back over the list you sent.

Thanks for your help :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cannot remove one of the x-ticks from a 2x1 subplot. generalzu10 1 355 Mar-20-2024, 04:24 AM
Last Post: generalzu10
  Adding MIDI Channels to each Input zach1234 6 1,357 Apr-20-2023, 11:51 AM
Last Post: jefsummers
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 1,355 Jul-27-2022, 12:31 PM
Last Post: Larz60+
  How to bind a midi signal to tkinter? philipbergwerf 1 1,677 Feb-09-2022, 05:17 PM
Last Post: Gribouillis
  mido MIDI-file; how to distinguis tracks? philipbergwerf 2 2,392 Dec-10-2021, 05:06 PM
Last Post: philipbergwerf
  Missing ticks on X axis drunkenneo 0 1,789 May-29-2021, 04:16 PM
Last Post: drunkenneo
  How to open MIDI-file and get events in a list? philipbergwerf 7 5,215 May-29-2021, 08:24 AM
Last Post: j.crater
  Mido and virtual midi ports dore_m 2 4,764 Dec-27-2020, 06:02 AM
Last Post: dore_m
  MIDI FILES TEMPO - MIDO Tetsuo30 9 5,252 Sep-06-2020, 08:17 PM
Last Post: jefsummers
  Real time MIDI with python eythoralex 1 3,568 May-19-2020, 07:17 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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