Tips about pygame.mixer
Here are a couple of things I've learned about using pygame.mixer in the last couple of days. I'm posting them here in case they're of benefit to anyone.-
If you get something that sounds like an elephant has trodden on the keyboard of your pipe organ, you've probably got a "play this sound now" condition that's triggering on every frame instead of just once.
- If you need accurate timing of sounds, you may need to reduce the buffer size that pygame.mixer is initialised with. In a post-comp effort to add more sounds to 5B, I found I needed to use a buffer of around 64 bytes to be able to play short sounds at regular intervals of 100ms without the result sounding ragged. (This is on MacOSX, not sure if other platforms behave the same way.)
(log in to comment)
gcewing on 2007/09/12 00:45:
Further investigation reveals that the buffer doesn't have to be small, it just has to be a submultiple of the frame time.E.g. for a frame time of 50ms and a sample rate of 22050, a buffer size of 1102 bytes works well, but 1024 doesn't.
I'm guessing that pygame.mixer can only start playing a sound on a buffer boundary, so if the time to play a buffer's worth of sound isn't coordinated with the frame time, they drift in and out of phase and you get unpredictable sound start times.