move conversion code dsp.c

This commit is contained in:
Alexandre Ratchov 2014-12-30 19:17:32 +01:00
parent f3ef9df7b4
commit 1602b19c78
5 changed files with 195 additions and 147 deletions

View File

@ -245,7 +245,7 @@ slot_new(char *path, int mode, struct aparams *par, int hdr,
case ENC_ALAW:
log_puts("alaw");
break;
case ENC_F32LE:
case ENC_FLOAT:
log_puts("f32le");
break;
}
@ -308,16 +308,7 @@ slot_init(struct slot *s)
0, dev_pchan - 1,
0, dev_pchan - 1);
if (s->wav.enc != ENC_PCM || !aparams_native(&s->wav.par)) {
switch (s->wav.enc) {
case ENC_PCM:
dec_init(&s->conv, &s->wav.par, slot_nch);
break;
case ENC_ALAW:
case ENC_ULAW:
case ENC_F32LE:
/* nothing */
break;
};
dec_init(&s->conv, &s->wav.par, slot_nch);
s->convbuf =
xmalloc(s->round * slot_nch * sizeof(adata_t));
}
@ -475,13 +466,13 @@ play_filt_dec(struct slot *s, void *in, void *out, int todo)
dec_do(&s->conv, in, tmp, todo);
break;
case ENC_ULAW:
wav_dec_ulaw(in, tmp, todo * s->wav.nch);
dec_do_ulaw(&s->conv, in, tmp, todo);
break;
case ENC_ALAW:
wav_dec_alaw(in, tmp, todo * s->wav.nch);
dec_do_alaw(&s->conv, in, tmp, todo);
break;
case ENC_F32LE:
wav_dec_f32le(in, tmp, todo * s->wav.nch);
case ENC_FLOAT:
dec_do_float(&s->conv, in, tmp, todo);
break;
}
}

View File

