MarkCWirt / MIDIUtil

A pure Python library for creating multi-track MIDI files
Other
243 stars 49 forks source link

How to change beats? #26

Open mr-52hz opened 5 years ago

giuliano-macedo commented 4 years ago

basically you set the change with .addTempo.

the following script doubles the 120 BPM three times in the major scale

from midiutil import MIDIFile

degrees  = [60, 62, 64, 65, 67, 69, 71, 72]  # MIDI note number
track    = 0
channel  = 0
time     = 0    # In beats
duration = 1    # In beats
tempo    = 60   # In BPM
volume   = 100  # 0-127, as per the MIDI standard

midi = MIDIFile(1)  # One track
midi.addTempo(track, time, tempo)
midi.addTempo(track, len(degrees), tempo*2)
midi.addTempo(track, len(degrees)*2, tempo*4)

for i, pitch in enumerate(degrees+degrees+degrees):
    midi.addNote(track, channel, pitch, time + i, duration, volume)

with open("major-scale.mid", "wb") as f:
    midi.writeFile(f)