tags:

views:

1267

answers:

2

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).

+5  A: 

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: http://stackoverflow.com/questions/569321/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
Vinko Vrsalovic
Ah well, doesn't look like I'm going to get any more direct technique, so thanks for this one, @Vinko.
Alex Martelli
So much for SO being excellent, huh? :-) When you **REALLY** need it, it always fails.
Vinko Vrsalovic
+7  A: 

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...]

alex rae
Python module for reading, writing, creating/editing etc midi files: <http://www.mxm.dk/products/public/pythonmidi>
alex rae
Class for reading and writing midi files (this posting seems to be the only source): <http://groups.google.com/group/alt.sources/msg/0c5fc523e050c35e>
alex rae
I also found this, though i haven't tried it: <http://groups.google.com/group/comp.lang.python/browse_thread/thread/65fa6c4bb74bca17>
alex rae
@alex, excellent pointers, thanks!
Alex Martelli