Replace ``short'' by a new ``adata_t'' typedef corresponding to

audio samples and cleanup ADATA_XXX macros. This allows easilly
switching to 24 bit fixed point arithmetic by simply redefining
the adata_t typedef to int and updating ADATA_XXX macros. No
object change.
This commit is contained in:
Alexandre Ratchov 2010-11-04 18:56:53 +01:00
parent 26c9cecb73
commit 4ba7c28c93
5 changed files with 51 additions and 50 deletions

View File

@ -1,4 +1,4 @@
/* $OpenBSD: aparams.c,v 1.10 2010/01/10 21:47:41 ratchov Exp $ */
/* $OpenBSD$ */
/*
* Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
*
@ -174,11 +174,11 @@ done:
void
aparams_init(struct aparams *par, unsigned cmin, unsigned cmax, unsigned rate)
{
par->bps = 2; /* 2 bytes per sample */
par->bits = 16; /* 16 significant bits per sample */
par->sig = 1; /* samples are signed */
par->bps = sizeof(adata_t);
par->bits = ADATA_BITS;
par->sig = 1;
par->le = NATIVE_LE;
par->msb = 1; /* msb justified */
par->msb = ADATA_MSB;
par->cmin = cmin;
par->cmax = cmax;
par->rate = rate;

View File

@ -1,4 +1,4 @@
/* $OpenBSD: aparams.h,v 1.8 2009/09/27 11:51:20 ratchov Exp $ */
/* $OpenBSD$ */
/*
* Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
*
@ -63,14 +63,17 @@ struct aparams {
* numbers, so that we can do all multiplications and divisions in
* 32-bit precision without having to deal with overflows.
*/
typedef short adata_t;
#define ADATA_BITS 16
#define ADATA_MSB 1
#define ADATA_MUL(x,y) (((int)(x) * (int)(y)) >> (ADATA_BITS - 1))
#define ADATA_MULDIV(x,y,z) ((int)(x) * (int)(y) / (int)(z))
#define ADATA_SHIFT (8 * sizeof(short) - 1)
#define ADATA_UNIT (1 << ADATA_SHIFT)
#define ADATA_UNIT (1 << (ADATA_BITS - 1))
#define ADATA_MAX (ADATA_UNIT - 1)
#define ADATA_MUL(x,y) (((x) * (y)) >> ADATA_SHIFT)
#define MIDI_MAXCTL 127
#define MIDI_TO_ADATA(m) (aparams_ctltovol[m])
#define MIDI_TO_ADATA(m) (aparams_ctltovol[m] << (ADATA_BITS - 16))
extern int aparams_ctltovol[128];
extern struct aparams aparams_none;

View File

