954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Using pygame to get MIDI file length

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.

G-Do
Junior Poster
147 posts since Jun 2005
Reputation Points: 41
Solved Threads: 31
 

G-Do,

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.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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?

G-Do
Junior Poster
147 posts since Jun 2005
Reputation Points: 41
Solved Threads: 31
 

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.

G-Do
Junior Poster
147 posts since Jun 2005
Reputation Points: 41
Solved Threads: 31
 

G-Do,
you and vegaseat seem to share fascination with midi music files. I have explored vegaseat's code snippet
http://www.daniweb.com/code/snippet431.html
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.

bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

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:
http://www.daniweb.com/code/snippet467.html

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

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Thanks vegaseat, I will take a look.

G-Do
Junior Poster
147 posts since Jun 2005
Reputation Points: 41
Solved Threads: 31
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You