pythonmidimido

How to set ticks_per_beat under MIDO to a new MIDI file?


Reading ticks_per_beat under MIDO can be done via mid.ticks_per_beat. However, if I want to save the value of ticks_per_beat (e.g., 120 or 480) to a new MIDI file, how it can be done? (p.s. I can set "time signature" or "tempo" as follows. But, it seems to me that there is no relationship between these values and ticks_per_beat.)

track.append(MetaMessage('time_signature', numerator=4, denominator=4, clocks_per_click=24, notated_32nd_notes_per_beat=8, time=0))

track.append(MetaMessage('set_tempo', tempo=100000, time=0))

Solution

  • The ticks per beat value is not stored with a MIDI message; it's a field in the MIDI file header.

    The MidiFile constructor has a bunch of undocumented parameters:

    class MidiFile(object):
        def __init__(self, filename=None, file=None,
                     type=1, ticks_per_beat=DEFAULT_TICKS_PER_BEAT,
                     charset='latin1',
                     debug=False,
                     clip=False
                     ):