Hi all,

I'm writing a simple MIDI player (which I might eventually turn into a editor, if I feel ambitious). To that end, I've been futzing around with the pygame.mixer and pygame.mixer.music modules. I can get MIDI files to play, but I would also like to be able to grab and display their lengths (in minutes and seconds) upon loading them. Can this be done within pygame? Does anyone out there know?

I know you can use get_length() on Sound objects, but so far I have been unable to coax the mixer into converting MIDI files into Sound objects (probably because they really aren't sounds, at all). Am I barking up the wrong tree, here?

Thanks in advance.

Recommended Answers

All 6 Replies


what operating system are you using?

I am a little frustrated with PyGame, since I can never get a midi file to play! PyGame is based on SDL, when I use its SDL counterpart in Ruby it plays midi files!

In my Delphi days I used the MPlayer component, and that one plays midi files and shows a progress bar. I even bound the progress information to a slide show. So it must be possible to get the length information for midi files.

Hi vegaseat,

I'm running Debian Linux, stock kernel 2.4.27. I'm softsynthesizing with TiMidity and the eawpatches patch set. In general, I have found getting MIDI to work in Linux to be a real pain, though it is working now - pygame can play MIDI files perfectly. If you want to trade code, we can, but I suspect that your problems have more to do with your MIDI/OS setup, since the pygame code is pretty straightforward. On a more general level, I sympathize with your MIDI-frustration :cry:

(What's really got my goat now is that I wrote a basic MIDI editor/player in Java more than a year ago. Since that time, I've reinstalled Debian and now I can't get the thing to work!)

I think the problem with going through pygame.mixer.Sound is that MIDI files aren't waveform data, per se, and that's what the Sound constructor expects. Another strategy would be to use the Python Midi package from mxm (here), which involves actually looking at the nuts and bolts (and notes) in the file and figuring out a length from there, but I want to avoid that headache if I can.

So - any pygamers out there know how to do this?

Ooh, update.

I just got the Java player/editor working five minutes ago. And I'm apparently not synthesizing with Timidity/eawpatches - Python and Java both synthesize the sound themselves (I think).

Got myself turned around, sorry, sorry. I'd still like to know if there's a way to check MIDI file length in pygame, though.

you and vegaseat seem to share fascination with midi music files. I have explored vegaseat's code snippet
and it work well at least on Windows XP. Midi is great stuff!

I am thinking that pythonmidi is the same package as PMIDI, at least the files/directories are the same.

I am getting old!

I did suddenly remember that I posted a Python snippet that used the length of an audio file, including .MID files.

Take a look at:

It uses wx.media.MediaCtrl() and should work on Linux. You may have to give it the right backend.

Thanks vegaseat, I will take a look.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.