aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/sound/pcm/buffer.c
diff options
context:
space:
mode:
authorAriff Abdullah <ariff@FreeBSD.org>2009-06-07 19:12:08 +0000
committerAriff Abdullah <ariff@FreeBSD.org>2009-06-07 19:12:08 +0000
commit90da2b2859b259fca1ab223931f549a72e21a3a5 (patch)
tree906f402638735c3f32e226f6868f207db569d6a9 /sys/dev/sound/pcm/buffer.c
parent0a276edef96edc86d1af97b39f76ad168599ceb4 (diff)
downloadsrc-90da2b2859b259fca1ab223931f549a72e21a3a5.tar.gz
src-90da2b2859b259fca1ab223931f549a72e21a3a5.zip
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.
Notes
Notes: svn path=/head/; revision=193640
Diffstat (limited to 'sys/dev/sound/pcm/buffer.c')
-rw-r--r--sys/dev/sound/pcm/buffer.c105
1 files changed, 76 insertions, 29 deletions
diff --git a/sys/dev/sound/pcm/buffer.c b/sys/dev/sound/pcm/buffer.c
index 98fb60ede539..6e8259202dc0 100644
--- a/sys/dev/sound/pcm/buffer.c
+++ b/sys/dev/sound/pcm/buffer.c
@@ -1,5 +1,7 @@
/*-
- * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
+ * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
+ * Portions Copyright (c) Ryan Beasley <ryan.beasley@gmail.com> - GSoC 2006
+ * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -24,10 +26,17 @@
* SUCH DAMAGE.
*/
+#ifdef HAVE_KERNEL_OPTION_HEADERS
+#include "opt_snd.h"
+#endif
+
#include <dev/sound/pcm/sound.h>
#include "feeder_if.h"
+#define SND_USE_FXDIV
+#include "snd_fxdiv_gen.h"
+
SND_DECLARE_FILE("$FreeBSD$");
struct snd_dbuf *
@@ -154,7 +163,7 @@ sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
unsigned int bufsize, allocsize;
u_int8_t *tmpbuf;
- chn_lock(b->channel);
+ CHN_LOCK(b->channel);
if (b->maxsize == 0)
goto out;
if (blkcnt == 0)
@@ -162,7 +171,7 @@ sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
if (blksz == 0)
blksz = b->blksz;
if (blkcnt < 2 || blksz < 16 || (blkcnt * blksz) > b->maxsize) {
- chn_unlock(b->channel);
+ CHN_UNLOCK(b->channel);
return EINVAL;
}
if (blkcnt == b->blkcnt && blksz == b->blksz)
@@ -173,9 +182,9 @@ sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
if (bufsize > b->allocsize ||
bufsize < (b->allocsize >> SNDBUF_CACHE_SHIFT)) {
allocsize = round_page(bufsize);
- chn_unlock(b->channel);
+ CHN_UNLOCK(b->channel);
tmpbuf = malloc(allocsize, M_DEVBUF, M_WAITOK);
- chn_lock(b->channel);
+ CHN_LOCK(b->channel);
if (snd_verbose > 3)
printf("%s(): b=%p %p -> %p [%d -> %d : %d]\n",
__func__, b, b->tmpbuf, tmpbuf,
@@ -194,7 +203,7 @@ sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
sndbuf_reset(b);
out:
- chn_unlock(b->channel);
+ CHN_UNLOCK(b->channel);
return 0;
}
@@ -212,11 +221,11 @@ sndbuf_remalloc(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz)
if (bufsize > b->allocsize ||
bufsize < (b->allocsize >> SNDBUF_CACHE_SHIFT)) {
allocsize = round_page(bufsize);
- chn_unlock(b->channel);
+ CHN_UNLOCK(b->channel);
buf = malloc(allocsize, M_DEVBUF, M_WAITOK);
tmpbuf = malloc(allocsize, M_DEVBUF, M_WAITOK);
shadbuf = malloc(allocsize, M_DEVBUF, M_WAITOK);
- chn_lock(b->channel);
+ CHN_LOCK(b->channel);
if (b->buf != NULL)
free(b->buf, M_DEVBUF);
b->buf = buf;
@@ -334,14 +343,17 @@ int
sndbuf_setfmt(struct snd_dbuf *b, u_int32_t fmt)
{
b->fmt = fmt;
- b->bps = 1;
- b->bps <<= (b->fmt & AFMT_STEREO)? 1 : 0;
+ b->bps = AFMT_BPS(b->fmt);
+ b->align = AFMT_ALIGN(b->fmt);
+#if 0
+ b->bps = AFMT_CHANNEL(b->fmt);
if (b->fmt & AFMT_16BIT)
b->bps <<= 1;
else if (b->fmt & AFMT_24BIT)
b->bps *= 3;
else if (b->fmt & AFMT_32BIT)
b->bps <<= 2;
+#endif
return 0;
}
@@ -360,9 +372,7 @@ sndbuf_setspd(struct snd_dbuf *b, unsigned int spd)
unsigned int
sndbuf_getalign(struct snd_dbuf *b)
{
- static int align[] = {0, 1, 1, 2, 2, 2, 2, 3};
-
- return align[b->bps - 1];
+ return (b->align);
}
unsigned int
@@ -515,7 +525,7 @@ sndbuf_getfreeptr(struct snd_dbuf *b)
return (b->rp + b->rl) % b->bufsize;
}
-unsigned int
+u_int64_t
sndbuf_getblocks(struct snd_dbuf *b)
{
SNDBUF_LOCKASSERT(b);
@@ -523,7 +533,7 @@ sndbuf_getblocks(struct snd_dbuf *b)
return b->total / b->blksz;
}
-unsigned int
+u_int64_t
sndbuf_getprevblocks(struct snd_dbuf *b)
{
SNDBUF_LOCKASSERT(b);
@@ -531,7 +541,7 @@ sndbuf_getprevblocks(struct snd_dbuf *b)
return b->prev_total / b->blksz;
}
-unsigned int
+u_int64_t
sndbuf_gettotal(struct snd_dbuf *b)
{
SNDBUF_LOCKASSERT(b);
@@ -539,6 +549,14 @@ sndbuf_gettotal(struct snd_dbuf *b)
return b->total;
}
+u_int64_t
+sndbuf_getprevtotal(struct snd_dbuf *b)
+{
+ SNDBUF_LOCKASSERT(b);
+
+ return b->prev_total;
+}
+
void
sndbuf_updateprevtotal(struct snd_dbuf *b)
{
@@ -577,14 +595,14 @@ sndbuf_xbytes(unsigned int v, struct snd_dbuf *from, struct snd_dbuf *to)
if (from == NULL || to == NULL || v == 0)
return 0;
- return snd_xbytes(v, sndbuf_getbps(from) * sndbuf_getspd(from),
- sndbuf_getbps(to) * sndbuf_getspd(to));
+ return snd_xbytes(v, sndbuf_getalign(from) * sndbuf_getspd(from),
+ sndbuf_getalign(to) * sndbuf_getspd(to));
}
u_int8_t
sndbuf_zerodata(u_int32_t fmt)
{
- if (fmt & AFMT_SIGNED)
+ if (fmt & (AFMT_SIGNED | AFMT_PASSTHROUGH))
return (0x00);
else if (fmt & AFMT_MU_LAW)
return (0x7f);
@@ -670,26 +688,55 @@ sndbuf_dispose(struct snd_dbuf *b, u_int8_t *to, unsigned int count)
return 0;
}
+#ifdef SND_DIAGNOSTIC
+static uint32_t snd_feeder_maxfeed = 0;
+SYSCTL_INT(_hw_snd, OID_AUTO, feeder_maxfeed, CTLFLAG_RD,
+ &snd_feeder_maxfeed, 0, "maximum feeder count request");
+
+static uint32_t snd_feeder_maxcycle = 0;
+SYSCTL_INT(_hw_snd, OID_AUTO, feeder_maxcycle, CTLFLAG_RD,
+ &snd_feeder_maxcycle, 0, "maximum feeder cycle");
+#endif
+
/* count is number of bytes we want added to destination buffer */
int
sndbuf_feed(struct snd_dbuf *from, struct snd_dbuf *to, struct pcm_channel *channel, struct pcm_feeder *feeder, unsigned int count)
{
- unsigned int cnt;
+ unsigned int cnt, maxfeed;
+#ifdef SND_DIAGNOSTIC
+ unsigned int cycle;
+
+ if (count > snd_feeder_maxfeed)
+ snd_feeder_maxfeed = count;
+
+ cycle = 0;
+#endif
KASSERT(count > 0, ("can't feed 0 bytes"));
if (sndbuf_getfree(to) < count)
- return EINVAL;
+ return (EINVAL);
+
+ maxfeed = SND_FXROUND(SND_FXDIV_MAX, sndbuf_getalign(to));
do {
- cnt = FEEDER_FEED(feeder, channel, to->tmpbuf, count, from);
- if (cnt) {
- sndbuf_acquire(to, to->tmpbuf, cnt);
- count -= cnt;
- }
- } while (count && cnt);
+ cnt = FEEDER_FEED(feeder, channel, to->tmpbuf,
+ min(count, maxfeed), from);
+ if (cnt == 0)
+ break;
+ sndbuf_acquire(to, to->tmpbuf, cnt);
+ count -= cnt;
+#ifdef SND_DIAGNOSTIC
+ cycle++;
+#endif
+ } while (count != 0);
- return 0;
+#ifdef SND_DIAGNOSTIC
+ if (cycle > snd_feeder_maxcycle)
+ snd_feeder_maxcycle = cycle;
+#endif
+
+ return (0);
}
/************************************************************/
@@ -703,7 +750,7 @@ sndbuf_dump(struct snd_dbuf *b, char *s, u_int32_t what)
if (what & 0x02)
printf(" dl: %d, rp: %d, rl: %d, hp: %d", b->dl, b->rp, b->rl, b->hp);
if (what & 0x04)
- printf(" total: %d, prev_total: %d, xrun: %d", b->total, b->prev_total, b->xrun);
+ printf(" total: %ju, prev_total: %ju, xrun: %d", (uintmax_t)b->total, (uintmax_t)b->prev_total, b->xrun);
if (what & 0x08)
printf(" fmt: 0x%x, spd: %d", b->fmt, b->spd);
if (what & 0x10)