From c712271732b0b981657f918570d39500211ef80e Mon Sep 17 00:00:00 2001 From: Claudio Matsuoka Date: Sat, 24 Mar 2012 18:47:21 -0300 Subject: [PATCH] Add sndio sound driver Signed-off-by: Claudio Matsuoka --- configure.ac | 10 ++-- src/Makefile | 2 +- src/drivers/sndio.c | 110 ------------------------------------------ src/sound.c | 4 ++ src/sound_sndio.c | 114 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 126 insertions(+), 114 deletions(-) delete mode 100644 src/drivers/sndio.c create mode 100644 src/sound_sndio.c diff --git a/configure.ac b/configure.ac index ce8faec..02f7905 100644 --- a/configure.ac +++ b/configure.ac @@ -15,7 +15,7 @@ fi AC_CHECK_HEADERS(xmp.h getopt.h signal.h termios.h) AC_CHECK_HEADER(windows.h,,[ AC_CHECK_HEADER(CoreAudio/CoreAudio.h,,[ - AC_CHECK_HEADERS(sys/soundcard.h alsa/asoundlib.h) + AC_CHECK_HEADERS(sys/soundcard.h alsa/asoundlib.h sndio.h) ]) ]) @@ -35,6 +35,11 @@ elif test "${ac_cv_header_CoreAudio_CoreAudio_h}" = "yes"; then DRIVERS="${DRIVERS} sound_coreaudio.o" LIBS="${LIBS} -framework AudioToolbox -framework AudioUnit -framework CoreServices" else + if test "${ac_cv_header_sndio_h}" = "yes"; then + AC_DEFINE(SOUND_SNDIO) + DRIVERS="${DRIVERS} sound_sndio.o" + LIBS="${LIBS} -lsndio" + fi if test "${ac_cv_header_sys_soundcard_h}" = "yes"; then AC_DEFINE(SOUND_OSS) DRIVERS="${DRIVERS} sound_oss.o" @@ -42,8 +47,7 @@ else if test "${ac_cv_header_alsa_asoundlib_h}" = "yes"; then AC_DEFINE(SOUND_ALSA) DRIVERS="${DRIVERS} sound_alsa.o" - AC_CHECK_LIB(asound,snd_pcm_open,, - AC_MSG_ERROR(Can't find libasound)) + LIBS="${LIBS} -lasound" fi fi diff --git a/src/Makefile b/src/Makefile index b33f578..c24c0f1 100644 --- a/src/Makefile +++ b/src/Makefile @@ -2,7 +2,7 @@ SRC_OBJS = sound.o sound_null.o terminal.o info.o commands.o \ options.o getopt.o getopt1.o main.o SRC_DFILES = Makefile xmp.1 $(SRC_OBJS:.o=.c) common.h getopt.h \ - sound_alsa.c sound_coreaudio.c + sound_alsa.c sound_coreaudio.c sound_oss.c sound_sndio.c SRC_PATH = src SRC_OBJS += $(DRIVERS) diff --git a/src/drivers/sndio.c b/src/drivers/sndio.c deleted file mode 100644 index 5121995..0000000 --- a/src/drivers/sndio.c +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright (c) 2009 Thomas Pfaff - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include -#include - -#include - -#include "driver.h" -#include "mixer.h" -#include "common.h" - -static struct sio_hdl *hdl; - -static int init (struct context_data *); -static void bufdump (struct context_data *, void *, int); -static void shutdown (struct context_data *); -static void dummy (void); - -struct xmp_drv_info drv_sndio = { - "sndio", /* driver ID */ - "OpenBSD sndio", /* driver description */ - NULL, /* help */ - init, /* init */ - shutdown, /* shutdown */ - dummy, /* starttimer */ - dummy, /* stoptimer */ - bufdump, /* bufdump */ -}; - -static void -dummy (void) -{ -} - -static int -init (struct context_data *ctx) -{ - struct sio_par par, askpar; - struct xmp_options *opt = &ctx->o; - - hdl = sio_open (NULL, SIO_PLAY, 0); - if (hdl == NULL) { - fprintf (stderr, "%s: failed to open audio device\n", - __func__); - return XMP_ERR_DINIT; - } - - sio_initpar (&par); - par.pchan = opt->outfmt & XMP_FORMAT_MONO ? 1 : 2; - par.rate = opt->freq; - par.bits = opt->resol; - par.sig = opt->resol > 8 ? 1 : 0; - par.le = SIO_LE_NATIVE; - par.appbufsz = par.rate / 4; - - askpar = par; - if (!sio_setpar (hdl, &par) || !sio_getpar (hdl, &par)) { - fprintf (stderr, "%s: failed to set parameters\n", __func__); - goto error; - } - - if ((par.bits == 16 && par.le != askpar.le) || - par.bits != askpar.bits || - par.sig != askpar.sig || - par.pchan != askpar.pchan || - ((par.rate * 1000 < askpar.rate * 995) || - (par.rate * 1000 > askpar.rate * 1005))) { - fprintf (stderr, "%s: parameters not supported\n", __func__); - goto error; - } - - if (!sio_start (hdl)) { - fprintf (stderr, "%s: failed to start audio device\n", - __func__); - goto error; - } - return 0; - -error: - sio_close (hdl); - return XMP_ERR_DINIT; -} - -static void -bufdump (struct context_data *ctx, void *b, int len) -{ - if (b != NULL) - sio_write (hdl, buf, len); -} - -static void -shutdown (struct context_data *ctx) -{ - sio_close (hdl); - hdl = NULL; -} diff --git a/src/sound.c b/src/sound.c index cc9ef1c..1651f2f 100644 --- a/src/sound.c +++ b/src/sound.c @@ -6,6 +6,7 @@ extern struct sound_driver sound_oss; extern struct sound_driver sound_alsa; extern struct sound_driver sound_win32; extern struct sound_driver sound_coreaudio; +extern struct sound_driver sound_sndio; LIST_HEAD(sound_driver_list); @@ -16,6 +17,9 @@ static void register_sound_driver(struct sound_driver *sd) void init_sound_drivers() { +#ifdef SOUND_SNDIO + register_sound_driver(&sound_sndio); +#endif #ifdef SOUND_COREAUDIO register_sound_driver(&sound_coreaudio); #endif diff --git a/src/sound_sndio.c b/src/sound_sndio.c new file mode 100644 index 0000000..eeef5d5 --- /dev/null +++ b/src/sound_sndio.c @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2009 Thomas Pfaff + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include "sound.h" + +static struct sio_hdl *hdl; + +static int init(int *rate, int *format) +{ + struct sio_par par, askpar; + struct xmp_options *opt = &ctx->o; + + hdl = sio_open(NULL, SIO_PLAY, 0); + if (hdl == NULL) { + fprintf(stderr, "%s: failed to open audio device\n", __func__); + return -1; + } + + sio_initpar(&par); + par.pchan = *format & XMP_FORMAT_MONO ? 1 : 2; + par.rate = *rate; + par.le = SIO_LE_NATIVE; + par.appbufsz = par.rate / 4; + + if (*format & XMP_FORMAT_8BIT) { + par.bits = 8; + par.sig = 0; + *format |= XMP_FORMAT_UNSIGNED; + } else { + par.bits = 16; + par.sig = 1; + *format &= ~XMP_FORMAT_UNSIGNED; + } + + + askpar = par; + if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) { + fprintf(stderr, "%s: failed to set parameters\n", __func__); + goto error; + } + + if ((par.bits == 16 && par.le != askpar.le) || + par.bits != askpar.bits || + par.sig != askpar.sig || + par.pchan != askpar.pchan || + ((par.rate * 1000 < askpar.rate * 995) || + (par.rate * 1000 > askpar.rate * 1005))) { + fprintf(stderr, "%s: parameters not supported\n", __func__); + goto error; + } + + if (!sio_start(hdl)) { + fprintf(stderr, "%s: failed to start audio device\n", __func__); + goto error; + } + return 0; + + error: + sio_close(hdl); + return -1; +} + +static void deinit() +{ + sio_close(hdl); + hdl = NULL; +} + +static void play(void *b, int len) +{ + if (b != NULL) { + sio_write(hdl, buf, len); + } +} + +static void flush() +{ +} + +static void onpause() +{ +} + +static void onresume() +{ +} + +struct sound_driver sound_sndio = { + "sndio", + "OpenBSD sndio", + NULL, + init, + deint, + play, + flush, + onpause, + onresume +};