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> * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
* *
@ -174,11 +174,11 @@ done:
void void
aparams_init(struct aparams *par, unsigned cmin, unsigned cmax, unsigned rate) aparams_init(struct aparams *par, unsigned cmin, unsigned cmax, unsigned rate)
{ {
par->bps = 2; /* 2 bytes per sample */ par->bps = sizeof(adata_t);
par->bits = 16; /* 16 significant bits per sample */ par->bits = ADATA_BITS;
par->sig = 1; /* samples are signed */ par->sig = 1;
par->le = NATIVE_LE; par->le = NATIVE_LE;
par->msb = 1; /* msb justified */ par->msb = ADATA_MSB;
par->cmin = cmin; par->cmin = cmin;
par->cmax = cmax; par->cmax = cmax;
par->rate = rate; 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> * 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 * numbers, so that we can do all multiplications and divisions in
* 32-bit precision without having to deal with overflows. * 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_BITS - 1))
#define ADATA_UNIT (1 << ADATA_SHIFT)
#define ADATA_MAX (ADATA_UNIT - 1) #define ADATA_MAX (ADATA_UNIT - 1)
#define ADATA_MUL(x,y) (((x) * (y)) >> ADATA_SHIFT)
#define MIDI_MAXCTL 127 #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 int aparams_ctltovol[128];
extern struct aparams aparams_none; extern struct aparams aparams_none;

View File

