Search code examples
c++sdl-2elfalsaconan

Unable to set up SDL2 to run with audio (alsa) on linux ubuntu 20.04 using conan


Depending on what I do I either get

"No such audio device"

if I leave determining my audio device up to SDL2 or I get

Failed loading libasound.a: /home/aypahyo/.conan/data/libalsa/1.2.4///package/d48130e0dd76369b1338deb3b2372c5a649f9f2d/lib/libasound.a: invalid ELF header

when I set SDL_AUDIODRIVER=alsa. (The error message is actually cut off, I added an r at the end).

Here is an implementation that shows the error:

#include <SDL.h>
#include <iostream>

void SetupWant(SDL_AudioSpec * want);
void PrintError(std::string source);

void run()
{
  //https://wiki.libsdl.org/FAQUsingSDL
  putenv((char *)"SDL_AUDIODRIVER=alsa");

  if(0 != SDL_Init(SDL_INIT_AUDIO)) PrintError("SDL_Init");

  SDL_AudioSpec want, have;
  SDL_AudioDeviceID dev;

  SetupWant(&want);
  dev = SDL_OpenAudioDevice(NULL, 0, &want, &have, SDL_AUDIO_ALLOW_FORMAT_CHANGE);
  if (0 == dev) PrintError("SDL_OpenAudioDevice");
}

void SetupWant(SDL_AudioSpec * want)
{
  SDL_memset(want, 0, sizeof(SDL_AudioSpec));
  want->freq = 48000;
  want->format = AUDIO_F32;
  want->channels = 2;
  want->samples = 4096;
}

void PrintError(std::string source)
{
  std::cout << "error " << source << ": " << SDL_GetError() << "\n";
}

I installed the dependencies in my CMake Project using conan. Here is the conan file:

[requires]
sdl2/2.0.12@bincrafters/stable
sdl2_image/2.0.5@bincrafters/stable
sdl2_mixer/2.0.4@bincrafters/stable
flac/1.3.2@bincrafters/stable
glib/2.64.0@bincrafters/stable
boost/1.71.0@conan/stable
zlib/1.2.11@conan/stable
bzip2/1.0.8@conan/stable
gtest/1.10.0
glm/0.9.9.8
libmpg123/1.25.13@bincrafters/stable
libalsa/1.2.4

[generators]
cmake

The dependency sdl2/2.0.12@bincrafters/stable does not come with a source folder, however I can use the sdl2/2.0.10@bincrafters/stable package to run sdl2 ./configure.

Here is the summary:

SDL2 Configure Summary:
Building Shared Libraries
Building Static Libraries
Enabled modules : atomic audio video render events joystick haptic sensor power filesystem threads timers file loadso cpuinfo assembly
Assembly Math   : mmx 3dnow sse sse2 sse3
Audio drivers   : disk dummy oss alsa(dynamic) pulse(dynamic) jack(dynamic) nas(dynamic)
Video drivers   : dummy x11(dynamic) opengl opengl_es2 vulkan
X11 libraries   : xcursor xdbe xinerama xinput2 xinput2_multitouch xrandr xscrnsaver xshape xvidmode
Input drivers   : linuxev linuxkd
Using libsamplerate : NO
Using libudev       : NO
Using dbus          : YES
Using ime           : YES
Using ibus          : NO
Using fcitx         : NO

Most google results basically give package names that should be installed and afterwards sdl2 needs to be rebuilt. I did that with any library I dould find that way.

The only other tip I found was to set the audio driver explicitely which is what leads to the ELF header issue.

I am fairly new to linux as well as conan and cmake. I am struggling to determine what else could be broken.

This may be an issue that is local to my machine but judging by the number of hits I found all over the web I think this general issue is of broader interest.

My question is what is wrong with my setup and how do I fix it?

EDIT:

I hex dumped the libasound.a file, here are the first few bits:

00000000: 21 3C 61 72 63 68 3E 0A 2F 20 20 20 20 20 20 20    !<arch>./.......
00000010: 20 20 20 20 20 20 20 20 30 20 20 20 20 20 20 20    ........0.......
00000020: 20 20 20 20 30 20 20 20 20 20 30 20 20 20 20 20    ....0.....0.....
00000030: 30 20 20 20 20 20 20 20 36 33 33 30 32 20 20 20    0.......63302...

Reading up on ELF, this does not look like an elf file which would explain why loading it under the assumption that it is an elf file would fail. So maybe this is where my incompatibility comes from.

Edit #1: I followed up with a post on libsdl.org.

Edit #2: I followed up a post on bincrafters/community

Eidt #3: Eric Lemanissier created a pull request which seems to be part of the solution.


Solution

  • The Issue is solved and the traces in the question show how.

    Ultimately were several layered problems.

    • libalsa/1.2.4 needed a fix for shared libraries and I had to add that option to the conan file.
    • I had to plug my boxes in because the default device was a channel I do not normally use
    • Ultimately I will need to set up sound properly on my system.