All of lore.kernel.org
 help / color / mirror / Atom feed
* Using snd_pcm_set_params() on a capture handle
@ 2015-10-25  6:44 Erik de Castro Lopo
  2015-10-25 20:11 ` Clemens Ladisch
  2015-10-26 15:26 ` Raymond Yau
  0 siblings, 2 replies; 11+ messages in thread
From: Erik de Castro Lopo @ 2015-10-25  6:44 UTC (permalink / raw)
  To: alsa-devel

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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2015-10-27  5:01 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-10-25  6:44 Using snd_pcm_set_params() on a capture handle Erik de Castro Lopo
2015-10-25 20:11 ` Clemens Ladisch
2015-10-26  8:47   ` Erik de Castro Lopo
2015-10-26  9:17     ` Clemens Ladisch
2015-10-26  9:23       ` Erik de Castro Lopo
2015-10-26  9:44         ` Clemens Ladisch
2015-10-26  9:54           ` Erik de Castro Lopo
2015-10-26 11:27             ` Lars-Peter Clausen
2015-10-26 15:26 ` Raymond Yau
2015-10-26 20:00   ` Erik de Castro Lopo
2015-10-27  5:01     ` Raymond Yau

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.