pygame sound is delayed a lot

Bug #295369 reported by almejo
58
This bug affects 8 people
Affects Status Importance Assigned to Milestone
Pygame
New
Undecided
Unassigned
pygame (Ubuntu)
Confirmed
Medium
Unassigned
Lucid
Won't Fix
Medium
Unassigned

Bug Description

I am developing a game in pygame for a couple of weeks. the other day i upgraded to ubuntu intrepid and now, all the sound that i play through pygame is delayed a whole second

I dont know if its pulse audio or pygame. but now all the sound effects are delayed in my game. I didnt change anything in my code after the upgrade and all was working ok

Thanks for this wonderful os. i only want it to get better

Revision history for this message
Vladimir-csp (vladimir-csp) wrote :

I had a terrible delay in every game from Maelstrom to Urban terror. Sound was playing 0.1-0.2 seconds later than it should. That's already too large delay, very annoying. Maybe your whole second delay is some complex problem, or your cpu is slower.

Anyway I've just disabled and removed Pulseaudio from the system, and sound got back where it should be. Try it.

Revision history for this message
almejo (alejandro-vera) wrote :

As I said, i use the same computer, i upgraded to intrepid from hardy inthe same machine

I'll try removing pulse... is there any step other than removing it via apt-get? howdo i install it again? something to change in gconf?

Revision history for this message
pymike (pymike) wrote :

I get the same problem. I upgraded from Hardy to Intrepid and sounds are now delaying atrociously. I tried re-installing pygame, re-booting ubuntu, and that didn't fix it at all. :-( By the way, I'm using ALSA, so I doubt it's a pulseaudio problem...

Revision history for this message
illume (illume) wrote :

hi,

check out this thread:
http://ubuntuforums.org/showthread.php?t=885437

From this thread and notes on the SDL mailing list, it appears pulse audio is emulating alsa, and causing the delays. Are you sure you don't have pulse audio installed?

Apparently removing pulse audio fixes the problem... hopefully the ubuntu/pulse audio people will fix it though. Better would be if ubuntu tested their OS with realtime/multimedia apps... and consider problems like non-working sound, and poor latency serious issues.

People on that thread, and on the SDL mailing list have fixed their sound issues by following the instructions in that thread. The issue is with pulse audio, SDL, and pygame.

cheers,

Revision history for this message
illume (illume) wrote :

This is the SDL mailing list thread where the pulseaudio issues are also noted:

http://www.nabble.com/Mix_PlayChannel-delay-with-Ubuntu-8.1-td20542915.html

Revision history for this message
pymike (pymike) wrote :

I'd rather not remove pulse. Using apt-get remove pulseaudio makes me remove ubuntu-desktop as well. Is there a way to just fix the latency issue?

Revision history for this message
Lauri Niskanen (ape3000) wrote :

The problem might also be too big buffer. Try something like this: pygame.mixer.init(44100, -16, 2, 512)

summary: - sound is delayed a whole second in intrepid
+ pygame sound is delayed a lot
Revision history for this message
Matt Joiner (anacrolix) wrote :

I have this problem too. Lauri, your suggestion fixed it.

Revision history for this message
almejo (alejandro-vera) wrote :

I tried it,.. but still not working..

Will be this be addressed in Karmic??

Revision history for this message
Jon White (molip) wrote :

> I tried it,.. but still not working..

try this before calling pygame.init():
pygame.mixer.pre_init(44100, -16, 2, 512)

Revision history for this message
Matt Joiner (anacrolix) wrote :

You can try removing pulse audio, or hope that in the next release, pulse audio is somehow (unlikely) less broken.

Revision history for this message
pymike (pymike) wrote : Re: [Bug 295369] Re: pygame sound is delayed a lot

This problem goes away when I kill pulseaudio. I haven't experienced any
side-effects either.

--
- pymike

Revision history for this message
almejo (alejandro-vera) wrote :

Great!!!

the last indication did the magic. Pre_init was the missing key.

Now after months I can resume programing my game :D

Thanks to all!!

btw: how did you know what to do??

Revision history for this message
Hans L (thehans) wrote :

Could the pygame package be set so that the default mixer setting is something more sane, such as what Lauri has suggested?

Revision history for this message
Matt Joiner (anacrolix) wrote :

almejo i just googled for it. however the large sound delay would be
indicative of an issue somewhere in the sound system.

On Fri, Jul 24, 2009 at 12:13 PM, almejo <email address hidden> wrote:
> Great!!!
>
> the last indication did the magic. Pre_init was the missing key.
>
> Now after months I can resume programing my game :D
>
> Thanks to all!!
>
> btw: how did you know what to do??
>
> --
> pygame sound is delayed a lot
> https://bugs.launchpad.net/bugs/295369
> You received this bug notification because you are a direct subscriber
> of the bug.
>

Revision history for this message
almejo (alejandro-vera) wrote :

