freebsd-dev/sys/dev/sound/pcm/feeder_volume.c
Ariff Abdullah 90da2b2859 Sound Mega-commit. Expect further cleanup until code freeze.
For a slightly thorough explaination, please refer to
	[1] http://people.freebsd.org/~ariff/SOUND_4.TXT.html .

Summary of changes includes:

1 Volume Per-Channel (vpc).  Provides private / standalone volume control
  unique per-stream pcm channel without touching master volume / pcm.
  Applications can directly use SNDCTL_DSP_[GET|SET][PLAY|REC]VOL, or for
  backwards compatibility, SOUND_MIXER_PCM through the opened dsp device
  instead of /dev/mixer.  Special "bypass" mode is enabled through
  /dev/mixer which will automatically detect if the adjustment is made
  through /dev/mixer and forward its request to this private volume
  controller.  Changes to this volume object will not interfere with
  other channels.

  Requirements:
    - SNDCTL_DSP_[GET|SET][PLAY|REC]_VOL are newer ioctls (OSSv4) which
      require specific application modifications (preferred).
    - No modifications required for using bypass mode, so applications
      like mplayer or xmms should work out of the box.

  Kernel hints:
    - hint.pcm.%d.vpc (0 = disable vpc).

  Kernel sysctls:
    - hw.snd.vpc_mixer_bypass (default: 1).  Enable or disable /dev/mixer
      bypass mode.
    - hw.snd.vpc_autoreset (default: 1).  By default, closing/opening
      /dev/dsp will reset the volume back to 0 db gain/attenuation.
      Setting this to 0 will preserve its settings across device
      closing/opening.
    - hw.snd.vpc_reset (default: 0).  Panic/reset button to reset all
      volume settings back to 0 db.
    - hw.snd.vpc_0db (default: 45).  0 db relative to linear mixer value.

