SDL_mixer issues on Ubuntu 20.04

This first problem has been solved. A follow-up problem is in my reply below this one.

I started tinkering with SDL’s audio capabilities and ran into an issue with opening audio. Here’s a few code snippets of how I am initializing the libraries:

  printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
  success = false;


int mixFlags = MIX_INIT_MP3;
if(!(Mix_Init(mixFlags) & mixFlags)) {
  printf("SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError());
  success = false;

The initialization runs with no errors and executing the following code shows I am using the alsa driver:

printf("Current audio driver: %s\n", SDL_GetCurrentAudioDriver());

Current audio driver: alsa

Running code to open audio is where issues start. If I call the Mix_OpenAudio function, I get an alsa error.

if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 2048) < 0) {
    printf("Mix_OpenAudio: %s\n", Mix_GetError());
    success = false;

ALSA lib pcm_dmix.c:1089:(snd_pcm_dmix_open) unable to open slave
Mix_OpenAudio: ALSA: Couldn't open audio device: No such file or directory

Now, the real confusion comes from when I replace the Mix_OpenAudio call with
Mix_OpenAudioDevice (sidenote, is this function even in the docs?).

if(Mix_OpenAudioDevice(44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 2048, SDL_GetAudioDeviceName(7, 0), SDL_AUDIO_ALLOW_ANY_CHANGE) < 0) {
    printf("Mix_OpenAudioDevice: %s\n", Mix_GetError());
    success = false;

This code actually runs fine given the specific index of the device I want to use. Unfortunately, this isn’t exactly the greatest option since a default device isn’t being automatically used.
I should note that all the libraries I’m using are coming from the current ubuntu packages, libraries are being applied in the make file of my project, and g++ is current as well.

Ubuntu 20.04
libsdl2-2.0-0 – Version: 2.0.10+dfsg1-3
libsdl2-mixer-2.0-0 – Version: 2.0.4+dfsg1-2build1
libasound2 – Version: 1.2.2-2.1

alsa driver version: k5.4.0-33-generic

The dev libraries are the same version.
Anyone know what’s going on here? I feel like it’s a silly mistake on my part but I don’t know. Any help is appreciated!

I edited the file /usr/share/alsa/alsa.conf and changed a few lines suggested here. They basically say to set defaults.ctl.card and defaults.pcm.card to whichever card number you want. Doing that made the Mix_OpenAudio call work.

This is great, but now whenever I play other application audio in parallel alsa refuses connections. Does this mean I need to switch to pulseaudio instead? If so, I keep getting this error after setting the environment variables appropriately:

putenv((char *)"SDL_AUDIODRIVER=pulseaudio");

SDL could not initialize! SDL_Error: Could not setup connection to PulseAudio

Okay, after setting this aside and coming back to it recently, the issue has been resolved. This has also solved the device hogging problem.

Thankfully the ubuntu distribution of the lib SDL_mixer dev package provided example code which worked flawlessly on 20.04. The main takeaway is probably that the Mix_QuerySpec function was what I was missing. If you have installed the dev package, the example code can be found in /usr/share/doc/libsdl2-mixer-dev/examples. I strongly encourage anyone who reads this to look at those examples and test them out.

Curiously enough I didn’t even need to use Init_Mixer… it plays wav and mp3 files just fine.

Another note, for some reason audio does not work right when using a terminal inside VSCode. Running the program from a standard terminal works.