SDL audio not working with ALSA

Hi,
I got video part of my SDL applicatio working fine but facing lots if
issues with audio build and run. The application gives a segmentation fault
in Mix_OpenAudio call.

My environment:
host - linux machine
target - arm-linux
debug tool - gdb

I have done SDL_AUDIODRIVER=“alsa” and AUDIODEV=“hw”

I want SDL to use my ALSA driver which is already running fine with my mp3
decoder and aplay applications. In mySDL_Init, I can see that ALSA is
getting invoked and Audio_CreateDevice in SDL_alsa_audio.c goes through
fine.

To debug this issue, I wanted to build a statically build image but ran
into many issues. First of all I’m not able to generate libasound.a for ALSA
and its giving only libasound.so.2.* even if I change the configure option
to --disable-shared, but this does work fine for SDL, SDL_image and
SDL_mixer libraries. After so many work around like linking libasound from
.a files generated as intermediate files I got it built but only if *sdl
configure option is --enable-alsa-shared * but if add -static to my final
image I get errors like

…/SDL-1.2.13/bin/lib/libSDL.a(SDL_alsa_audio.o): In function
Audio_Available': ./src/audio/alsa/SDL_alsa_audio.c:198: undefined reference tosnd_pcm_open’
./src/audio/alsa/SDL_alsa_audio.c:201: undefined reference to
snd_pcm_close' ../SDL-1.2.13/bin/lib/libSDL.a(SDL_alsa_audio.o): In functionALSA_PlayAudio’:
./src/audio/alsa/SDL_alsa_audio.c:336: undefined reference to
snd_pcm_writei' ./src/audio/alsa/SDL_alsa_audio.c:345: undefined reference tosnd_pcm_resume’
./src/audio/alsa/SDL_alsa_audio.c:349: undefined reference to
snd_pcm_prepare' ../SDL-1.2.13/bin/lib/libSDL.a(SDL_alsa_audio.o): In functionALSA_CloseAudio’:
./src/audio/alsa/SDL_alsa_audio.c:378: undefined reference to
snd_pcm_drain' ./src/audio/alsa/SDL_alsa_audio.c:379: undefined reference tosnd_pcm_close’
…/SDL-1.2.13/bin/lib/libSDL.a(SDL_alsa_audio.o): In function
ALSA_OpenAudio': ./src/audio/alsa/SDL_alsa_audio.c:401: undefined reference tosnd_pcm_open’
./src/audio/alsa/SDL_alsa_audio.c:404: undefined reference to snd_strerror' ./src/audio/alsa/SDL_alsa_audio.c:408: undefined reference tosnd_pcm_hw_params_sizeof’
./src/audio/alsa/SDL_alsa_audio.c:408: undefined reference to
snd_pcm_hw_params_sizeof' ./src/audio/alsa/SDL_alsa_audio.c:409: undefined reference tosnd_pcm_hw_params_any’
./src/audio/alsa/SDL_alsa_audio.c:411: undefined reference to snd_strerror' ./src/audio/alsa/SDL_alsa_audio.c:416: undefined reference tosnd_pcm_hw_params_set_access’
./src/audio/alsa/SDL_alsa_audio.c:418: undefined reference to snd_strerror' ./src/audio/alsa/SDL_alsa_audio.c:450: undefined reference tosnd_pcm_hw_params_set_format’
./src/audio/alsa/SDL_alsa_audio.c:466: undefined reference to
snd_pcm_hw_params_set_channels' ./src/audio/alsa/SDL_alsa_audio.c:468: undefined reference tosnd_pcm_hw_params_get_channels at ALSA_0.9’
./src/audio/alsa/SDL_alsa_audio.c:478: undefined reference to
snd_pcm_hw_params_set_rate_near at ALSA_0.9' ./src/audio/alsa/SDL_alsa_audio.c:480: undefined reference tosnd_strerror’
./src/audio/alsa/SDL_alsa_audio.c:490: undefined reference to
snd_pcm_hw_params_set_period_size_near at ALSA_0.9' ./src/audio/alsa/SDL_alsa_audio.c:492: undefined reference tosnd_pcm_hw_params_set_periods_near at ALSA_0.9’
./src/audio/alsa/SDL_alsa_audio.c:495: undefined reference to
snd_pcm_hw_params' ./src/audio/alsa/SDL_alsa_audio.c:497: undefined reference tosnd_strerror’
./src/audio/alsa/SDL_alsa_audio.c:514: undefined reference to
snd_pcm_sw_params_sizeof' ./src/audio/alsa/SDL_alsa_audio.c:514: undefined reference tosnd_pcm_sw_params_sizeof’
./src/audio/alsa/SDL_alsa_audio.c:515: undefined reference to
snd_pcm_sw_params_current' ./src/audio/alsa/SDL_alsa_audio.c:517: undefined reference tosnd_strerror’
./src/audio/alsa/SDL_alsa_audio.c:521: undefined reference to
snd_pcm_sw_params_set_start_threshold' ./src/audio/alsa/SDL_alsa_audio.c:523: undefined reference tosnd_strerror’
./src/audio/alsa/SDL_alsa_audio.c:527: undefined reference to
snd_pcm_sw_params_set_avail_min' ./src/audio/alsa/SDL_alsa_audio.c:529: undefined reference tosnd_strerror’
./src/audio/alsa/SDL_alsa_audio.c:533: undefined reference to
snd_pcm_sw_params' ./src/audio/alsa/SDL_alsa_audio.c:535: undefined reference tosnd_strerror’
./src/audio/alsa/SDL_alsa_audio.c:556: undefined reference to
`snd_pcm_nonblock’
collect2: ld returned 1 exit status
make: *** [bin] Error 1

Whatever I configure options I try I get, I get the following error in
many cases
./src/audio/alsa/SDL_alsa_audio.c:468: undefined reference to
snd_pcm_hw_params_get_channels at ALSA_0.9' ./src/audio/alsa/SDL_alsa_audio.c:478: undefined reference tosnd_pcm_hw_params_set_rate_near at ALSA_0.9’
./src/audio/alsa/SDL_alsa_audio.c:490: undefined reference to
snd_pcm_hw_params_set_period_size_near at ALSA_0.9' ./src/audio/alsa/SDL_alsa_audio.c:492: undefined reference tosnd_pcm_hw_params_set_periods_near at ALSA_0.9’

When I played around with --enable/disable-loadso, --enable-shared-alsa
etc, once when I got the application built I could step in into
ALSA_OpenAudio but it crashes in one the calls to snd_pcm_hw_params_xxx. ANy
issue with dynamic loading ?

I have spent almost a week on this and I’m not able to conclude anything.
Any sort of help will get me out of this trouble

Thanks in advance
Sanu

have you tried running the sdl example programs to see if they work?

Hi,
I started from scratch once again and built all the libraries except
libasound in static mode. I can run the code but it crashes if it executes
one of these.

‘snd_pcm_hw_params_get_channels at ALSA_0.9’<‘snd_pcm_hw_params_get_channels at ALSA_0.9’>
snd_pcm_hw_params_set_rate_near at ALSA_0.9'snd_pcm_hw_params_set_period_size_near at ALSA_0.9’
`snd_pcm_hw_params_set_periods_near at ALSA_0.9’