@ -583,13 +583,13 @@ mix_drop(struct abuf *buf, int extra)
void
mix_bzero(struct abuf *obuf, unsigned maxtodo)
{
short *odata;
adata_t *odata;
unsigned ocount, todo;
if (obuf->w.mix.todo >= maxtodo)
return;
todo = maxtodo - obuf->w.mix.todo;
odata = (short *)abuf_wgetblk(obuf, &ocount, obuf->w.mix.todo);
odata = (adata_t *)abuf_wgetblk(obuf, &ocount, obuf->w.mix.todo);
if (ocount > todo)
ocount = todo;
if (ocount == 0)
@ -612,7 +612,7 @@ mix_bzero(struct abuf *obuf, unsigned maxtodo)
unsigned
mix_badd(struct abuf *ibuf, struct abuf *obuf)
{
short *idata, *odata;
adata_t *idata, *odata;
unsigned cmin, cmax;
unsigned i, j, cc, istart, inext, onext, ostart;
unsigned scount, icount, ocount;
@ -647,21 +647,21 @@ mix_badd(struct abuf *ibuf, struct abuf *obuf)
/*
* Calculate the maximum we can read.
*/
idata = (short *)abuf_rgetblk(ibuf, &icount, 0);
idata = (adata_t *)abuf_rgetblk(ibuf, &icount, 0);
if (icount == 0)
return 0;
/*
* Calculate the maximum we can write.
*/
odata = (short *)abuf_wgetblk(obuf, &ocount, ibuf->r.mix.done);
odata = (adata_t *)abuf_wgetblk(obuf, &ocount, ibuf->r.mix.done);
if (ocount == 0)
return 0;
scount = (icount < ocount) ? icount : ocount;
mix_bzero(obuf, scount + ibuf->r.mix.done);
vol = (ibuf->r.mix.weight * ibuf->r.mix.vol) >> ADATA_SHIFT;
vol = ADATA_MUL(ibuf->r.mix.weight, ibuf->r.mix.vol);
cmin = obuf->cmin > ibuf->cmin ? obuf->cmin : ibuf->cmin;
cmax = obuf->cmax < ibuf->cmax ? obuf->cmax : ibuf->cmax;
ostart = cmin - obuf->cmin;
@ -673,7 +673,7 @@ mix_badd(struct abuf *ibuf, struct abuf *obuf)
idata += istart;
for (i = scount; i > 0; i--) {
for (j = cc; j > 0; j--) {
*odata += (*idata * vol) >> ADATA_SHIFT;
*odata += ADATA_MUL(*idata, vol);
idata++;
odata++;
}
@ -1155,7 +1155,7 @@ sub_silence(struct abuf *buf, int extra)
void
sub_bcopy(struct abuf *ibuf, struct abuf *obuf)
{
short *idata, *odata;
adata_t *idata, *odata;
unsigned cmin, cmax;
unsigned i, j, cc, istart, inext, onext, ostart;
unsigned icount, ocount, scount;
@ -1171,10 +1171,10 @@ sub_bcopy(struct abuf *ibuf, struct abuf *obuf)
obuf->w.sub.silence += scount;
}
idata = (short *)abuf_rgetblk(ibuf, &icount, obuf->w.sub.done);
idata = (adata_t *)abuf_rgetblk(ibuf, &icount, obuf->w.sub.done);
if (icount == 0)
return;
odata = (short *)abuf_wgetblk(obuf, &ocount, 0);
odata = (adata_t *)abuf_wgetblk(obuf, &ocount, 0);
if (ocount == 0)
return;
cmin = obuf->cmin > ibuf->cmin ? obuf->cmin : ibuf->cmin;
@ -1459,26 +1459,26 @@ void
resamp_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
{
unsigned inch;
short *idata;
adata_t *idata;
unsigned oblksz;
unsigned ifr;
unsigned onch;
int s1, s2, diff;
short *odata;
int s, ds, diff;
adata_t *odata;
unsigned iblksz;
unsigned ofr;
unsigned c;
short *ctxbuf, *ctx;
adata_t *ctxbuf, *ctx;
unsigned ctx_start;
unsigned icount, ocount;
/*
* Calculate max frames readable at once from the input buffer.
*/
idata = (short *)abuf_rgetblk(ibuf, &icount, 0);
idata = (adata_t *)abuf_rgetblk(ibuf, &icount, 0);
ifr = icount;
odata = (short *)abuf_wgetblk(obuf, &ocount, 0);
odata = (adata_t *)abuf_wgetblk(obuf, &ocount, 0);
ofr = ocount;
/*
@ -1526,10 +1526,10 @@ resamp_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
break;
ctx = ctxbuf;
for (c = onch; c > 0; c--) {
s1 = ctx[ctx_start];
s2 = ctx[ctx_start ^ 1];
s = ctx[ctx_start];
ds = ctx[ctx_start ^ 1] - s;
ctx += RESAMP_NCTX;
*odata++ = s1 + (s2 - s1) * diff / (int)oblksz;
*odata++ = s + ADATA_MULDIV(ds, diff, oblksz);
}
diff -= iblksz;
ofr--;
@ -1685,7 +1685,7 @@ enc_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
{
unsigned nch, scount, icount, ocount;
unsigned f;
short *idata;
adata_t *idata;
int s;
unsigned oshift;
int osigbit;
@ -1698,7 +1698,7 @@ enc_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
/*
* Calculate max frames readable at once from the input buffer.
*/
idata = (short *)abuf_rgetblk(ibuf, &icount, 0);
idata = (adata_t *)abuf_rgetblk(ibuf, &icount, 0);
if (icount == 0)
return;
odata = abuf_wgetblk(obuf, &ocount, 0);
@ -1733,7 +1733,7 @@ enc_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
odata += p->u.conv.bfirst;
for (f = scount * nch; f > 0; f--) {
s = *idata++;
s <<= 16;
s <<= 32 - ADATA_BITS;
s >>= oshift;
s ^= osigbit;
for (i = obps; i > 0; i--) {
@ -1851,7 +1851,7 @@ dec_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
int isnext;
int isigbit;
unsigned ishift;
short *odata;
adata_t *odata;
/*
* Calculate max frames readable at once from the input buffer.
@ -1859,7 +1859,7 @@ dec_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
idata = abuf_rgetblk(ibuf, &icount, 0);
if (icount == 0)
return;
odata = (short *)abuf_wgetblk(obuf, &ocount, 0);
odata = (adata_t *)abuf_wgetblk(obuf, &ocount, 0);
if (ocount == 0)
return;
scount = (icount < ocount) ? icount : ocount;
@ -1898,7 +1898,7 @@ dec_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
idata += isnext;
s ^= isigbit;
s <<= ishift;
s >>= 16;
s >>= 32 - ADATA_BITS;
*odata++ = s;
}
@ -2001,19 +2001,19 @@ join_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
{
unsigned h, hops;
unsigned inch, inext;
short *idata;
adata_t *idata;
unsigned onch, onext;
short *odata;
adata_t *odata;
int scale;
unsigned c, f, scount, icount, ocount;
/*
* Calculate max frames readable at once from the input buffer.
*/
idata = (short *)abuf_rgetblk(ibuf, &icount, 0);
idata = (adata_t *)abuf_rgetblk(ibuf, &icount, 0);
if (icount == 0)
return;
odata = (short *)abuf_wgetblk(obuf, &ocount, 0);
odata = (adata_t *)abuf_wgetblk(obuf, &ocount, 0);
if (ocount == 0)
return;
scount = icount < ocount ? icount : ocount;
@ -2043,13 +2043,11 @@ join_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
hops--;
for (f = scount; f > 0; f--) {
for (c = onch; c > 0; c--)
*odata++ = (*idata++ * scale)
>> ADATA_SHIFT;
*odata++ = ADATA_MUL(*idata++, scale);
for (h = hops; h > 0; h--) {
odata -= onch;
for (c = onch; c > 0; c--)
*odata++ += (*idata++ * scale)
>> ADATA_SHIFT;
*odata++ += ADATA_MUL(*idata++, scale);
}
idata += inext;
}
@ -2178,7 +2176,7 @@ mon_snoop(struct aproc *p, struct abuf *ibuf, unsigned pos, unsigned todo)
{
struct abuf *obuf = LIST_FIRST(&p->outs);
unsigned scount, icount, ocount;
short *idata, *odata;
adata_t *idata, *odata;
#ifdef DEBUG
if (debug_level >= 4) {
@ -2197,8 +2195,8 @@ mon_snoop(struct aproc *p, struct abuf *ibuf, unsigned pos, unsigned todo)
/*
* Calculate max frames readable at once from the input buffer.
*/
idata = (short *)abuf_rgetblk(ibuf, &icount, pos);
odata = (short *)abuf_wgetblk(obuf, &ocount, p->u.mon.pending);
idata = (adata_t *)abuf_rgetblk(ibuf, &icount, pos);
odata = (adata_t *)abuf_wgetblk(obuf, &ocount, p->u.mon.pending);
scount = (icount < ocount) ? icount : ocount;
#ifdef DEBUG
if (debug_level >= 4) {

View File

@ -1,4 +1,4 @@
/* $OpenBSD: aproc.h,v 1.37 2010/06/04 06:15:28 ratchov Exp $ */
/* $OpenBSD$ */
/*
* Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
*
@ -157,7 +157,7 @@ struct aproc {
struct {
#define RESAMP_NCTX 2
unsigned ctx_start;
short ctx[NCHAN_MAX * RESAMP_NCTX];
adata_t ctx[NCHAN_MAX * RESAMP_NCTX];
unsigned iblksz, oblksz;
int diff;
int idelta, odelta; /* remainder of resamp_xpos */

View File

@ -1329,8 +1329,8 @@ sock_execmsg(struct sock *f)
(f->opt->rpar.cmax - f->opt->rpar.cmin + 1) : 0;
m->u.cap.rchan = (f->opt->mode & (MODE_PLAY | MODE_REC)) ?
(f->opt->wpar.cmax - f->opt->wpar.cmin + 1) : 0;
m->u.cap.bits = sizeof(short) * 8;
m->u.cap.bps = sizeof(short);
m->u.cap.bits = ADATA_BITS;
m->u.cap.bps = sizeof(adata_t);
f->rstate = SOCK_RRET;
f->rtodo = sizeof(struct amsg);
break;