2001-06-07 15:43:47 +00:00
|
|
|
/*
|
|
|
|
ao_alsa9 - ALSA-0.9.x output plugin for MPlayer
|
|
|
|
|
|
|
|
(C) Alex Beregszaszi <alex@naxine.org>
|
2001-06-21 21:27:56 +00:00
|
|
|
|
2001-10-15 11:17:19 +00:00
|
|
|
modified for better alsa-0.9.0beta8a-support by Joy Winter <joy@pingfm.org>
|
|
|
|
|
|
|
|
This driver is still at alpha stage.
|
|
|
|
If you want stable sound-support use the OSS emulation instead.
|
|
|
|
|
|
|
|
Any bugreports regarding to this driver are welcome either to the mplayer-user-mailinglist or directly to the authors.
|
2001-06-07 15:43:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
2002-04-27 01:25:32 +00:00
|
|
|
|
2002-04-27 08:38:33 +00:00
|
|
|
#include "../config.h"
|
|
|
|
|
2002-04-27 01:25:32 +00:00
|
|
|
#if HAVE_SYS_ASOUNDLIB_H
|
2001-06-07 15:43:47 +00:00
|
|
|
#include <sys/asoundlib.h>
|
2002-04-27 01:25:32 +00:00
|
|
|
#elif HAVE_ALSA_ASOUNDLIB_H
|
|
|
|
#include <alsa/asoundlib.h>
|
|
|
|
#else
|
|
|
|
#error "asoundlib.h is not in sys/ or alsa/ - please bugreport"
|
|
|
|
#endif
|
2001-06-07 15:43:47 +00:00
|
|
|
|
|
|
|
#include "audio_out.h"
|
|
|
|
#include "audio_out_internal.h"
|
2001-06-08 23:31:06 +00:00
|
|
|
#include "afmt.h"
|
2001-06-07 15:43:47 +00:00
|
|
|
|
|
|
|
extern int verbose;
|
|
|
|
|
|
|
|
static ao_info_t info =
|
|
|
|
{
|
|
|
|
"ALSA-0.9.x audio output",
|
|
|
|
"alsa9",
|
2001-10-15 11:17:19 +00:00
|
|
|
"Alex Beregszaszi <alex@naxine.org>, Joy Winter <joy@pingfm.org>",
|
2001-06-07 15:43:47 +00:00
|
|
|
"under developement"
|
|
|
|
};
|
|
|
|
|
|
|
|
LIBAO_EXTERN(alsa9)
|
|
|
|
|
|
|
|
|
|
|
|
static snd_pcm_t *alsa_handler;
|
|
|
|
static snd_pcm_format_t alsa_format;
|
|
|
|
static snd_pcm_hw_params_t *alsa_hwparams;
|
|
|
|
static snd_pcm_sw_params_t *alsa_swparams;
|
|
|
|
static char *alsa_device;
|
2001-10-15 11:17:19 +00:00
|
|
|
#define ALSA_DEVICE_SIZE 48
|
|
|
|
|
|
|
|
static int alsa_fragsize = 8192; /* possible 4096, original 8192 */
|
|
|
|
static int alsa_fragcount = 8;
|
2001-06-07 15:43:47 +00:00
|
|
|
|
2001-10-15 11:17:19 +00:00
|
|
|
static int chunk_size = -1;
|
|
|
|
static int start_delay = 1;
|
2001-06-14 18:00:13 +00:00
|
|
|
|
2001-06-07 15:43:47 +00:00
|
|
|
/* to set/get/query special features/parameters */
|
|
|
|
static int control(int cmd, int arg)
|
|
|
|
{
|
|
|
|
switch(cmd)
|
|
|
|
{
|
|
|
|
case AOCONTROL_GET_DEVICE:
|
2001-06-17 17:44:56 +00:00
|
|
|
return((char *)alsa_device); /* egy kicsit brutalis, dehat :) */
|
2001-06-07 15:43:47 +00:00
|
|
|
case AOCONTROL_SET_DEVICE:
|
2001-06-17 17:44:56 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2001-06-07 15:43:47 +00:00
|
|
|
strncpy(alsa_device, (char *)arg, ALSA_DEVICE_SIZE);
|
2001-06-17 17:44:56 +00:00
|
|
|
uninit();
|
2001-11-24 05:21:22 +00:00
|
|
|
ret = init(ao_data.samplerate, ao_data.channels, ao_data.format, 0);
|
2001-06-17 17:44:56 +00:00
|
|
|
if (ret == 0)
|
|
|
|
return(CONTROL_ERROR);
|
|
|
|
else
|
|
|
|
return(CONTROL_OK);
|
|
|
|
}
|
2001-06-07 15:43:47 +00:00
|
|
|
}
|
|
|
|
return(CONTROL_UNKNOWN);
|
|
|
|
}
|
|
|
|
|
2001-10-15 11:17:19 +00:00
|
|
|
#undef start /* orig. undef */
|
2001-11-26 02:16:01 +00:00
|
|
|
#define buffsize
|
2001-10-15 11:17:19 +00:00
|
|
|
#define buffertime /* orig. undef? */
|
2001-06-14 18:00:13 +00:00
|
|
|
#define set_period
|
2001-10-15 11:17:19 +00:00
|
|
|
#define sw_params /* orig. undef */
|
|
|
|
#undef set_start_mode /* orig. undef */
|
2001-06-14 18:00:13 +00:00
|
|
|
|
2001-06-07 15:43:47 +00:00
|
|
|
/*
|
|
|
|
open & setup audio device
|
|
|
|
return: 1=success 0=fail
|
|
|
|
*/
|
|
|
|
static int init(int rate_hz, int channels, int format, int flags)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
int cards = -1;
|
|
|
|
snd_pcm_info_t *alsa_info;
|
2001-06-21 21:27:56 +00:00
|
|
|
|
2001-10-15 11:17:19 +00:00
|
|
|
size_t xfer_align; //new
|
|
|
|
snd_pcm_uframes_t start_threshold, stop_threshold; //new
|
2001-06-07 15:43:47 +00:00
|
|
|
|
2001-10-15 11:17:19 +00:00
|
|
|
printf("alsa-init: this driver is still at alpha-stage. if you want stable sound support use the OSS emulation instead.\n");
|
2001-06-07 15:43:47 +00:00
|
|
|
printf("alsa-init: requested format: %d Hz, %d channels, %s\n", rate_hz,
|
|
|
|
channels, audio_out_format_name(format));
|
|
|
|
|
|
|
|
alsa_handler = NULL;
|
|
|
|
|
|
|
|
if (verbose)
|
2001-10-03 18:09:51 +00:00
|
|
|
printf("alsa-init: compiled for ALSA-%s\n", SND_LIB_VERSION_STR);
|
2001-06-07 15:43:47 +00:00
|
|
|
|
|
|
|
if ((err = snd_card_next(&cards)) < 0 || cards < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: no soundcards found: %s\n", snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2001-11-24 05:21:22 +00:00
|
|
|
ao_data.samplerate = rate_hz;
|
|
|
|
ao_data.bps = channels; /* really this is bytes per frame so bad varname */
|
|
|
|
ao_data.format = format;
|
|
|
|
ao_data.channels = channels;
|
|
|
|
ao_data.outburst = OUTBURST;
|
|
|
|
ao_data.buffersize = 16384;
|
2001-06-07 15:43:47 +00:00
|
|
|
|
|
|
|
switch (format)
|
|
|
|
{
|
|
|
|
case AFMT_S8:
|
|
|
|
alsa_format = SND_PCM_FORMAT_S8;
|
|
|
|
break;
|
|
|
|
case AFMT_U8:
|
|
|
|
alsa_format = SND_PCM_FORMAT_U8;
|
|
|
|
break;
|
|
|
|
case AFMT_U16_LE:
|
|
|
|
alsa_format = SND_PCM_FORMAT_U16_LE;
|
|
|
|
break;
|
|
|
|
case AFMT_U16_BE:
|
|
|
|
alsa_format = SND_PCM_FORMAT_U16_BE;
|
|
|
|
break;
|
2002-04-22 22:33:06 +00:00
|
|
|
#ifndef WORDS_BIGENDIAN
|
|
|
|
case AFMT_AC3:
|
|
|
|
#endif
|
2001-06-07 15:43:47 +00:00
|
|
|
case AFMT_S16_LE:
|
|
|
|
alsa_format = SND_PCM_FORMAT_S16_LE;
|
|
|
|
break;
|
2002-04-22 22:33:06 +00:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
case AFMT_AC3:
|
|
|
|
#endif
|
2001-06-07 15:43:47 +00:00
|
|
|
case AFMT_S16_BE:
|
|
|
|
alsa_format = SND_PCM_FORMAT_S16_BE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
alsa_format = SND_PCM_FORMAT_MPEG;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(alsa_format)
|
|
|
|
{
|
|
|
|
case SND_PCM_FORMAT_S16_LE:
|
|
|
|
case SND_PCM_FORMAT_U16_LE:
|
2001-11-24 05:21:22 +00:00
|
|
|
ao_data.bps *= 2;
|
2001-06-07 15:43:47 +00:00
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
printf("alsa-init: invalid format (%s) requested - output disabled\n",
|
|
|
|
audio_out_format_name(format));
|
|
|
|
return(0);
|
2001-06-12 15:42:21 +00:00
|
|
|
default:
|
|
|
|
break;
|
2001-06-07 15:43:47 +00:00
|
|
|
}
|
2001-06-14 18:00:13 +00:00
|
|
|
|
2001-06-07 15:43:47 +00:00
|
|
|
if ((err = snd_pcm_info_malloc(&alsa_info)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: memory allocation error: %s\n", snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2001-11-26 00:49:39 +00:00
|
|
|
if (ao_subdevice != NULL)
|
|
|
|
alsa_device = ao_subdevice;
|
2001-06-23 18:59:54 +00:00
|
|
|
|
2001-06-17 17:44:56 +00:00
|
|
|
if (alsa_device == NULL)
|
2001-06-07 15:43:47 +00:00
|
|
|
{
|
2001-06-17 17:44:56 +00:00
|
|
|
if ((alsa_device = malloc(ALSA_DEVICE_SIZE)) == NULL)
|
|
|
|
{
|
|
|
|
printf("alsa-init: memory allocation error: %s\n", strerror(errno));
|
|
|
|
return(0);
|
|
|
|
}
|
2001-06-07 15:43:47 +00:00
|
|
|
|
2001-06-17 17:44:56 +00:00
|
|
|
snprintf(alsa_device, ALSA_DEVICE_SIZE, "hw:%d,%d",
|
|
|
|
snd_pcm_info_get_device(alsa_info),
|
|
|
|
snd_pcm_info_get_subdevice(alsa_info));
|
2001-06-07 15:43:47 +00:00
|
|
|
|
2001-06-17 17:44:56 +00:00
|
|
|
snd_pcm_info_free(alsa_info);
|
|
|
|
}
|
2001-06-07 15:43:47 +00:00
|
|
|
|
|
|
|
printf("alsa-init: %d soundcard%s found, using: %s\n", cards+1,
|
|
|
|
(cards >= 0) ? "" : "s", alsa_device);
|
|
|
|
|
|
|
|
if ((err = snd_pcm_open(&alsa_handler, alsa_device, SND_PCM_STREAM_PLAYBACK,
|
|
|
|
0)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: playback open error: %s\n", snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
snd_pcm_hw_params_malloc(&alsa_hwparams);
|
2001-10-15 11:17:19 +00:00
|
|
|
//snd_pcm_sw_params_malloc(&alsa_swparams);
|
|
|
|
snd_pcm_sw_params_alloca(&alsa_swparams);
|
2001-06-07 15:43:47 +00:00
|
|
|
if ((err = snd_pcm_hw_params_any(alsa_handler, alsa_hwparams)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: unable to get initial parameters: %s\n",
|
|
|
|
snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((err = snd_pcm_hw_params_set_access(alsa_handler, alsa_hwparams,
|
|
|
|
SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: unable to set access type: %s\n",
|
|
|
|
snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((err = snd_pcm_hw_params_set_format(alsa_handler, alsa_hwparams,
|
|
|
|
alsa_format)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: unable to set format: %s\n",
|
|
|
|
snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((err = snd_pcm_hw_params_set_channels(alsa_handler, alsa_hwparams,
|
2001-11-24 05:21:22 +00:00
|
|
|
ao_data.channels)) < 0)
|
2001-06-07 15:43:47 +00:00
|
|
|
{
|
|
|
|
printf("alsa-init: unable to set channels: %s\n",
|
|
|
|
snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2001-11-24 05:21:22 +00:00
|
|
|
if ((err = snd_pcm_hw_params_set_rate_near(alsa_handler, alsa_hwparams, ao_data.samplerate, 0)) < 0)
|
2001-10-15 11:17:19 +00:00
|
|
|
/* was originally only snd_pcm_hw_params_set_rate jp*/
|
2001-10-03 18:09:51 +00:00
|
|
|
{
|
|
|
|
printf("alsa-init: unable to set samplerate-2: %s\n",
|
|
|
|
snd_strerror(err));
|
2001-10-15 11:17:19 +00:00
|
|
|
//snd_pcm_hw_params_dump(alsa_hwparams, errlog); jp
|
2001-10-03 18:09:51 +00:00
|
|
|
return(0);
|
|
|
|
}
|
2001-06-07 15:43:47 +00:00
|
|
|
|
2001-06-14 18:00:13 +00:00
|
|
|
#ifdef set_period
|
2001-06-12 15:42:21 +00:00
|
|
|
{
|
2001-06-14 18:00:13 +00:00
|
|
|
if ((err = snd_pcm_hw_params_set_period_size(alsa_handler, alsa_hwparams, alsa_fragsize / 4, 0)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: unable to set periodsize: %s\n",
|
|
|
|
snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
2001-10-15 11:17:19 +00:00
|
|
|
if ((err = snd_pcm_hw_params_set_periods(alsa_handler, alsa_hwparams, alsa_fragcount, 0)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: unable to set periods: %s\n",
|
|
|
|
snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
2001-06-12 15:42:21 +00:00
|
|
|
}
|
2001-06-14 18:00:13 +00:00
|
|
|
#endif
|
2001-11-26 02:16:01 +00:00
|
|
|
#ifdef buffsize
|
2001-06-07 15:43:47 +00:00
|
|
|
if ((err = snd_pcm_hw_params_get_buffer_size(alsa_hwparams)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: unable to get buffer size: %s\n",
|
|
|
|
snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
} else
|
2001-06-14 18:00:13 +00:00
|
|
|
{
|
2001-11-24 05:21:22 +00:00
|
|
|
ao_data.buffersize = err;
|
2001-06-17 17:44:56 +00:00
|
|
|
if (verbose)
|
2001-11-24 05:21:22 +00:00
|
|
|
printf("alsa-init: got buffersize %i\n", ao_data.buffersize);
|
2001-06-14 18:00:13 +00:00
|
|
|
}
|
2001-06-07 15:43:47 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef buffertime
|
|
|
|
{
|
2001-10-15 11:17:19 +00:00
|
|
|
int alsa_buffer_time = 500000; /* original 60 */
|
2001-06-07 15:43:47 +00:00
|
|
|
|
2001-10-03 18:09:51 +00:00
|
|
|
if ((err = snd_pcm_hw_params_set_buffer_time_near(alsa_handler, alsa_hwparams, alsa_buffer_time, 0)) < 0)
|
2001-06-07 15:43:47 +00:00
|
|
|
{
|
|
|
|
printf("alsa-init: unable to set buffer time near: %s\n",
|
|
|
|
snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
} else
|
|
|
|
alsa_buffer_time = err;
|
|
|
|
|
2001-10-15 11:17:19 +00:00
|
|
|
if ((err = snd_pcm_hw_params_set_period_time_near(alsa_handler, alsa_hwparams, alsa_buffer_time/4, 0)) < 0)
|
2001-11-24 05:21:22 +00:00
|
|
|
/* original: alsa_buffer_time/ao_data.bps */
|
2001-06-07 15:43:47 +00:00
|
|
|
{
|
|
|
|
printf("alsa-init: unable to set period time: %s\n",
|
|
|
|
snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
2001-06-14 18:00:13 +00:00
|
|
|
printf("alsa-init: buffer_time: %d, period_time :%d\n",
|
|
|
|
alsa_buffer_time, err);
|
2001-06-07 15:43:47 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if ((err = snd_pcm_hw_params(alsa_handler, alsa_hwparams)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: unable to set parameters: %s\n",
|
|
|
|
snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
2001-06-14 18:00:13 +00:00
|
|
|
|
|
|
|
#ifdef sw_params
|
2001-10-15 11:17:19 +00:00
|
|
|
{
|
|
|
|
chunk_size = snd_pcm_hw_params_get_period_size(alsa_hwparams, 0);
|
2001-11-24 05:21:22 +00:00
|
|
|
start_threshold = (double) ao_data.samplerate * start_delay / 1000000;
|
2001-10-15 11:17:19 +00:00
|
|
|
xfer_align = snd_pcm_sw_params_get_xfer_align(alsa_swparams);
|
|
|
|
|
2001-06-07 15:43:47 +00:00
|
|
|
if ((err = snd_pcm_sw_params_current(alsa_handler, alsa_swparams)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: unable to get parameters: %s\n",
|
|
|
|
snd_strerror(err));
|
|
|
|
return(0);
|
2001-10-15 11:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((err = snd_pcm_sw_params_set_avail_min(alsa_handler, alsa_swparams, chunk_size)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: unable to set avail_min %s\n",snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((err = snd_pcm_sw_params_set_start_threshold(alsa_handler, alsa_swparams, start_threshold)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: unable to set start_threshold %s\n",snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if ((err = snd_pcm_sw_params_set_xfer_align(alsa_handler, alsa_swparams, xfer_align)) < 0)
|
|
|
|
//{
|
|
|
|
// printf("alsa-init: unable to set xfer_align: %s\n",
|
|
|
|
// snd_strerror(err));
|
|
|
|
// return(0);
|
|
|
|
//}
|
2001-06-07 15:43:47 +00:00
|
|
|
|
2001-06-14 18:00:13 +00:00
|
|
|
#ifdef set_start_mode
|
2001-06-07 15:43:47 +00:00
|
|
|
if ((err = snd_pcm_sw_params_set_start_mode(alsa_handler, alsa_swparams,
|
|
|
|
SND_PCM_START_DATA)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: unable to set start mode: %s\n",
|
|
|
|
snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
2001-06-14 18:00:13 +00:00
|
|
|
#endif
|
2001-06-07 15:43:47 +00:00
|
|
|
|
|
|
|
if ((err = snd_pcm_sw_params(alsa_handler, alsa_swparams)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: unable to set parameters: %s\n",
|
|
|
|
snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2001-06-14 18:00:13 +00:00
|
|
|
// snd_pcm_sw_params_default(alsa_handler, alsa_swparams);
|
|
|
|
#endif
|
2001-06-07 15:43:47 +00:00
|
|
|
if ((err = snd_pcm_prepare(alsa_handler)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: pcm prepare error: %s\n", snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2001-06-12 15:42:21 +00:00
|
|
|
#ifdef start
|
2001-06-07 15:43:47 +00:00
|
|
|
if ((err = snd_pcm_start(alsa_handler)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: pcm start error: %s\n", snd_strerror(err));
|
|
|
|
if (err != -EPIPE)
|
|
|
|
return(0);
|
|
|
|
if ((err = snd_pcm_start(alsa_handler)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: pcm start error: %s\n", snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
}
|
2001-06-12 15:42:21 +00:00
|
|
|
#endif
|
2001-06-14 18:00:13 +00:00
|
|
|
printf("AUDIO: %d Hz/%d channels/%d bpf/%d bytes buffer/%s\n",
|
2001-11-24 05:21:22 +00:00
|
|
|
ao_data.samplerate, ao_data.channels, ao_data.bps, ao_data.buffersize,
|
2001-06-07 15:43:47 +00:00
|
|
|
snd_pcm_format_description(alsa_format));
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* close audio device */
|
|
|
|
static void uninit()
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (alsa_device != NULL)
|
|
|
|
free(alsa_device);
|
|
|
|
|
|
|
|
snd_pcm_hw_params_free(alsa_hwparams);
|
|
|
|
snd_pcm_sw_params_free(alsa_swparams);
|
|
|
|
|
|
|
|
if ((err = snd_pcm_drain(alsa_handler)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-uninit: pcm drain error: %s\n", snd_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-06-14 18:29:05 +00:00
|
|
|
#ifdef start
|
2001-06-07 15:43:47 +00:00
|
|
|
if ((err = snd_pcm_reset(alsa_handler)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-uninit: pcm reset error: %s\n", snd_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
2001-06-14 18:29:05 +00:00
|
|
|
#endif
|
2001-06-07 15:43:47 +00:00
|
|
|
|
|
|
|
if ((err = snd_pcm_close(alsa_handler)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-uninit: pcm close error: %s\n", snd_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void audio_pause()
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if ((err = snd_pcm_drain(alsa_handler)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-pause: pcm drain error: %s\n", snd_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-06-14 18:29:05 +00:00
|
|
|
#ifdef reset
|
2001-06-07 15:43:47 +00:00
|
|
|
if ((err = snd_pcm_reset(alsa_handler)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-pause: pcm reset error: %s\n", snd_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
2001-06-14 18:29:05 +00:00
|
|
|
#endif
|
2001-06-07 15:43:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void audio_resume()
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if ((err = snd_pcm_prepare(alsa_handler)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-resume: pcm prepare error: %s\n", snd_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-06-14 18:29:05 +00:00
|
|
|
#ifdef start
|
2001-06-07 15:43:47 +00:00
|
|
|
if ((err = snd_pcm_start(alsa_handler)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-resume: pcm start error: %s\n", snd_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
2001-06-14 18:29:05 +00:00
|
|
|
#endif
|
2001-06-07 15:43:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* stop playing and empty buffers (for seeking/pause) */
|
|
|
|
static void reset()
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if ((err = snd_pcm_drain(alsa_handler)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-reset: pcm drain error: %s\n", snd_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-06-14 18:29:05 +00:00
|
|
|
#ifdef start
|
2001-06-07 15:43:47 +00:00
|
|
|
if ((err = snd_pcm_reset(alsa_handler)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-reset: pcm reset error: %s\n", snd_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
2001-06-14 18:29:05 +00:00
|
|
|
#endif
|
2001-06-07 15:43:47 +00:00
|
|
|
|
|
|
|
if ((err = snd_pcm_prepare(alsa_handler)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-reset: pcm prepare error: %s\n", snd_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-06-14 18:29:05 +00:00
|
|
|
#ifdef start
|
2001-06-07 15:43:47 +00:00
|
|
|
if ((err = snd_pcm_start(alsa_handler)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-reset: pcm start error: %s\n", snd_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
2001-06-14 18:29:05 +00:00
|
|
|
#endif
|
2001-06-07 15:43:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
plays 'len' bytes of 'data'
|
|
|
|
returns: number of bytes played
|
|
|
|
*/
|
|
|
|
static int play(void* data, int len, int flags)
|
|
|
|
{
|
2001-06-12 15:42:21 +00:00
|
|
|
int got_len;
|
|
|
|
|
2001-10-03 18:09:51 +00:00
|
|
|
got_len = snd_pcm_writei(alsa_handler, data, len / 4);
|
|
|
|
|
2001-11-24 05:21:22 +00:00
|
|
|
//if ((got_len = snd_pcm_writei(alsa_handler, data, (len/ao_data.bps))) != (len/ao_data.bps)) {
|
2001-10-15 11:17:19 +00:00
|
|
|
//SHOULD BE FIXED
|
2001-06-12 15:42:21 +00:00
|
|
|
if (got_len == -EPIPE) /* underrun? */
|
2001-06-07 15:43:47 +00:00
|
|
|
{
|
|
|
|
printf("alsa-play: alsa underrun, resetting stream\n");
|
2001-06-14 18:00:13 +00:00
|
|
|
if ((got_len = snd_pcm_prepare(alsa_handler)) < 0)
|
2001-06-07 15:43:47 +00:00
|
|
|
{
|
2001-06-14 18:00:13 +00:00
|
|
|
printf("alsa-play: playback prepare error: %s\n", snd_strerror(got_len));
|
2001-06-07 15:43:47 +00:00
|
|
|
return(0);
|
|
|
|
}
|
2001-11-24 05:21:22 +00:00
|
|
|
if ((got_len = snd_pcm_writei(alsa_handler, data, (len/ao_data.bps))) != (len/ao_data.bps))
|
2001-06-07 15:43:47 +00:00
|
|
|
{
|
|
|
|
printf("alsa-play: write error after reset: %s - giving up\n",
|
2001-06-14 18:00:13 +00:00
|
|
|
snd_strerror(got_len));
|
2001-06-07 15:43:47 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
return(len); /* 2nd write was ok */
|
|
|
|
}
|
|
|
|
return(len);
|
2001-10-15 11:17:19 +00:00
|
|
|
//}
|
2001-06-07 15:43:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* how many byes are free in the buffer */
|
|
|
|
static int get_space()
|
|
|
|
{
|
|
|
|
snd_pcm_status_t *status;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = snd_pcm_status_malloc(&status)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-space: memory allocation error: %s\n", snd_strerror(ret));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = snd_pcm_status(alsa_handler, status)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-space: cannot get pcm status: %s\n", snd_strerror(ret));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(snd_pcm_status_get_state(status))
|
|
|
|
{
|
|
|
|
case SND_PCM_STATE_OPEN:
|
|
|
|
case SND_PCM_STATE_PREPARED:
|
|
|
|
case SND_PCM_STATE_RUNNING:
|
2001-11-24 05:21:22 +00:00
|
|
|
ret = snd_pcm_status_get_avail(status) * ao_data.bps;
|
2001-06-07 15:43:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
snd_pcm_status_free(status);
|
2001-06-14 18:29:05 +00:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
ret = 0;
|
2001-06-07 15:43:47 +00:00
|
|
|
return(ret);
|
|
|
|
}
|
|
|
|
|
2001-11-24 05:21:22 +00:00
|
|
|
/* delay in seconds between first and last sample in buffer */
|
|
|
|
static float get_delay()
|
2001-06-07 15:43:47 +00:00
|
|
|
{
|
|
|
|
snd_pcm_status_t *status;
|
2001-11-24 05:21:22 +00:00
|
|
|
float ret;
|
2001-06-07 15:43:47 +00:00
|
|
|
|
|
|
|
if ((ret = snd_pcm_status_malloc(&status)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-delay: memory allocation error: %s\n", snd_strerror(ret));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = snd_pcm_status(alsa_handler, status)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-delay: cannot get pcm status: %s\n", snd_strerror(ret));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(snd_pcm_status_get_state(status))
|
|
|
|
{
|
|
|
|
case SND_PCM_STATE_OPEN:
|
|
|
|
case SND_PCM_STATE_PREPARED:
|
|
|
|
case SND_PCM_STATE_RUNNING:
|
2001-11-24 05:21:22 +00:00
|
|
|
ret = (float)snd_pcm_status_get_delay(status)/(float)ao_data.samplerate;
|
2001-06-07 15:43:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
snd_pcm_status_free(status);
|
2001-06-14 18:29:05 +00:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
ret = 0;
|
2001-06-07 15:43:47 +00:00
|
|
|
return(ret);
|
|
|
|
}
|