MidiGen is a Python class that helps you create, modify, and save MIDI files using the Mido library. It provides an object-oriented approach for managing MIDI files and simplifies common MIDI operations such as adding notes, chords, and arpeggios, changing instruments, and managing tempo, time signatures, and key signatures.
from midigen import MidiGen, Track, Note, Chord, Key, KEY_MAP
midi_gen = MidiGen(tempo=120, time_signature=(4, 4), key_signature=Key("C"))
note_c = Note(pitch=KEY_MAP["C"], velocity=64, duration=480, time=0)
note_e = Note(pitch=KEY_MAP["E"], velocity=64, duration=480, time=0)
note_g = Note(pitch=KEY_MAP["G"], velocity=64, duration=480, time=0)
c_major_chord = Chord([note_c, note_e, note_g])
track = midi_gen.get_active_track()
track.add_chord(c_major_chord)
midi_gen.save("example_song_with_chord.mid")
The constructor creates a new MidiGen instance with the specified tempo, time signature, and key signature.
Sets the tempo (in BPM) for the MIDI file.
Sets the time signature for the MIDI file.
Sets the key signature for the MIDI file.
Adds a program change (instrument) event to the specified channel.
A program change is a MIDI message used to change the instrument or sound (also known as "patch" or "preset") used by a synthesizer or other MIDI device. In a General MIDI (GM) system, there are 128 standard instruments that can be selected using program change messages, allowing you to switch between different instrument sounds such as piano, guitar, strings, and more.
In the context of the MidiGen class, the add_program_change method allows you to change the instrument sound for a specific MIDI channel.
Adds a control change event to the specified channel.
Adds a pitch bend event to the specified channel.
Adds a note with the specified pitch, velocity, and duration at the given time (in ticks).
Adds a chord with the specified notes, velocity, and duration at the given time (in ticks).
Adds an arpeggio with the specified notes, velocity, duration, and arpeggio duration at the given time (in ticks).
Quantizes the given time value to the nearest multiple of the quantization value.
Loads an existing MIDI file for further processing or manipulation.
Saves the MIDI file to the specified filename.
The tempo (in BPM) of the MIDI file.
The time signature of the MIDI file.
The key signature of the MIDI file.
The Mido MidiTrack object associated with the MidiGen instance.