2001-04-08 20:26:22 +00:00
|
|
|
/*
|
2003-01-20 00:54:24 +00:00
|
|
|
* Copyright (c) 2003 Orion Hodson <orion@freebsd.org>
|
2001-04-08 20:26:22 +00:00
|
|
|
* 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.
|
2003-01-20 00:54:24 +00:00
|
|
|
*
|
|
|
|
* MAINTAINER: Orion Hodson <orion@freebsd.org>
|
|
|
|
*
|
|
|
|
* This rate conversion code uses linear interpolation without any
|
|
|
|
* pre- or post- interpolation filtering to combat aliasing. This
|
|
|
|
* greatly limits the sound quality and should be addressed at some
|
|
|
|
* stage in the future.
|
|
|
|
*
|
|
|
|
* Since this accuracy of interpolation is sensitive and examination
|
|
|
|
* of the algorithm output is harder from the kernel, th code is
|
|
|
|
* designed to be compiled in the kernel and in a userland test
|
|
|
|
* harness. This is done by selectively including and excluding code
|
|
|
|
* with several portions based on whether _KERNEL is defined. It's a
|
2003-01-30 16:32:56 +00:00
|
|
|
* little ugly, but exceedingly useful. The testsuite and its
|
|
|
|
* revisions can be found at:
|
|
|
|
* http://people.freebsd.org/~orion/feedrate/
|
|
|
|
*
|
|
|
|
* Special thanks to Ken Marx for exposing flaws in the code and for
|
|
|
|
* testing revisions.
|
2001-04-08 20:26:22 +00:00
|
|
|
*/
|
|
|
|
|
2003-01-20 00:54:24 +00:00
|
|
|
#ifdef _KERNEL
|
2001-04-08 20:26:22 +00:00
|
|
|
|
2003-01-20 00:54:24 +00:00
|
|
|
#include <dev/sound/pcm/sound.h>
|
2001-04-08 20:26:22 +00:00
|
|
|
#include "feeder_if.h"
|
|
|
|
|
2001-08-23 11:30:52 +00:00
|
|
|
SND_DECLARE_FILE("$FreeBSD$");
|
2003-02-06 17:32:02 +00:00
|
|
|
|
|
|
|
#endif /* _KERNEL */
|
|
|
|
|
2001-04-08 20:26:22 +00:00
|
|
|
MALLOC_DEFINE(M_RATEFEEDER, "ratefeed", "pcm rate feeder");
|
|
|
|
|
2003-02-06 17:32:02 +00:00
|
|
|
#ifndef RATE_ASSERT
|
|
|
|
#define RATE_ASSERT(x, y) /* KASSERT(x) */
|
|
|
|
#endif /* RATE_ASSERT */
|
2003-01-20 00:54:24 +00:00
|
|
|
|
2003-02-06 17:32:02 +00:00
|
|
|
#ifndef RATE_TRACE
|
|
|
|
#define RATE_TRACE(x...) /* printf(x) */
|
|
|
|
#endif
|
2003-01-20 00:54:24 +00:00
|
|
|
|
2003-01-30 16:32:56 +00:00
|
|
|
/*****************************************************************************/
|
2003-02-06 17:32:02 +00:00
|
|
|
|
|
|
|
/* The following coefficients are coupled. They are chosen to be
|
|
|
|
* guarantee calculable factors for the interpolation routine. They
|
|
|
|
* have been tested over the range of RATEMIN-RATEMAX Hz. Decreasing
|
|
|
|
* the granularity increases the required buffer size and affects the
|
|
|
|
* gain values at different points in the space. These values were
|
|
|
|
* found by running the test program with -p (probe) and some trial
|
|
|
|
* and error.
|
2003-01-30 16:32:56 +00:00
|
|
|
*
|
|
|
|
* ROUNDHZ the granularity of sample rates (fits n*11025 and n*8000).
|
|
|
|
* FEEDBUFSZ the amount of buffer space.
|
|
|
|
* MINGAIN the minimum acceptable gain in coefficients search.
|
|
|
|
*/
|
|
|
|
#define ROUNDHZ 25
|
2003-02-06 17:32:02 +00:00
|
|
|
#define FEEDBUFSZ 8192
|
2003-01-30 16:32:56 +00:00
|
|
|
#define MINGAIN 92
|
|
|
|
|
2003-02-06 17:32:02 +00:00
|
|
|
#define RATEMIN 4000
|
|
|
|
#define RATEMAX 48000
|
2003-01-20 00:54:24 +00:00
|
|
|
|
|
|
|
struct feed_rate_info;
|
|
|
|
|
|
|
|
typedef int (*rate_convert_method)(struct feed_rate_info *,
|
|
|
|
uint32_t, uint32_t, int16_t *);
|
|
|
|
|
|
|
|
static int
|
|
|
|
convert_stereo_up(struct feed_rate_info *info,
|
|
|
|
uint32_t src_ticks, uint32_t dst_ticks, int16_t *dst);
|
|
|
|
|
|
|
|
static int
|
|
|
|
convert_stereo_down(struct feed_rate_info *info,
|
|
|
|
uint32_t src_ticks, uint32_t dst_ticks, int16_t *dst);
|
|
|
|
|
2001-04-08 20:26:22 +00:00
|
|
|
struct feed_rate_info {
|
2003-01-20 00:54:24 +00:00
|
|
|
uint32_t src, dst; /* source and destination rates */
|
|
|
|
uint16_t buffer_ticks; /* number of available samples in buffer */
|
|
|
|
uint16_t buffer_pos; /* next available sample in buffer */
|
|
|
|
uint16_t rounds; /* maximum number of cycle rounds w buffer */
|
|
|
|
uint16_t alpha; /* interpolation distance */
|
|
|
|
uint16_t sscale; /* src clock scale */
|
|
|
|
uint16_t dscale; /* dst clock scale */
|
|
|
|
uint16_t mscale; /* scale factor to avoid divide per sample */
|
|
|
|
uint16_t mroll; /* roll to again avoid divide per sample */
|
|
|
|
uint16_t channels; /* 1 = mono, 2 = stereo */
|
|
|
|
|
|
|
|
rate_convert_method convert;
|
2003-01-30 16:32:56 +00:00
|
|
|
int16_t buffer[FEEDBUFSZ];
|
2001-04-08 20:26:22 +00:00
|
|
|
};
|
|
|
|
|
2003-01-30 16:32:56 +00:00
|
|
|
#define bytes_per_sample 2
|
|
|
|
#define src_ticks_per_cycle(info) (info->dscale * info->rounds)
|
|
|
|
#define dst_ticks_per_cycle(info) (info->sscale * info->rounds)
|
|
|
|
#define bytes_per_tick(info) (info->channels * bytes_per_sample)
|
2003-01-20 00:54:24 +00:00
|
|
|
#define src_bytes_per_cycle(info) \
|
2003-01-30 16:32:56 +00:00
|
|
|
(src_ticks_per_cycle(info) * bytes_per_tick(info))
|
2003-01-20 00:54:24 +00:00
|
|
|
#define dst_bytes_per_cycle(info) \
|
2003-01-30 16:32:56 +00:00
|
|
|
(dst_ticks_per_cycle(info) * bytes_per_tick(info))
|
2003-01-20 00:54:24 +00:00
|
|
|
|
|
|
|
static uint32_t
|
2003-01-30 16:32:56 +00:00
|
|
|
gcd(uint32_t x, uint32_t y)
|
2003-01-20 00:54:24 +00:00
|
|
|
{
|
2003-01-30 16:32:56 +00:00
|
|
|
uint32_t w;
|
|
|
|
while (y != 0) {
|
|
|
|
w = x % y;
|
|
|
|
x = y;
|
|
|
|
y = w;
|
|
|
|
}
|
|
|
|
return x;
|
2003-01-20 00:54:24 +00:00
|
|
|
}
|
|
|
|
|
2001-04-08 20:26:22 +00:00
|
|
|
static int
|
|
|
|
feed_rate_setup(struct pcm_feeder *f)
|
|
|
|
{
|
|
|
|
struct feed_rate_info *info = f->data;
|
2003-01-20 00:54:24 +00:00
|
|
|
uint32_t mscale, mroll, l, r, g;
|
|
|
|
|
|
|
|
/* Beat sample rates down by greatest common divisor */
|
|
|
|
g = gcd(info->src, info->dst);
|
|
|
|
info->sscale = info->dst / g;
|
|
|
|
info->dscale = info->src / g;
|
2001-04-08 20:26:22 +00:00
|
|
|
|
2003-01-20 00:54:24 +00:00
|
|
|
info->alpha = 0;
|
|
|
|
info->buffer_ticks = 0;
|
|
|
|
info->buffer_pos = 0;
|
|
|
|
|
|
|
|
/* Pick suitable conversion routine */
|
|
|
|
if (info->src > info->dst) {
|
|
|
|
info->convert = convert_stereo_down;
|
|
|
|
} else {
|
|
|
|
info->convert = convert_stereo_up;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine number of conversion rounds that will fit into
|
|
|
|
* buffer. NB Must set info->rounds to one before using
|
|
|
|
* src_ticks_per_cycle here since it used by src_ticks_per_cycle.
|
|
|
|
*/
|
|
|
|
info->rounds = 1;
|
2003-01-30 16:32:56 +00:00
|
|
|
r = (FEEDBUFSZ - bytes_per_tick(info)) /
|
|
|
|
(src_ticks_per_cycle(info) * bytes_per_tick(info));
|
|
|
|
if (r == 0) {
|
|
|
|
RATE_TRACE("Insufficient buffer space for conversion %d -> %d "
|
|
|
|
"(%d < %d)\n", info->src, info->dst, FEEDBUFSZ,
|
|
|
|
src_ticks_per_cycle(info) * bytes_per_tick(info));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
info->rounds = r;
|
|
|
|
|
2003-01-20 00:54:24 +00:00
|
|
|
/*
|
|
|
|
* Find scale and roll combination that allows us to trade
|
|
|
|
* costly divide operations in the main loop for multiply-rolls.
|
|
|
|
*/
|
2003-01-30 16:32:56 +00:00
|
|
|
for (l = 96; l >= MINGAIN; l -= 3) {
|
|
|
|
for (mroll = 0; mroll < 16; mroll ++) {
|
2003-01-20 00:54:24 +00:00
|
|
|
mscale = (1 << mroll) / info->sscale;
|
2003-01-30 16:32:56 +00:00
|
|
|
|
2003-01-20 00:54:24 +00:00
|
|
|
r = (mscale * info->sscale * 100) >> mroll;
|
|
|
|
if (r > l && r <= 100) {
|
|
|
|
info->mscale = mscale;
|
|
|
|
info->mroll = mroll;
|
|
|
|
RATE_TRACE("Converting %d to %d with "
|
|
|
|
"mscale = %d and mroll = %d "
|
|
|
|
"(gain = %d / 100)\n",
|
|
|
|
info->src, info->dst,
|
|
|
|
info->mscale, info->mroll, r);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-01-30 16:32:56 +00:00
|
|
|
|
|
|
|
RATE_TRACE("Failed to find a converter within %d%% gain for "
|
|
|
|
"%d to %d.\n", l, info->src, info->dst);
|
2003-01-20 00:54:24 +00:00
|
|
|
|
2003-01-30 16:32:56 +00:00
|
|
|
return -2;
|
2001-04-08 20:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
feed_rate_set(struct pcm_feeder *f, int what, int value)
|
|
|
|
{
|
|
|
|
struct feed_rate_info *info = f->data;
|
2003-01-30 16:32:56 +00:00
|
|
|
int rvalue;
|
|
|
|
|
|
|
|
if (value < RATEMIN || value > RATEMAX) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rvalue = (value / ROUNDHZ) * ROUNDHZ;
|
|
|
|
if (value - rvalue > ROUNDHZ / 2) {
|
|
|
|
rvalue += ROUNDHZ;
|
|
|
|
}
|
|
|
|
|
2001-04-08 20:26:22 +00:00
|
|
|
switch(what) {
|
|
|
|
case FEEDRATE_SRC:
|
2003-01-30 16:32:56 +00:00
|
|
|
info->src = rvalue;
|
2001-04-08 20:26:22 +00:00
|
|
|
break;
|
|
|
|
case FEEDRATE_DST:
|
2003-01-30 16:32:56 +00:00
|
|
|
info->dst = rvalue;
|
2001-04-08 20:26:22 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
2003-01-20 00:54:24 +00:00
|
|
|
|
2001-04-08 20:26:22 +00:00
|
|
|
return feed_rate_setup(f);
|
|
|
|
}
|
|
|
|
|
2001-05-27 14:49:14 +00:00
|
|
|
static int
|
|
|
|
feed_rate_get(struct pcm_feeder *f, int what)
|
|
|
|
{
|
|
|
|
struct feed_rate_info *info = f->data;
|
|
|
|
|
|
|
|
switch(what) {
|
|
|
|
case FEEDRATE_SRC:
|
|
|
|
return info->src;
|
|
|
|
case FEEDRATE_DST:
|
|
|
|
return info->dst;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-04-08 20:26:22 +00:00
|
|
|
static int
|
|
|
|
feed_rate_init(struct pcm_feeder *f)
|
|
|
|
{
|
|
|
|
struct feed_rate_info *info;
|
|
|
|
|
2003-03-05 14:48:28 +00:00
|
|
|
info = malloc(sizeof(*info), M_RATEFEEDER, M_NOWAIT | M_ZERO);
|
2001-04-08 20:26:22 +00:00
|
|
|
info->src = DSP_DEFAULT_SPEED;
|
|
|
|
info->dst = DSP_DEFAULT_SPEED;
|
2003-01-30 16:32:56 +00:00
|
|
|
info->channels = 2;
|
2003-01-20 00:54:24 +00:00
|
|
|
|
2001-04-08 20:26:22 +00:00
|
|
|
f->data = info;
|
2003-01-30 16:32:56 +00:00
|
|
|
return 0;
|
2001-04-08 20:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
feed_rate_free(struct pcm_feeder *f)
|
|
|
|
{
|
|
|
|
struct feed_rate_info *info = f->data;
|
|
|
|
|
|
|
|
if (info) {
|
|
|
|
free(info, M_RATEFEEDER);
|
|
|
|
}
|
|
|
|
f->data = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-01-20 00:54:24 +00:00
|
|
|
convert_stereo_up(struct feed_rate_info *info,
|
2003-01-30 16:32:56 +00:00
|
|
|
uint32_t src_ticks,
|
|
|
|
uint32_t dst_ticks,
|
|
|
|
int16_t *dst)
|
2001-04-08 20:26:22 +00:00
|
|
|
{
|
2003-01-20 00:54:24 +00:00
|
|
|
uint32_t max_dst_ticks;
|
|
|
|
int32_t alpha, dalpha, malpha, mroll, sp, dp, se, de, x, o;
|
|
|
|
int16_t *src;
|
|
|
|
|
|
|
|
sp = info->buffer_pos * 2;
|
|
|
|
se = sp + src_ticks * 2;
|
|
|
|
|
|
|
|
src = info->buffer;
|
|
|
|
alpha = info->alpha * info->mscale;
|
|
|
|
dalpha = info->dscale * info->mscale; /* Alpha increment */
|
|
|
|
malpha = info->sscale * info->mscale; /* Maximum allowed alpha value */
|
|
|
|
mroll = info->mroll;
|
2001-04-08 20:26:22 +00:00
|
|
|
|
|
|
|
/*
|
2003-01-20 00:54:24 +00:00
|
|
|
* For efficiency the main conversion loop should only depend on
|
|
|
|
* one variable. We use the state to work out the maximum number
|
|
|
|
* of output samples that are available and eliminate the checking of
|
|
|
|
* sp from the loop.
|
2001-04-08 20:26:22 +00:00
|
|
|
*/
|
2003-01-20 00:54:24 +00:00
|
|
|
max_dst_ticks = src_ticks * info->dst / info->src - alpha / dalpha;
|
|
|
|
if (max_dst_ticks < dst_ticks) {
|
|
|
|
dst_ticks = max_dst_ticks;
|
|
|
|
}
|
2001-04-08 20:26:22 +00:00
|
|
|
|
2003-01-20 00:54:24 +00:00
|
|
|
dp = 0;
|
|
|
|
de = dst_ticks * 2;
|
2001-04-08 20:26:22 +00:00
|
|
|
/*
|
2003-01-30 16:32:56 +00:00
|
|
|
* Unrolling this loop manually does not help much here because
|
2003-01-20 00:54:24 +00:00
|
|
|
* of the alpha, malpha comparison.
|
2001-04-08 20:26:22 +00:00
|
|
|
*/
|
2003-01-20 00:54:24 +00:00
|
|
|
while (dp < de) {
|
|
|
|
o = malpha - alpha;
|
|
|
|
x = alpha * src[sp + 2] + o * src[sp];
|
|
|
|
dst[dp++] = x >> mroll;
|
|
|
|
x = alpha * src[sp + 3] + o * src[sp + 1];
|
|
|
|
dst[dp++] = x >> mroll;
|
|
|
|
alpha += dalpha;
|
|
|
|
if (alpha >= malpha) {
|
|
|
|
alpha -= malpha;
|
|
|
|
sp += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RATE_ASSERT(sp <= se, ("%s: Source overrun\n", __func__));
|
|
|
|
|
|
|
|
info->buffer_pos = sp / info->channels;
|
|
|
|
info->alpha = alpha / info->mscale;
|
|
|
|
|
|
|
|
return dp / info->channels;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
convert_stereo_down(struct feed_rate_info *info,
|
2003-01-30 16:32:56 +00:00
|
|
|
uint32_t src_ticks,
|
|
|
|
uint32_t dst_ticks,
|
|
|
|
int16_t *dst)
|
2003-01-20 00:54:24 +00:00
|
|
|
{
|
|
|
|
int32_t alpha, dalpha, malpha, mroll, sp, dp, se, de, x, o, m,
|
|
|
|
mdalpha, mstep;
|
|
|
|
int16_t *src;
|
|
|
|
|
|
|
|
sp = info->buffer_pos * 2;
|
|
|
|
se = sp + src_ticks * 2;
|
|
|
|
|
|
|
|
src = info->buffer;
|
|
|
|
alpha = info->alpha * info->mscale;
|
|
|
|
dalpha = info->dscale * info->mscale; /* Alpha increment */
|
|
|
|
malpha = info->sscale * info->mscale; /* Maximum allowed alpha value */
|
|
|
|
mroll = info->mroll;
|
|
|
|
|
|
|
|
dp = 0;
|
|
|
|
de = dst_ticks * 2;
|
|
|
|
|
|
|
|
m = dalpha / malpha;
|
|
|
|
mstep = m * 2;
|
|
|
|
mdalpha = dalpha - m * malpha;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO: eliminate sp or dp from this loop comparison for a few
|
|
|
|
* extra % performance.
|
|
|
|
*/
|
|
|
|
while (sp < se && dp < de) {
|
|
|
|
o = malpha - alpha;
|
|
|
|
x = alpha * src[sp + 2] + o * src[sp];
|
|
|
|
dst[dp++] = x >> mroll;
|
|
|
|
x = alpha * src[sp + 3] + o * src[sp + 1];
|
|
|
|
dst[dp++] = x >> mroll;
|
|
|
|
|
|
|
|
alpha += mdalpha;
|
|
|
|
sp += mstep;
|
|
|
|
if (alpha >= malpha) {
|
|
|
|
alpha -= malpha;
|
|
|
|
sp += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-30 16:32:56 +00:00
|
|
|
info->buffer_pos = sp / 2;
|
2003-01-20 00:54:24 +00:00
|
|
|
info->alpha = alpha / info->mscale;
|
|
|
|
|
|
|
|
RATE_ASSERT(info->buffer_pos <= info->buffer_ticks,
|
|
|
|
("%s: Source overrun\n", __func__));
|
|
|
|
|
2003-01-30 16:32:56 +00:00
|
|
|
return dp / 2;
|
2003-01-20 00:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
feed_rate(struct pcm_feeder *f,
|
|
|
|
struct pcm_channel *c,
|
|
|
|
uint8_t *b,
|
|
|
|
uint32_t count,
|
|
|
|
void *source)
|
|
|
|
{
|
|
|
|
struct feed_rate_info *info = f->data;
|
|
|
|
|
|
|
|
uint32_t done, s_ticks, d_ticks;
|
|
|
|
done = 0;
|
2001-04-08 20:26:22 +00:00
|
|
|
|
2003-01-20 00:54:24 +00:00
|
|
|
RATE_ASSERT(info->channels == 2,
|
|
|
|
("%s: channels (%d) != 2", __func__, info->channels));
|
|
|
|
|
|
|
|
while (done < count) {
|
|
|
|
/* Slurp in more data if input buffer is not full */
|
2003-01-30 16:32:56 +00:00
|
|
|
while (info->buffer_ticks < src_ticks_per_cycle(info)) {
|
2003-01-20 00:54:24 +00:00
|
|
|
uint8_t *u8b;
|
|
|
|
int fetch;
|
|
|
|
fetch = src_bytes_per_cycle(info) -
|
|
|
|
info->buffer_ticks * bytes_per_tick(info);
|
|
|
|
u8b = (uint8_t*)info->buffer +
|
|
|
|
(info->buffer_ticks + 1) *
|
|
|
|
bytes_per_tick(info);
|
|
|
|
fetch = FEEDER_FEED(f->source, c, u8b, fetch, source);
|
|
|
|
RATE_ASSERT(fetch % bytes_per_tick(info) == 0,
|
|
|
|
("%s: fetched unaligned bytes (%d)",
|
|
|
|
__func__, fetch));
|
|
|
|
info->buffer_ticks += fetch / bytes_per_tick(info);
|
|
|
|
RATE_ASSERT(src_ticks_per_cycle(info) >=
|
|
|
|
info->buffer_ticks,
|
|
|
|
("%s: buffer overfilled (%d > %d).",
|
|
|
|
__func__, info->buffer_ticks,
|
|
|
|
src_ticks_per_cycle(info)));
|
2003-01-30 16:32:56 +00:00
|
|
|
if (fetch == 0)
|
2003-01-20 00:54:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find amount of input buffer data that should be processed */
|
|
|
|
d_ticks = (count - done) / bytes_per_tick(info);
|
|
|
|
s_ticks = info->buffer_ticks - info->buffer_pos;
|
2003-01-30 16:32:56 +00:00
|
|
|
if (info->buffer_ticks != src_ticks_per_cycle(info)) {
|
|
|
|
if (s_ticks > 8)
|
|
|
|
s_ticks -= 8;
|
|
|
|
else
|
|
|
|
s_ticks = 0;
|
|
|
|
}
|
2003-01-20 00:54:24 +00:00
|
|
|
|
|
|
|
d_ticks = info->convert(info, s_ticks, d_ticks,
|
2003-01-30 16:32:56 +00:00
|
|
|
(int16_t*)(b + done));
|
2003-01-20 00:54:24 +00:00
|
|
|
if (d_ticks == 0)
|
|
|
|
break;
|
|
|
|
done += d_ticks * bytes_per_tick(info);
|
|
|
|
|
|
|
|
RATE_ASSERT(info->buffer_pos <= info->buffer_ticks,
|
|
|
|
("%s: buffer_ticks too big\n", __func__));
|
2003-02-06 17:32:02 +00:00
|
|
|
RATE_ASSERT(info->buffer_ticks <= src_ticks_per_cycle(info),
|
|
|
|
("too many ticks %d / %d\n",
|
|
|
|
info->buffer_ticks, src_ticks_per_cycle(info)));
|
|
|
|
RATE_TRACE("%s: ticks %5d / %d pos %d\n", __func__,
|
|
|
|
info->buffer_ticks, src_ticks_per_cycle(info),
|
|
|
|
info->buffer_pos);
|
2003-01-20 00:54:24 +00:00
|
|
|
|
|
|
|
if (src_ticks_per_cycle(info) <= info->buffer_pos) {
|
|
|
|
/* End of cycle reached, copy last samples to start */
|
|
|
|
uint8_t *u8b;
|
|
|
|
u8b = (uint8_t*)info->buffer;
|
|
|
|
bcopy(u8b + src_bytes_per_cycle(info), u8b,
|
|
|
|
bytes_per_tick(info));
|
|
|
|
|
|
|
|
RATE_ASSERT(info->alpha == 0,
|
2003-01-30 16:32:56 +00:00
|
|
|
("%s: completed cycle with "
|
|
|
|
"alpha non-zero", __func__, info->alpha));
|
2003-01-20 00:54:24 +00:00
|
|
|
|
|
|
|
info->buffer_pos = 0;
|
|
|
|
info->buffer_ticks = 0;
|
|
|
|
}
|
2001-04-08 20:26:22 +00:00
|
|
|
}
|
2003-01-20 00:54:24 +00:00
|
|
|
|
|
|
|
RATE_ASSERT(count >= done,
|
|
|
|
("%s: generated too many bytes of data (%d > %d).",
|
|
|
|
__func__, done, count));
|
2001-04-08 20:26:22 +00:00
|
|
|
|
2003-01-30 16:32:56 +00:00
|
|
|
if (done != count) {
|
|
|
|
RATE_TRACE("Only did %d of %d\n", done, count);
|
|
|
|
}
|
|
|
|
|
2003-01-20 00:54:24 +00:00
|
|
|
return done;
|
2001-04-08 20:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct pcm_feederdesc feeder_rate_desc[] = {
|
|
|
|
{FEEDER_RATE, AFMT_S16_LE | AFMT_STEREO, AFMT_S16_LE | AFMT_STEREO, 0},
|
2003-02-06 17:32:02 +00:00
|
|
|
{0, 0, 0, 0},
|
2001-04-08 20:26:22 +00:00
|
|
|
};
|
|
|
|
static kobj_method_t feeder_rate_methods[] = {
|
|
|
|
KOBJMETHOD(feeder_init, feed_rate_init),
|
|
|
|
KOBJMETHOD(feeder_free, feed_rate_free),
|
|
|
|
KOBJMETHOD(feeder_set, feed_rate_set),
|
2001-05-27 14:49:14 +00:00
|
|
|
KOBJMETHOD(feeder_get, feed_rate_get),
|
2001-04-08 20:26:22 +00:00
|
|
|
KOBJMETHOD(feeder_feed, feed_rate),
|
2003-02-06 17:32:02 +00:00
|
|
|
{0, 0}
|
2001-04-08 20:26:22 +00:00
|
|
|
};
|
|
|
|
FEEDER_DECLARE(feeder_rate, 2, NULL);
|
|
|
|
|