Audio-LibSampleRate

 view release on metacpan or  search on metacpan

libsamplerate/examples/audio_out.c  view on Meta::CPAN

		} ;

	if (alsa_out->magic != ALSA_MAGIC)
	{	printf ("alsa_close : Bad magic number.\n") ;
		return ;
		} ;

	while ((read_frames = callback (callback_data, buffer, BUFFER_LEN / alsa_out->channels)))
		alsa_write_float (alsa_out->dev, buffer, read_frames, alsa_out->channels) ;

	return ;
} /* alsa_play */

static int
alsa_write_float (snd_pcm_t *alsa_dev, float *data, int frames, int channels)
{	static	int epipe_count = 0 ;

	int total = 0 ;
	int retval ;

	if (epipe_count > 0)
		epipe_count -- ;

	while (total < frames)
	{	retval = snd_pcm_writei (alsa_dev, data + total * channels, frames - total) ;

		if (retval >= 0)
		{	total += retval ;
			if (total == frames)
				return total ;

			continue ;
			} ;

		switch (retval)
		{	case -EAGAIN :
					puts ("alsa_write_float: EAGAIN") ;
					continue ;
					break ;

			case -EPIPE :
					if (epipe_count > 0)
					{	printf ("alsa_write_float: EPIPE %d\n", epipe_count) ;
						if (epipe_count > 140)
							return retval ;
						} ;
					epipe_count += 100 ;

#if 0
					if (0)
					{	snd_pcm_status_t *status ;

						snd_pcm_status_alloca (&status) ;
						if ((retval = snd_pcm_status (alsa_dev, status)) < 0)
							fprintf (stderr, "alsa_out: xrun. can't determine length\n") ;
						else if (snd_pcm_status_get_state (status) == SND_PCM_STATE_XRUN)
						{	struct timeval now, diff, tstamp ;

							gettimeofday (&now, 0) ;
							snd_pcm_status_get_trigger_tstamp (status, &tstamp) ;
							timersub (&now, &tstamp, &diff) ;

							fprintf (stderr, "alsa_write_float xrun: of at least %.3f msecs. resetting stream\n",
									diff.tv_sec * 1000 + diff.tv_usec / 1000.0) ;
							}
						else
							fprintf (stderr, "alsa_write_float: xrun. can't determine length\n") ;
						} ;
#endif

					snd_pcm_prepare (alsa_dev) ;
					break ;

			case -EBADFD :
					fprintf (stderr, "alsa_write_float: Bad PCM state.n") ;
					return 0 ;
					break ;

			case -ESTRPIPE :
					fprintf (stderr, "alsa_write_float: Suspend event.n") ;
					return 0 ;
					break ;

			case -EIO :
					puts ("alsa_write_float: EIO") ;
					return 0 ;

			default :
					fprintf (stderr, "alsa_write_float: retval = %d\n", retval) ;
					return 0 ;
					break ;
			} ; /* switch */
		} ; /* while */

	return total ;
} /* alsa_write_float */

static void
alsa_close (AUDIO_OUT *audio_out)
{	ALSA_AUDIO_OUT *alsa_out ;

	if ((alsa_out = (ALSA_AUDIO_OUT*) audio_out) == NULL)
	{	printf ("alsa_close : AUDIO_OUT is NULL.\n") ;
		return ;
		} ;

	if (alsa_out->magic != ALSA_MAGIC)
	{	printf ("alsa_close : Bad magic number.\n") ;
		return ;
		} ;

	memset (alsa_out, 0, sizeof (ALSA_AUDIO_OUT)) ;

	free (alsa_out) ;

	return ;
} /* alsa_close */

#endif /* HAVE_ALSA_ASOUNDLIB_H */

#include <fcntl.h>



( run in 1.345 second using v1.01-cache-2.11-cpan-6b5c3043376 )