ALSA init failed – how to detect?

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

The output I get is:

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):

    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

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

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.

    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

1 Like