@ -583,13 +583,13 @@ mix_drop(struct abuf *buf, int extra)
void void
mix_bzero(struct abuf *obuf, unsigned maxtodo) mix_bzero(struct abuf *obuf, unsigned maxtodo)
{ {
short *odata; adata_t *odata;
unsigned ocount, todo; unsigned ocount, todo;
if (obuf->w.mix.todo >= maxtodo) if (obuf->w.mix.todo >= maxtodo)
return; return;
todo = maxtodo - obuf->w.mix.todo; 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) if (ocount > todo)
ocount = todo; ocount = todo;
if (ocount == 0) if (ocount == 0)
@ -612,7 +612,7 @@ mix_bzero(struct abuf *obuf, unsigned maxtodo)
unsigned unsigned
mix_badd(struct abuf *ibuf, struct abuf *obuf) mix_badd(struct abuf *ibuf, struct abuf *obuf)
{ {
short *idata, *odata; adata_t *idata, *odata;
unsigned cmin, cmax; unsigned cmin, cmax;
unsigned i, j, cc, istart, inext, onext, ostart; unsigned i, j, cc, istart, inext, onext, ostart;
unsigned scount, icount, ocount; unsigned scount, icount, ocount;
@ -647,21 +647,21 @@ mix_badd(struct abuf *ibuf, struct abuf *obuf)
/* /*
* Calculate the maximum we can read. * Calculate the maximum we can read.
*/ */
idata = (short *)abuf_rgetblk(ibuf, &icount, 0); idata = (adata_t *)abuf_rgetblk(ibuf, &icount, 0);
if (icount == 0) if (icount == 0)
return 0; return 0;
/* /*
* Calculate the maximum we can write. * 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) if (ocount == 0)
return 0; return 0;
scount = (icount < ocount) ? icount : ocount; scount = (icount < ocount) ? icount : ocount;
mix_bzero(obuf, scount + ibuf->r.mix.done); 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; cmin = obuf->cmin > ibuf->cmin ? obuf->cmin : ibuf->cmin;
cmax = obuf->cmax < ibuf->cmax ? obuf->cmax : ibuf->cmax; cmax = obuf->cmax < ibuf->cmax ? obuf->cmax : ibuf->cmax;
ostart = cmin - obuf->cmin; ostart = cmin - obuf->cmin;
@ -673,7 +673,7 @@ mix_badd(struct abuf *ibuf, struct abuf *obuf)
idata += istart; idata += istart;
for (i = scount; i > 0; i--) { for (i = scount; i > 0; i--) {
for (j = cc; j > 0; j--) { for (j = cc; j > 0; j--) {
*odata += (*idata * vol) >> ADATA_SHIFT; *odata += ADATA_MUL(*idata, vol);
idata++; idata++;
odata++; odata++;
} }
@ -1155,7 +1155,7 @@ sub_silence(struct abuf *buf, int extra)
void void
sub_bcopy(struct abuf *ibuf, struct abuf *obuf) sub_bcopy(struct abuf *ibuf, struct abuf *obuf)
{ {
short *idata, *odata; adata_t *idata, *odata;
unsigned cmin, cmax; unsigned cmin, cmax;
unsigned i, j, cc, istart, inext, onext, ostart; unsigned i, j, cc, istart, inext, onext, ostart;
unsigned icount, ocount, scount; unsigned icount, ocount, scount;
@ -1171,10 +1171,10 @@ sub_bcopy(struct abuf *ibuf, struct abuf *obuf)
obuf->w.sub.silence += scount; 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) if (icount == 0)
return; return;
odata = (short *)abuf_wgetblk(obuf, &ocount, 0); odata = (adata_t *)abuf_wgetblk(obuf, &ocount, 0);
if (ocount == 0) if (ocount == 0)
return; return;
cmin = obuf->cmin > ibuf->cmin ? obuf->cmin : ibuf->cmin; 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) resamp_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
{ {
unsigned inch; unsigned inch;
short *idata; adata_t *idata;
unsigned oblksz; unsigned oblksz;
unsigned ifr; unsigned ifr;
unsigned onch; unsigned onch;
int s1, s2, diff; int s, ds, diff;
short *odata; adata_t *odata;
unsigned iblksz; unsigned iblksz;
unsigned ofr; unsigned ofr;
unsigned c; unsigned c;
short *ctxbuf, *ctx; adata_t *ctxbuf, *ctx;
unsigned ctx_start; unsigned ctx_start;
unsigned icount, ocount; unsigned icount, ocount;
/* /*
* Calculate max frames readable at once from the input buffer. * 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; ifr = icount;
odata = (short *)abuf_wgetblk(obuf, &ocount, 0); odata = (adata_t *)abuf_wgetblk(obuf, &ocount, 0);
ofr = ocount; ofr = ocount;
/* /*
@ -1526,10 +1526,10 @@ resamp_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
break; break;
ctx = ctxbuf; ctx = ctxbuf;
for (c = onch; c > 0; c--) { for (c = onch; c > 0; c--) {
s1 = ctx[ctx_start]; s = ctx[ctx_start];
s2 = ctx[ctx_start ^ 1]; ds = ctx[ctx_start ^ 1] - s;
ctx += RESAMP_NCTX; ctx += RESAMP_NCTX;
*odata++ = s1 + (s2 - s1) * diff / (int)oblksz; *odata++ = s + ADATA_MULDIV(ds, diff, oblksz);
} }
diff -= iblksz; diff -= iblksz;
ofr--; ofr--;
@ -1685,7 +1685,7 @@ enc_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
{ {
unsigned nch, scount, icount, ocount; unsigned nch, scount, icount, ocount;
unsigned f; unsigned f;
short *idata; adata_t *idata;
int s; int s;
unsigned oshift; unsigned oshift;
int osigbit; 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. * 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) if (icount == 0)
return; return;
odata = abuf_wgetblk(obuf, &ocount, 0); 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; odata += p->u.conv.bfirst;
for (f = scount * nch; f > 0; f--) { for (f = scount * nch; f > 0; f--) {
s = *idata++; s = *idata++;
s <<= 16; s <<= 32 - ADATA_BITS;
s >>= oshift; s >>= oshift;
s ^= osigbit; s ^= osigbit;
for (i = obps; i > 0; i--) { for (i = obps; i > 0; i--) {
@ -1851,7 +1851,7 @@ dec_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
int isnext; int isnext;
int isigbit; int isigbit;
unsigned ishift; unsigned ishift;
short *odata; adata_t *odata;
/* /*
* Calculate max frames readable at once from the input buffer. * 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); idata = abuf_rgetblk(ibuf, &icount, 0);
if (icount == 0) if (icount == 0)
return; return;
odata = (short *)abuf_wgetblk(obuf, &ocount, 0); odata = (adata_t *)abuf_wgetblk(obuf, &ocount, 0);
if (ocount == 0) if (ocount == 0)
return; return;
scount = (icount < ocount) ? icount : ocount; scount = (icount < ocount) ? icount : ocount;
@ -1898,7 +1898,7 @@ dec_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
idata += isnext; idata += isnext;
s ^= isigbit; s ^= isigbit;
s <<= ishift; s <<= ishift;
s >>= 16; s >>= 32 - ADATA_BITS;
*odata++ = s; *odata++ = s;
} }
@ -2001,19 +2001,19 @@ join_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
{ {
unsigned h, hops; unsigned h, hops;
unsigned inch, inext; unsigned inch, inext;
short *idata; adata_t *idata;
unsigned onch, onext; unsigned onch, onext;
short *odata; adata_t *odata;
int scale; int scale;
unsigned c, f, scount, icount, ocount; unsigned c, f, scount, icount, ocount;
/* /*
* Calculate max frames readable at once from the input buffer. * 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) if (icount == 0)
return; return;
odata = (short *)abuf_wgetblk(obuf, &ocount, 0); odata = (adata_t *)abuf_wgetblk(obuf, &ocount, 0);
if (ocount == 0) if (ocount == 0)
return; return;
scount = icount < ocount ? icount : ocount; scount = icount < ocount ? icount : ocount;
@ -2043,13 +2043,11 @@ join_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
hops--; hops--;
for (f = scount; f > 0; f--) { for (f = scount; f > 0; f--) {
for (c = onch; c > 0; c--) for (c = onch; c > 0; c--)
*odata++ = (*idata++ * scale) *odata++ = ADATA_MUL(*idata++, scale);
>> ADATA_SHIFT;
for (h = hops; h > 0; h--) { for (h = hops; h > 0; h--) {
odata -= onch; odata -= onch;
for (c = onch; c > 0; c--) for (c = onch; c > 0; c--)
*odata++ += (*idata++ * scale) *odata++ += ADATA_MUL(*idata++, scale);
>> ADATA_SHIFT;
} }
idata += inext; 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); struct abuf *obuf = LIST_FIRST(&p->outs);
unsigned scount, icount, ocount; unsigned scount, icount, ocount;
short *idata, *odata; adata_t *idata, *odata;
#ifdef DEBUG #ifdef DEBUG
if (debug_level >= 4) { 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. * Calculate max frames readable at once from the input buffer.
*/ */
idata = (short *)abuf_rgetblk(ibuf, &icount, pos); idata = (adata_t *)abuf_rgetblk(ibuf, &icount, pos);
odata = (short *)abuf_wgetblk(obuf, &ocount, p->u.mon.pending); odata = (adata_t *)abuf_wgetblk(obuf, &ocount, p->u.mon.pending);
scount = (icount < ocount) ? icount : ocount; scount = (icount < ocount) ? icount : ocount;
#ifdef DEBUG #ifdef DEBUG
if (debug_level >= 4) { 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> * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
* *
@ -157,7 +157,7 @@ struct aproc {
struct { struct {
#define RESAMP_NCTX 2 #define RESAMP_NCTX 2
unsigned ctx_start; unsigned ctx_start;
short ctx[NCHAN_MAX * RESAMP_NCTX]; adata_t ctx[NCHAN_MAX * RESAMP_NCTX];
unsigned iblksz, oblksz; unsigned iblksz, oblksz;
int diff; int diff;
int idelta, odelta; /* remainder of resamp_xpos */ 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; (f->opt->rpar.cmax - f->opt->rpar.cmin + 1) : 0;
m->u.cap.rchan = (f->opt->mode & (MODE_PLAY | MODE_REC)) ? m->u.cap.rchan = (f->opt->mode & (MODE_PLAY | MODE_REC)) ?
(f->opt->wpar.cmax - f->opt->wpar.cmin + 1) : 0; (f->opt->wpar.cmax - f->opt->wpar.cmin + 1) : 0;
m->u.cap.bits = sizeof(short) * 8; m->u.cap.bits = ADATA_BITS;
m->u.cap.bps = sizeof(short); m->u.cap.bps = sizeof(adata_t);
f->rstate = SOCK_RRET; f->rstate = SOCK_RRET;
f->rtodo = sizeof(struct amsg); f->rtodo = sizeof(struct amsg);
break; break;