@ -38,6 +38,76 @@ int aparams_ctltovol[128] = {
26008, 27029, 28090, 29193, 30339, 31530, 32768
};
short wav_ulawmap[256] = {
-32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956,
-23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764,
-15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412,
-11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316,
-7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
-5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
-3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
-2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
-1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
-1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
-876, -844, -812, -780, -748, -716, -684, -652,
-620, -588, -556, -524, -492, -460, -428, -396,
-372, -356, -340, -324, -308, -292, -276, -260,
-244, -228, -212, -196, -180, -164, -148, -132,
-120, -112, -104, -96, -88, -80, -72, -64,
-56, -48, -40, -32, -24, -16, -8, 0,
32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
876, 844, 812, 780, 748, 716, 684, 652,
620, 588, 556, 524, 492, 460, 428, 396,
372, 356, 340, 324, 308, 292, 276, 260,
244, 228, 212, 196, 180, 164, 148, 132,
120, 112, 104, 96, 88, 80, 72, 64,
56, 48, 40, 32, 24, 16, 8, 0
};
short wav_alawmap[256] = {
-5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
-7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
-2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
-3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
-22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944,
-30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136,
-11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472,
-15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568,
-344, -328, -376, -360, -280, -264, -312, -296,
-472, -456, -504, -488, -408, -392, -440, -424,
-88, -72, -120, -104, -24, -8, -56, -40,
-216, -200, -248, -232, -152, -136, -184, -168,
-1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
-1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
-688, -656, -752, -720, -560, -528, -624, -592,
-944, -912, -1008, -976, -816, -784, -880, -848,
5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
344, 328, 376, 360, 280, 264, 312, 296,
472, 456, 504, 488, 408, 392, 440, 424,
88, 72, 120, 104, 24, 8, 56, 40,
216, 200, 248, 232, 152, 136, 184, 168,
1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
688, 656, 752, 720, 560, 528, 624, 592,
944, 912, 1008, 976, 816, 784, 880, 848
};
/*
* Generate a string corresponding to the encoding in par,
* return the length of the resulting string.
@ -522,6 +592,119 @@ dec_do(struct conv *p, unsigned char *in, unsigned char *out, int todo)
}
}
/*
* convert a 32-bit float to adata_t, clipping to -1:1, boundaries
* excluded
*/
static inline int
f32_to_adata(unsigned int x)
{
unsigned int s, e, m, y;
s = (x >> 31);
e = (x >> 23) & 0xff;
m = (x << 8) | 0x80000000;
if (e < 127 - 24)
y = 0;
else if (e > 127 - 1)
y = ADATA_UNIT - 1;
else
y = m >> (127 + (32 - ADATA_BITS) - e);
return (y ^ -s) + s;
}
/*
* convert samples from little endian ieee 754 floats to adata_t
*/
void
dec_do_float(struct conv *p, unsigned char *in, unsigned char *out, int todo)
{
unsigned int f;
unsigned int i;
unsigned int s = 0xdeadbeef;
unsigned char *idata;
int ibnext;
int isnext;
adata_t *odata;
#ifdef DEBUG
if (log_level >= 4) {
log_puts("dec_float: copying ");
log_putu(todo);
log_puts(" frames\n");
}
#endif
/*
* Partially copy structures into local variables, to avoid
* unnecessary indirections; this also allows the compiler to
* order local variables more "cache-friendly".
*/
idata = in;
odata = (adata_t *)out;
ibnext = p->bnext;
isnext = p->snext;
/*
* Start conversion.
*/
idata += p->bfirst;
for (f = todo * p->nch; f > 0; f--) {
for (i = 4; i > 0; i--) {
s <<= 8;
s |= *idata;
idata += ibnext;
}
idata += isnext;
*odata++ = f32_to_adata(s);
}
}
/*
* convert samples from ulaw to adata_t
*/
void
dec_do_ulaw(struct conv *p, unsigned char *in, unsigned char *out, int todo)
{
unsigned int f;
unsigned char *idata;
adata_t *odata;
#ifdef DEBUG
if (log_level >= 4) {
log_puts("dec_ulaw: copying ");
log_putu(todo);
log_puts(" frames\n");
}
#endif
idata = in;
odata = (adata_t *)out;
for (f = todo * p->nch; f > 0; f--)
*odata++ = wav_ulawmap[*idata++] << (ADATA_BITS - 16);
}
/*
* convert samples from alaw to adata_t
*/
void
dec_do_alaw(struct conv *p, unsigned char *in, unsigned char *out, int todo)
{
unsigned int f;
unsigned char *idata;
adata_t *odata;
#ifdef DEBUG
if (log_level >= 4) {
log_puts("dec_alaw: copying ");
log_putu(todo);
log_puts(" frames\n");
}
#endif
idata = in;
odata = (adata_t *)out;
for (f = todo * p->nch; f > 0; f--)
*odata++ = wav_alawmap[*idata++] << (ADATA_BITS - 16);
}
/*
* initialize decoder from foreign to native encoding
*/

View File

@ -153,6 +153,9 @@ void enc_do(struct conv *, unsigned char *, unsigned char *, int);
void enc_sil_do(struct conv *, unsigned char *, int);
void enc_init(struct conv *, struct aparams *, int);
void dec_do(struct conv *, unsigned char *, unsigned char *, int);
void dec_do_float(struct conv *, unsigned char *, unsigned char *, int);
void dec_do_alaw(struct conv *, unsigned char *, unsigned char *, int);
void dec_do_ulaw(struct conv *, unsigned char *, unsigned char *, int);
void dec_init(struct conv *, struct aparams *, int);
void cmap_add(struct cmap *, void *, void *, int, int);
void cmap_copy(struct cmap *, void *, void *, int, int);

View File

