Re: Playing audio with ALSA api(newbie)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



You solved my problem. I made some changes to the program, I now know that the problem was not the small buffer. It's all working perfect. I got the segmentation fault. It plays it receives on stdin. I send you the program as an attachment.

2012/4/11 Torsten Schenk <torsten.schenk@xxxxxxxx>
Hello Fabio,

> snd_pcm_writei (playback_handle, p, 131072)
writes 131072 FRAMES

> p += 131072
increases the pointer by 131072 SAMPLES.

If you write
p += 131072 * number_of_channels
instead, your program should do the job.

Greets,
Torsten


#include <stdio.h>
#include <stdlib.h>
#include <alsa/asoundlib.h>
	
#define BUFFER 128

	
int main (int argc, char *argv[]) 
{
	int err;
	short buf[BUFFER*2];
	int rate = 44100;
	snd_pcm_t *playback_handle;
	snd_pcm_hw_params_t *hw_params;
	char* out;
	
	if( argc <= 1 )
	  out = "default";
	else 
	  out = argv[1];
	
	if ((err = snd_pcm_open (&playback_handle, out, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
		fprintf (stderr, "cannot open audio device %s (%s)\n", 
			 argv[1],
			 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 (playback_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 (playback_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 (playback_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 (playback_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 (playback_handle, hw_params, 2)) < 0) {
		fprintf (stderr, "cannot set channel count (%s)\n",
			 snd_strerror (err));
		exit (1);
	}

	if ((err = snd_pcm_hw_params (playback_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_prepare (playback_handle)) < 0) {
		fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
			 snd_strerror (err));
		exit (1);
	}
	
	while( !feof(stdin) ) {
		 
	  fread( &buf, 1, BUFFER * sizeof(short) * 2, stdin );
	  
		if ((err = snd_pcm_writei (playback_handle, &buf, BUFFER)) != BUFFER) {
			fprintf (stderr, "write to audio interface failed (%s)\n",
				 snd_strerror (err));
			exit (1);
		}
		
	}
	
	snd_pcm_close (playback_handle);
	exit (0);
}
------------------------------------------------------------------------------
For Developers, A Lot Can Happen In A Second.
Boundary is the first to Know...and Tell You.
Monitor Your Applications in Ultra-Fine Resolution. Try it FREE!
http://p.sf.net/sfu/Boundary-d2dvs2
_______________________________________________
Alsa-user mailing list
Alsa-user@xxxxxxxxxxxxxxxxxxxxx
https://lists.sourceforge.net/lists/listinfo/alsa-user

[Index of Archives]     [ALSA Devel]     [Linux Audio Users]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]

  Powered by Linux