2 High quality fixed-point Bandlimited SINC sampling rate converter,
  based on Julius O'Smith's Digital Audio Resampling -
  http://ccrma.stanford.edu/~jos/resample/.  It includes a filter design
  script written in awk (the clumsiest joke I've ever written)
    - 100% 32bit fixed-point, 64bit accumulator.
    - Possibly among the fastest (if not fastest) of its kind.
    - Resampling quality is tunable, either runtime or during kernel
      compilation (FEEDER_RATE_PRESETS).
    - Quality can be further customized during kernel compilation by
      defining FEEDER_RATE_PRESETS in /etc/make.conf.

  Kernel sysctls:
    - hw.snd.feeder_rate_quality.
      0 - Zero-order Hold (ZOH).  Fastest, bad quality.
      1 - Linear Interpolation (LINEAR).  Slightly slower than ZOH,
          better quality but still does not eliminate aliasing.
      2 - (and above) - Sinc Interpolation(SINC).  Best quality.  SINC
          quality always start from 2 and above.

  Rough quality comparisons:
    - http://people.freebsd.org/~ariff/z_comparison/

3 Bit-perfect mode.  Bypasses all feeder/dsp effects.  Pure sound will be
  directly fed into the hardware.

4 Parametric (compile time) Software Equalizer (Bass/Treble mixer). Can
  be customized by defining FEEDER_EQ_PRESETS in /etc/make.conf.

5 Transparent/Adaptive Virtual Channel. Now you don't have to disable
  vchans in order to make digital format pass through.  It also makes
  vchans more dynamic by choosing a better format/rate among all the
  concurrent streams, which means that dev.pcm.X.play.vchanformat/rate
  becomes sort of optional.

6 Exclusive Stream, with special open() mode O_EXCL.  This will "mute"
  other concurrent vchan streams and only allow a single channel with
  O_EXCL set to keep producing sound.

Other Changes:
    * most feeder_* stuffs are compilable in userland. Let's not
      speculate whether we should go all out for it (save that for
      FreeBSD 16.0-RELEASE).
    * kobj signature fixups, thanks to Andriy Gapon <avg@freebsd.org>
    * pull out channel mixing logic out of vchan.c and create its own
      feeder_mixer for world justice.
    * various refactoring here and there, for good or bad.
    * activation of few more OSSv4 ioctls() (see [1] above).
    * opt_snd.h for possible compile time configuration:
      (mostly for debugging purposes, don't try these at home)
        SND_DEBUG
        SND_DIAGNOSTIC
        SND_FEEDER_MULTIFORMAT
        SND_FEEDER_FULL_MULTIFORMAT
        SND_FEEDER_RATE_HP
        SND_PCM_64
        SND_OLDSTEREO

Manual page updates are on the way.

Tested by:	joel, Olivier SMEDTS <olivier at gid0 d org>, too many
          	unsung / unnamed heroes.
2009-06-07 19:12:08 +00:00

342 lines
8.6 KiB
C

/*-
* Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
*/
/* feeder_volume, a long 'Lost Technology' rather than a new feature. */
#ifdef _KERNEL
#ifdef HAVE_KERNEL_OPTION_HEADERS
#include "opt_snd.h"
#endif
#include <dev/sound/pcm/sound.h>
#include <dev/sound/pcm/pcm.h>
#include "feeder_if.h"
#define SND_USE_FXDIV
#include "snd_fxdiv_gen.h"
SND_DECLARE_FILE("$FreeBSD$");
#endif
typedef void (*feed_volume_t)(int *, int *, uint32_t, uint8_t *, uint32_t);
#define FEEDVOLUME_CALC8(s, v) (SND_VOL_CALC_SAMPLE((intpcm_t) \
(s) << 8, v) >> 8)
#define FEEDVOLUME_CALC16(s, v) SND_VOL_CALC_SAMPLE((intpcm_t)(s), v)
#define FEEDVOLUME_CALC24(s, v) SND_VOL_CALC_SAMPLE((intpcm64_t)(s), v)
#define FEEDVOLUME_CALC32(s, v) SND_VOL_CALC_SAMPLE((intpcm64_t)(s), v)
#define FEEDVOLUME_DECLARE(SIGN, BIT, ENDIAN) \
static void \
feed_volume_##SIGN##BIT##ENDIAN(int *vol, int *matrix, \
uint32_t channels, uint8_t *dst, uint32_t count) \
{ \
intpcm##BIT##_t v; \
intpcm_t x; \
uint32_t i; \
\
dst += count * PCM_##BIT##_BPS * channels; \
do { \
i = channels; \
do { \
dst -= PCM_##BIT##_BPS; \
i--; \
x = PCM_READ_##SIGN##BIT##_##ENDIAN(dst); \
v = FEEDVOLUME_CALC##BIT(x, vol[matrix[i]]); \
x = PCM_CLAMP_##SIGN##BIT(v); \
_PCM_WRITE_##SIGN##BIT##_##ENDIAN(dst, x); \
} while (i != 0); \
} while (--count != 0); \
}
#if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
FEEDVOLUME_DECLARE(S, 16, LE)
FEEDVOLUME_DECLARE(S, 32, LE)
#endif
#if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
FEEDVOLUME_DECLARE(S, 16, BE)
FEEDVOLUME_DECLARE(S, 32, BE)
#endif
#ifdef SND_FEEDER_MULTIFORMAT
FEEDVOLUME_DECLARE(S, 8, NE)
FEEDVOLUME_DECLARE(S, 24, LE)
FEEDVOLUME_DECLARE(S, 24, BE)
FEEDVOLUME_DECLARE(U, 8, NE)
FEEDVOLUME_DECLARE(U, 16, LE)
FEEDVOLUME_DECLARE(U, 24, LE)
FEEDVOLUME_DECLARE(U, 32, LE)
FEEDVOLUME_DECLARE(U, 16, BE)
FEEDVOLUME_DECLARE(U, 24, BE)
FEEDVOLUME_DECLARE(U, 32, BE)
#endif
struct feed_volume_info {
uint32_t bps, channels;
feed_volume_t apply;
int volume_class;
int state;
int matrix[SND_CHN_MAX];
};
#define FEEDVOLUME_ENTRY(SIGN, BIT, ENDIAN) \
{ \
AFMT_##SIGN##BIT##_##ENDIAN, \
feed_volume_##SIGN##BIT##ENDIAN \
}
static const struct {
uint32_t format;
feed_volume_t apply;
} feed_volume_info_tab[] = {
#if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
FEEDVOLUME_ENTRY(S, 16, LE),
FEEDVOLUME_ENTRY(S, 32, LE),
#endif
#if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
FEEDVOLUME_ENTRY(S, 16, BE),
FEEDVOLUME_ENTRY(S, 32, BE),
#endif
#ifdef SND_FEEDER_MULTIFORMAT
FEEDVOLUME_ENTRY(S, 8, NE),
FEEDVOLUME_ENTRY(S, 24, LE),
FEEDVOLUME_ENTRY(S, 24, BE),
FEEDVOLUME_ENTRY(U, 8, NE),
FEEDVOLUME_ENTRY(U, 16, LE),
FEEDVOLUME_ENTRY(U, 24, LE),
FEEDVOLUME_ENTRY(U, 32, LE),
FEEDVOLUME_ENTRY(U, 16, BE),
FEEDVOLUME_ENTRY(U, 24, BE),
FEEDVOLUME_ENTRY(U, 32, BE)
#endif
};
#define FEEDVOLUME_TAB_SIZE ((int32_t) \
(sizeof(feed_volume_info_tab) / \
sizeof(feed_volume_info_tab[0])))
static int
feed_volume_init(struct pcm_feeder *f)
{
struct feed_volume_info *info;
struct pcmchan_matrix *m;
uint32_t i;
int ret;
if (f->desc->in != f->desc->out ||
AFMT_CHANNEL(f->desc->in) > SND_CHN_MAX)
return (EINVAL);
for (i = 0; i < FEEDVOLUME_TAB_SIZE; i++) {
if (AFMT_ENCODING(f->desc->in) ==
feed_volume_info_tab[i].format) {
info = malloc(sizeof(*info), M_DEVBUF,
M_NOWAIT | M_ZERO);
if (info == NULL)
return (ENOMEM);
info->bps = AFMT_BPS(f->desc->in);
info->channels = AFMT_CHANNEL(f->desc->in);
info->apply = feed_volume_info_tab[i].apply;
info->volume_class = SND_VOL_C_PCM;
info->state = FEEDVOLUME_ENABLE;
f->data = info;
m = feeder_matrix_default_channel_map(info->channels);
if (m == NULL) {
free(info, M_DEVBUF);
return (EINVAL);
}
ret = feeder_volume_apply_matrix(f, m);
if (ret != 0)
free(info, M_DEVBUF);
return (ret);
}
}
return (EINVAL);
}
static int
feed_volume_free(struct pcm_feeder *f)
{
struct feed_volume_info *info;
info = f->data;
if (info != NULL)
free(info, M_DEVBUF);
f->data = NULL;
return (0);
}
static int
feed_volume_set(struct pcm_feeder *f, int what, int value)
{
struct feed_volume_info *info;
struct pcmchan_matrix *m;
int ret;
info = f->data;
ret = 0;
switch (what) {
case FEEDVOLUME_CLASS:
if (value < SND_VOL_C_BEGIN || value > SND_VOL_C_END)
return (EINVAL);
info->volume_class = value;
break;
case FEEDVOLUME_CHANNELS:
if (value < SND_CHN_MIN || value > SND_CHN_MAX)
return (EINVAL);
m = feeder_matrix_default_channel_map(value);
if (m == NULL)
return (EINVAL);
ret = feeder_volume_apply_matrix(f, m);
break;
case FEEDVOLUME_STATE:
if (!(value == FEEDVOLUME_ENABLE || value == FEEDVOLUME_BYPASS))
return (EINVAL);
info->state = value;
break;
default:
return (EINVAL);
break;
}
return (ret);
}
static int
feed_volume_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b,
uint32_t count, void *source)
{
struct feed_volume_info *info;
uint32_t j, align;
int i, *vol, *matrix;
uint8_t *dst;
/*
* Fetch filter data operation.
*/
info = f->data;
if (info->state == FEEDVOLUME_BYPASS)
return (FEEDER_FEED(f->source, c, b, count, source));
vol = c->volume[SND_VOL_C_VAL(info->volume_class)];
matrix = info->matrix;
/*
* First, let see if we really need to apply gain at all.
*/
j = 0;
i = info->channels;
do {
if (vol[matrix[--i]] != SND_VOL_FLAT) {
j = 1;
break;
}
} while (i != 0);
/* Nope, just bypass entirely. */
if (j == 0)
return (FEEDER_FEED(f->source, c, b, count, source));
dst = b;
align = info->bps * info->channels;
do {
if (count < align)
break;
j = SND_FXDIV(FEEDER_FEED(f->source, c, dst, count, source),
align);
if (j == 0)
break;
info->apply(vol, matrix, info->channels, dst, j);
j *= align;
dst += j;
count -= j;
} while (count != 0);
return (dst - b);
}
static struct pcm_feederdesc feeder_volume_desc[] = {
{ FEEDER_VOLUME, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 }
};
static kobj_method_t feeder_volume_methods[] = {
KOBJMETHOD(feeder_init, feed_volume_init),
KOBJMETHOD(feeder_free, feed_volume_free),
KOBJMETHOD(feeder_set, feed_volume_set),
KOBJMETHOD(feeder_feed, feed_volume_feed),
KOBJMETHOD_END
};
FEEDER_DECLARE(feeder_volume, NULL);
/* Extern */
/*
* feeder_volume_apply_matrix(): For given matrix map, apply its configuration
* to feeder_volume matrix structure. There are
* possibilites that feeder_volume be inserted
* before or after feeder_matrix, which in this
* case feeder_volume must be in a good terms
* with _current_ matrix.
*/
int
feeder_volume_apply_matrix(struct pcm_feeder *f, struct pcmchan_matrix *m)
{
struct feed_volume_info *info;
uint32_t i;
if (f == NULL || f->desc == NULL || f->desc->type != FEEDER_VOLUME ||
f->data == NULL || m == NULL || m->channels < SND_CHN_MIN ||
m->channels > SND_CHN_MAX)
return (EINVAL);
info = f->data;
for (i = 0; i < (sizeof(info->matrix) / sizeof(info->matrix[0])); i++) {
if (i < m->channels)
info->matrix[i] = m->map[i].type;
else
info->matrix[i] = SND_CHN_T_FL;
}
info->channels = m->channels;
return (0);
}