pygame / pygame

🐍🎮 pygame (the library) is a Free and Open Source python programming language library for making multimedia applications like games built on top of the excellent SDL library. C, Python, Native, OpenGL.
https://www.pygame.org
7.32k stars 3.25k forks source link

SDL2: FastTracker error with pygame2-dev2 #1211

Closed mcpalmer1980 closed 2 years ago

mcpalmer1980 commented 5 years ago

This is probably a minor issue or perhaps just a bad file, but I'm getting an error trying to play a specific xm file with music.mixer. It plays fine in pygame1.9.1, as well as in MilkyTracker and VLC.

mega_destruction.xm is attached. All other mod, xm, and it files I tried work fine. Getting the error in python 2.7.12 and 3.5.2

mega_destruction.xm.zip

Test program:

import pygame

pygame.init()
window = pygame.display.set_mode((120, 120))
pygame.mixer.music.load('mega_destruction.xm')
pygame.mixer.music.play()
running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    pygame.display.update()

Error:

ALSA lib pcm.c:8432:(snd_pcm_recover) underrun occurred
ALSA lib pcm.c:8432:(snd_pcm_recover) underrun occurred
Fatal Python error: PyEval_SaveThread: NULL tstate
Current thread 0x00007f31abd5c740 (most recent call first):

My system is Linux Mint 18.3 pygame 2.0.0.dev2 (SDL 2.0.9, python 2.7.12) pygame 2.0.0.dev2 (SDL 2.0.9, python 3.5.2)

Related docs: https://www.pygame.org/docs/ref/music.html#pygame.mixer.music.load

To Do

MyreMylar commented 3 years ago

Test program:

import pygame

pygame.init()

window = pygame.display.set_mode((120, 120))

pygame.mixer.music.load('mega_destruction.xm')
pygame.mixer.music.play()

running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    pygame.display.update()

This plays the file OK for me using windows, python 3.9 and pygame 2.0.0.

MyreMylar commented 3 years ago

@mcpalmer1980 Can you reproduce this anymore with 2.0.0?

mcpalmer1980 commented 3 years ago

pygame 2.0.0 (SDL 2.0.12, python 3.7.5) Hello from the pygame community. https://www.pygame.org/contribute.html ALSA lib pcm.c:8432:(snd_pcm_recover) underrun occurred ALSA lib pcm.c:8432:(snd_pcm_recover) underrun occurred Fatal Python error: PyEval_SaveThread: NULL tstate

Current thread 0x00007f31abd5c740 (most recent call first): File "play.py", line 8 in Aborted (core dumped)

MyreMylar commented 3 years ago

Suppose it must be a platform/linux specific thing? I'm assuming you are still on linux mint.

mcpalmer1980 commented 3 years ago

Ubuntu 19.10 KDE

Likely unrelated, but I can't play MP3 either

MyreMylar commented 3 years ago

I see some fixes for libmodplug are still making their way into into SDL, like these ones:

https://github.com/SDL-mirror/SDL_mixer/commit/5b00537e50ba6e47ae03685eb9fdfc80b1100bcc

which went in only last month - one of which may be relevant for .xm files, I think mp3 is a different library.

MyreMylar commented 3 years ago

The patch by @AliceLR may be relevant since it seems to be gcc specific?

AliceLR commented 3 years ago

I don't think that patch I submitted to libmodplug would fix any stability issues, just garbage audio output when ModPlug_Settings::mBits is configured to 24. I wouldn't rule out a libmodplug (or libmikmod) bug entirely, but I am able to run that test program with no issue using pygame 1.9.6 from the Fedora repository. I get a similar crash when I attempt to run it with pygame 2.0.0:

pygame 2.0.0 (SDL 2.0.12, python 3.9.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
ALSA lib pcm.c:8545:(snd_pcm_recover) underrun occurred
ALSA lib pcm.c:8545:(snd_pcm_recover) underrun occurred
ALSA lib pcm.c:8545:(snd_pcm_recover) underrun occurred
...a few dozen more of those...
ALSA lib pcm.c:8545:(snd_pcm_recover) underrun occurred
Fatal Python error: PyEval_SaveThread: the function must be called with the GIL held, but the GIL is released (the current Python thread state is NULL)
Python runtime state: initialized
MyreMylar commented 3 years ago

@AliceLR Hmm, since you also have the SDL bug I don't suppose you know if that particular xm file runs in MegaZeux ? pygame basically just passes the file over to SDL, which I assume passes it over to libmodplug (not certain on this part).

AliceLR commented 3 years ago

MegaZeux uses SDL_Audio directly (not SDL_mixer) so I'm not sure it's the best comparison. That XM file does load/play in MegaZeux with both libmodplug and libmikmod with no issue. (full disclosure: the VM I tested in last night had unrelated audio problems after I restarted it so that result is from a different Fedora VM.)

MyreMylar commented 3 years ago

Thanks for trying it! It's always hard to track down these relatively obscure platform + data combo issues - every clue can help.

AliceLR commented 3 years ago

I figured out the problem with my VM audio (I had a build of libvorbis with ASan enabled in the library path... oof) and did some more testing. The underrun occurred messages are a red herring I think, because I get them even when pygame successfully loads a (different) module.

Using this test program (based on this) I am able to play this XM using SDL2_mixer (I had to manually install libmodplug first though...):

#if 0
gcc -O0 -g mixer.c -omixer -lSDL2_mixer -lSDL2
exit
#endif
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>

#define O_(...) do{ fprintf(stderr, __VA_ARGS__); fflush(stderr); }while(0)

Mix_Music *music = NULL;

int main(int argc, char *argv[])
{
  if(argc < 2)
  {
    O_("Usage: mixer filename.ext\n");
    return 0;
  }

  if(SDL_Init(SDL_INIT_AUDIO) < 0)
  {
    O_("Failed to init SDL audio.\n");
    return -1;
  }

  if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1)
  {
    O_("Failed to init SDL_mixer: %s\n", Mix_GetError());
    return -1;
  }

  music = Mix_LoadMUS(argv[1]);
  if(music == NULL)
  {
    O_("Failed to load %s: %s\n", argv[1], Mix_GetError());
    return -1;
  }

  if(Mix_PlayMusic(music, -1) == -1)
  {
    O_("Failed to play %s: %s\n", argv[1], Mix_GetError());
    return -1;
  }

  while(Mix_PlayingMusic());

  Mix_FreeMusic(music);
  Mix_CloseAudio();
  return 0;
}
ankith26 commented 2 years ago

This issue seems to be fixed by the latest manylinux and deps improvements in 2.0.3. Confirmed by testing on Ubuntu 21.10 running under WSL+WSLg

ankith@Ankiths-HP-Laptop:~$ python3 test.py
pygame 2.0.1 (SDL 2.0.14, Python 3.9.7)
Hello from the pygame community. https://www.pygame.org/contribute.html
Fatal Python error: PyEval_SaveThread: the function must be called with the GIL held, but the GIL is released (the current Python thread state is NULL)
Python runtime state: initialized

Current thread 0x00007f56df650fc0 (most recent call first):
  File "/home/ankith/test.py", line 5 in <module>
Aborted
ankith@Ankiths-HP-Laptop:~$ python3 test.py
pygame 2.1.0 (SDL 2.0.16, Python 3.9.7)
Hello from the pygame community. https://www.pygame.org/contribute.html
ankith@Ankiths-HP-Laptop:~$

With 2.1 I hear the sound playing aight