@ -90,76 +90,6 @@ char wav_guid[14] = {
0x9B, 0x71
};
short wav_ulawmap[256] = {
-32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956,
-23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764,
-15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412,
-11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316,
-7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
-5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
-3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
-2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
-1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
-1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
-876, -844, -812, -780, -748, -716, -684, -652,
-620, -588, -556, -524, -492, -460, -428, -396,
-372, -356, -340, -324, -308, -292, -276, -260,
-244, -228, -212, -196, -180, -164, -148, -132,
-120, -112, -104, -96, -88, -80, -72, -64,
-56, -48, -40, -32, -24, -16, -8, 0,
32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
876, 844, 812, 780, 748, 716, 684, 652,
620, 588, 556, 524, 492, 460, 428, 396,
372, 356, 340, 324, 308, 292, 276, 260,
244, 228, 212, 196, 180, 164, 148, 132,
120, 112, 104, 96, 88, 80, 72, 64,
56, 48, 40, 32, 24, 16, 8, 0
};
short wav_alawmap[256] = {
-5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
-7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
-2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
-3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
-22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944,
-30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136,
-11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472,
-15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568,
-344, -328, -376, -360, -280, -264, -312, -296,
-472, -456, -504, -488, -408, -392, -440, -424,
-88, -72, -120, -104, -24, -8, -56, -40,
-216, -200, -248, -232, -152, -136, -184, -168,
-1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
-1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
-688, -656, -752, -720, -560, -528, -624, -592,
-944, -912, -1008, -976, -816, -784, -880, -848,
5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
344, 328, 376, 360, 280, 264, 312, 296,
472, 456, 504, 488, 408, 392, 440, 424,
88, 72, 120, 104, 24, 8, 56, 40,
216, 200, 248, 232, 152, 136, 184, 168,
1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
688, 656, 752, 720, 560, 528, 624, 592,
944, 912, 1008, 976, 816, 784, 880, 848
};
static inline unsigned int
le16_get(le16_t *p)
{
@ -191,65 +121,6 @@ le32_set(le32_t *p, unsigned int v)
p->ld[3] = v >> 24;
}
/*
* convert a 32-bit float to adata_t, clipping to -1:1, boundaries
* excluded
*/
static inline int
f32_to_adata(unsigned int x)
{
unsigned int s, e, m, y;
s = (x >> 31);
e = (x >> 23) & 0xff;
m = (x << 8) | 0x80000000;
if (e < 127 - 24)
y = 0;
else if (e > 127 - 1)
y = ADATA_UNIT - 1;
else
y = m >> (127 + (32 - ADATA_BITS) - e);
return (y ^ -s) + s;
}
/*
* convert samples from little endian ieee 754 floats to adata_t
*/
void
wav_dec_f32le(unsigned char *in, adata_t *out, int count)
{
while (count > 0) {
*out = f32_to_adata(le32_get((le32_t *)in));
in += 4;
out++;
count--;
}
}
/*
* convert samples from ulaw to adata_t
*/
void
wav_dec_ulaw(unsigned char *in, adata_t *out, int count)
{
while (count > 0) {
*out++ = (adata_t)(wav_ulawmap[*in++]) << (ADATA_BITS - 16);
count--;
}
}
/*
* convert samples from alaw to adata_t
*/
void
wav_dec_alaw(unsigned char *in, adata_t *out, int count)
{
while (count > 0) {
*out++ = (adata_t)(wav_alawmap[*in++]) << (ADATA_BITS - 16);
count--;
}
}
static int
wav_readfmt(struct wav *w, unsigned int csize)
{
@ -326,7 +197,7 @@ wav_readfmt(struct wav *w, unsigned int csize)
w->par.msb = 0;
break;
case WAV_FMT_FLOAT:
w->enc = ENC_F32LE;
w->enc = ENC_FLOAT;
if (bits != 32) {
log_puts("only 32-bit float supported\n");
return 0;
@ -334,7 +205,7 @@ wav_readfmt(struct wav *w, unsigned int csize)
w->par.bits = 32;
w->par.bps = 4;
w->par.le = 1;
w->par.sig = 1;
w->par.sig = 0;
w->par.msb = 0;
break;
default:

View File

@ -25,7 +25,7 @@ struct wav {
#define ENC_PCM 0 /* simple integers (fixed point) */
#define ENC_ULAW 1 /* 8-bit mu-law */
#define ENC_ALAW 2 /* 8-bit a-law */
#define ENC_F32LE 3 /* IEEE 754 32-bit floats */
#define ENC_FLOAT 3 /* IEEE 754 32-bit floats */
int enc; /* one of above */
int rate; /* file sample rate */
int nch; /* file channel count */