Skip to contentSkip to conversation

Docu:: Alsa Audio API:: A minimal capture program

DISCUSSION Torsten Mohr 3 participants 7 messages Jan 27, 2004 – Feb 9, 2004

lore
Share & tools
lore
#0 lore raw

Hi,

i copied the "minimal capture program" from the Alsa page -> Documentation -> Tutorial on using the ALSA Audio API -> A minimal capture program.

I added "int rate = 44100" and changed the wrong parameter "44100" in "snd_pcm_hw_params_set_rate_near" to "&rate".

bash# gcc capture.c -lasound
bash# a.out default

But if i add a "sleep(1)" in the "for(i = 0; i < 10; i++)", the whole program crashes.

I want to write a program where some chunk of data is sampled from time to time, with some delay inbetween. The behaviour above (with sleep(1)) is a good example for what i want to do.

Can anybody tell me what i need to change to make the program work ok?

Thanks for any hints,
Torsten.

-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004 Premiere Conference on Open Tools Development and Integration See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn

#1 lore raw
Quoted 3 lines · Torsten Mohr

I want to write a program where some chunk of data is sampled from time to time, with some delay inbetween.

Your program wants to ignore any buffer overruns. To do this, you have to change the sw_params: set the stop_threshold to either 0 or the buffer size in frames (I don't remember which).

HTH
Clemens

-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004 Premiere Conference on Open Tools Development and Integration See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn

#2 lore raw

Hi,

thanks for that hint.

But sadly, the attached program doesn't work, though i set stop_threshold to 0. I didn't find any functions to set buffersize to 0, it doesn't seem to be in the software parameters.

It would be great, if anybody had a hint.

Regards,
Torsten.

Quoted 16 lines
Quoted 3 lines · Torsten Mohr

I want to write a program where some chunk of data is sampled from time to time, with some delay inbetween.

Your program wants to ignore any buffer overruns. To do this, you have to change the sw_params: set the stop_threshold to either 0 or the buffer size in frames (I don't remember which).

HTH
Clemens

Quoted 9 lines

-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004 Premiere Conference on Open Tools Development and Integration See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn
_______________________________________________
Alsa-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/alsa-devel

#define _GNU_SOURCE
#include <stdio.h>
#include <malloc.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <alsa/asoundlib.h>
#include <sys/signal.h>

#include "sound.h"
#include "settings.h"

#define QWE fprintf(stderr, "File %s, Line %i\n", __FILE__, __LINE__)

int periodsize = 128;
int rate = 44100;
int i;
int err;
unsigned char* data;
snd_pcm_t *capture_handle;
snd_pcm_hw_params_t *hw_params;
snd_pcm_sw_params_t *sw_params;

unsigned char* sound_get_data(void) {
	return data;
}

int sound_init2(void) {

	if ((err = snd_pcm_open (&capture_handle, "plughw:0,0", SND_PCM_STREAM_CAPTURE, 0)) < 0) {
		fprintf (stderr, "cannot open audio device plughw:0,0 (%s)\n",
			 snd_strerror (err));
		exit (1);
	}
		   
	if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
		fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
			 snd_strerror (err));
		exit (1);
	}

	if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0) {
		fprintf (stderr, "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) {
		fprintf (stderr, "cannot set access type (%s)\n",
			 snd_strerror (err));
		exit (1);
	}
	
	if ((err = snd_pcm_hw_params_set_format (capture_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
		fprintf (stderr, "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) {
		fprintf (stderr, "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) {
		fprintf (stderr, "cannot set channel count (%s)\n",
			 snd_strerror (err));
		exit (1);
	}
	
	if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) {
		fprintf (stderr, "cannot set parameters (%s)\n",
			 snd_strerror (err));
		exit (1);
	}
	
	snd_pcm_hw_params_free (hw_params);

	if ((err = snd_pcm_sw_params_malloc (&sw_params)) < 0) {
		fprintf (stderr, "cannot allocate software parameter structure (%s)\n",
			 snd_strerror (err));
		exit (1);
	}

	if ((err = snd_pcm_sw_params_current (capture_handle, sw_params)) < 0) {
		fprintf (stderr, "cannot initialize software parameter structure (%s)\n",
			 snd_strerror (err));
		exit (1);
	}

	if ((err = snd_pcm_sw_params_set_stop_threshold (capture_handle, sw_params, 0)) < 0) {
		fprintf (stderr, "cannot set access type (%s)\n",
			 snd_strerror (err));
		exit (1);
	}

	if ((err = snd_pcm_sw_params (capture_handle, sw_params)) < 0) {
		fprintf (stderr, "cannot set sw-parameters (%s)\n",
			 snd_strerror (err));
		exit (1);
	} // */
	snd_pcm_sw_params_free (sw_params);

	
	if ((err = snd_pcm_prepare (capture_handle)) < 0) {
		fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
			 snd_strerror (err));
		exit (1);
	}
}
int sound_init(void) {
    data = (unsigned char *)calloc(SAMPLES*4, 0);
	sound_init2();
}

void sound_capture(void) {

QWE;
	if ((err = snd_pcm_readi (capture_handle, data, periodsize)) != periodsize) {
		fprintf (stderr, "read from audio interface failed (%s)\n",
			 snd_strerror (err));
		exit (1);
	}

QWE;
}

int main(int argc, char** argv) {
	sound_init();
	sound_capture();
	sound_capture();
	sleep(1);
	sound_capture();

	snd_pcm_close(capture_handle);
QWE;
	return 0;
}
#3 lore raw
Quoted 6 lines · Torsten Mohr

Hi,

thanks for that hint.

But sadly, the attached program doesn't work, though i set stop_threshold to 0. I didn't find any functions

This is wrong. You need to set this value to sw_params->boundary or greater value to eliminate the stop detection, otherwise with zero, the stream is immediately stopped.

Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SuSE Labs

-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004 Premiere Conference on Open Tools Development and Integration See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn

#4 lore raw

Hi,

Quoted 8 lines
Quoted 4 lines

thanks for that hint.

But sadly, the attached program doesn't work, though i set stop_threshold to 0. I didn't find any functions

This is wrong. You need to set this value to sw_params->boundary or greater value to eliminate the stop detection, otherwise with zero, the stream is immediately stopped.

thanks again.

But this doesn't seem to be the problem, the attached file is basically the example "minimal capture program" with the suggested additions.

The program fails badly. It would be so very great if somebody had a hint for me on that one. I tried to find the source of the problem, but i didn't succeed.

Thanks,
Torsten.

#define _GNU_SOURCE
#include <stdio.h>
#include <malloc.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <alsa/asoundlib.h>
#include <sys/signal.h>

//#include "sound.h"
//#include "settings.h"

#ifndef SAMPLES
#define SAMPLES 2048
#endif

#define QWE fprintf(stderr, "File %s, Line %i\n", __FILE__, __LINE__)

int periodsize = 4096;
int rate = 44100;
int i;
int err;
unsigned char* data;
snd_pcm_t *capture_handle;
snd_pcm_hw_params_t *hw_params;
snd_pcm_sw_params_t *sw_params;

snd_pcm_uframes_t boundary;

unsigned char* sound_get_data(void) {
	return data;
}

int sound_init2(void) {

	if ((err = snd_pcm_open (&capture_handle, "plughw:0,0", SND_PCM_STREAM_CAPTURE, 0)) < 0) {
		fprintf (stderr, "cannot open audio device plughw:0,0 (%s)\n",
			 snd_strerror (err));
		exit (1);
	}
		   
	if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
		fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
			 snd_strerror (err));
		exit (1);
	}

	if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0) {
		fprintf (stderr, "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) {
		fprintf (stderr, "cannot set access type (%s)\n",
			 snd_strerror (err));
		exit (1);
	}
	
	if ((err = snd_pcm_hw_params_set_format (capture_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
		fprintf (stderr, "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) {
		fprintf (stderr, "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) {
		fprintf (stderr, "cannot set channel count (%s)\n",
			 snd_strerror (err));
		exit (1);
	}
	
	if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) {
		fprintf (stderr, "cannot set parameters (%s)\n",
			 snd_strerror (err));
		exit (1);
	}
	
	snd_pcm_hw_params_free (hw_params);

	if ((err = snd_pcm_sw_params_malloc (&sw_params)) < 0) {
		fprintf (stderr, "cannot allocate software parameter structure (%s)\n",
			 snd_strerror (err));
		exit (1);
	}

	if ((err = snd_pcm_sw_params_current (capture_handle, sw_params)) < 0) {
		fprintf (stderr, "cannot initialize software parameter structure (%s)\n",
			 snd_strerror (err));
		exit (1);
	}

	if ((err = snd_pcm_sw_params_get_boundary (sw_params, &boundary)) < 0) {
		fprintf (stderr, "cannot set access type (%s)\n",
			 snd_strerror (err));
		exit (1);
	}


	if ((err = snd_pcm_sw_params_set_stop_threshold (capture_handle, sw_params, boundary)) < 0) {
		fprintf (stderr, "cannot set access type (%s)\n",
			 snd_strerror (err));
		exit (1);
	}

	if ((err = snd_pcm_sw_params (capture_handle, sw_params)) < 0) {
		fprintf (stderr, "cannot set sw-parameters (%s)\n",
			 snd_strerror (err));
		exit (1);
	} // */
	snd_pcm_sw_params_free (sw_params);

	
	if ((err = snd_pcm_prepare (capture_handle)) < 0) {
		fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
			 snd_strerror (err));
		exit (1);
	}
}
int sound_init(void) {
    data = (unsigned char *)calloc(SAMPLES*4, 0);
	sound_init2();
}

void sound_capture(void) {

QWE;
	if ((err = snd_pcm_readi (capture_handle, data, periodsize)) != periodsize) {
		fprintf (stderr, "read from audio interface failed (%s)\n",
			 snd_strerror (err));
		exit (1);
	}

QWE;
}

int main(int argc, char** argv) {
	sound_init();
	sound_capture();
	sound_capture();
	sleep(1);
	sound_capture();
	sound_capture();
QWE;
	snd_pcm_drop(capture_handle);
QWE;
//	snd_pcm_drain(capture_handle);
QWE;
	snd_pcm_close(capture_handle);
QWE;
	return 0;
} // */
#5 lore raw
Quoted 13 lines · Torsten Mohr

Hi,

Quoted 8 lines
Quoted 4 lines

thanks for that hint.

But sadly, the attached program doesn't work, though i set stop_threshold to 0. I didn't find any functions

This is wrong. You need to set this value to sw_params->boundary or greater value to eliminate the stop detection, otherwise with zero, the stream is immediately stopped.

thanks again.

Quoted 7 lines

But this doesn't seem to be the problem, the attached file is basically the example "minimal capture program" with the suggested additions.

The program fails badly. It would be so very great if somebody had a hint for me on that one. I tried to find the source of the problem, but i didn't succeed.

You've made this stupid bug:

data = (unsigned char *)calloc(SAMPLES*4, 0);

should be:

data = (unsigned char *)calloc(periodsize*4, 1);

Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SuSE Labs

-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004 Premiere Conference on Open Tools Development and Integration See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn

#6 lore raw

Hi,

Quoted 7 lines

You've made this stupid bug:

data = (unsigned char *)calloc(SAMPLES*4, 0);

should be:

data = (unsigned char *)calloc(periodsize*4, 1);

thank you so very much for that hint, that was the problem.

Best regards,
Torsten.

-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004 Premiere Conference on Open Tools Development and Integration See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn