Implement volume controls for pseudo audio device.

This commit is contained in:
jmcneill 2007-11-11 19:53:38 +00:00
parent 96689562e7
commit b6ed7a1236
5 changed files with 335 additions and 23 deletions

View File

@ -1,4 +1,5 @@
# $NetBSD: files.pad,v 1.1 2007/11/11 17:37:45 jmcneill Exp $
# $NetBSD: files.pad,v 1.2 2007/11/11 19:53:38 jmcneill Exp $
defpseudo pad: audiobus, auconv, aurateconv
file dev/pad/pad.c pad
file dev/pad/padvol.c pad

View File

@ -1,4 +1,4 @@
/* $NetBSD: pad.c,v 1.1 2007/11/11 17:37:45 jmcneill Exp $ */
/* $NetBSD: pad.c,v 1.2 2007/11/11 19:53:38 jmcneill Exp $ */
/*-
* Copyright (c) 2007 Jared D. McNeill <jmcneill@invisible.ca>
@ -33,7 +33,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: pad.c,v 1.1 2007/11/11 17:37:45 jmcneill Exp $");
__KERNEL_RCSID(0, "$NetBSD: pad.c,v 1.2 2007/11/11 19:53:38 jmcneill Exp $");
#include <sys/types.h>
#include <sys/param.h>
@ -51,6 +51,9 @@ __KERNEL_RCSID(0, "$NetBSD: pad.c,v 1.1 2007/11/11 17:37:45 jmcneill Exp $");
#include <dev/audiovar.h>
#include <dev/auconv.h>
#include <dev/pad/padvar.h>
#include <dev/pad/padvol.h>
#define PADUNIT(x) minor(x)
extern struct cfdriver pad_cd;
@ -66,26 +69,13 @@ typedef struct pad_block {
int pb_len;
} pad_block_t;
typedef struct pad_softc {
struct device sc_dev;
int sc_open;
struct audio_encoding_set *sc_encodings;
void (*sc_intr)(void *);
void *sc_intrarg;
kcondvar_t sc_condvar;
kmutex_t sc_mutex;
struct audio_softc *sc_audiodev;
int sc_blksize;
#define PAD_BLKSIZE 1024
#define PAD_BUFSIZE 65536
uint8_t sc_audiobuf[PAD_BUFSIZE];
uint32_t sc_buflen;
uint32_t sc_rpos, sc_wpos;
} pad_softc_t;
enum {
PAD_OUTPUT_CLASS,
PAD_INPUT_CLASS,
PAD_OUTPUT_MASTER_VOLUME,
PAD_INPUT_DAC_VOLUME,
PAD_ENUM_LAST,
};
static int pad_match(struct device *, struct cfdata *, void *);
static void pad_attach(struct device *, struct device *, void *);
@ -277,6 +267,7 @@ pad_attach(struct device *parent, struct device *self, void *opaque)
cv_init(&sc->sc_condvar, device_xname(self));
mutex_init(&sc->sc_mutex, MUTEX_DRIVER, IPL_AUDIO);
sc->sc_swvol = 255;
sc->sc_buflen = 0;
sc->sc_rpos = sc->sc_wpos = 0;
sc->sc_audiodev = (void *)audio_attach_mi(&pad_hw_if, sc, &sc->sc_dev);
@ -395,6 +386,21 @@ pad_set_params(void *opaque, int setmode, int usemode,
rec, true, rfil) < 0)
return EINVAL;
if (pfil->req_size > 0)
play = &pfil->filters[0].param;
switch (play->encoding) {
case AUDIO_ENCODING_SLINEAR_LE:
if (play->precision == 16 && play->validbits == 16)
pfil->prepend(pfil, pad_vol_slinear16_le, play);
break;
case AUDIO_ENCODING_SLINEAR_BE:
if (play->precision == 16 && play->validbits == 16)
pfil->prepend(pfil, pad_vol_slinear16_be, play);
break;
default:
break;
}
return 0;
}
@ -461,18 +467,75 @@ pad_getdev(void *opaque, struct audio_device *ret)
static int
pad_set_port(void *opaque, mixer_ctrl_t *mc)
{
pad_softc_t *sc;
sc = (pad_softc_t *)opaque;
switch (mc->dev) {
case PAD_OUTPUT_MASTER_VOLUME:
case PAD_INPUT_DAC_VOLUME:
sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
return 0;
}
return ENXIO;
}
static int
pad_get_port(void *opaque, mixer_ctrl_t *mc)
{
pad_softc_t *sc;
sc = (pad_softc_t *)opaque;
switch (mc->dev) {
case PAD_OUTPUT_MASTER_VOLUME:
case PAD_INPUT_DAC_VOLUME:
mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_swvol;
return 0;
}
return ENXIO;
}
static int
pad_query_devinfo(void *opaque, mixer_devinfo_t *di)
{
pad_softc_t *sc;
sc = (pad_softc_t *)opaque;
switch (di->index) {
case PAD_OUTPUT_CLASS:
di->mixer_class = PAD_OUTPUT_CLASS;
strcpy(di->label.name, AudioCoutputs);
di->type = AUDIO_MIXER_CLASS;
di->next = di->prev = AUDIO_MIXER_LAST;
return 0;
case PAD_INPUT_CLASS:
di->mixer_class = PAD_INPUT_CLASS;
strcpy(di->label.name, AudioCinputs);
di->type = AUDIO_MIXER_CLASS;
di->next = di->prev = AUDIO_MIXER_LAST;
return 0;
case PAD_OUTPUT_MASTER_VOLUME:
di->mixer_class = PAD_OUTPUT_CLASS;
strcpy(di->label.name, AudioNmaster);
di->type = AUDIO_MIXER_VALUE;
di->next = di->prev = AUDIO_MIXER_LAST;
di->un.v.num_channels = 1;
strcpy(di->un.v.units.name, AudioNvolume);
return 0;
case PAD_INPUT_DAC_VOLUME:
di->mixer_class = PAD_INPUT_CLASS;
strcpy(di->label.name, AudioNdac);
di->type = AUDIO_MIXER_VALUE;
di->next = di->prev = AUDIO_MIXER_LAST;
di->un.v.num_channels = 1;
strcpy(di->un.v.units.name, AudioNvolume);
return 0;
}
return ENXIO;
}

61
sys/dev/pad/padvar.h Normal file
View File

@ -0,0 +1,61 @@
/* $NetBSD: padvar.h,v 1.1 2007/11/11 19:53:38 jmcneill Exp $ */
/*-
* Copyright (c) 2007 Jared D. McNeill <jmcneill@invisible.ca>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Jared D. McNeill.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _SYS_DEV_PAD_PADVAR_H
#define _SYS_DEV_PAD_PADVAR_H
typedef struct pad_softc {
struct device sc_dev;
int sc_open;
struct audio_encoding_set *sc_encodings;
void (*sc_intr)(void *);
void *sc_intrarg;
kcondvar_t sc_condvar;
kmutex_t sc_mutex;
struct audio_softc *sc_audiodev;
int sc_blksize;
#define PAD_BLKSIZE 1024
#define PAD_BUFSIZE 65536
uint8_t sc_audiobuf[PAD_BUFSIZE];
uint32_t sc_buflen;
uint32_t sc_rpos, sc_wpos;
u_int sc_swvol;
} pad_softc_t;
#endif /* !_SYS_DEV_PAD_PADVAR_H */

