Python Forum
How to execute tempo changes in Mido?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to execute tempo changes in Mido?
#1
I'm attempting to create a simple Python script using Mido to execute the writing of a midi file that has tempo changes embedded. Right now I think I'm getting stuck on how to operate "delta time" when iterating over the different tempo changes. I'll post the code I've currently got. The issue I keep running into is that the first tempo change occurs at the correct time (referring to the first tempo CHANGE of 98 bpm, not the INITIAL tempo of 98b bpm, but all of the tempo changes after that) but all of the changes after that are too late and spread out. Any help is greatly appreciated:

import mido
from mido import MidiFile, MidiTrack, MetaMessage

def create_tempo_map_midi(positions, tempos, sample_rate=44100, output_midi_file='tempo_map.mid'):
    midi = MidiFile()
    track = MidiTrack()
    midi.tracks.append(track)
    ticks_per_beat = 480

    current_ticks = 0  # Track the cumulative ticks

    for position, tempo in zip(positions, tempos):
        bpm_to_microseconds = 60_000_000 / tempo  # Convert BPM to microseconds per beat
        time_seconds = position / sample_rate

        # Calculate the ticks directly without using mido.second2tick
        time_ticks = int(time_seconds * ticks_per_beat * tempo / 60)

        # Ensure that the calculated time_ticks is non-negative
        delta_ticks = max(0, time_ticks - current_ticks)

        # Add the tempo change event to the track
        track.append(MetaMessage('set_tempo', tempo=int(bpm_to_microseconds), time=delta_ticks))

        # Update current_ticks
        current_ticks = time_ticks

    # Save the MIDI file
    midi.save(output_midi_file)

if __name__ == "__main__":
    tempo_values = [98.0, 98.0, 101.5467, 103.3155, 105.0865, 106.8571, 108.6168, 110.3756, 112.1227, 113.8698, 115.6076, 117.3423, 119.0782, 120.8079, 122.5382, 124.2676, 126.0, 156.0, 156.0, 152.5883, 149.1766, 145.7649, 142.3532, 138.9415, 135.5298, 132.1181, 128.7064, 125.2947, 121.883, 118.4713, 115.0596, 111.6479, 108.2362, 104.8245, 98.0]
    pos_values = [0, 1404000, 1417500, 1430528, 1443333, 1455922, 1468303, 1480483, 1492469, 1504268, 1515886, 1527329, 1538603, 1549713, 1560664, 1571460, 1592242, 1592745, 8309514, 8317994, 8326664, 8335532, 8344608, 8353901, 8363422, 8373183, 8383196, 8393475, 8404034, 8414888, 8426055, 8437553, 8449402, 8461625, 8474246]
    create_tempo_map_midi(pos_values, tempo_values)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  mido MIDI-file; how to distinguis tracks? philipbergwerf 2 2,313 Dec-10-2021, 05:06 PM
Last Post: philipbergwerf
  Mido and virtual midi ports dore_m 2 4,683 Dec-27-2020, 06:02 AM
Last Post: dore_m
  MIDI FILES TEMPO - MIDO Tetsuo30 9 5,095 Sep-06-2020, 08:17 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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