Re: [pygame] duration of song
Ian Mallett [EMAIL PROTECTED] wrote: On 7/13/07, Jason Coggins [EMAIL PROTECTED] wrote: Actually I was thinking of using the length of the music played as a variable in the program. my_sound = pygame.mixer.sound([filename]) number_of_seconds_long = my_sound.get_length() That works for OGG and WAV sounds, but does not work for other music files, like MP3, MOD, and MIDI, right? The only thing I can think of is: - play the music through pygame.mixer.music.play() - monitor the playback time with pygame.mixer.music.get_pos() - when pygame.mixer.music.get_busy() returns False, you know how long the music file was. If it helps, you'll know for next time. That's probably not very helpful, either. Which file format are you using for your music? Perhaps there is another Python library that you can use to get the run length of your file. -Dave LeCompte
Re: [pygame] duration of song
On 7/14/07, Dave LeCompte (really) [EMAIL PROTECTED] wrote: Ian Mallett [EMAIL PROTECTED] wrote: On 7/13/07, Jason Coggins [EMAIL PROTECTED] wrote: Actually I was thinking of using the length of the music played as a variable in the program. my_sound = pygame.mixer.sound([filename]) number_of_seconds_long = my_sound.get_length() That works for OGG and WAV sounds, but does not work for other music files, like MP3, MOD, and MIDI, right? Oh, opps. You're probably right. I'm curious now, why don't you know the length of the file? The only thing I can think of is: - play the music through pygame.mixer.music.play() - monitor the playback time with pygame.mixer.music.get_pos() - when pygame.mixer.music.get_busy() returns False, you know how long the music file was. If it helps, you'll know for next time. That's probably not very helpful, either. Which file format are you using for your music? Perhaps there is another Python library that you can use to get the run length of your file. -Dave LeCompte
Re: [pygame] duration of song
Ian Mallett [EMAIL PROTECTED] wrote: On 7/14/07, Dave LeCompte (really) [EMAIL PROTECTED] wrote: That works for OGG and WAV sounds, but does not work for other music files, like MP3, MOD, and MIDI, right? Oh, opps. You're probably right. I'm curious now, why don't you know the length of the file? Conceivably, you might want to intelligently deal with music provided by the user. For example: - some games, including The Sims, allow players to put their own music into a certain directory, and the game will take advantage of the music. - some games, including Monster Rancher, use user-provided music as an integral part of gameplay. With Monster Rancher, players inserted a CD, which seeded the procedural content routines for making monsters. I was thinking more about some crude ways to get approximate answers, and I was thinking that the file size on disk might be a guide to the duration of the song. For MP3, I think typical compression ratios (vs a 44kHz 16 bit stereo WAV file) is something like 11:1, so you can do the math (or, better yet, just look at a collection of known MP3s and figure out an average multipler) to convert from bytes to seconds. However, this doesn't work for MIDI or MOD music - for MIDI, a song with four instruments playing in harmony might use four times the space as a solo for the same length of time. MOD music is even less predictable, as the sound patches are a large part of the file size, and there's little knowing how many times any patch might be used. That said, I think that specific modules for the various file types exist that could be used to determine the duration of MP3 and MIDI, and maybe even MOD. -Dave LeCompte
Re: [pygame] duration of song
PyMedia claims to support this, though it mentions you may need to play the file for a second or two to initialize decoding and get the full length. --Noah Dave LeCompte (really) wrote: Ian Mallett [EMAIL PROTECTED] wrote: On 7/14/07, Dave LeCompte (really) [EMAIL PROTECTED] wrote: That works for OGG and WAV sounds, but does not work for other music files, like MP3, MOD, and MIDI, right? Oh, opps. You're probably right. I'm curious now, why don't you know the length of the file? Conceivably, you might want to intelligently deal with music provided by the user. For example: - some games, including The Sims, allow players to put their own music into a certain directory, and the game will take advantage of the music. - some games, including Monster Rancher, use user-provided music as an integral part of gameplay. With Monster Rancher, players inserted a CD, which seeded the procedural content routines for making monsters. I was thinking more about some crude ways to get approximate answers, and I was thinking that the file size on disk might be a guide to the duration of the song. For MP3, I think typical compression ratios (vs a 44kHz 16 bit stereo WAV file) is something like 11:1, so you can do the math (or, better yet, just look at a collection of known MP3s and figure out an average multipler) to convert from bytes to seconds. However, this doesn't work for MIDI or MOD music - for MIDI, a song with four instruments playing in harmony might use four times the space as a solo for the same length of time. MOD music is even less predictable, as the sound patches are a large part of the file size, and there's little knowing how many times any patch might be used. That said, I think that specific modules for the various file types exist that could be used to determine the duration of MP3 and MIDI, and maybe even MOD. -Dave LeCompte signature.asc Description: OpenPGP digital signature
Re: [pygame] duration of song
Upon further experimentation I have found that this method does only work for .ogg and .wav files but not .mp3 files and such. I can tell the size of the file in bytes simply by viewing details of the file in a window but I was wanting to use different sound files (including some the user could load) and thus I needed a way for the program to determine the duration (in seconds) of the music file since the music file being played would vary. Jason - Original Message - From: Ian Mallett To: pygame-users@seul.org Sent: Saturday, July 14, 2007 4:16 AM Subject: Re: [pygame] duration of song On 7/14/07, Dave LeCompte (really) [EMAIL PROTECTED] wrote: Ian Mallett [EMAIL PROTECTED] wrote: On 7/13/07, Jason Coggins [EMAIL PROTECTED] wrote: Actually I was thinking of using the length of the music played as a variable in the program. my_sound = pygame.mixer.sound([filename]) number_of_seconds_long = my_sound.get_length() That works for OGG and WAV sounds, but does not work for other music files, like MP3, MOD, and MIDI, right? Oh, opps. You're probably right. I'm curious now, why don't you know the length of the file? The only thing I can think of is: - play the music through pygame.mixer.music.play () - monitor the playback time with pygame.mixer.music.get_pos() - when pygame.mixer.music.get_busy() returns False, you know how long the music file was. If it helps, you'll know for next time. That's probably not very helpful, either. Which file format are you using for your music? Perhaps there is another Python library that you can use to get the run length of your file. -Dave LeCompte
Re: [pygame] duration of song
Jason Coggins [EMAIL PROTECTED] wrote: I was wanting to use .mp3 files. Perhaps this suits your needs: http://py.vaults.ca/apyllo.py?i=6226313 import mp3 mp3.mp3info(you-spin-me-round.mp3) {'STEREO': False, 'FREQUENCY': 44100, 'COPYRIGHT': 0L, 'MM': 3, 'LAYER': 2, 'SS' : 2, 'VERSION': 1L, 'MODE': 3L, 'BITRATE': 128} The duration in seconds would be MM*60+SS - although, I'd be suspicious of the reliability of the data it generates. For variable bit rate MP3 files, it uses an average bit rate field in the header, which I suspect might not always be there, and if it is, it may not be accurate. It might be close enough for your needs, however. -Dave LeCompte
[pygame] What is wrong with this
I am trying to create a very simple program that makes a list of the music files in a folder, sorts the files into alphabetical order and then plays each file in the folder. The program below plays the first file in the folder and then freezes without playing any of the remaining songs. import pygame, os def findFiles(location): files = [] for item in os.listdir(location): files.append(item) files.sort() return files def playMusic(music) clock=pygame.time.Clock() pygame.mixer.music.load(music) pygame.mixer.music.play(0, 0.0) while pygame.mixer.music.get_busy(): clock.tick(300) return def main(): pygame.init() pygame.mixer.init() destinationPath = /home/owner/Desktop/playList os.chdir(destinationPath) playList = [] playList = findFiles(destinationPath) for item in playList: playMusic(item) Jason
[pygame] Re: What is wrong with this
I found the problem. It had nothing to do with the part of the program that was playing the music (which I reproduced with the original email) but another part of the program that was starting after the music finished and was holding up the entire program. Thanks for taking a look though. Jason
Re: [pygame] What is wrong with this
On Sat, Jul 14, 2007 at 02:31:20PM -0400, Jason Coggins wrote: I am trying to create a very simple program that makes a list of the music files in a folder, sorts the files into alphabetical order and then plays each file in the folder. The program below plays the first file in the folder and then freezes without playing any of the remaining songs. I'm glad you found the problem, but I cannot refuse the opportunity to offer some generic Python style suggestions. import pygame, os def findFiles(location): files = [] for item in os.listdir(location): files.append(item) This is a bit pointless. You're taking a list of files and converting it to another list, one item at a time. files = os.listdir(location) would work perfectly well. files.sort() return files (And if you don't care about older Python versions, you could do def findFiles(location): return sorted(os.listdir(location) ) def playMusic(music) clock=pygame.time.Clock() I cringe when I see an assignment statement squished together without any spaces around the '='. pygame.mixer.music.load(music) pygame.mixer.music.play(0, 0.0) while pygame.mixer.music.get_busy(): clock.tick(300) return Empty returns at the end of a function are a bit pointless. def main(): pygame.init() pygame.mixer.init() destinationPath = /home/owner/Desktop/playList os.chdir(destinationPath) playList = [] This assignment is pointless. You're reassigning a different value to playList on the very next line. playList = findFiles(destinationPath) for item in playList: playMusic(item) Cheers! Marius Gedminas -- C, n: A programming language that is sort of like Pascal except more like assembly except that it isn't very much like either one, or anything else. It is either the best language available to the art today, or it isn't. -- Ray Simard signature.asc Description: Digital signature
Re: [pygame] What is wrong with this
Thanks for the suggestions. I am just learning python and I will take them to heart. Jason - Original Message - From: Marius Gedminas [EMAIL PROTECTED] To: pygame-users@seul.org Sent: Saturday, July 14, 2007 5:45 PM Subject: Re: [pygame] What is wrong with this