Skip to content

Instantly share code, notes, and snippets.

@alpereira7
Last active March 14, 2021 20:05
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/020ded221d24ed9fcc8d92a902efc20f to your computer and use it in GitHub Desktop.
Save alpereira7/020ded221d24ed9fcc8d92a902efc20f to your computer and use it in GitHub Desktop.
Simple alsa API example. Get the min and max number of channels supported by capture audio device and other values.
/*
* file: capture_nb_chan.c
* description: Simple alsa API example. Get the min and max number of channels supported by capture 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 capture_nb_chan capture_nb_chan.c -lasound
* execute: ./capture_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 *capture_handle;
snd_pcm_hw_params_t *hw_params;
unsigned int min, max;
// Open audio device
if ((err = snd_pcm_open (&capture_handle, "hw:0,0", SND_PCM_STREAM_CAPTURE, 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 (capture_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