For eg if I skip
status = SDL_NAME(snd_pcm_hw_params_set_rate_near)(pcm_handle, hwparams,
spec->freq, NULL); of SDL_alsa_audio.c / static int ALSA_OpenAudio(_THIS,
SDL_AudioSpec *spec)
it will crash in
SDL_NAME(snd_pcm_hw_params_set_periods_near)(pcm_handle, hwparams, 2,
NULL); of
SDL_alsa_audio.c / static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec)

Anything special for these functions ?

Thanks in advance
Sanu Mathews

The configure option for SDL is set as follows

                            CFLAGS="$(TARGET_CFLAGS) -g -O0" \
                            LDFLAGS="$(TARGET_LDFLAGS)" \
                            ./configure --target=arm-linux \
                            --host=arm-linux \
                            --disable-esd \
                            --disable-video-x11 \
                            --disable-video-x11-dgamouse \
                            --disable-x11-shared \
                            --disable-dga \
                            --disable-input-events \
                            --disable-input-tslib \
                            --disable-shared \
                            --enable-static \
                            --enable-alsa \
                            --enable-alsa-shared \
                            --prefix=${LIBSDL_DIR}/bin \
                            --cache-file=config.cache \On 7/10/08, Sanu Mathews <@Sanu_Mathews> wrote:

Hi,
I got video part of my SDL applicatio working fine but facing lots if
issues with audio build and run. The application gives a segmentation fault
in Mix_OpenAudio call.

My environment:
host - linux machine
target - arm-linux
debug tool - gdb

I have done SDL_AUDIODRIVER=“alsa” and AUDIODEV=“hw”

I want SDL to use my ALSA driver which is already running fine with my
mp3 decoder and aplay applications. In mySDL_Init, I can see that ALSA is
getting invoked and Audio_CreateDevice in SDL_alsa_audio.c goes through
fine.

To debug this issue, I wanted to build a statically build image but ran
into many issues. First of all I’m not able to generate libasound.a for ALSA
and its giving only libasound.so.2.* even if I change the configure option
to --disable-shared, but this does work fine for SDL, SDL_image and
SDL_mixer libraries. After so many work around like linking libasound from
.a files generated as intermediate files I got it built but only if *sdl
configure option is --enable-alsa-shared * but if add -static to my final
image I get errors like

…/SDL-1.2.13/bin/lib/libSDL.a(SDL_alsa_audio.o): In function
Audio_Available': ./src/audio/alsa/SDL_alsa_audio.c:198: undefined reference tosnd_pcm_open’
./src/audio/alsa/SDL_alsa_audio.c:201: undefined reference to
snd_pcm_close' ../SDL-1.2.13/bin/lib/libSDL.a(SDL_alsa_audio.o): In functionALSA_PlayAudio’:
./src/audio/alsa/SDL_alsa_audio.c:336: undefined reference to
snd_pcm_writei' ./src/audio/alsa/SDL_alsa_audio.c:345: undefined reference tosnd_pcm_resume’
./src/audio/alsa/SDL_alsa_audio.c:349: undefined reference to
snd_pcm_prepare' ../SDL-1.2.13/bin/lib/libSDL.a(SDL_alsa_audio.o): In functionALSA_CloseAudio’:
./src/audio/alsa/SDL_alsa_audio.c:378: undefined reference to
snd_pcm_drain' ./src/audio/alsa/SDL_alsa_audio.c:379: undefined reference tosnd_pcm_close’
…/SDL-1.2.13/bin/lib/libSDL.a(SDL_alsa_audio.o): In function
ALSA_OpenAudio': ./src/audio/alsa/SDL_alsa_audio.c:401: undefined reference tosnd_pcm_open’
./src/audio/alsa/SDL_alsa_audio.c:404: undefined reference to
snd_strerror' ./src/audio/alsa/SDL_alsa_audio.c:408: undefined reference tosnd_pcm_hw_params_sizeof’
./src/audio/alsa/SDL_alsa_audio.c:408: undefined reference to
snd_pcm_hw_params_sizeof' ./src/audio/alsa/SDL_alsa_audio.c:409: undefined reference tosnd_pcm_hw_params_any’
./src/audio/alsa/SDL_alsa_audio.c:411: undefined reference to
snd_strerror' ./src/audio/alsa/SDL_alsa_audio.c:416: undefined reference tosnd_pcm_hw_params_set_access’
./src/audio/alsa/SDL_alsa_audio.c:418: undefined reference to
snd_strerror' ./src/audio/alsa/SDL_alsa_audio.c:450: undefined reference tosnd_pcm_hw_params_set_format’
./src/audio/alsa/SDL_alsa_audio.c:466: undefined reference to
snd_pcm_hw_params_set_channels' ./src/audio/alsa/SDL_alsa_audio.c:468: undefined reference tosnd_pcm_hw_params_get_channels at ALSA_0.9’<%60snd_pcm_hw_params_get_channels at ALSA_0.9’>
./src/audio/alsa/SDL_alsa_audio.c:478: undefined reference to
snd_pcm_hw_params_set_rate_near at ALSA_0.9'<%60snd_pcm_hw_params_set_rate_near at ALSA_0.9'> ./src/audio/alsa/SDL_alsa_audio.c:480: undefined reference tosnd_strerror’
./src/audio/alsa/SDL_alsa_audio.c:490: undefined reference to
snd_pcm_hw_params_set_period_size_near at ALSA_0.9'<%60snd_pcm_hw_params_set_period_size_near at ALSA_0.9'> ./src/audio/alsa/SDL_alsa_audio.c:492: undefined reference tosnd_pcm_hw_params_set_periods_near at ALSA_0.9’<%60snd_pcm_hw_params_set_periods_near at ALSA_0.9’>
./src/audio/alsa/SDL_alsa_audio.c:495: undefined reference to
snd_pcm_hw_params' ./src/audio/alsa/SDL_alsa_audio.c:497: undefined reference tosnd_strerror’
./src/audio/alsa/SDL_alsa_audio.c:514: undefined reference to
snd_pcm_sw_params_sizeof' ./src/audio/alsa/SDL_alsa_audio.c:514: undefined reference tosnd_pcm_sw_params_sizeof’
./src/audio/alsa/SDL_alsa_audio.c:515: undefined reference to
snd_pcm_sw_params_current' ./src/audio/alsa/SDL_alsa_audio.c:517: undefined reference tosnd_strerror’
./src/audio/alsa/SDL_alsa_audio.c:521: undefined reference to
snd_pcm_sw_params_set_start_threshold' ./src/audio/alsa/SDL_alsa_audio.c:523: undefined reference tosnd_strerror’
./src/audio/alsa/SDL_alsa_audio.c:527: undefined reference to
snd_pcm_sw_params_set_avail_min' ./src/audio/alsa/SDL_alsa_audio.c:529: undefined reference tosnd_strerror’
./src/audio/alsa/SDL_alsa_audio.c:533: undefined reference to
snd_pcm_sw_params' ./src/audio/alsa/SDL_alsa_audio.c:535: undefined reference tosnd_strerror’
./src/audio/alsa/SDL_alsa_audio.c:556: undefined reference to
`snd_pcm_nonblock’
collect2: ld returned 1 exit status
make: *** [bin] Error 1

Whatever I configure options I try I get, I get the following error in
many cases
./src/audio/alsa/SDL_alsa_audio.c:468: undefined reference to
snd_pcm_hw_params_get_channels at ALSA_0.9'<%60snd_pcm_hw_params_get_channels at ALSA_0.9'> ./src/audio/alsa/SDL_alsa_audio.c:478: undefined reference tosnd_pcm_hw_params_set_rate_near at ALSA_0.9’<%60snd_pcm_hw_params_set_rate_near at ALSA_0.9’>
./src/audio/alsa/SDL_alsa_audio.c:490: undefined reference to
snd_pcm_hw_params_set_period_size_near at ALSA_0.9'<%60snd_pcm_hw_params_set_period_size_near at ALSA_0.9'> ./src/audio/alsa/SDL_alsa_audio.c:492: undefined reference tosnd_pcm_hw_params_set_periods_near at ALSA_0.9’<%60snd_pcm_hw_params_set_periods_near at ALSA_0.9’>

When I played around with --enable/disable-loadso, --enable-shared-alsa
etc, once when I got the application built I could step in into
ALSA_OpenAudio but it crashes in one the calls to snd_pcm_hw_params_xxx. ANy
issue with dynamic loading ?

I have spent almost a week on this and I’m not able to conclude anything.
Any sort of help will get me out of this trouble

Thanks in advance
Sanu

Here is the full story:

  • SDL currently uses the asla 0.9 API
  • A couple of years ago, the alsa 0.9 API was marked as deprecated.
    But you could keep compatibility by adding defines for the old API as
    follows (I did this change back then):
    #define ALSA_PCM_OLD_HW_PARAMS_API
    #define ALSA_PCM_OLD_SW_PARAMS_API
    #include <alsa/asoundlib.h>
  • Now the old API has been completely removed, your system seems to be
    the first one to have this
  • Someone has to port SDL to the alsa 1.0 API now

Stephane

Hi,
I got video part of my SDL applicatio working fine but facing lots if
issues with audio build and run. The application gives a segmentation fault
in Mix_OpenAudio call.

What version of libasound are you using? Are you using USB audio?

[snipped details]

-CrystalOn Thu, 10 Jul 2008, Sanu Mathews wrote:

Hi,

I got the build issue resolved. Dynamic loading was failing because the
prototype of the four function for which I was getting a segmentation fault
were different from that of in ALSA driver.

What was present in SDL_alsa_audio.c under

#ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC was

static int (*SDL_NAME(snd_pcm_hw_params_get_channels))(const
snd_pcm_hw_params_t *params);
static unsigned int (*SDL_NAME(snd_pcm_hw_params_set_rate_near))(snd_pcm_t
*pcm, snd_pcm_hw_params_t *params, unsigned int val, int *dir);
static snd_pcm_uframes_t
(*SDL_NAME(snd_pcm_hw_params_set_period_size_near))(snd_pcm_t *pcm,
snd_pcm_hw_params_t *params, snd_pcm_uframes_t val, int *dir);
static int (*SDL_NAME(snd_pcm_hw_params_get_period_size))(const
snd_pcm_hw_params_t *params);
static unsigned int
(*SDL_NAME(snd_pcm_hw_params_set_periods_near))(snd_pcm_t *pcm,
snd_pcm_hw_params_t *params, unsigned int val, int *dir);

and what is there in pcm.c of ALSA driver is
static int (*SDL_NAME(snd_pcm_hw_params_get_channels))(const
snd_pcm_hw_params_t *params, unsigned int *val);
static int (*SDL_NAME(snd_pcm_hw_params_set_rate_near))(snd_pcm_t *pcm,
snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
static snd_pcm_uframes_t
(*SDL_NAME(snd_pcm_hw_params_set_period_size_near))(snd_pcm_t *pcm,
snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir);
static int (*SDL_NAME(snd_pcm_hw_params_set_periods_near))(snd_pcm_t *pcm,
snd_pcm_hw_params_t *params, unsigned int *val, int *dir);

  This change fixed issue with crash.
  • Now I’m getting an error*

  • Error: Mix_OpenAudio: Couldn’t set software audio parameters: Invalid
    argument*

  • This is coming from snd_pcm_sw_params of alsa-lib-1.0.13/src/pcm/pcm.c.
    So I’m bypassing that error by not returning an error

(snd_pcm_sw_params_t )params->avail_min = 0. How is this supposed to get
set ?

Another issue is SDL_CreateThread(SDL_RunAudio, audio); is failing

Thanks and regards
Sanu

Gentoo is failing with SDL + asla too.

here’s one of the bugs…
http://bugs.gentoo.org/show_bug.cgi?id=223055On Thu, Jul 10, 2008 at 10:21 PM, Sanu Mathews <sanu.mathews at gmail.com> wrote:

Hi,

I got the build issue resolved. Dynamic loading was failing because the
prototype of the four function for which I was getting a segmentation fault
were different from that of in ALSA driver.

What was present in SDL_alsa_audio.c under

#ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC was

static int (*SDL_NAME(snd_pcm_hw_params_get_channels))(const
snd_pcm_hw_params_t *params);
static unsigned int (*SDL_NAME(snd_pcm_hw_params_set_rate_near))(snd_pcm_t
*pcm, snd_pcm_hw_params_t *params, unsigned int val, int *dir);
static snd_pcm_uframes_t
(*SDL_NAME(snd_pcm_hw_params_set_period_size_near))(snd_pcm_t *pcm,
snd_pcm_hw_params_t *params, snd_pcm_uframes_t val, int *dir);
static int (*SDL_NAME(snd_pcm_hw_params_get_period_size))(const
snd_pcm_hw_params_t *params);
static unsigned int
(*SDL_NAME(snd_pcm_hw_params_set_periods_near))(snd_pcm_t *pcm,
snd_pcm_hw_params_t *params, unsigned int val, int *dir);

and what is there in pcm.c of ALSA driver is
static int (*SDL_NAME(snd_pcm_hw_params_get_channels))(const
snd_pcm_hw_params_t *params, unsigned int *val);
static int (*SDL_NAME(snd_pcm_hw_params_set_rate_near))(snd_pcm_t *pcm,
snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
static snd_pcm_uframes_t
(*SDL_NAME(snd_pcm_hw_params_set_period_size_near))(snd_pcm_t *pcm,
snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir);
static int (*SDL_NAME(snd_pcm_hw_params_set_periods_near))(snd_pcm_t *pcm,
snd_pcm_hw_params_t *params, unsigned int *val, int *dir);

  This change fixed issue with crash.

Now I’m getting an error
Error: Mix_OpenAudio: Couldn’t set software audio parameters: Invalid
argument

This is coming from snd_pcm_sw_params of alsa-lib-1.0.13/src/pcm/pcm.c.
So I’m bypassing that error by not returning an error

(snd_pcm_sw_params_t *)params->avail_min = 0. How is this supposed to get
set ?

Another issue is SDL_CreateThread(SDL_RunAudio, audio); is failing
Thanks and regards
Sanu


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org