Maybe is something in the sound system, but is something that is happening in my home pc and in my work pc... so I don't think that is my foult. In my job I have a jaunty default instalation so i don't know what it is.

thanks for the help..

Revision history for this message
Rick Spencer (rick-rickspencer3) wrote :

Confirmed on my dell mini 10v with spinning media, and also on my desktop. Note that the delay is much less on my desktop.

Changed in pygame (Ubuntu):
assignee: nobody → Luke Yelavich (themuso)
importance: Undecided → High
milestone: none → ubuntu-10.04-beta-1
status: New → Confirmed
Revision history for this message
Horst F JENS (horst.jens) wrote :

Like John wrote,
adding this line into the python/pygame code helps:

    pygame.mixer.pre_init(44100,-16,2,2048)
    pygame.init()

no more sound delay, tested with various computers (Ubuntu 9.10 karmic)

Revision history for this message
Luke Yelavich (themuso) wrote : Re: [Bug 295369] Re: pygame sound is delayed a lot

Has anybody tested this in lucid? Also please double check that if running lucid, you have libsdl1.2debian-pulseaudio installed and not libsdl1.2debian-alsa.

Steve Langasek (vorlon)
Changed in pygame (Ubuntu Lucid):
milestone: ubuntu-10.04-beta-1 → ubuntu-10.04-beta-2
Revision history for this message
Rick Spencer (rick-rickspencer3) wrote :

Setting to medium, as there is a work around, and no responsed on this bug.

Luke, I am still seeing this issue, will implement work around in my code.

Changed in pygame (Ubuntu Lucid):
importance: High → Medium
Steve Langasek (vorlon)
Changed in pygame (Ubuntu Lucid):
milestone: ubuntu-10.04-beta-2 → none
Revision history for this message
Sebastien Bacher (seb128) wrote :

Luke, what is the status of this bug?

Revision history for this message
sy12 (informfr) wrote :

I have reported a similar bug in 2011 at http://hg.pygame.org/pygame/issue/89/sounds-arent-played-at-the-expected-moment . The problem was with Windows XP. The init line solves the problem, but it makes the line mandatory if you are writing games.

I wanted to know if the regression appeared in SDL or in pygame, so today I have examined pygame's repository. Here are the results. The canonical version with the expected behavior is (in my opinion) the 1.7.1. The changes occurred in src/mixer.c .

In 1.7.1:
#define MIX_DEFAULT_CHUNKSIZE 1024

In 1.8.0rc5:
/* 1024 * 3 seems to be the magic number to stop scratchy sound. On some systems. */
#define MIX_DEFAULT_CHUNKSIZE 3072

In 1.9.1release:
/* Since they are documented, the default init values are defined here
   rather than taken from SDL_mixer. It also means that the default
   size is defined in Pygame, rather than SDL AUDIO_xxx, terms.
 */
#define PYGAME_MIXER_DEFAULT_FREQUENCY 22050
#define PYGAME_MIXER_DEFAULT_SIZE -16
#define PYGAME_MIXER_DEFAULT_CHANNELS 2
#define PYGAME_MIXER_DEFAULT_CHUNKSIZE 4096

3 commits explain the reason of the changes:

https://bitbucket.org/pygame/pygame/commits/05f77b15cd466411a13cf4ce97217a34da6aeeb0
" [...] Also changed default chunk size to avoid a common scratchy sound problem."

https://bitbucket.org/pygame/pygame/commits/a59e9011ebbf28290c40f7150bed30ff56425539
"Added comment to explain change of chunksize."

https://bitbucket.org/pygame/pygame/commits/c12cc74fe422a38bc93a1cfbca14660008693db9
"Bugzilla issue 6: add keyword arguments to mixer.init (and pre_init).
Default buffer size is now explicitly 4096 to be consistent with the
documented power of 2 requirement. It was already implicitly 4096
as buffer values are rounded up to the nearest power of two anyway."

So the error appeared in 2 stages:
- First, a developer changed the default value from 1024 to 3072 to solve a problem on "some systems", causing a regression (unbearable delay).
- Later, another developer rounded 3072 to 4096, which shouldn't change the result.

Actually, the 1024 value was mentioned in the 1.7.1 docs:
"pygame.mixer.init [...] The default buffersize is 1024 samples, sometimes a larger value is required. "

So I am wondering, should they go back to the 1.7.1 value, since the defaults are not suited for games? What do you think?

Luke Yelavich (themuso)
Changed in pygame (Ubuntu):
assignee: Luke Yelavich (themuso) → nobody
Changed in pygame (Ubuntu Lucid):
assignee: Luke Yelavich (themuso) → nobody
Revision history for this message
Rolf Leggewie (r0lf) wrote :

lucid has seen the end of its life and is no longer receiving any updates. Marking the lucid task for this ticket as "Won't Fix".

Changed in pygame (Ubuntu Lucid):
status: Confirmed → Won't Fix
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.