Repository: libsdl-org/SDL
File: src/audio/alsa/SDL_alsa_audio.c (C)

Code before:
    }

    /* "set" the hardware with the desired parameters */
    status = ALSA_snd_pcm_hw_params(this->hidden->pcm_handle, hwparams);
    if (status < 0) {
        return -1;
    }

    this->spec.samples = persize;

    /* This is useful for debugging */
    if (SDL_getenv("SDL_AUDIO_ALSA_DEBUG")) {
        snd_pcm_uframes_t bufsize;

        ALSA_snd_pcm_hw_params_get_buffer_size(hwparams, &bufsize);

        SDL_LogError(SDL_LOG_CATEGORY_AUDIO,
                     "ALSA: period size = %ld, periods = %u, buffer size = %lu\n",
                     persize, periods, bufsize);
    }

    return 0;
}

static int ALSA_OpenDevice(_THIS, const char *devname)
{
    int status = 0;
    SDL_bool iscapture = this->iscapture;
    snd_pcm_t *pcm_handle = NULL;
    snd_pcm_hw_params_t *hwparams = NULL;
    snd_pcm_sw_params_t *swparams = NULL;
    snd_pcm_format_t format = 0;
    SDL_AudioFormat test_format = 0;
    unsigned int rate = 0;
    unsigned int channels = 0;
#ifdef SND_CHMAP_API_VERSION
    snd_pcm_chmap_t *chmap;
    char chmap_str[64];
#endif

    /* Initialize all variables that we clean on shutdown */
    this->hidden = (struct SDL_PrivateAudioData *)SDL_malloc(sizeof(*this->hidden));
    if (!this->hidden) {
        return SDL_OutOfMemory();
    }
    SDL_zerop(this->hidden);

    /* Open the audio device */
    /* Name of device should depend on # channels in spec */
    status = ALSA_snd_pcm_open(&pcm_handle,
                               get_audio_device(this->handle, this->spec.channels),

Review comment (suggestion):
Not sure what ALSA prefers by default if the hardware doesn't specify; the channel count is Good Enough(TM) but I wonder if there's a preference for `format` and `samples` in particular.

Code after:
        ALSA_snd_pcm_close(pcm_handle);
        return SDL_SetError("ALSA: Couldn't allocate pcm_info");
    }

    if (ALSA_snd_pcm_info(pcm_handle, pcm_info) < 0) {
        ALSA_snd_pcm_info_free(pcm_info);
        ALSA_snd_pcm_close(pcm_handle);
        return SDL_SetError("ALSA: Couldn't get PCM info");
    }

    card_index = ALSA_snd_pcm_info_get_card(pcm_info);
    dev_name = ALSA_snd_pcm_info_get_name(pcm_info);

    if (card_index >= 0 && ALSA_snd_card_get_name(card_index, &card_name) >= 0) {
        SDL_snprintf(final_name, sizeof(final_name), "%s, %s", card_name, dev_name);
        *name = SDL_strdup(final_name);
    } else {
        *name = SDL_strdup(dev_name ? dev_name : "Unknown ALSA Device");
    }

    if (spec) {
        SDL_zero(*spec);
        spec->freq = 48000;
        spec->format = AUDIO_S16SYS;
        spec->channels = 2;
        spec->samples = 512;
    }

    ALSA_snd_pcm_info_free(pcm_info);
    ALSA_snd_pcm_close(pcm_handle);
    return 0;
}

static int ALSA_set_buffer_size(_THIS, snd_pcm_hw_params_t *params)
{
    int status;
    snd_pcm_hw_params_t *hwparams;
    snd_pcm_uframes_t persize;
    unsigned int periods;

    /* Copy the hardware parameters for this setup */
    snd_pcm_hw_params_alloca(&hwparams);
    ALSA_snd_pcm_hw_params_copy(hwparams, params);

    /* Attempt to match the period size to the requested buffer size */
    persize = this->spec.samples;
    status = ALSA_snd_pcm_hw_params_set_period_size_near(
        this->hidden->pcm_handle, hwparams, &persize, NULL);
    if (status < 0) {
        return -1;
    }