Mar-19-2022, 10:02 AM
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:
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.
Does anyone know if what im after is possible using mido, or will i have to work with just the append() function?
Thank you!
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!