Couldn't open audio device: No available audio device

I need a step-by-step guide on how to remedy the following error:

Couldn’t open audio device: No available audio device

I am running Ubuntu 11.04. I have been using SDL for OpenGL for a while now without any problems, but when I recently added sound support, it claims not to find any audio devices on my system.

This following code compiles perfectly with gcc but fails with the aforementioned error message.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_audio.h>

void mixaudio(void *unused, Uint8 *stream, int len) { }

int main(void) {
if( SDL_Init(SDL_INIT_AUDIO) < 0 ) exit(1);

    SDL_AudioSpec fmt;
    fmt.freq = 44100;
    fmt.format = AUDIO_S16;
    fmt.channels = 2;
    fmt.samples = 512;        /* A good value for games */
    fmt.callback = mixaudio;
    fmt.userdata = NULL;

    if ( SDL_OpenAudio(&fmt, NULL) < 0 ) {
        fprintf(stderr, "Couldn't open audio device: %s\n", SDL_GetError());
        exit(1);
    }
    SDL_Quit();

}

I know this code does nothing but it is not supposed to fail either. I have downloaded demo SDL programs from the SDL website and they all produce similar messages. I have tried using the SDL_Mixer sample code and that also fails.

Yet, if I use Wine and run Windows SDL demo’s with sound they work fine. Furthermore, programs such as Firefox, the Gnome movie player and games in Ubuntu all have sound as expected. If I boot to Windows on the same machine, SDL demo programs run without a hitch.

I have looked around google for ideas on how to fix this, and many times they talk about removing pulseaudio and installing libsdl1.2debian-all. That didn’t seem to help anything.

Any help is appreciated.

I have looked around google for ideas on how to fix this, and many times
they talk about removing pulseaudio and installing libsdl1.2debian-all.
That didn’t seem to help anything.

If you built your own SDL, you probably didn’t have development headers
for PulseAudio (or ALSA), so it’s trying to use /dev/dsp, which doesn’t
exist on many modern Linux systems (hence, SDL_Init(SDL_INIT_AUDIO)
succeeds, but no devices are found when you try to open one). “apt-get
install libasound2-dev libpulse-dev” and rebuild SDL…let the configure
script find the new headers so it includes PulseAudio and ALSA support.

If you didn’t build your own SDL, maybe you can force it to use a
different audio path:

SDL_AUDIODRIVER=pulse ./mytestprogram

or
SDL_AUDIODRIVER=alsa ./mytestprogram

One of those two solutions will (probably) fix your problem.

–ryan.

I tried your first suggestion of running apt-get for those libraries, then downloading the latest SDL source code and recompiling the library. It now works perfectly!

In a related question, when running this program outside of my development environment on another computer, what Ubuntu packages should I install to ensure that it plays sound correctly?

And finally, what the heck is asla or pulseaudio? No one really explains how it relates to SDL and what’s its for and how its used.

**
I tried your first suggestion of running apt-get for those libraries, then
downloading the latest SDL source code and recompiling the library. It now
works perfectly!

In a related question, when running this program outside of my development
environment on another computer, what Ubuntu packages should I install to
ensure that it plays sound correctly?

And finally, what the heck is asla or pulseaudio? No one really explains
how it relates to SDL and what’s its for and how its used.

ALSA: Advanced Linux Sound Architecture.
PulseAudio: A sound “server”.

Both of these are well explained in so many other sources it isn’t really
worth trying to give a poor explanation.

SDL uses different “backends” to support Linux sound. As you’ll find out,
Linux has a very fragmented and somewhat incompatible series of sound APIs.
It is hit or miss whether a certain API is available/useful on a given Linux
system. To counteract that, SDL can try different APIs to see which one
works. In the end, you see the clean SDL API and don’t have to deal with
writing backends to support every sound API under the sun.On Thu, Oct 6, 2011 at 2:36 PM, NiGHTS wrote:


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

Hello. I have the same problem with audio device. I am recently started with SDL. Can you explain me more detailed where to write this code SDL_AUDIODRIVER = pulse or SDL_AUDIODRIVER = alsa. Thank you.

If you’re on Linux and building your own copy of SDL, it’ll include support for various audio subsystems based on what it finds on your system, but those things don’t need to be available on the system that actually runs an app with that SDL library. So you probably need to install a “development package” for pulseaudio and alsa on your development machine.

docs/README-linux.md in SDL’s source tree has details on this.

If you don’t want to build SDL yourself, the build included in the Steam Runtime has all this compiled correctly; I tend to copy it out of there, even for games that aren’t shipping on Steam.

Otherwise, if you need to force a specific audio subsystem at runtime, you can in one of two ways:

  • Set an environment variable: export SDL_AUDIODRIVER=alsa
  • In your program, set it before calling SDL_Init: SDL_setenv("SDL_AUDIODRIVER", "alsa", 0);

Using SDL_setenv will still allow the environment variable to take precedence.

BUT! You probably don’t have to use either of these. Almost always, this happens in modern times because SDL was misbuilt (as described at the start of this message) and when that’s corrected, SDL will do the right thing by default.

My OS is Ubuntu 20.04. I have installed all the dependency that are presented in the README .I have tried with export SDL_AUDIODRIVER=alsa and with SDL_setenv before SDL_Init but still the same error appears. My IDE is Codeblocks and this is the code that gives me error:

if( Mix_OpenAudio( MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024) < 0 )
{
printf( “SDL_mixer could not initialize! SDL_mixer Error:%s\n”,Mix_GetError());
}
Error: SDL_mixer could not initialize! SDL_mixer Error: No such audio device

This is the error code with SDL_setenv after try to initialize:
SDL could not initialize! SDL Error: Audio target ‘alsa’ not available

Advanced Linux Sound Architecture Driver Version k5.4.0-33-generic. ALSA version

Can you please try :

On Linux:

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

On Windows, I’m using:

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

And it works perfectly on both …

Still nothing. I have removed and installed again alsa and pulseaudio library but again same error : No such device.
With putenv((char *)“SDL_AUDIODRIVER=alsa”) error is Audio target ‘alsa’ not available.

This error message means SDL was built without ALSA support, fwiw.

Can you explain me how to build SDL with ALSA support?

Rather than building it yourself, can you not download SDL2 from the Ubuntu repository? That should definitely have been built with ALSA support, I think:

sudo apt-get install libsdl2-2.0-0

Already have libsdl2-2.0-0 version (2.0.10+dfsg1-3).

1 Like