2001-06-07 15:43:47 +00:00
|
|
|
/*
|
|
|
|
ao_alsa9 - ALSA-0.9.x output plugin for MPlayer
|
|
|
|
|
|
|
|
(C) Alex Beregszaszi <alex@naxine.org>
|
2002-05-26 12:23:19 +00:00
|
|
|
|
2002-08-23 13:03:00 +00:00
|
|
|
modified for real alsa-0.9.0-support by Joy Winter <joy@pingfm.org>
|
|
|
|
additional AC3 passthrough support by Andy Lo A Foe <andy@alsaplayer.org>
|
|
|
|
08/22/2002 iec958-init rewritten and merged with common init, joy
|
2002-05-26 12:23:19 +00:00
|
|
|
|
2002-08-23 13:03:00 +00:00
|
|
|
Any bugreports regarding to this driver are welcome.
|
2001-06-07 15:43:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
2002-06-28 16:48:10 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <stdlib.h>
|
2002-07-03 21:43:28 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
2002-07-18 16:09:54 +00:00
|
|
|
#include <sys/poll.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;
|
2002-11-02 10:59:27 +00:00
|
|
|
static char *alsa_device;
|
2001-10-15 11:17:19 +00:00
|
|
|
|
2002-07-18 16:09:54 +00:00
|
|
|
/* possible 4096, original 8192
|
|
|
|
* was only needed for calculating chunksize? */
|
|
|
|
static int alsa_fragsize = 4096;
|
|
|
|
/* 16 sets buffersize to 16 * chunksize is as default 1024
|
|
|
|
* which seems to be good avarge for most situations
|
|
|
|
* so buffersize is 16384 frames by default */
|
|
|
|
static int alsa_fragcount = 16;
|
|
|
|
static int chunk_size = 1024; //is alsa_fragsize / 4
|
2001-06-07 15:43:47 +00:00
|
|
|
|
2003-01-10 18:28:18 +00:00
|
|
|
#define MIN_CHUNK_SIZE 1024
|
|
|
|
|
2002-10-07 19:21:35 +00:00
|
|
|
static size_t bits_per_sample, bytes_per_sample, bits_per_frame;
|
2002-06-28 16:48:10 +00:00
|
|
|
static size_t chunk_bytes;
|
|
|
|
|
2002-07-03 21:43:28 +00:00
|
|
|
int ao_mmap = 0;
|
|
|
|
int ao_noblock = 0;
|
2002-07-18 16:09:54 +00:00
|
|
|
int first = 1;
|
2002-07-03 21:43:28 +00:00
|
|
|
|
|
|
|
static int open_mode;
|
|
|
|
static int set_block_mode;
|
2002-06-28 16:48:10 +00:00
|
|
|
|
2002-07-03 21:43:28 +00:00
|
|
|
#define ALSA_DEVICE_SIZE 48
|
2002-06-28 16:48:10 +00:00
|
|
|
|
2002-07-03 21:43:28 +00:00
|
|
|
#undef BUFFERTIME
|
|
|
|
#define SET_CHUNKSIZE
|
2002-07-18 16:09:54 +00:00
|
|
|
#undef USE_POLL
|
2001-06-14 18:00:13 +00:00
|
|
|
|
2002-05-26 12:23:19 +00:00
|
|
|
|
2001-06-07 15:43:47 +00:00
|
|
|
/* to set/get/query special features/parameters */
|
2003-03-21 16:42:50 +00:00
|
|
|
static int control(int cmd, void *arg)
|
2001-06-07 15:43:47 +00:00
|
|
|
{
|
2002-07-03 21:43:28 +00:00
|
|
|
switch(cmd) {
|
|
|
|
case AOCONTROL_QUERY_FORMAT:
|
|
|
|
return CONTROL_TRUE;
|
|
|
|
case AOCONTROL_GET_VOLUME:
|
|
|
|
case AOCONTROL_SET_VOLUME:
|
2002-08-20 14:15:25 +00:00
|
|
|
#ifndef WORDS_BIGENDIAN
|
|
|
|
{ //seems to be a problem on macs?
|
2002-07-03 21:43:28 +00:00
|
|
|
{
|
|
|
|
ao_control_vol_t *vol = (ao_control_vol_t *)arg;
|
|
|
|
|
|
|
|
int err;
|
|
|
|
snd_mixer_t *handle;
|
|
|
|
snd_mixer_elem_t *elem;
|
|
|
|
snd_mixer_selem_id_t *sid;
|
|
|
|
|
|
|
|
const char *mix_name = "PCM";
|
|
|
|
char *card = "default";
|
|
|
|
|
|
|
|
long pmin, pmax;
|
|
|
|
long get_vol, set_vol;
|
|
|
|
float calc_vol, diff, f_multi;
|
|
|
|
|
|
|
|
if(ao_data.format == AFMT_AC3)
|
|
|
|
return CONTROL_TRUE;
|
|
|
|
|
|
|
|
//allocate simple id
|
|
|
|
snd_mixer_selem_id_alloca(&sid);
|
|
|
|
|
|
|
|
//sets simple-mixer index and name
|
|
|
|
snd_mixer_selem_id_set_index(sid, 0);
|
|
|
|
snd_mixer_selem_id_set_name(sid, mix_name);
|
|
|
|
|
|
|
|
if ((err = snd_mixer_open(&handle, 0)) < 0) {
|
|
|
|
printf("alsa-control: mixer open error: %s\n", snd_strerror(err));
|
|
|
|
return CONTROL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((err = snd_mixer_attach(handle, card)) < 0) {
|
|
|
|
printf("alsa-control: mixer attach %s error: %s", card, snd_strerror(err));
|
|
|
|
snd_mixer_close(handle);
|
|
|
|
return CONTROL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((err = snd_mixer_selem_register(handle, NULL, NULL)) < 0) {
|
|
|
|
printf("alsa-control: mixer register error: %s", snd_strerror(err));
|
|
|
|
snd_mixer_close(handle);
|
|
|
|
return CONTROL_ERROR;
|
|
|
|
}
|
|
|
|
err = snd_mixer_load(handle);
|
|
|
|
if (err < 0) {
|
|
|
|
printf("alsa-control: mixer load error: %s", snd_strerror(err));
|
|
|
|
snd_mixer_close(handle);
|
|
|
|
return CONTROL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
elem = snd_mixer_find_selem(handle, sid);
|
|
|
|
if (!elem) {
|
|
|
|
printf("alsa-control: unable to find simple control '%s',%i\n", snd_mixer_selem_id_get_name(sid), snd_mixer_selem_id_get_index(sid));
|
|
|
|
snd_mixer_close(handle);
|
|
|
|
return CONTROL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
snd_mixer_selem_get_playback_volume_range(elem,&pmin,&pmax);
|
|
|
|
f_multi = (100 / (float)pmax);
|
|
|
|
|
|
|
|
if (cmd == AOCONTROL_SET_VOLUME) {
|
|
|
|
|
|
|
|
diff = (vol->left+vol->right) / 2;
|
|
|
|
set_vol = rint(diff / f_multi);
|
|
|
|
|
|
|
|
if (set_vol < 0)
|
|
|
|
set_vol = 0;
|
|
|
|
else if (set_vol > pmax)
|
|
|
|
set_vol = pmax;
|
|
|
|
|
|
|
|
//setting channels
|
|
|
|
if ((err = snd_mixer_selem_set_playback_volume(elem, 0, set_vol)) < 0) {
|
|
|
|
printf("alsa-control: error setting left channel, %s",snd_strerror(err));
|
|
|
|
return CONTROL_ERROR;
|
|
|
|
}
|
|
|
|
if ((err = snd_mixer_selem_set_playback_volume(elem, 1, set_vol)) < 0) {
|
|
|
|
printf("alsa-control: error setting right channel, %s",snd_strerror(err));
|
|
|
|
return CONTROL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
//printf("diff=%f, set_vol=%i, pmax=%i, mult=%f\n", diff, set_vol, pmax, f_multi);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
snd_mixer_selem_get_playback_volume(elem, 0, &get_vol);
|
|
|
|
calc_vol = get_vol;
|
|
|
|
calc_vol = rintf(calc_vol * f_multi);
|
|
|
|
|
|
|
|
vol->left = vol->right = (int)calc_vol;
|
|
|
|
|
|
|
|
//printf("get_vol = %i, calc=%i\n",get_vol, calc_vol);
|
|
|
|
}
|
|
|
|
snd_mixer_close(handle);
|
|
|
|
return CONTROL_OK;
|
|
|
|
}
|
2002-08-20 14:15:25 +00:00
|
|
|
}// end big-endian
|
|
|
|
#endif
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return (CONTROL_UNKNOWN);
|
2002-07-03 21:43:28 +00:00
|
|
|
}
|
2002-08-20 14:15:25 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} //end witch
|
2002-07-03 21:43:28 +00:00
|
|
|
return(CONTROL_UNKNOWN);
|
2001-06-07 15:43:47 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2002-07-03 21:43:28 +00:00
|
|
|
int period_val;
|
2001-06-07 15:43:47 +00:00
|
|
|
snd_pcm_info_t *alsa_info;
|
2002-07-03 21:43:28 +00:00
|
|
|
char *str_block_mode;
|
2002-07-11 10:20:54 +00:00
|
|
|
int device_set = 0;
|
2001-06-21 21:27:56 +00:00
|
|
|
|
2002-06-28 16:48:10 +00:00
|
|
|
printf("alsa-init: testing and bugreports are welcome.\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;
|
|
|
|
|
2002-11-01 17:46:45 +00:00
|
|
|
if (verbose>0)
|
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;
|
2002-10-07 19:21:35 +00:00
|
|
|
ao_data.bps = channels * rate_hz;
|
2001-11-24 05:21:22 +00:00
|
|
|
ao_data.format = format;
|
|
|
|
ao_data.channels = channels;
|
|
|
|
ao_data.outburst = OUTBURST;
|
2002-07-18 16:09:54 +00:00
|
|
|
//ao_data.buffersize = MAX_OUTBURST; // was 16384
|
2001-06-07 15:43:47 +00:00
|
|
|
|
|
|
|
switch (format)
|
2002-08-20 14:15:25 +00:00
|
|
|
{
|
|
|
|
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
|
2002-08-20 14:15:25 +00:00
|
|
|
case AFMT_AC3:
|
2002-04-22 22:33:06 +00:00
|
|
|
#endif
|
2002-08-20 14:15:25 +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
|
2002-08-20 14:15:25 +00:00
|
|
|
case AFMT_AC3:
|
2002-04-22 22:33:06 +00:00
|
|
|
#endif
|
2002-08-20 14:15:25 +00:00
|
|
|
case AFMT_S16_BE:
|
|
|
|
alsa_format = SND_PCM_FORMAT_S16_BE;
|
|
|
|
break;
|
|
|
|
case AFMT_S32_LE:
|
|
|
|
alsa_format = SND_PCM_FORMAT_S32_LE;
|
|
|
|
break;
|
|
|
|
case AFMT_S32_BE:
|
|
|
|
alsa_format = SND_PCM_FORMAT_S32_BE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
alsa_format = SND_PCM_FORMAT_MPEG;
|
|
|
|
break;
|
|
|
|
}
|
2001-06-07 15:43:47 +00:00
|
|
|
|
|
|
|
switch(alsa_format)
|
2002-08-20 14:15:25 +00:00
|
|
|
{
|
|
|
|
case SND_PCM_FORMAT_S16_LE:
|
|
|
|
case SND_PCM_FORMAT_U16_LE:
|
|
|
|
ao_data.bps *= 2;
|
|
|
|
break;
|
|
|
|
case SND_PCM_FORMAT_S32_LE:
|
|
|
|
case SND_PCM_FORMAT_S32_BE:
|
|
|
|
ao_data.bps *= 4;
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
printf("alsa-init: invalid format (%s) requested - output disabled\n",
|
|
|
|
audio_out_format_name(format));
|
|
|
|
return(0);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2002-10-07 19:21:35 +00:00
|
|
|
bytes_per_sample = ao_data.bps / ao_data.samplerate;
|
|
|
|
|
2002-07-03 21:43:28 +00:00
|
|
|
if (ao_subdevice) {
|
|
|
|
//start parsing ao_subdevice, ugly and not thread safe!
|
|
|
|
//maybe there's a better way?
|
|
|
|
int i2 = 1;
|
|
|
|
int i3 = 0;
|
|
|
|
char *sub_str;
|
|
|
|
|
|
|
|
char *token_str[3];
|
|
|
|
char* test_str = strdup(ao_subdevice);
|
|
|
|
|
|
|
|
|
|
|
|
if ((strcspn(ao_subdevice, ":")) > 0) {
|
|
|
|
|
|
|
|
sub_str = strtok(test_str, ":");
|
|
|
|
*(token_str) = sub_str;
|
|
|
|
|
|
|
|
while (((sub_str = strtok(NULL, ":")) != NULL) && (i2 <= 3)) {
|
|
|
|
*(token_str+i2) = sub_str;
|
|
|
|
i2 += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i3=0; i3 <= i2-1; i3++) {
|
|
|
|
if (strcmp(*(token_str + i3), "mmap") == 0) {
|
|
|
|
ao_mmap = 1;
|
|
|
|
}
|
|
|
|
else if (strcmp(*(token_str+i3), "noblock") == 0) {
|
|
|
|
ao_noblock = 1;
|
|
|
|
}
|
|
|
|
else if (strcmp(*(token_str+i3), "hw") == 0) {
|
2002-07-11 10:20:54 +00:00
|
|
|
if ((i3 < i2-1) && (strcmp(*(token_str+i3+1), "noblock") != 0) && (strcmp(*(token_str+i3+1), "mmap") != 0)) {
|
|
|
|
alsa_device = alloca(ALSA_DEVICE_SIZE);
|
|
|
|
snprintf(alsa_device, ALSA_DEVICE_SIZE, "hw:%s", *(token_str+(i3+1)));
|
|
|
|
device_set = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
alsa_device = *(token_str+i3);
|
|
|
|
device_set = 1;
|
|
|
|
}
|
2002-07-03 21:43:28 +00:00
|
|
|
}
|
2002-07-11 10:20:54 +00:00
|
|
|
else if (device_set == 0 && (!ao_mmap || !ao_noblock)) {
|
2002-07-03 21:43:28 +00:00
|
|
|
alsa_device = *(token_str+i3);
|
2002-07-11 10:20:54 +00:00
|
|
|
device_set = 1;
|
2002-07-03 21:43:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} //end parsing ao_subdevice
|
2001-06-23 18:59:54 +00:00
|
|
|
|
2002-08-23 13:03:00 +00:00
|
|
|
/* switch for spdif
|
|
|
|
* sets opening sequence for SPDIF
|
|
|
|
* sets also the playback and other switches 'on the fly'
|
|
|
|
* while opening the abstract alias for the spdif subdevice
|
|
|
|
* 'iec958'
|
|
|
|
*/
|
|
|
|
if (format == AFMT_AC3) {
|
|
|
|
char devstr[128];
|
|
|
|
unsigned char s[4];
|
|
|
|
int err, c;
|
|
|
|
|
|
|
|
switch (channels) {
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
|
|
|
|
s[0] = IEC958_AES0_NONAUDIO |
|
|
|
|
IEC958_AES0_CON_EMPHASIS_NONE;
|
|
|
|
s[1] = IEC958_AES1_CON_ORIGINAL |
|
|
|
|
IEC958_AES1_CON_PCM_CODER;
|
|
|
|
s[2] = 0;
|
|
|
|
s[3] = IEC958_AES3_CON_FS_48000;
|
|
|
|
|
|
|
|
sprintf(devstr, "iec958:AES0=0x%x,AES1=0x%x,AES2=0x%x,AES3=0x%x",
|
|
|
|
s[0], s[1], s[2], s[3]);
|
|
|
|
|
2002-11-01 17:46:45 +00:00
|
|
|
if (verbose>0)
|
2002-08-23 13:03:00 +00:00
|
|
|
printf("alsa-spdif-init: playing AC3, %i channels\n", channels);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
strcpy(devstr, "surround40");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
strcpy(devstr, "surround51");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%d channels are not supported\n", channels);
|
|
|
|
exit (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
alsa_device = devstr;
|
|
|
|
}
|
|
|
|
|
2001-06-17 17:44:56 +00:00
|
|
|
if (alsa_device == NULL)
|
2002-06-28 16:48:10 +00:00
|
|
|
{
|
|
|
|
int tmp_device, tmp_subdevice, err;
|
|
|
|
|
|
|
|
if ((err = snd_pcm_info_malloc(&alsa_info)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: memory allocation error: %s\n", snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((alsa_device = alloca(ALSA_DEVICE_SIZE)) == NULL)
|
|
|
|
{
|
2001-06-17 17:44:56 +00:00
|
|
|
printf("alsa-init: memory allocation error: %s\n", strerror(errno));
|
|
|
|
return(0);
|
2002-06-28 16:48:10 +00:00
|
|
|
}
|
2001-06-07 15:43:47 +00:00
|
|
|
|
2002-06-28 16:48:10 +00:00
|
|
|
if ((tmp_device = snd_pcm_info_get_device(alsa_info)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: cant get device\n");
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((tmp_subdevice = snd_pcm_info_get_subdevice(alsa_info)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: cant get subdevice\n");
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2002-11-01 17:46:45 +00:00
|
|
|
if (verbose>0)
|
2002-06-28 16:48:10 +00:00
|
|
|
printf("alsa-init: got device=%i, subdevice=%i\n", tmp_device, tmp_subdevice);
|
|
|
|
|
|
|
|
if ((err = snprintf(alsa_device, ALSA_DEVICE_SIZE, "hw:%1d,%1d", tmp_device, tmp_subdevice)) <= 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: cant wrote device-id\n");
|
|
|
|
}
|
2001-06-07 15:43:47 +00:00
|
|
|
|
2001-06-17 17:44:56 +00:00
|
|
|
snd_pcm_info_free(alsa_info);
|
2002-07-11 10:20:54 +00:00
|
|
|
printf("alsa-init: %d soundcard%s found, using: %s\n", cards+1,
|
|
|
|
(cards >= 0) ? "" : "s", alsa_device);
|
|
|
|
} else if (strcmp(alsa_device, "help") == 0) {
|
|
|
|
printf("alsa-help: available options are:\n");
|
|
|
|
printf(" mmap: sets mmap-mode\n");
|
|
|
|
printf(" noblock: sets noblock-mode\n");
|
|
|
|
printf(" device-name: sets device name\n");
|
|
|
|
printf(" example -ao alsa9:mmap:noblock:hw:0,3 sets noblock-mode,\n");
|
|
|
|
printf(" mmap-mode and the device-name as first card third device\n");
|
|
|
|
return(0);
|
|
|
|
} else {
|
|
|
|
printf("alsa-init: soundcard set to %s\n", alsa_device);
|
2002-06-28 16:48:10 +00:00
|
|
|
}
|
2001-06-07 15:43:47 +00:00
|
|
|
|
2002-07-03 21:43:28 +00:00
|
|
|
//setting modes for block or nonblock-mode
|
|
|
|
if (ao_noblock) {
|
|
|
|
open_mode = SND_PCM_NONBLOCK;
|
|
|
|
set_block_mode = 1;
|
|
|
|
str_block_mode = "nonblock-mode";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
open_mode = 0;
|
|
|
|
set_block_mode = 0;
|
|
|
|
str_block_mode = "block-mode";
|
|
|
|
}
|
2002-08-20 14:15:25 +00:00
|
|
|
|
2002-07-18 16:09:54 +00:00
|
|
|
//sets buff/chunksize if its set manually
|
|
|
|
if (ao_data.buffersize) {
|
|
|
|
switch (ao_data.buffersize)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
alsa_fragcount = 16;
|
|
|
|
chunk_size = 512;
|
2002-11-01 17:46:45 +00:00
|
|
|
if (verbose>0) {
|
2002-07-18 16:09:54 +00:00
|
|
|
printf("alsa-init: buffersize set manually to 8192\n");
|
|
|
|
printf("alsa-init: chunksize set manually to 512\n");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
alsa_fragcount = 8;
|
|
|
|
chunk_size = 1024;
|
2002-11-01 17:46:45 +00:00
|
|
|
if (verbose>0) {
|
2002-07-18 16:09:54 +00:00
|
|
|
printf("alsa-init: buffersize set manually to 8192\n");
|
|
|
|
printf("alsa-init: chunksize set manually to 1024\n");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
alsa_fragcount = 32;
|
|
|
|
chunk_size = 512;
|
2002-11-01 17:46:45 +00:00
|
|
|
if (verbose>0) {
|
2002-07-18 16:09:54 +00:00
|
|
|
printf("alsa-init: buffersize set manually to 16384\n");
|
|
|
|
printf("alsa-init: chunksize set manually to 512\n");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
alsa_fragcount = 16;
|
|
|
|
chunk_size = 1024;
|
2002-11-01 17:46:45 +00:00
|
|
|
if (verbose>0) {
|
2002-07-18 16:09:54 +00:00
|
|
|
printf("alsa-init: buffersize set manually to 16384\n");
|
|
|
|
printf("alsa-init: chunksize set manually to 1024\n");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
alsa_fragcount = 16;
|
|
|
|
if (ao_mmap)
|
|
|
|
chunk_size = 512;
|
|
|
|
else
|
|
|
|
chunk_size = 1024;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-26 12:23:19 +00:00
|
|
|
if (!alsa_handler) {
|
2002-07-11 10:20:54 +00:00
|
|
|
//modes = 0, SND_PCM_NONBLOCK, SND_PCM_ASYNC
|
2002-07-03 21:43:28 +00:00
|
|
|
if ((err = snd_pcm_open(&alsa_handler, alsa_device, SND_PCM_STREAM_PLAYBACK, open_mode)) < 0)
|
|
|
|
{
|
|
|
|
if (ao_noblock) {
|
|
|
|
printf("alsa-init: open in nonblock-mode failed, trying to open in block-mode\n");
|
|
|
|
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);
|
|
|
|
} else {
|
|
|
|
set_block_mode = 0;
|
|
|
|
str_block_mode = "block-mode";
|
2002-05-26 12:23:19 +00:00
|
|
|
}
|
2002-07-03 21:43:28 +00:00
|
|
|
} else {
|
|
|
|
printf("alsa-init: playback open error: %s\n", snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
}
|
2001-06-07 15:43:47 +00:00
|
|
|
|
2002-07-11 10:20:54 +00:00
|
|
|
if ((err = snd_pcm_nonblock(alsa_handler, set_block_mode)) < 0) {
|
|
|
|
printf("alsa-init: error set block-mode %s\n", snd_strerror(err));
|
|
|
|
}
|
2002-11-01 17:46:45 +00:00
|
|
|
else if (verbose>0) {
|
2002-07-11 10:20:54 +00:00
|
|
|
printf("alsa-init: pcm opend in %s\n", str_block_mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
snd_pcm_hw_params_alloca(&alsa_hwparams);
|
|
|
|
snd_pcm_sw_params_alloca(&alsa_swparams);
|
2002-06-28 16:48:10 +00:00
|
|
|
|
2002-07-11 10:20:54 +00:00
|
|
|
// setting hw-parameters
|
|
|
|
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);
|
|
|
|
}
|
2001-06-07 15:43:47 +00:00
|
|
|
|
2002-07-11 10:20:54 +00:00
|
|
|
if (ao_mmap) {
|
|
|
|
snd_pcm_access_mask_t *mask = alloca(snd_pcm_access_mask_sizeof());
|
|
|
|
snd_pcm_access_mask_none(mask);
|
|
|
|
snd_pcm_access_mask_set(mask, SND_PCM_ACCESS_MMAP_INTERLEAVED);
|
|
|
|
snd_pcm_access_mask_set(mask, SND_PCM_ACCESS_MMAP_NONINTERLEAVED);
|
|
|
|
snd_pcm_access_mask_set(mask, SND_PCM_ACCESS_MMAP_COMPLEX);
|
|
|
|
err = snd_pcm_hw_params_set_access_mask(alsa_handler, alsa_hwparams, mask);
|
|
|
|
printf("alsa-init: mmap set\n");
|
|
|
|
} else {
|
|
|
|
err = snd_pcm_hw_params_set_access(alsa_handler, alsa_hwparams,SND_PCM_ACCESS_RW_INTERLEAVED);
|
|
|
|
}
|
|
|
|
if (err < 0) {
|
|
|
|
printf("alsa-init: unable to set access type: %s\n", snd_strerror(err));
|
|
|
|
return (0);
|
|
|
|
}
|
2001-06-07 15:43:47 +00:00
|
|
|
|
2002-07-11 10:20:54 +00:00
|
|
|
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);
|
|
|
|
}
|
2001-06-07 15:43:47 +00:00
|
|
|
|
2002-07-11 10:20:54 +00:00
|
|
|
if ((err = snd_pcm_hw_params_set_channels(alsa_handler, alsa_hwparams,
|
|
|
|
ao_data.channels)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: unable to set channels: %s\n",
|
|
|
|
snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
2001-06-07 15:43:47 +00:00
|
|
|
|
2002-07-11 10:20:54 +00:00
|
|
|
if ((err = snd_pcm_hw_params_set_rate_near(alsa_handler, alsa_hwparams, ao_data.samplerate, 0)) < 0)
|
2001-10-03 18:09:51 +00:00
|
|
|
{
|
2002-07-11 10:20:54 +00:00
|
|
|
printf("alsa-init: unable to set samplerate-2: %s\n",
|
|
|
|
snd_strerror(err));
|
|
|
|
return(0);
|
2001-10-03 18:09:51 +00:00
|
|
|
}
|
2001-06-07 15:43:47 +00:00
|
|
|
|
2002-06-28 16:48:10 +00:00
|
|
|
#ifdef BUFFERTIME
|
2002-07-11 10:20:54 +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)
|
2002-07-11 10:20:54 +00:00
|
|
|
{
|
2001-06-07 15:43:47 +00:00
|
|
|
printf("alsa-init: unable to set buffer time near: %s\n",
|
2002-07-11 10:20:54 +00:00
|
|
|
snd_strerror(err));
|
2001-06-07 15:43:47 +00:00
|
|
|
return(0);
|
2002-07-11 10:20:54 +00:00
|
|
|
} else
|
2001-06-07 15:43:47 +00:00
|
|
|
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 */
|
2002-07-11 10:20:54 +00:00
|
|
|
{
|
2001-06-07 15:43:47 +00:00
|
|
|
printf("alsa-init: unable to set period time: %s\n",
|
2002-07-11 10:20:54 +00:00
|
|
|
snd_strerror(err));
|
2001-06-07 15:43:47 +00:00
|
|
|
return(0);
|
2002-07-11 10:20:54 +00:00
|
|
|
}
|
2002-11-01 17:46:45 +00:00
|
|
|
if (verbose>0)
|
2002-06-28 16:48:10 +00:00
|
|
|
printf("alsa-init: buffer_time: %d, period_time :%d\n",alsa_buffer_time, err);
|
2002-07-11 10:20:54 +00:00
|
|
|
}
|
2001-06-07 15:43:47 +00:00
|
|
|
#endif
|
|
|
|
|
2002-07-03 21:43:28 +00:00
|
|
|
#ifdef SET_CHUNKSIZE
|
2002-06-28 16:48:10 +00:00
|
|
|
{
|
2002-07-11 10:20:54 +00:00
|
|
|
//set chunksize
|
|
|
|
if ((err = snd_pcm_hw_params_set_period_size(alsa_handler, alsa_hwparams, chunk_size, 0)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: unable to set periodsize: %s\n", snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
2002-11-01 17:46:45 +00:00
|
|
|
else if (verbose>0) {
|
2002-07-11 10:20:54 +00:00
|
|
|
printf("alsa-init: chunksize set to %i\n", chunk_size);
|
2002-06-28 16:48:10 +00:00
|
|
|
}
|
|
|
|
|
2002-07-11 10:20:54 +00:00
|
|
|
//set period_count
|
|
|
|
if ((period_val = snd_pcm_hw_params_get_periods_max(alsa_hwparams, 0)) < alsa_fragcount) {
|
|
|
|
alsa_fragcount = period_val;
|
|
|
|
}
|
2002-06-28 16:48:10 +00:00
|
|
|
|
2002-11-01 17:46:45 +00:00
|
|
|
if (verbose>0)
|
2002-07-11 10:20:54 +00:00
|
|
|
printf("alsa-init: current val=%i, fragcount=%i\n", period_val, alsa_fragcount);
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
2002-07-03 21:43:28 +00:00
|
|
|
#endif
|
2002-06-28 16:48:10 +00:00
|
|
|
|
2002-07-11 10:20:54 +00:00
|
|
|
/* finally install hardware parameters */
|
|
|
|
if ((err = snd_pcm_hw_params(alsa_handler, alsa_hwparams)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: unable to set hw-parameters: %s\n",
|
|
|
|
snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
// end setting hw-params
|
2001-06-14 18:00:13 +00:00
|
|
|
|
2002-06-28 16:48:10 +00:00
|
|
|
|
2002-07-11 10:20:54 +00:00
|
|
|
// gets buffersize for control
|
|
|
|
if ((err = snd_pcm_hw_params_get_buffer_size(alsa_hwparams)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: unable to get buffersize: %s\n", snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
else {
|
2002-12-04 22:01:59 +00:00
|
|
|
ao_data.buffersize = err * bytes_per_sample;
|
2002-11-01 17:46:45 +00:00
|
|
|
if (verbose>0)
|
2002-07-11 10:20:54 +00:00
|
|
|
printf("alsa-init: got buffersize=%i\n", ao_data.buffersize);
|
2002-06-28 16:48:10 +00:00
|
|
|
}
|
|
|
|
|
2002-07-11 10:20:54 +00:00
|
|
|
// setting sw-params (only avail-min) if noblocking mode was choosed
|
|
|
|
if (ao_noblock)
|
|
|
|
{
|
2001-10-15 11:17:19 +00:00
|
|
|
|
2002-07-11 10:20:54 +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);
|
|
|
|
}
|
2002-06-28 16:48:10 +00:00
|
|
|
|
2002-07-11 10:20:54 +00:00
|
|
|
//set min available frames to consider pcm ready (4)
|
|
|
|
//increased for nonblock-mode should be set dynamically later
|
2002-07-18 16:09:54 +00:00
|
|
|
if ((err = snd_pcm_sw_params_set_avail_min(alsa_handler, alsa_swparams, 4)) < 0)
|
2002-07-11 10:20:54 +00:00
|
|
|
{
|
|
|
|
printf("alsa-init: unable to set avail_min %s\n",snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
2001-06-07 15:43:47 +00:00
|
|
|
|
2002-07-11 10:20:54 +00:00
|
|
|
if ((err = snd_pcm_sw_params(alsa_handler, alsa_swparams)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: unable to install sw-params\n");
|
|
|
|
return(0);
|
|
|
|
}
|
2002-06-28 16:48:10 +00:00
|
|
|
|
2002-07-11 10:20:54 +00:00
|
|
|
bits_per_sample = snd_pcm_format_physical_width(alsa_format);
|
|
|
|
bits_per_frame = bits_per_sample * channels;
|
|
|
|
chunk_bytes = chunk_size * bits_per_frame / 8;
|
2002-06-28 16:48:10 +00:00
|
|
|
|
2002-11-01 17:46:45 +00:00
|
|
|
if (verbose>0) {
|
2002-07-11 10:20:54 +00:00
|
|
|
printf("alsa-init: bits per sample (bps)=%i, bits per frame (bpf)=%i, chunk_bytes=%i\n",bits_per_sample,bits_per_frame,chunk_bytes);}
|
2001-06-07 15:43:47 +00:00
|
|
|
|
2002-07-11 10:20:54 +00:00
|
|
|
}//end swparams
|
2002-06-28 16:48:10 +00:00
|
|
|
|
2002-07-11 10:20:54 +00:00
|
|
|
if ((err = snd_pcm_prepare(alsa_handler)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-init: pcm prepare error: %s\n", snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("alsa9: %d Hz/%d channels/%d bpf/%d bytes buffer/%s\n",
|
2002-10-07 19:21:35 +00:00
|
|
|
ao_data.samplerate, ao_data.channels, bytes_per_sample, ao_data.buffersize,
|
2002-07-11 10:20:54 +00:00
|
|
|
snd_pcm_format_description(alsa_format));
|
2001-06-07 15:43:47 +00:00
|
|
|
|
2002-07-11 10:20:54 +00:00
|
|
|
} // end switch alsa_handler (spdif)
|
2001-06-07 15:43:47 +00:00
|
|
|
return(1);
|
2002-06-28 16:48:10 +00:00
|
|
|
} // end init
|
|
|
|
|
2001-06-07 15:43:47 +00:00
|
|
|
|
|
|
|
/* close audio device */
|
|
|
|
static void uninit()
|
|
|
|
{
|
|
|
|
|
2002-07-03 21:43:28 +00:00
|
|
|
if (alsa_handler) {
|
|
|
|
int err;
|
2001-06-07 15:43:47 +00:00
|
|
|
|
2002-07-03 21:43:28 +00:00
|
|
|
if (!ao_noblock) {
|
|
|
|
if ((err = snd_pcm_drain(alsa_handler)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-uninit: pcm drain error: %s\n", snd_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
2001-06-07 15:43:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((err = snd_pcm_close(alsa_handler)) < 0)
|
2002-07-03 21:43:28 +00:00
|
|
|
{
|
2001-06-07 15:43:47 +00:00
|
|
|
printf("alsa-uninit: pcm close error: %s\n", snd_strerror(err));
|
|
|
|
return;
|
2002-07-03 21:43:28 +00:00
|
|
|
}
|
2002-06-28 16:48:10 +00:00
|
|
|
else {
|
|
|
|
alsa_handler = NULL;
|
|
|
|
alsa_device = NULL;
|
|
|
|
printf("alsa-uninit: pcm closed\n");
|
|
|
|
}
|
2002-07-03 21:43:28 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("alsa-uninit: no handler defined!\n");
|
|
|
|
}
|
2001-06-07 15:43:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void audio_pause()
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2002-07-03 21:43:28 +00:00
|
|
|
if (!ao_noblock) {
|
|
|
|
//drain causes error in nonblock-mode!
|
|
|
|
if ((err = snd_pcm_drain(alsa_handler)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-pause: pcm drain error: %s\n", snd_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
2001-06-07 15:43:47 +00:00
|
|
|
}
|
2002-07-03 21:43:28 +00:00
|
|
|
else {
|
2002-11-01 17:46:45 +00:00
|
|
|
if (verbose>0)
|
2002-07-03 21:43:28 +00:00
|
|
|
printf("alsa-pause: paused nonblock\n");
|
2001-06-07 15:43:47 +00:00
|
|
|
|
2002-07-03 21:43:28 +00:00
|
|
|
return;
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* stop playing and empty buffers (for seeking/pause) */
|
|
|
|
static void reset()
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2003-03-14 20:36:26 +00:00
|
|
|
if ((err = snd_pcm_drop(alsa_handler)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-reset: pcm drop error: %s\n", snd_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((err = snd_pcm_prepare(alsa_handler)) < 0)
|
|
|
|
{
|
|
|
|
printf("alsa-reset: pcm prepare error: %s\n", snd_strerror(err));
|
|
|
|
return;
|
2001-06-07 15:43:47 +00:00
|
|
|
}
|
2003-03-14 20:36:26 +00:00
|
|
|
return;
|
2001-06-07 15:43:47 +00:00
|
|
|
}
|
|
|
|
|
2002-07-18 16:09:54 +00:00
|
|
|
#ifdef USE_POLL
|
|
|
|
static int wait_for_poll(snd_pcm_t *handle, struct pollfd *ufds, unsigned int count)
|
|
|
|
{
|
|
|
|
unsigned short revents;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
poll(ufds, count, -1);
|
|
|
|
snd_pcm_poll_descriptors_revents(handle, ufds, count, &revents);
|
|
|
|
if (revents & POLLERR)
|
|
|
|
return -EIO;
|
|
|
|
if (revents & POLLOUT)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-06-28 16:48:10 +00:00
|
|
|
#ifndef timersub
|
|
|
|
#define timersub(a, b, result) \
|
|
|
|
do { \
|
|
|
|
(result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
|
|
|
|
(result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
|
|
|
|
if ((result)->tv_usec < 0) { \
|
|
|
|
--(result)->tv_sec; \
|
|
|
|
(result)->tv_usec += 1000000; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#endif
|
|
|
|
|
2002-07-03 21:43:28 +00:00
|
|
|
/* I/O error handler */
|
|
|
|
static int xrun(u_char *str_mode)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
snd_pcm_status_t *status;
|
|
|
|
|
|
|
|
snd_pcm_status_alloca(&status);
|
|
|
|
|
|
|
|
if ((err = snd_pcm_status(alsa_handler, status))<0) {
|
|
|
|
printf("status error: %s", snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
printf("alsa-%s: xrun of at least %.3f msecs. resetting stream\n",
|
|
|
|
str_mode,
|
|
|
|
diff.tv_sec * 1000 + diff.tv_usec / 1000.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((err = snd_pcm_prepare(alsa_handler))<0) {
|
|
|
|
printf("xrun: prepare error: %s", snd_strerror(err));
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return(1); /* ok, data should be accepted again */
|
|
|
|
}
|
|
|
|
|
2002-11-02 10:59:27 +00:00
|
|
|
static int play_normal(void* data, int len);
|
|
|
|
static int play_mmap(void* data, int len);
|
|
|
|
|
2002-07-18 16:09:54 +00:00
|
|
|
static int play(void* data, int len, int flags)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
if (ao_mmap)
|
|
|
|
result = play_mmap(data, len);
|
|
|
|
else
|
|
|
|
result = play_normal(data, len);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2001-06-07 15:43:47 +00:00
|
|
|
/*
|
|
|
|
plays 'len' bytes of 'data'
|
|
|
|
returns: number of bytes played
|
2002-07-03 21:43:28 +00:00
|
|
|
modified last at 29.06.02 by jp
|
|
|
|
thanxs for marius <marius@rospot.com> for giving us the light ;)
|
2001-06-07 15:43:47 +00:00
|
|
|
*/
|
2002-05-26 12:23:19 +00:00
|
|
|
|
2002-07-18 16:09:54 +00:00
|
|
|
static int play_normal(void* data, int len)
|
2001-06-07 15:43:47 +00:00
|
|
|
{
|
2002-06-28 16:48:10 +00:00
|
|
|
|
2002-10-07 19:21:35 +00:00
|
|
|
//bytes_per_sample is always 4 for 2 chn S16_LE
|
|
|
|
int num_frames = len / bytes_per_sample;
|
2002-12-04 21:42:22 +00:00
|
|
|
char *output_samples = (char *)data;
|
2002-06-28 16:48:10 +00:00
|
|
|
snd_pcm_sframes_t res = 0;
|
|
|
|
|
2002-12-04 21:42:22 +00:00
|
|
|
//fprintf(stderr,"alsa-play: frames=%i, len=%i\n",num_frames,len);
|
2002-07-03 21:43:28 +00:00
|
|
|
|
2002-06-28 16:48:10 +00:00
|
|
|
if (!alsa_handler) {
|
|
|
|
printf("alsa-play: device configuration error");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-03 21:43:28 +00:00
|
|
|
while (num_frames > 0) {
|
|
|
|
|
2002-07-18 16:09:54 +00:00
|
|
|
res = snd_pcm_writei(alsa_handler, (void *)output_samples, num_frames);
|
2002-06-28 16:48:10 +00:00
|
|
|
|
2002-07-03 21:43:28 +00:00
|
|
|
if (res == -EAGAIN) {
|
|
|
|
snd_pcm_wait(alsa_handler, 1000);
|
|
|
|
}
|
|
|
|
else if (res == -EPIPE) { /* underrun */
|
|
|
|
if (xrun("play") <= 0) {
|
|
|
|
printf("alsa-play: xrun reset error");
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (res == -ESTRPIPE) { /* suspend */
|
|
|
|
printf("alsa-play: pcm in suspend mode. trying to resume\n");
|
|
|
|
while ((res = snd_pcm_resume(alsa_handler)) == -EAGAIN)
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
else if (res < 0) {
|
|
|
|
printf("alsa-play: unknown status, trying to reset soundcard\n");
|
|
|
|
if ((res = snd_pcm_prepare(alsa_handler)) < 0) {
|
|
|
|
printf("alsa-play: snd prepare error");
|
|
|
|
return(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res > 0) {
|
2002-12-04 21:42:22 +00:00
|
|
|
|
|
|
|
/* output_samples += ao_data.channels * res; */
|
|
|
|
output_samples += res * bytes_per_sample;
|
|
|
|
|
2002-07-03 21:43:28 +00:00
|
|
|
num_frames -= res;
|
|
|
|
}
|
|
|
|
|
|
|
|
} //end while
|
2002-06-28 16:48:10 +00:00
|
|
|
|
|
|
|
if (res < 0) {
|
|
|
|
printf("alsa-play: write error %s", snd_strerror(res));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return res < 0 ? (int)res : len;
|
2001-06-07 15:43:47 +00:00
|
|
|
}
|
|
|
|
|
2002-07-18 16:09:54 +00:00
|
|
|
/* mmap-mode mainly based on descriptions by Joshua Haberman <joshua@haberman.com>
|
|
|
|
* 'An overview of the ALSA API' http://people.debian.org/~joshua/x66.html
|
|
|
|
* and some help by Paul Davis <pbd@op.net> */
|
|
|
|
|
|
|
|
static int play_mmap(void* data, int len)
|
|
|
|
{
|
|
|
|
snd_pcm_sframes_t commitres, frames_available;
|
|
|
|
snd_pcm_uframes_t frames_transmit, size, offset;
|
|
|
|
const snd_pcm_channel_area_t *area;
|
|
|
|
void *outbuffer;
|
|
|
|
int err, result;
|
|
|
|
|
|
|
|
#ifdef USE_POLL //seems not really be needed
|
|
|
|
struct pollfd *ufds;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
count = snd_pcm_poll_descriptors_count (alsa_handler);
|
|
|
|
ufds = malloc(sizeof(struct pollfd) * count);
|
|
|
|
snd_pcm_poll_descriptors(alsa_handler, ufds, count);
|
|
|
|
|
|
|
|
//first wait_for_poll
|
|
|
|
if (err = (wait_for_poll(alsa_handler, ufds, count) < 0)) {
|
|
|
|
if (snd_pcm_state(alsa_handler) == SND_PCM_STATE_XRUN ||
|
|
|
|
snd_pcm_state(alsa_handler) == SND_PCM_STATE_SUSPENDED) {
|
|
|
|
xrun("play");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
outbuffer = alloca(ao_data.buffersize);
|
|
|
|
|
|
|
|
//don't trust get_space() ;)
|
2002-10-07 19:21:35 +00:00
|
|
|
frames_available = snd_pcm_avail_update(alsa_handler) * bytes_per_sample;
|
2002-07-18 16:09:54 +00:00
|
|
|
if (frames_available < 0)
|
|
|
|
xrun("play");
|
|
|
|
|
|
|
|
if (frames_available < 4) {
|
|
|
|
if (first) {
|
|
|
|
first = 0;
|
|
|
|
snd_pcm_start(alsa_handler);
|
|
|
|
}
|
|
|
|
else { //FIXME should break and return 0?
|
|
|
|
snd_pcm_wait(alsa_handler, -1);
|
|
|
|
first = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* len is simply the available bufferspace got by get_space()
|
2002-10-07 19:21:35 +00:00
|
|
|
* but real avail_buffer in frames is ab/bytes_per_sample */
|
|
|
|
size = len / bytes_per_sample;
|
2002-07-18 16:09:54 +00:00
|
|
|
|
|
|
|
//if (verbose)
|
2002-10-07 19:21:35 +00:00
|
|
|
//printf("len: %i size %i, f_avail %i, bps %i ...\n", len, size, frames_available, bytes_per_sample);
|
2002-07-18 16:09:54 +00:00
|
|
|
|
|
|
|
frames_transmit = size;
|
|
|
|
|
|
|
|
/* prepare areas and set sw-pointers
|
|
|
|
* frames_transmit returns the real available buffer-size
|
|
|
|
* sometimes != frames_available cause of ringbuffer 'emulation' */
|
|
|
|
snd_pcm_mmap_begin(alsa_handler, &area, &offset, &frames_transmit);
|
|
|
|
|
|
|
|
/* this is specific to interleaved streams (or non-interleaved
|
|
|
|
* streams with only one channel) */
|
|
|
|
outbuffer = ((char *) area->addr + (area->first + area->step * offset) / 8); //8
|
|
|
|
|
|
|
|
//write data
|
2002-10-07 19:21:35 +00:00
|
|
|
memcpy(outbuffer, data, (frames_transmit * bytes_per_sample));
|
2002-07-18 16:09:54 +00:00
|
|
|
|
|
|
|
commitres = snd_pcm_mmap_commit(alsa_handler, offset, frames_transmit);
|
|
|
|
|
|
|
|
if (commitres < 0 || commitres != frames_transmit) {
|
|
|
|
if (snd_pcm_state(alsa_handler) == SND_PCM_STATE_XRUN ||
|
|
|
|
snd_pcm_state(alsa_handler) == SND_PCM_STATE_SUSPENDED) {
|
|
|
|
xrun("play");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//if (verbose)
|
|
|
|
//printf("mmap ft: %i, cres: %i\n", frames_transmit, commitres);
|
|
|
|
|
|
|
|
/* err = snd_pcm_area_copy(&area, offset, &data, offset, len, alsa_format); */
|
|
|
|
/* if (err < 0) { */
|
|
|
|
/* printf("area-copy-error\n"); */
|
|
|
|
/* return 0; */
|
|
|
|
/* } */
|
|
|
|
|
|
|
|
|
|
|
|
//calculate written frames!
|
2002-10-07 19:21:35 +00:00
|
|
|
result = commitres * bytes_per_sample;
|
2002-07-18 16:09:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* if (verbose) { */
|
|
|
|
/* if (len == result) */
|
|
|
|
/* printf("result: %i, frames written: %i ...\n", result, frames_transmit); */
|
|
|
|
/* else */
|
|
|
|
/* printf("result: %i, frames written: %i, result != len ...\n", result, frames_transmit); */
|
|
|
|
/* } */
|
|
|
|
|
|
|
|
//mplayer doesn't like -result
|
|
|
|
if (result < 0)
|
|
|
|
result = 0;
|
|
|
|
|
|
|
|
#ifdef USE_POLL
|
|
|
|
free(ufds);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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;
|
2002-07-03 21:43:28 +00:00
|
|
|
char *str_status;
|
|
|
|
|
|
|
|
//snd_pcm_sframes_t avail_frames = 0;
|
2001-06-07 15:43:47 +00:00
|
|
|
|
|
|
|
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))
|
|
|
|
{
|
2002-07-03 21:43:28 +00:00
|
|
|
case SND_PCM_STATE_OPEN:
|
|
|
|
str_status = "open";
|
|
|
|
case SND_PCM_STATE_PREPARED:
|
2002-07-18 16:09:54 +00:00
|
|
|
if (str_status != "open") {
|
2002-07-03 21:43:28 +00:00
|
|
|
str_status = "prepared";
|
2002-07-18 16:09:54 +00:00
|
|
|
first = 1;
|
2002-10-07 19:21:35 +00:00
|
|
|
ret = snd_pcm_status_get_avail(status) * bytes_per_sample;
|
2002-07-18 16:09:54 +00:00
|
|
|
if (ret == 0) //ugly workaround for hang in mmap-mode
|
|
|
|
ret = 10;
|
|
|
|
break;
|
|
|
|
}
|
2002-07-03 21:43:28 +00:00
|
|
|
case SND_PCM_STATE_RUNNING:
|
2002-10-07 19:21:35 +00:00
|
|
|
ret = snd_pcm_status_get_avail(status) * bytes_per_sample;
|
|
|
|
//avail_frames = snd_pcm_avail_update(alsa_handler) * bytes_per_sample;
|
2002-07-03 21:43:28 +00:00
|
|
|
if (str_status != "open" && str_status != "prepared")
|
|
|
|
str_status = "running";
|
|
|
|
break;
|
|
|
|
case SND_PCM_STATE_PAUSED:
|
2002-11-01 17:46:45 +00:00
|
|
|
if (verbose>0) printf("alsa-space: paused");
|
2002-07-03 21:43:28 +00:00
|
|
|
str_status = "paused";
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
case SND_PCM_STATE_XRUN:
|
|
|
|
xrun("space");
|
|
|
|
str_status = "xrun";
|
2002-07-18 16:09:54 +00:00
|
|
|
first = 1;
|
2002-07-03 21:43:28 +00:00
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
str_status = "undefined";
|
2002-10-07 19:21:35 +00:00
|
|
|
ret = snd_pcm_status_get_avail(status) * bytes_per_sample;
|
2002-07-18 16:09:54 +00:00
|
|
|
if (ret <= 0) {
|
|
|
|
xrun("space");
|
|
|
|
}
|
2001-06-07 15:43:47 +00:00
|
|
|
}
|
2002-07-03 21:43:28 +00:00
|
|
|
|
2002-11-01 17:46:45 +00:00
|
|
|
if (verbose>0 && str_status != "running")
|
2002-07-03 21:43:28 +00:00
|
|
|
printf("alsa-space: free space = %i, status=%i, %s --\n", ret, status, str_status);
|
2001-06-07 15:43:47 +00:00
|
|
|
snd_pcm_status_free(status);
|
2001-06-14 18:29:05 +00:00
|
|
|
|
2002-07-03 21:43:28 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
printf("negative value!!\n");
|
2002-06-28 16:48:10 +00:00
|
|
|
ret = 0;
|
2002-07-03 21:43:28 +00:00
|
|
|
}
|
|
|
|
|
2003-01-10 18:28:18 +00:00
|
|
|
// workaround for too small value returned
|
|
|
|
if (ret < MIN_CHUNK_SIZE)
|
|
|
|
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
|
|
|
{
|
2002-07-03 21:43:28 +00:00
|
|
|
|
|
|
|
if (alsa_handler) {
|
|
|
|
|
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)
|
2002-06-28 16:48:10 +00:00
|
|
|
ret = 0;
|
2001-06-07 15:43:47 +00:00
|
|
|
return(ret);
|
2002-07-03 21:43:28 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
return(0);
|
|
|
|
}
|
2001-06-07 15:43:47 +00:00
|
|
|
}
|