# ALSA init failed – how to detect?

**URL:** <https://qa.fmod.com/t/alsa-init-failed-how-to-detect/15508>\
**Category:** FMOD Engine\
**Created:** [February 29, 2020, 9:46pm UTC](https://qa.fmod.com/t/alsa-init-failed-how-to-detect/15508 "2020-02-29T21:46:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![granjow](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/granjow/32/928_2.png) [@granjow](https://qa.fmod.com/u/granjow)\
**Post date:** [February 29, 2020, 9:46pm UTC](https://qa.fmod.com/t/alsa-init-failed-how-to-detect/15508/1 "2020-02-29T21:46:33Z")

</div>

I am starting an FMOD application on a Raspberry at startup using systemd. There is a ESI Gigaport HD+ connected to the Raspberry, which works very nicely usually.

Startup sometimes fails with `snd_pcm_open returned -2 = No such file or directory`, but it seems I do not have a way to react on this error programmatically. Is this true or am I missing something?

I could not reproduce the error by manually running the application (as root), which makes debugging a bit tricky. Raspbian has a `alsa-restore.service` which runs at startup, and the error might well be related to this service restoring ALSA settings. What ever reason – even exiting with a non-zero exit code would already help a lot as systemd would then restart the service a bit more successfully (I hope).

Probably related topic: [FMOD\_System\_Init fails in presence of USB MIDI device](https://qa.fmod.com/t/fmod-system-init-fails-in-presence-of-usb-midi-device/13163)

The output I get is:

```auto
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [LOG] System::create : Header version = 2.00.07. Current version = 2.00.07.
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [LOG] Manager::init : maxchannels = 1024 studioflags = 00000000 flags 00000000 extradriverdata (nil).
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [LOG] SystemI::init : Initialize version=20007 (107206), maxchannels=1024, flags=0x00020000
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [LOG] FMOD_OS_Init : Detected Neon instruction support, will use Neon optimized mixing and resampling.
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [LOG] SystemI::setOutputInternal : Setting output to 'FMOD ALSA Output'
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [LOG] OutputALSA::registerLib : Loaded ALSA version 1.1.3.
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [LOG] OutputALSA::enumerate : Found device NAME:null IOID:(null).
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [LOG] OutputALSA::enumerate : Found device NAME:default IOID:(null).
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [LOG] OutputALSA::enumerate : Found device NAME:sysdefault:CARD=ALSA IOID:Output.
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [LOG] OutputALSA::enumerate : Found device NAME:dmix:CARD=ALSA,DEV=0 IOID:Output.
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [LOG] OutputALSA::enumerate : Found device NAME:dmix:CARD=ALSA,DEV=1 IOID:Output.
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [LOG] OutputALSA::enumerate : Found device NAME:dmix:CARD=ALSA,DEV=2 IOID:Output.
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [LOG] OutputALSA::enumerate : Found device NAME:dsnoop:CARD=ALSA,DEV=0 IOID:Output.
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [LOG] OutputALSA::enumerate : Found device NAME:dsnoop:CARD=ALSA,DEV=1 IOID:Output.
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [LOG] OutputALSA::enumerate : Found device NAME:dsnoop:CARD=ALSA,DEV=2 IOID:Output.
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [LOG] OutputALSA::enumerate : Found device NAME:hw:CARD=ALSA,DEV=0 IOID:Output.
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [LOG] OutputALSA::enumerate : Found device NAME:hw:CARD=ALSA,DEV=1 IOID:Output.
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [LOG] OutputALSA::enumerate : Found device NAME:hw:CARD=ALSA,DEV=2 IOID:Output.
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [LOG] OutputALSA::enumerate : Found device NAME:plughw:CARD=ALSA,DEV=0 IOID:Output.
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [LOG] OutputALSA::enumerate : Found device NAME:plughw:CARD=ALSA,DEV=1 IOID:Output.
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [LOG] OutputALSA::enumerate : Found device NAME:plughw:CARD=ALSA,DEV=2 IOID:Output.
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: ALSA lib pcm_hw.c:1713:(_snd_pcm_hw_open) Invalid value for card
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [ERR] OutputALSA::init : snd_pcm_open returned -2 = No such file or directory.
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [LOG] SystemI::close : Closed.
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [LOG] LiveUpdate::release :
Feb 29 12:28:46 cc-fmod-sound cave-sound-zmq[376]: [LOG] LiveUpdate::reset : Reset connection (reason Disconnected)

```

And the code for this part looks as follows (mostly copy/pasted from the examples):

```auto
    system = nullptr;
    ERRCHECK(FMOD::Studio::System::create(&system));

    FMOD::System *coreSystem = nullptr;
    ERRCHECK(system->getCoreSystem(&coreSystem));
    ERRCHECK(coreSystem->setSoftwareFormat(12000, FMOD_SPEAKERMODE_7POINT1, 0));

```

Thanks,  
Simon

---

<div class="post-metadata">

**Author:** ![cameron-fmod](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/cameron-fmod/32/261_2.png) [@cameron-fmod](https://qa.fmod.com/u/cameron-fmod)\
**Post date:** [March 5, 2020, 1:25am UTC](https://qa.fmod.com/t/alsa-init-failed-how-to-detect/15508/2 "2020-03-05T01:25:53Z")

</div>

Not sure why the device is failing but FMOD’s system init should return and error which can be used to react.

---

<div class="post-metadata">

**Author:** ![granjow](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/granjow/32/928_2.png) [@granjow](https://qa.fmod.com/u/granjow)\
**Post date:** [March 6, 2020, 8:27pm UTC](https://qa.fmod.com/t/alsa-init-failed-how-to-detect/15508/3 "2020-03-06T20:27:45Z")

</div>

Thanks Cameron – I thought I had tried that already, but apparently I have been doing it the wrong way. With the return code of `system->initialize()` I do get an error code indeed if initialisation fails.

```auto
    FMOD_RESULT result = system->initialize(1024, FMOD_STUDIO_INIT_NORMAL, FMOD_INIT_NORMAL, extraDriverData);
    if (result != FMOD_RESULT::FMOD_OK) {
        std::cerr << "system->initialize() returned " << result << " in " << __FILE__ << " on line " << __LINE__
                  << std::endl;
        std::cerr << "Exiting because ALSA failed." << std::endl;
        exit(1);
    }

```

The service fails approximately every other run, but now systemd restarts it in that case and after a restart it initialises ALSA correctly.

Simon
