Skip to content

Instantly share code, notes, and snippets.

@alpereira7
Created March 14, 2021 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alpereira7/3b50b9261a03de7ce58e16e6cdee4660 to your computer and use it in GitHub Desktop.
Save alpereira7/3b50b9261a03de7ce58e16e6cdee4660 to your computer and use it in GitHub Desktop.
Simple alsa API example. Get the min and max number of channels supported by playback audio device and other values.
/*
* file: playback_nb_chan.c
* description: Simple alsa API example. Get the min and max number of channels supported by playback audio device and other values.
* doc: https://www.alsa-project.org/alsa-doc/alsa-lib/index.html
* install asound: apt-get install libasound2
* apt-get install libasound2-dev
* compile: gcc -o playback_nb_chan playback_nb_chan.c -lasound
* execute: ./playback_nb_chan
* expected output (something similar to) :
* Min channels: 2. (0)
* Max channels: 2. (0)
* ...
*/
#include <stdio.h>
#include <alsa/asoundlib.h>
int main(int argc, char* argv[])
{
int err;
snd_pcm_t *playback_handle;
snd_pcm_hw_params_t *hw_params;
unsigned int min, max;
// Open audio device
if ((err = snd_pcm_open (&playback_handle, "hw:0,0", SND_PCM_STREAM_PLAYBACK, 0)) < 0)
exit (1);
// Allocate hardware parameters
if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0)
exit (1);
// Initialize parameters
if ((err = snd_pcm_hw_params_any (playback_handle, hw_params)) < 0)
exit (1);
// Get min and max number of channels
err = snd_pcm_hw_params_get_channels_min(hw_params, &min);
if (err < 0)
exit (1);
fprintf(stdout, "Min channels: %u. (%d)\n", min, err);
err = snd_pcm_hw_params_get_channels_max(hw_params, &max);
if (err < 0)
exit (1);
fprintf(stdout, "Max channels: %u. (%d)\n", max, err);
// Get min and max sample rate
err = snd_pcm_hw_params_get_rate_min(hw_params, &min, 0);
if (err < 0)
exit (1);
fprintf(stdout, "Min sample rate: %u. (%d)\n", min, err);
err = snd_pcm_hw_params_get_rate_max(hw_params, &max, 0);
if (err < 0)
exit (1);
fprintf(stdout, "Max sample rate: %u. (%d)\n", max, err);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment