Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "slow down" a MIDI file (ideally in Python)?

Tags:

python

midi

I have background music for some songs available in both .MID and .KAR formats, but in each case it's being played somewhat faster than I'd like. What's the simplest way to create either .MID or .KAR files with the same content but at a slower tempo -- say, one slowed down by 20% or so, another by 15%, a third by 25%, and so on?

Ideally, I'd prefer a cross-platform Python script (since that would allow me to easily experimentally tweak the source to converge to the exact effect I want;-), but I'll take any free solution that runs in Linux (Ubuntu 8.04 if it matters) and Mac (Mac OS X 10.5, but 10.6 compatibility preferred).

like image 736
Alex Martelli Avatar asked Sep 06 '09 00:09

Alex Martelli


People also ask

Do MIDI files have tempo?

Unlike music, tempo in MIDI is not given as beats per minute, but rather in microseconds per beat. The default tempo is 500000 microseconds per beat, which is 120 beats per minute.

Can Python read MIDI files?

MIDI is an extremely popular and versatile format for music data, whether you're using it as a digital musical instrument interface or just transcribing music in it to show your bandmates new songs. Mido is a Python library you can use to interact with MIDI in your code.


2 Answers

As Vinko says, you can edit the midifile, but since it's a binary format, squeezed into the least number of bits possible, it helps to have help.

This is a midi-to-text converter (and vice-versa):
http://midicomp.opensrc.org/
I've been using it quite a bit lately. it's pretty trivial to do text processing (e.g. searching for line with "Tempo") for simple operations once you have the midifile as text. haven't tried on mac (compiled with no problem on ubuntu 8.04).

Regarding midifile tempo specifically, it's really easy to slow down or speed up playback since the timing of events is specified in terms of "ticks", whose real duration in seconds is determined by the tempo parameter described in Vinko's quote. I believe time signature is not so relevant, and is mainly for displaying bars/beats correctly when opened in a midi sequencer.

Also, aside from pyPortMidi, there are a couple of other python midi modules around.

[hmmm... it seems i can only post on link per post, being a new user. i'll try posting the links to the python modules in a couple comments or another couple answers...]

like image 195
alex rae Avatar answered Oct 05 '22 22:10

alex rae


You could edit the file, as per http://www.sonicspot.com/guide/midifiles.html

Although there probably is a MIDI reading/writing library already. In fact, it was a matter of seeing the related questions: Simple, Cross Platform MIDI Library for Python

Set Tempo

This meta event sets the sequence tempo in terms of microseconds per quarter-note which is encoded in three bytes. It usually is found in the first track chunk, time-aligned to occur at the same time as a MIDI clock message to promote more accurate synchronization. If no set tempo event is present, 120 beats per minute is assumed. The following formula's can be used to translate the tempo from microseconds per quarter-note to beats per minute and back.

MICROSECONDS_PER_MINUTE = 60000000

BPM = MICROSECONDS_PER_MINUTE / MPQN
MPQN = MICROSECONDS_PER_MINUTE / BPM
Meta Event  Type    Length  Microseconds/Quarter-Note
255 (0xFF)  81 (0x51)   3   0-8355711
like image 31
Vinko Vrsalovic Avatar answered Oct 06 '22 00:10

Vinko Vrsalovic