[alsa-devel] Using snd_pcm_set_params() on a capture handle

Erik de Castro Lopo mle+la at mega-nerd.com
Sun Oct 25 07:44:42 CET 2015


Hi all,

I've got a simple PCM capture test program below, which is basically
a slightly tweak version of something from:

    http://equalarea.com/paul/alsa-audio.html


The program uses two different methods of setting up the capture
handle; one that works (and is complex) and one that doesn't but
seems like it should (and is simple). I'm trying to figure out
why the simple one (using snd_pcm_set_params) doesn't work.

The program can be compiled and run in two ways:

    gcc -Wall -O2 -DUSE_SET_PARAMS=1 alsa_capture.c -lasound -o alsa_capture 
    ./alsa_capture

which fails with:

    Read from audio interface failed (Input/output error)

and:

    gcc -Wall -O2 -DUSE_SET_PARAMS=0 alsa_capture.c -lasound -o alsa_capture
    ./alsa_capture

which works as expected.

Questions:

* Is snd_pcm_set_params() supposed to work on capture handles (I've used
  it successfully on playback handles)?

* Is my use of snd_pcm_set_params() missing something and if so what?

* If snd_pcm_set_params() is not supposed to work on capture handles
  would it be possible to update the documentation to reflect that?

Thanks,
Erik

//---------------------------------------------
#include <stdio.h>
#include <stdlib.h>

#include <alsa/asoundlib.h>
#include <alsa/pcm.h>

int
main (int argc, char *argv[])
{
	int i;
	int err;
	char *buffer;
	int buffer_frames = 128;
	unsigned int rate = 44100;
	snd_pcm_t *capture_handle;
	snd_pcm_format_t format = SND_PCM_FORMAT_S32_LE;

	char* device = "default";
	if (argc > 1) device = argv[1];


	if ((err = snd_pcm_open (&capture_handle, device, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
		printf ("Cannot open audio device %s (%s)\n", device, snd_strerror (err)); exit (1) ;
	}

	if (USE_SET_PARAMS) {
		if ((err = snd_pcm_set_params (capture_handle, format, SND_PCM_ACCESS_RW_INTERLEAVED, 2, rate, 0, 500000)) < 0) {   /* 0.5sec */
			printf ("Capture open error: %s\n", snd_strerror (err)); exit (1) ;
		}
	}
	else {
		snd_pcm_hw_params_t *hw_params;

		if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
			printf ("Cannot allocate hardware parameter structure (%s)\n", snd_strerror (err)); exit (1) ;
		}

		if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0) {
			printf ("Cannot initialize hardware parameter structure (%s)\n", snd_strerror (err)); exit (1) ;
		}

		if ((err = snd_pcm_hw_params_set_access (capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
			printf ("cannot set access type (%s)\n", snd_strerror (err)); exit (1) ;
		}

		if ((err = snd_pcm_hw_params_set_format (capture_handle, hw_params, format)) < 0) {
			printf ("Cannot set sample format (%s)\n", snd_strerror (err)); exit (1) ;
		}

		if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, &rate, 0)) < 0) {
			printf ("Cannot set sample rate (%s)\n", snd_strerror (err)); exit (1) ;
		}

		if ((err = snd_pcm_hw_params_set_channels (capture_handle, hw_params, 2)) < 0) {
			printf ("cannot set channel count (%s)\n", snd_strerror (err)); exit (1) ;
		}

		if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) {
			printf ("cannot set parameters (%s)\n", snd_strerror (err)); exit (1) ;
		}

		snd_pcm_hw_params_free (hw_params);
	}

	buffer = malloc (buffer_frames * snd_pcm_format_width(format) / 8 * 2);

	if ((err = snd_pcm_prepare (capture_handle)) < 0) {
		printf ("cannot prepare audio interface for use (%s)\n", snd_strerror (err)); exit (1) ;
	}

	for (i = 0; i < 10; ++i) {
		if ((err = snd_pcm_readi (capture_handle, buffer, buffer_frames)) != buffer_frames) {
			printf ("Read from audio interface failed (%s)\n", snd_strerror (err)); exit (1) ;
			break;
			}
		printf("read %d done\n", i);
	}

	free(buffer);

	snd_pcm_close (capture_handle);
	puts ("Audio interface closed");

	return 0;
}

-- 
----------------------------------------------------------------------
Erik de Castro Lopo
http://www.mega-nerd.com/


More information about the Alsa-devel mailing list