129
sys/dev/pad/padvol.c Normal file
View File

@ -0,0 +1,129 @@
/* $NetBSD: padvol.c,v 1.1 2007/11/11 19:53:38 jmcneill Exp $ */
/*-
* Copyright (c) 2007 Jared D. McNeill <jmcneill@invisible.ca>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Jared D. McNeill.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: padvol.c,v 1.1 2007/11/11 19:53:38 jmcneill Exp $");
#include <sys/types.h>
#include <sys/select.h>
#include <sys/condvar.h>
#include <sys/mutex.h>
#include <sys/kmem.h>
#include <sys/device.h>
#include <dev/audiovar.h>
#include <dev/auconv.h>
#include <dev/pad/padvar.h>
#include <dev/pad/padvol.h>
typedef struct pad_filter {
stream_filter_t base;
struct audio_softc *audiosc;
} pad_filter_t;
static void
pad_filter_dtor(stream_filter_t *this)
{
if (this)
kmem_free(this, sizeof(pad_filter_t));
}
static stream_filter_t *
pad_filter_factory(struct audio_softc *sc,
int (*fetch_to)(stream_fetcher_t *, audio_stream_t *, int))
{
pad_filter_t *this;
this = kmem_alloc(sizeof(pad_filter_t), KM_SLEEP);
this->base.base.fetch_to = fetch_to;
this->base.dtor = pad_filter_dtor;
this->base.set_fetcher = stream_filter_set_fetcher;
this->base.set_inputbuffer = stream_filter_set_inputbuffer;
this->audiosc = sc;
return (stream_filter_t *)this;
}
PAD_DEFINE_FILTER(pad_vol_slinear16_le)
{
pad_filter_t *pf;
pad_softc_t *sc;
stream_filter_t *this;
int16_t j, *wp;
int m, err;
pf = (pad_filter_t *)self;
sc = (pad_softc_t *)pf->audiosc->sc_dev;
this = &pf->base;
max_used = (max_used + 1) & ~1;
if ((err = this->prev->fetch_to(this->prev, this->src, max_used)))
return err;
m = (dst->end - dst->start) & ~1;
m = min(m, max_used);
FILTER_LOOP_PROLOGUE(this->src, 2, dst, 2, m) {
j = (s[1] << 8 | s[0]);
wp = (int16_t *)d;
*wp = ((j * sc->sc_swvol) / 255);
} FILTER_LOOP_EPILOGUE(this->src, dst);
return 0;
}
PAD_DEFINE_FILTER(pad_vol_slinear16_be)
{
pad_filter_t *pf;
pad_softc_t *sc;
stream_filter_t *this;
int16_t j, *wp;
int m, err;
pf = (pad_filter_t *)self;
sc = (pad_softc_t *)pf->audiosc->sc_dev;
this = &pf->base;
max_used = (max_used + 1) & ~1;
if ((err = this->prev->fetch_to(this->prev, this->src, max_used)))
return err;
m = (dst->end - dst->start) & ~1;
m = min(m, max_used);
FILTER_LOOP_PROLOGUE(this->src, 2, dst, 2, m) {
j = (s[0] << 8 | s[1]);
wp = (int16_t *)d;
*wp = ((j * sc->sc_swvol) / 255);
} FILTER_LOOP_EPILOGUE(this->src, dst);
return 0;
}

58
sys/dev/pad/padvol.h Normal file
View File

@ -0,0 +1,58 @@
/* $NetBSD: padvol.h,v 1.1 2007/11/11 19:53:38 jmcneill Exp $ */
/*-
* Copyright (c) 2007 Jared D. McNeill <jmcneill@invisible.ca>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Jared D. McNeill.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _SYS_DEV_PAD_PADVOL_H
#define _SYS_DEV_PAD_PADVOL_H
stream_filter_t * pad_vol_slinear16_le(struct audio_softc *,
const audio_params_t *, const audio_params_t *);
stream_filter_t * pad_vol_slinear16_be(struct audio_softc *,
const audio_params_t *, const audio_params_t *);
#define PAD_DEFINE_FILTER(name) \
static int \
name##_fetch_to(stream_fetcher_t *, audio_stream_t *, int); \
stream_filter_t * name(struct audio_softc *, \
const audio_params_t *, const audio_params_t *); \
stream_filter_t * \
name(struct audio_softc *sc, const audio_params_t *from, \
const audio_params_t *to) \
{ \
return pad_filter_factory(sc, name##_fetch_to); \
} \
static int \
name##_fetch_to(stream_fetcher_t *self, audio_stream_t *dst, \
int max_used)
#endif /* !_SYS_DEV_PAD_PADVOL_H */