Re: [pygame] Obtain actual volume of sample being played
How do you access the sndarray for a sound loaded from a .wav sample? From what I can tell, you can either create a Sound object from a .wav file, or from a sndarray object, but I don't see how you can access the backing sndarray from a .wav Sound... Sorry if I am missing something obvious. Cheers Forrest Voight wrote: You could access a sndarray and average the current portion of it. You could probably even use numpy to do a fourier transform on it. On Tue, Jul 15, 2008 at 12:26 PM, Wyatt Olson [EMAIL PROTECTED] wrote: Hello, Does anyone know if it is possible to obtain the actual volume of the sample being played at a given moment? This is not the same as getting the volume level which was set earlier, on either the sample or the channel. For instance, assume that I am playing a sample which starts at low volume (-70dB), increases to 0dB after 5 seconds, then fades out to -70dB again. If I play that sample at volume = 1.0, I would want to be able to return an approximation of the actual dB value at the given time. For instance, if I were to query it at 0 seconds, I would get 0.01; querying at 5 seconds would return 1.0; querying at 7.5 seconds would give 0.5 or something. Make sense? I would want to have this controllable at the channel and / or sample level, not the mixer - I need to show each channel separately. I don't care about linear / logarithmic conversions or anything... I essentially just need a number from 0 to 9 indicating very approximately how loud the sound is. I want this information to display semi-accurate VU / peak meter information, based on actual samples. This is for my slave software for an electronic drum set. Currently I show VU information based on the velocity of a drum pad hit, which is decent, but it would be nice to use the actual sample information as well. I have looked through the documentation, and I don't think it can be done, but I figured I may as well submit this question to the gurus on teh interwebs. 8-) Cheers
Re: [pygame] Mixer Quit / Restart
Cool! I'll await the fix whenever SDL and then Pygame release the change. Cheers Lenard Lindstrom wrote: This bug was already known and was fixed in the SDL SVN. Lenard Lenard Lindstrom wrote: The problem is with Mix_SetPanning in SDL_mixer. The function is called by Pygame.mixer.Channel.set_volume. Lenard Wyatt Olson wrote: Oh well, that's fine. I will just inform potential users that they must convert all samples to a specific format first, and then not restart the mixer when loading new sounds. Given that this software is to be used in a (relatively complex) open source DIY electronic drum brain, I would imagine that converting audio samples is probably the easiest of all the other required steps (soldering ICs, programming microcontrollers, etc). ;-) Cheers FT wrote: Hi! It never worked, it is just that it never worked. Wyatt Olson mailto:[EMAIL PROTECTED] *To:* pygame-users@seul.org mailto:pygame-users@seul.org This appears to get the total number of channels, not the number of active channels. However, I tried using get_busy(), but it still does not seem to work. See the following modifications to my original code: import pygame, time for x in range(10): print(Starting iteration + str(x)) print(Initializing mixer) pygame.mixer.init() print(Loading sound) sound = pygame.mixer.Sound(foo.wav) print(Finding free channel) channel = pygame.mixer.find_channel() print(Channel object: + str(channel)) print(Setting volume) channel.set_volume(0.7) print(Playing sound) channel.play(sound) print(Sleeping until sound is finished playing...) while pygame.mixer.get_busy(): time.sleep(1) print(Quitting mixer\n\n) pygame.mixer.quit() This displays the same behaviour. In response to a previous comment about using channel.stop() before quitting the mixer: this does not work either, as can be seen by adding channel.stop() after the get_busy loop above. Has anyone tried running this code themselves? I am wondering if I am experiencing some obscure bug, possibly platform specific. I am running on OS X, but don't have access to other systems for troubleshooting. If someone else has access to a Windows or Linux box, and is able to prove if this fails on those systems as well, that may be useful. Cheers Ian Mallett wrote: On 7/2/08, *Wyatt Olson* [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Is there anything which can return a list of all currently active channels? pygame.mixer.get_num_channels() This returns 0 if none are playing and 0 if there are. signature.asc Description: OpenPGP digital signature
Re: [pygame] Mixer Quit / Restart
Oh well, that's fine. I will just inform potential users that they must convert all samples to a specific format first, and then not restart the mixer when loading new sounds. Given that this software is to be used in a (relatively complex) open source DIY electronic drum brain, I would imagine that converting audio samples is probably the easiest of all the other required steps (soldering ICs, programming microcontrollers, etc). ;-) Cheers FT wrote: Hi! It never worked, it is just that it never worked. Wyatt Olson mailto:[EMAIL PROTECTED] *To:* pygame-users@seul.org mailto:pygame-users@seul.org This appears to get the total number of channels, not the number of active channels. However, I tried using get_busy(), but it still does not seem to work. See the following modifications to my original code: import pygame, time for x in range(10): print(Starting iteration + str(x)) print(Initializing mixer) pygame.mixer.init() print(Loading sound) sound = pygame.mixer.Sound(foo.wav) print(Finding free channel) channel = pygame.mixer.find_channel() print(Channel object: + str(channel)) print(Setting volume) channel.set_volume(0.7) print(Playing sound) channel.play(sound) print(Sleeping until sound is finished playing...) while pygame.mixer.get_busy(): time.sleep(1) print(Quitting mixer\n\n) pygame.mixer.quit() This displays the same behaviour. In response to a previous comment about using channel.stop() before quitting the mixer: this does not work either, as can be seen by adding channel.stop() after the get_busy loop above. Has anyone tried running this code themselves? I am wondering if I am experiencing some obscure bug, possibly platform specific. I am running on OS X, but don't have access to other systems for troubleshooting. If someone else has access to a Windows or Linux box, and is able to prove if this fails on those systems as well, that may be useful. Cheers Ian Mallett wrote: On 7/2/08, *Wyatt Olson* [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Is there anything which can return a list of all currently active channels? pygame.mixer.get_num_channels() This returns 0 if none are playing and 0 if there are.
Re: [pygame] Mixer Quit / Restart
I have not done that, as I don't keep track of channels after they are obtained and started playing. Is there anything which can return a list of all currently active channels? Now that being said, in my real program, I am able to recreate the bug even when waiting for all currently playing sounds to stop, before selecting the new configuration. Unless channel.stop() results in a different state from just leaving the channel to finish playing the sound by itself, I don't think that it should matter. Cheers Lenard Lindstrom wrote: Did you try calling Channel.stop before calling quit? Lenard Wyatt Olson wrote: This seems to work. It would be nice to be able to re-init the mixer, in case there were different bitrates, but if that can't work, I can just specify that the samples must all be the same bitrate. Thanks for the quick replies, all! Cheers On Tue, Jul 1, 2008 at 2:22 PM, Wyatt Olson [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Cool, I'll give that a shot, and let you know what happens. Cheers On Tue, Jul 1, 2008 at 2:20 PM, Ian Mallett [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: James, yes, I realized this was a test case, the questions apply to that. Thanks for clearing that up. :-) Why reinit the mixer? Are the sounds necessarily different bitrates? If you want to delete things, I believe you can use del(pointer_to_sound) or something, which should not require a mixer reinit.
Re: [pygame] Mixer Quit / Restart
This appears to get the total number of channels, not the number of active channels. However, I tried using get_busy(), but it still does not seem to work. See the following modifications to my original code: import pygame, time for x in range(10): print(Starting iteration + str(x)) print(Initializing mixer) pygame.mixer.init() print(Loading sound) sound = pygame.mixer.Sound(foo.wav) print(Finding free channel) channel = pygame.mixer.find_channel() print(Channel object: + str(channel)) print(Setting volume) channel.set_volume(0.7) print(Playing sound) channel.play(sound) print(Sleeping until sound is finished playing...) while pygame.mixer.get_busy(): time.sleep(1) print(Quitting mixer\n\n) pygame.mixer.quit() This displays the same behaviour. In response to a previous comment about using channel.stop() before quitting the mixer: this does not work either, as can be seen by adding channel.stop() after the get_busy loop above. Has anyone tried running this code themselves? I am wondering if I am experiencing some obscure bug, possibly platform specific. I am running on OS X, but don't have access to other systems for troubleshooting. If someone else has access to a Windows or Linux box, and is able to prove if this fails on those systems as well, that may be useful. Cheers Ian Mallett wrote: On 7/2/08, *Wyatt Olson* [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Is there anything which can return a list of all currently active channels? pygame.mixer.get_num_channels() This returns 0 if none are playing and 0 if there are. signature.asc Description: OpenPGP digital signature
Re: [pygame] Mixer Quit / Restart
Cool, I'll give that a shot, and let you know what happens. Cheers On Tue, Jul 1, 2008 at 2:20 PM, Ian Mallett [EMAIL PROTECTED] wrote: James, yes, I realized this was a test case, the questions apply to that. Thanks for clearing that up. :-) Why reinit the mixer? Are the sounds necessarily different bitrates? If you want to delete things, I believe you can use del(pointer_to_sound) or something, which should not require a mixer reinit. Ian -- Wyatt Olson http://thecave.homeunix.org
Re: [pygame] Mixer Quit / Restart
This seems to work. It would be nice to be able to re-init the mixer, in case there were different bitrates, but if that can't work, I can just specify that the samples must all be the same bitrate. Thanks for the quick replies, all! Cheers On Tue, Jul 1, 2008 at 2:22 PM, Wyatt Olson [EMAIL PROTECTED] wrote: Cool, I'll give that a shot, and let you know what happens. Cheers On Tue, Jul 1, 2008 at 2:20 PM, Ian Mallett [EMAIL PROTECTED] wrote: James, yes, I realized this was a test case, the questions apply to that. Thanks for clearing that up. :-) Why reinit the mixer? Are the sounds necessarily different bitrates? If you want to delete things, I believe you can use del(pointer_to_sound) or something, which should not require a mixer reinit. Ian -- Wyatt Olson http://thecave.homeunix.org -- Wyatt Olson http://thecave.homeunix.org
Re: [pygame] full keyboard control
I could be wrong here (I have not used Windows for years), but I thought that the whole point of Ctrl Alt Delete to login was that this sequence of keys could not be grabbed by anything other than the OS itself - it is a 'secure channel' or something like that. My guess is that you will not be able to intercept this with a user level process, because that would defeat the security of this key sequence. (Basically, if you hit Ctrl Alt Delete, you can be sure that there is no trojan putting up a 'login' screen to steal your password). Cheers Lamonte(Scheols/Demonic) wrote: I'm pretty sure if you set full screen mode and do checks to see if they pressed ctrl+alt+del do nothing :) On Tue, Apr 8, 2008 at 11:22 AM, Brian Davis [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: I've read these arguments elsewhere and fully agree with them, except in this case. What I'm making is a program for my 1 year-old daughter to bang on the keyboard and hear cool sounds and see colors change on the screen. So I suppose ctrl-alt-delete isn't a big worry but the windows key definitely is! And even alt-tab would be possible for her to hit. The only option I've found so far is a registry hack and I haven't tested it out yet. Does anyone know if there are any options in pygame or SDL for disabling the windows key(s)? I should have said what I was doing in my original email. Sorry. Thanks, -Brian On Mon, Apr 7, 2008 at 6:59 PM, Greg Ewing [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: Ian Mallett wrote: I don't think so. Think about it. If your program were to crash and lock up, the only way to stop it would be to restart the computer. Besides, it annoys me when a game has the audacity to assume I won't want to do *anything* else with my computer while it's running. Like switching away to consult a hints file, for instance. -- Greg -- Join cleanscript.com http://cleanscript.com Come here for professional PHP coding.
Re: [pygame] Mixer latency
Well, this code gives the same results as pygame. That definitely lends to your thoughts that the latency is from somewhere else. While this is not directly related to pygame, I suppose the next question would be, do you have any thoughts on how to measure (and discover the source) of this latency? Since pygame sounds don't block when they are played, it is not possible to just measure time.clock() before and after the sound.play(). One could wait for the end sound event, but I doubt that these events themselves are sent with zero latency, so that would just add another potential source of latency. I have done tests on both the serial port and the sound processing code (i.e, that which determines which sound to play, all the way up to sound.play() itself), and both of those components appear to have minimal latency (less than 1 ms). The only source I can think of for this latency is the mixer / playing routines themselves. Anyways, I thank you all kindly for your help. Cheers Brian Fisher wrote: That's a fascinating way to measure the latency - however it may not be a good way to measure pygame mixer's own latency specifically (it's really odd if you can't see a difference with different mixer buffer settings). If you believe gamer keyboard sales people marketing, depending on the hardware keyboards can have latencies up to like 100ms all on there own. http://www.pcmag.com/article2/0,1759,1908876,00.asp Also, I wonder what results the same test would give using something other than pygame to play the sound? here's some code on the mac that may be able to do that: --- from AppKit import NSSound class MacSound: def __init__(self, filepath): self._sound = NSSound.alloc() self._sound.initWithContentsOfFile_byReference_(filepath, True) def play(self): self._sound.play() def stop(self): self._sound.stop() def is_playing(self): return self._sound.isPlaying() that way you'd have some other data point for comparison. On Sun, Mar 30, 2008 at 9:26 PM, Wyatt Olson [EMAIL PROTECTED] wrote: Once the application starts, type 0:1023 (without quotes), and hit enter. A drum sound will play. I am measuring latency by recording the whole thing in an audio recorder, and measuring the difference in time between the sound of me hitting enter and the sound of the sample being played. In my tests, it seems to average to about 65 or 70 ms, with a maximum of about 100ms. signature.asc Description: OpenPGP digital signature
Re: [pygame] Mixer latency
OK, I have done some more tests tonight on both NSSound and pygame, and I think that the results from yesterday may have been anomalies. Your thoughts on keyboard latency may also be correct. I did some more tests using the serial port device, instead of the keyboard, and this time the results were much more promising, with both sound libraries. I don't know what the difference could be; the only change was a reboot between yesterday and today, for a security update. Perhaps there was to much stuff running yesterday, and there was too much swapping going on; who knows. Anyways, I will keep you all informed of my progress, but hopefully this will continue to work tomorrow as well 8-) Cheers Brian Fisher wrote: That's a fascinating way to measure the latency - however it may not be a good way to measure pygame mixer's own latency specifically (it's really odd if you can't see a difference with different mixer buffer settings). If you believe gamer keyboard sales people marketing, depending on the hardware keyboards can have latencies up to like 100ms all on there own. http://www.pcmag.com/article2/0,1759,1908876,00.asp Also, I wonder what results the same test would give using something other than pygame to play the sound? here's some code on the mac that may be able to do that: --- from AppKit import NSSound class MacSound: def __init__(self, filepath): self._sound = NSSound.alloc() self._sound.initWithContentsOfFile_byReference_(filepath, True) def play(self): self._sound.play() def stop(self): self._sound.stop() def is_playing(self): return self._sound.isPlaying() that way you'd have some other data point for comparison. On Sun, Mar 30, 2008 at 9:26 PM, Wyatt Olson [EMAIL PROTECTED] wrote: Once the application starts, type 0:1023 (without quotes), and hit enter. A drum sound will play. I am measuring latency by recording the whole thing in an audio recorder, and measuring the difference in time between the sound of me hitting enter and the sound of the sample being played. In my tests, it seems to average to about 65 or 70 ms, with a maximum of about 100ms. signature.asc Description: OpenPGP digital signature
Re: [pygame] Mixer latency
It is a laptop, but I don't see this problem on any other applications. Being a Mac, there are lots of audio / video applications, and all work in real time without problem. It seems to be specific to Python. Anyways, thanks for your help - I will look around to see if I can find any settings which affect the output; if I do, I will post them here. Cheers René Dudfield wrote: Latency is really reliant on the sound card, driver and OS. If you've got an inbuilt sound card, the latency is generally pretty bad. Do you get bad latency with other programs? cheers, On Sun, Mar 30, 2008 at 3:16 PM, Wyatt Olson [EMAIL PROTECTED] wrote: Hello all, I am in the process of making a real time drum sequencer program, but I am experiencing some problems with using Pygame mixer. There seems to be approximately a 100ms delay between issuing the sound.play() command and actually hearing the sound play. To reach an acceptable level, I must decrease this to a maximum of about 10ms. I have tried adjusting the buffer size, the number of sounds loaded, etc, to no avail. I am running pygame 1.7.1 on OS X. Does anyone have any suggestions on what I can try? Thanks! signature.asc Description: OpenPGP digital signature
Re: [pygame] Mixer latency
Upgraded to pygame 1.8 / python 2.5 - still the same latency. I am thinking that it may be related to SDL itself, and not the python wrapper per se... I have seen some suggestions on lowering the chunk size in SDL. Does this map directly to the buffer size in pygame, or is there some magic happining behind the scenes? Thanks Wayne Koorts wrote: Does anyone have any suggestions on what I can try? Wouldn't hurt to upgrade to Pygame 1.8 for a start. Regards, Wayne signature.asc Description: OpenPGP digital signature
Re: [pygame] Mixer latency
Yes, I have tried all sorts of buffer sizes (all the way down to 8). I can't see any difference in performance with any of these. As for the source, I tool a look at mixer.c, but to be honest I am not as up to date on my C as I would like - I have concentrated on higher level languages for a bit too long, and have forgotten the details of these older ones. 8-( As for the program, you can access it at http://drummaster.thecave.homeunix.org/files/DrumSlave_Minimal.zip . This only has one sample (a snare drum) for testing, which is mapped to port 0. Other than that, it is exactly as I am using it. To run, unzip and use the command: python drum_slave.py --keyboard rock It should run on python 2.4 or 2.5, with any semi-recent version of pygame. The application is meant to be the slave for a hardware board I developed, which talks to the computer via a serial port. To allow testing, I added the keyboard mode, in which you can manually type the commands which otherwise would come over the serial port. Once the application starts, type 0:1023 (without quotes), and hit enter. A drum sound will play. I am measuring latency by recording the whole thing in an audio recorder, and measuring the difference in time between the sound of me hitting enter and the sound of the sample being played. In my tests, it seems to average to about 65 or 70 ms, with a maximum of about 100ms. As for program layout, the initialization is done in slave.py. drum_slave.py is just the command line wrapper. Pygame Sound objects are played in hardware/sensor.py. I don't think that anything else would be relevant to this discussion, but it should be simple enough to find your way around in, if you are interested - the program is not very large. All my testing so far has been done on OS X. I am planning to try some tests on Linux, once I can get a box put together. If anyone does test this on a different platform, I would be very interested in hearing the results, and how they compare to mine. Thanks for all your help! Cheers René Dudfield wrote: Hello, Do you have a link to your code? You've tried setting a really small buffer size? Like 512 or something? Have a look at src/mixer.c to see how SDL is used. cheers, On Mon, Mar 31, 2008 at 2:50 PM, Wyatt Olson [EMAIL PROTECTED] wrote: Upgraded to pygame 1.8 / python 2.5 - still the same latency. I am thinking that it may be related to SDL itself, and not the python wrapper per se... I have seen some suggestions on lowering the chunk size in SDL. Does this map directly to the buffer size in pygame, or is there some magic happining behind the scenes? Thanks Wayne Koorts wrote: Does anyone have any suggestions on what I can try? Wouldn't hurt to upgrade to Pygame 1.8 for a start. Regards, Wayne signature.asc Description: OpenPGP digital signature
[pygame] Mixer latency
Hello all, I am in the process of making a real time drum sequencer program, but I am experiencing some problems with using Pygame mixer. There seems to be approximately a 100ms delay between issuing the sound.play() command and actually hearing the sound play. To reach an acceptable level, I must decrease this to a maximum of about 10ms. I have tried adjusting the buffer size, the number of sounds loaded, etc, to no avail. I am running pygame 1.7.1 on OS X. Does anyone have any suggestions on what I can try? Thanks! signature.asc Description: OpenPGP digital signature
Re: [pygame] #pygame on irc.freenode.net
I'm putting in my vote with Devin and Ian as well... not that this is a democracy, but hey, still good to show my support! 8-) Ian Mallett wrote: I actually have to agree with Devin Jeanpierre. Pygame is a fantastic module and a fantastic project. If it is ever to suceed, become widely used, or be further recognised by anyone, it needs to look good when it is presented. Pygame needs to be represented as it really is. Let's not forget that.
Re: [pygame] Re: Check Sound status in Pygame
Wonderful! This seems to do the trick. Thanks for the tip. Lenard Lindstrom wrote: It seems you need to do your own channel management. It is easiest show with a code sample. Note this is untested. # A queue of channels from oldest to newest channel_queue = [] def play_sound(sound): Play a sound If all channels are busy then reuse the channel that was playing the longest. for i, c in enumerate(channel_queue): if not c.get_busy(): channel = channel_queue.pop(i) break else: if len(channel_queue) pygame.mixer.get_num_channels(): channel = Channel(len(channel_queue)) else: channel = channel_queue.pop(0) channel.play(sound) channel_queue.append(channel) Lenard Wyatt Olson wrote: OK, I found the method get_num_channels which seems to do this. However, I now need to be able to stop playing oldest sounds if I wish, to clear out space for other ones. For instance, assume that I have 16 channels defined. I am trying to play 30 notes on the ride cymbal in a 5 second interval. The ride cymbal sample is 10 seconds long. Without stopping sounds, once I play the sample 16 times, I cannot play any more until one of the samples finished playing. What I would like to do is check how many samples are playing, and if the count is greater than some threshold, stop the oldest instance of the playing sound. I can't seem to find any way to stop a single *instance* of a sound - if you call stop() or fadeout(), all instances of the sound will be stopped. I realize that this requirement is a bit more unique for my application, but I imagine that manipulating individual instances of existing sounds can be useful to anyone. Please let me know if I am missing something else which should be obvious to me. Cheers Wyatt Olson wrote: Hello all, I am developing a drum sequencer program using Pygame, and have a (hopefully easy) architectural question for you. How can you tell if a Sound object is currently playing? (On any channel - I don't care where it is playing, I just want to know if it is). There is no 'is_playing()' or similar method which I could find. I imagine that this is a pretty common request, so I must be just missing something. Thanks! signature.asc Description: OpenPGP digital signature
[pygame] Check Sound status in Pygame
Hello all, I am developing a drum sequencer program using Pygame, and have a (hopefully easy) architectural question for you. How can you tell if a Sound object is currently playing? (On any channel - I don't care where it is playing, I just want to know if it is). There is no 'is_playing()' or similar method which I could find. I imagine that this is a pretty common request, so I must be just missing something. Thanks! signature.asc Description: OpenPGP digital signature
[pygame] Re: Check Sound status in Pygame
OK, I found the method get_num_channels which seems to do this. However, I now need to be able to stop playing oldest sounds if I wish, to clear out space for other ones. For instance, assume that I have 16 channels defined. I am trying to play 30 notes on the ride cymbal in a 5 second interval. The ride cymbal sample is 10 seconds long. Without stopping sounds, once I play the sample 16 times, I cannot play any more until one of the samples finished playing. What I would like to do is check how many samples are playing, and if the count is greater than some threshold, stop the oldest instance of the playing sound. I can't seem to find any way to stop a single *instance* of a sound - if you call stop() or fadeout(), all instances of the sound will be stopped. I realize that this requirement is a bit more unique for my application, but I imagine that manipulating individual instances of existing sounds can be useful to anyone. Please let me know if I am missing something else which should be obvious to me. Cheers Wyatt Olson wrote: Hello all, I am developing a drum sequencer program using Pygame, and have a (hopefully easy) architectural question for you. How can you tell if a Sound object is currently playing? (On any channel - I don't care where it is playing, I just want to know if it is). There is no 'is_playing()' or similar method which I could find. I imagine that this is a pretty common request, so I must be just missing something. Thanks!