View Single Post
Old 07-10-2012, 08:01 PM   #80
twobob
( ͡° ͜ʖ ͡°){ʇlnɐɟ ƃǝs}Týr
twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.
 
twobob's Avatar
 
Posts: 6,586
Karma: 6299993
Join Date: Jun 2012
Location: uti gratia usura (Yao ying da ying; Mo ying da yieng)
Device: PW-WIFI|K5-3G+WIFI| K4|K3-3G|DXG|K2| Rooted Nook Touch
More code - More fail:

Another interrupt driven example that fails:

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

Title "A Minimal Interrupt-Driven Program" ( 3rd on page)

THE FAIL: straced.
Spoiler:
Code:
ioctl(4, SNDRV_PCM_IOCTL_SW_PARAMS, 0xbed9eb48) = 0         
ioctl(4, SNDRV_PCM_IOCTL_PREPARE, 0)    = 0                          
ioctl(4, SNDRV_PCM_IOCTL_SYNC_PTR, 0xe8240) = 0                       
ioctl(4, SNDRV_PCM_IOCTL_SW_PARAMS, 0xe8170) = 0              
ioctl(4, SNDRV_PCM_IOCTL_PREPARE, 0)    = 0
ioctl(4, SNDRV_PCM_IOCTL_SYNC_PTR, 0xe8240) = 0
poll([{fd=4, events=POLLOUT|POLLERR|POLLNVAL}], 1, 1000) = 0 (Timeout)
ioctl(4, SNDRV_PCM_IOCTL_SYNC_PTR, 0xe8240) = 0
fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
write(1, "playback callback called with 40"..., 42) = 42
ioctl(4, SNDRV_PCM_IOCTL_WRITEI_FRAMES, 0xbed9eb7c) = 0               
ioctl(4, SNDRV_PCM_IOCTL_SYNC_PTR, 0xe8240) = 0             
poll([{fd=4, events=POLLOUT|POLLERR|POLLNVAL}], 1, 1000) = 1 ([{fd=4,
ioctl(4, SNDRV_PCM_IOCTL_SYNC_PTR, 0xe8240) = 0
write(2, "poll failed (No such device or a"..., 40) = 40
ioctl(4, SNDRV_PCM_IOCTL_DROP, 0xdbe3c) = 0
ioctl(4, SNDRV_PCM_IOCTL_HW_FREE, 0x2b018) = 0


The Code
Spoiler:
Code:
	#include <stdio.h>
	#include <stdlib.h>
	#include <errno.h>
	#include <poll.h>
	#include <alsa/asoundlib.h>

	snd_pcm_t *playback_handle;
	short buf[4096];

	int
	playback_callback (snd_pcm_sframes_t nframes)
	{
		int err;

		printf ("playback callback called with %u frames\n", nframes);

		/* ... fill buf with data ... */

		if ((err = snd_pcm_writei (playback_handle, buf, nframes)) < 0) {
			fprintf (stderr, "write failed (%s)\n", snd_strerror (err));
		}

		return err;
	}

	main (int argc, char *argv[])
	{

		snd_pcm_hw_params_t *hw_params;
		snd_pcm_sw_params_t *sw_params;
		snd_pcm_sframes_t frames_to_deliver;
		int nfds;
		int err;
		struct pollfd *pfds;

		if ((err = snd_pcm_open (&playback_handle, argv[1], 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);
		}
   //     printf("here");
		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 (playback_handle, hw_params, 44100, 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);

		/* tell ALSA to wake us up whenever 4096 or more frames
		   of playback data can be delivered. Also, tell
		   ALSA that we'll start the device ourselves.
		*/

		if ((err = snd_pcm_sw_params_malloc (&sw_params)) < 0) {
			fprintf (stderr, "cannot allocate software parameters structure (%s)\n",
				 snd_strerror (err));
			exit (1);
		}
		if ((err = snd_pcm_sw_params_current (playback_handle, sw_params)) < 0) {
			fprintf (stderr, "cannot initialize software parameters structure (%s)\n",
				 snd_strerror (err));
			exit (1);
		}
		if ((err = snd_pcm_sw_params_set_avail_min (playback_handle, sw_params, 4096)) < 0) {
			fprintf (stderr, "cannot set minimum available count (%s)\n",
				 snd_strerror (err));
			exit (1);
		}
		if ((err = snd_pcm_sw_params_set_start_threshold (playback_handle, sw_params, 0U)) < 0) {
			fprintf (stderr, "cannot set start mode (%s)\n",
				 snd_strerror (err));
			exit (1);
		}
		if ((err = snd_pcm_sw_params (playback_handle, sw_params)) < 0) {
			fprintf (stderr, "cannot set software parameters (%s)\n",
				 snd_strerror (err));
			exit (1);
		}

		/* the interface will interrupt the kernel every 4096 frames, and ALSA
		   will wake up this program very soon after that.
		*/

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

		while (4) {

			/* wait till the interface is ready for data, or 1 second
			   has elapsed.
			*/

			if ((err = snd_pcm_wait (playback_handle, 1000)) < 0) {
			        fprintf (stderr, "poll failed (%s)\n", strerror (errno));
			        break;
			}

			/* find out how much space is available for playback data */

			if ((frames_to_deliver = snd_pcm_avail_update (playback_handle)) < 0) {
				if (frames_to_deliver == -EPIPE) {
					fprintf (stderr, "an xrun occured\n");
					break;
				} else {
					fprintf (stderr, "unknown ALSA avail update return value (%d)\n",
						 frames_to_deliver);
					break;
				}
			}

			frames_to_deliver = frames_to_deliver > 4096 ? 4096 : frames_to_deliver;

			/* deliver the data */

			if (playback_callback (frames_to_deliver) != frames_to_deliver) {
			        fprintf (stderr, "playback callback failed\n");
				break;
			}
		}

		snd_pcm_close (playback_handle);
		exit (0);
	}

So issues with that one... Polling doesn't look like it wants to play nice.
It's only an example. I prefer the previous posts design in oh so many ways.

Last edited by twobob; 07-10-2012 at 08:06 PM. Reason: Added Code ref
twobob is offline   Reply With Quote