summaryrefslogtreecommitdiff
path: root/usr.bin/aucat
diff options
context:
space:
mode:
authorAlexandre Ratchov <ratchov@cvs.openbsd.org>2009-04-22 10:57:34 +0000
committerAlexandre Ratchov <ratchov@cvs.openbsd.org>2009-04-22 10:57:34 +0000
commit009e58b17d67ab9736f3fa294d094c55c22e324a (patch)
tree9e5d1037f1ab177c688cbaac3122bc0150cafa14 /usr.bin/aucat
parent4a5083a2aa15ee1e8d3f57df7a35ef1eca35749b (diff)
move support for ulaw/alaw encoding in .wav files from legacy mode
to normal mode. ok jakemsr@
Diffstat (limited to 'usr.bin/aucat')
-rw-r--r--usr.bin/aucat/headers.c52
-rw-r--r--usr.bin/aucat/legacy.c200
-rw-r--r--usr.bin/aucat/wav.c99
-rw-r--r--usr.bin/aucat/wav.h9
4 files changed, 150 insertions, 210 deletions
diff --git a/usr.bin/aucat/headers.c b/usr.bin/aucat/headers.c
index fd8ae9b954e..8b62a3e49bf 100644
--- a/usr.bin/aucat/headers.c
+++ b/usr.bin/aucat/headers.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: headers.c,v 1.5 2009/04/11 10:24:21 jakemsr Exp $ */
+/* $OpenBSD: headers.c,v 1.6 2009/04/22 10:57:33 ratchov Exp $ */
/*
* Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
*
@@ -25,6 +25,14 @@
#include "conf.h"
#include "aparams.h"
+#include "wav.h"
+
+/*
+ * encoding IDs used in .wav headers
+ */
+#define WAV_ENC_PCM 1
+#define WAV_ENC_ALAW 6
+#define WAV_ENC_ULAW 7
struct wavriff {
char magic[4];
@@ -52,7 +60,7 @@ char wav_id_data[4] = { 'd', 'a', 't', 'a' };
char wav_id_fmt[4] = { 'f', 'm', 't', ' ' };
int
-wav_readfmt(int fd, unsigned csize, struct aparams *par, int *renc)
+wav_readfmt(int fd, unsigned csize, struct aparams *par, short **map)
{
struct wavfmt fmt;
unsigned nch, cmax, rate, bits, enc;
@@ -66,9 +74,18 @@ wav_readfmt(int fd, unsigned csize, struct aparams *par, int *renc)
return 0;
}
enc = letoh16(fmt.fmt);
- if (renc == NULL && enc != 1) {
- warnx("%u: only \"pcm\" encoding supported", enc);
- return 0;
+ switch (enc) {
+ case WAV_ENC_PCM:
+ *map = NULL;
+ break;
+ case WAV_ENC_ALAW:
+ *map = wav_alawmap;
+ break;
+ case WAV_ENC_ULAW:
+ *map = wav_ulawmap;
+ break;
+ default:
+ errx(1, "%u: unsupported encoding in .wav file", enc);
}
nch = letoh16(fmt.nch);
if (nch == 0) {
@@ -90,15 +107,24 @@ wav_readfmt(int fd, unsigned csize, struct aparams *par, int *renc)
warnx("%u: bad number of bits", bits);
return 0;
}
- par->bps = (bits + 7) / 8;
- par->bits = bits;
- par->le = 1;
- par->sig = (bits <= 8) ? 0 : 1; /* ask microsoft why... */
+ if (enc == WAV_ENC_PCM) {
+ par->bps = (bits + 7) / 8;
+ par->bits = bits;
+ par->le = 1;
+ par->sig = (bits <= 8) ? 0 : 1; /* ask microsoft why... */
+ } else {
+ if (bits != 8) {
+ warnx("%u: mulaw/alaw encoding not 8-bit", bits);
+ return 0;
+ }
+ par->bits = 8 * sizeof(short);
+ par->bps = sizeof(short);
+ par->le = NATIVE_LE;
+ par->sig = 1;
+ }
par->msb = 1;
par->cmax = cmax;
par->rate = rate;
- if (renc)
- *renc = enc;
#ifdef DEBUG
if (debug_level > 0) {
fprintf(stderr, "wav_readfmt: using ");
@@ -110,7 +136,7 @@ wav_readfmt(int fd, unsigned csize, struct aparams *par, int *renc)
}
int
-wav_readhdr(int fd, struct aparams *par, off_t *datasz, int *renc)
+wav_readhdr(int fd, struct aparams *par, off_t *datasz, short **map)
{
struct wavriff riff;
struct wavchunk chunk;
@@ -138,7 +164,7 @@ wav_readhdr(int fd, struct aparams *par, off_t *datasz, int *renc)
}
csize = letoh32(chunk.size);
if (memcmp(chunk.id, wav_id_fmt, 4) == 0) {
- if (!wav_readfmt(fd, csize, par, renc))
+ if (!wav_readfmt(fd, csize, par, map))
return 0;
fmt_done = 1;
} else if (memcmp(chunk.id, wav_id_data, 4) == 0) {
diff --git a/usr.bin/aucat/legacy.c b/usr.bin/aucat/legacy.c
index 7213e8d9652..b8bfc8121c5 100644
--- a/usr.bin/aucat/legacy.c
+++ b/usr.bin/aucat/legacy.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: legacy.c,v 1.5 2009/04/11 10:24:21 jakemsr Exp $ */
+/* $OpenBSD: legacy.c,v 1.6 2009/04/22 10:57:33 ratchov Exp $ */
/*
* Copyright (c) 1997 Kenneth Stailey. All rights reserved.
*
@@ -38,176 +38,6 @@
#include "wav.h"
-/*
- * This table converts a (8 bit) mu-law value two a 16 bit value.
- * The 16 bits are represented as an array of two bytes for easier access
- * to the individual bytes.
- */
-const u_char mulawtolin16[256][2] = {
- {0x02,0x84}, {0x06,0x84}, {0x0a,0x84}, {0x0e,0x84},
- {0x12,0x84}, {0x16,0x84}, {0x1a,0x84}, {0x1e,0x84},
- {0x22,0x84}, {0x26,0x84}, {0x2a,0x84}, {0x2e,0x84},
- {0x32,0x84}, {0x36,0x84}, {0x3a,0x84}, {0x3e,0x84},
- {0x41,0x84}, {0x43,0x84}, {0x45,0x84}, {0x47,0x84},
- {0x49,0x84}, {0x4b,0x84}, {0x4d,0x84}, {0x4f,0x84},
- {0x51,0x84}, {0x53,0x84}, {0x55,0x84}, {0x57,0x84},
- {0x59,0x84}, {0x5b,0x84}, {0x5d,0x84}, {0x5f,0x84},
- {0x61,0x04}, {0x62,0x04}, {0x63,0x04}, {0x64,0x04},
- {0x65,0x04}, {0x66,0x04}, {0x67,0x04}, {0x68,0x04},
- {0x69,0x04}, {0x6a,0x04}, {0x6b,0x04}, {0x6c,0x04},
- {0x6d,0x04}, {0x6e,0x04}, {0x6f,0x04}, {0x70,0x04},
- {0x70,0xc4}, {0x71,0x44}, {0x71,0xc4}, {0x72,0x44},
- {0x72,0xc4}, {0x73,0x44}, {0x73,0xc4}, {0x74,0x44},
- {0x74,0xc4}, {0x75,0x44}, {0x75,0xc4}, {0x76,0x44},
- {0x76,0xc4}, {0x77,0x44}, {0x77,0xc4}, {0x78,0x44},
- {0x78,0xa4}, {0x78,0xe4}, {0x79,0x24}, {0x79,0x64},
- {0x79,0xa4}, {0x79,0xe4}, {0x7a,0x24}, {0x7a,0x64},
- {0x7a,0xa4}, {0x7a,0xe4}, {0x7b,0x24}, {0x7b,0x64},
- {0x7b,0xa4}, {0x7b,0xe4}, {0x7c,0x24}, {0x7c,0x64},
- {0x7c,0x94}, {0x7c,0xb4}, {0x7c,0xd4}, {0x7c,0xf4},
- {0x7d,0x14}, {0x7d,0x34}, {0x7d,0x54}, {0x7d,0x74},
- {0x7d,0x94}, {0x7d,0xb4}, {0x7d,0xd4}, {0x7d,0xf4},
- {0x7e,0x14}, {0x7e,0x34}, {0x7e,0x54}, {0x7e,0x74},
- {0x7e,0x8c}, {0x7e,0x9c}, {0x7e,0xac}, {0x7e,0xbc},
- {0x7e,0xcc}, {0x7e,0xdc}, {0x7e,0xec}, {0x7e,0xfc},
- {0x7f,0x0c}, {0x7f,0x1c}, {0x7f,0x2c}, {0x7f,0x3c},
- {0x7f,0x4c}, {0x7f,0x5c}, {0x7f,0x6c}, {0x7f,0x7c},
- {0x7f,0x88}, {0x7f,0x90}, {0x7f,0x98}, {0x7f,0xa0},
- {0x7f,0xa8}, {0x7f,0xb0}, {0x7f,0xb8}, {0x7f,0xc0},
- {0x7f,0xc8}, {0x7f,0xd0}, {0x7f,0xd8}, {0x7f,0xe0},
- {0x7f,0xe8}, {0x7f,0xf0}, {0x7f,0xf8}, {0x80,0x00},
- {0xfd,0x7c}, {0xf9,0x7c}, {0xf5,0x7c}, {0xf1,0x7c},
- {0xed,0x7c}, {0xe9,0x7c}, {0xe5,0x7c}, {0xe1,0x7c},
- {0xdd,0x7c}, {0xd9,0x7c}, {0xd5,0x7c}, {0xd1,0x7c},
- {0xcd,0x7c}, {0xc9,0x7c}, {0xc5,0x7c}, {0xc1,0x7c},
- {0xbe,0x7c}, {0xbc,0x7c}, {0xba,0x7c}, {0xb8,0x7c},
- {0xb6,0x7c}, {0xb4,0x7c}, {0xb2,0x7c}, {0xb0,0x7c},
- {0xae,0x7c}, {0xac,0x7c}, {0xaa,0x7c}, {0xa8,0x7c},
- {0xa6,0x7c}, {0xa4,0x7c}, {0xa2,0x7c}, {0xa0,0x7c},
- {0x9e,0xfc}, {0x9d,0xfc}, {0x9c,0xfc}, {0x9b,0xfc},
- {0x9a,0xfc}, {0x99,0xfc}, {0x98,0xfc}, {0x97,0xfc},
- {0x96,0xfc}, {0x95,0xfc}, {0x94,0xfc}, {0x93,0xfc},
- {0x92,0xfc}, {0x91,0xfc}, {0x90,0xfc}, {0x8f,0xfc},
- {0x8f,0x3c}, {0x8e,0xbc}, {0x8e,0x3c}, {0x8d,0xbc},
- {0x8d,0x3c}, {0x8c,0xbc}, {0x8c,0x3c}, {0x8b,0xbc},
- {0x8b,0x3c}, {0x8a,0xbc}, {0x8a,0x3c}, {0x89,0xbc},
- {0x89,0x3c}, {0x88,0xbc}, {0x88,0x3c}, {0x87,0xbc},
- {0x87,0x5c}, {0x87,0x1c}, {0x86,0xdc}, {0x86,0x9c},
- {0x86,0x5c}, {0x86,0x1c}, {0x85,0xdc}, {0x85,0x9c},
- {0x85,0x5c}, {0x85,0x1c}, {0x84,0xdc}, {0x84,0x9c},
- {0x84,0x5c}, {0x84,0x1c}, {0x83,0xdc}, {0x83,0x9c},
- {0x83,0x6c}, {0x83,0x4c}, {0x83,0x2c}, {0x83,0x0c},
- {0x82,0xec}, {0x82,0xcc}, {0x82,0xac}, {0x82,0x8c},
- {0x82,0x6c}, {0x82,0x4c}, {0x82,0x2c}, {0x82,0x0c},
- {0x81,0xec}, {0x81,0xcc}, {0x81,0xac}, {0x81,0x8c},
- {0x81,0x74}, {0x81,0x64}, {0x81,0x54}, {0x81,0x44},
- {0x81,0x34}, {0x81,0x24}, {0x81,0x14}, {0x81,0x04},
- {0x80,0xf4}, {0x80,0xe4}, {0x80,0xd4}, {0x80,0xc4},
- {0x80,0xb4}, {0x80,0xa4}, {0x80,0x94}, {0x80,0x84},
- {0x80,0x78}, {0x80,0x70}, {0x80,0x68}, {0x80,0x60},
- {0x80,0x58}, {0x80,0x50}, {0x80,0x48}, {0x80,0x40},
- {0x80,0x38}, {0x80,0x30}, {0x80,0x28}, {0x80,0x20},
- {0x80,0x18}, {0x80,0x10}, {0x80,0x08}, {0x80,0x00},
-};
-
-const u_char alawtolin16[256][2] = {
- {0x6a,0x80}, {0x6b,0x80}, {0x68,0x80}, {0x69,0x80},
- {0x6e,0x80}, {0x6f,0x80}, {0x6c,0x80}, {0x6d,0x80},
- {0x62,0x80}, {0x63,0x80}, {0x60,0x80}, {0x61,0x80},
- {0x66,0x80}, {0x67,0x80}, {0x64,0x80}, {0x65,0x80},
- {0x75,0x40}, {0x75,0xc0}, {0x74,0x40}, {0x74,0xc0},
- {0x77,0x40}, {0x77,0xc0}, {0x76,0x40}, {0x76,0xc0},
- {0x71,0x40}, {0x71,0xc0}, {0x70,0x40}, {0x70,0xc0},
- {0x73,0x40}, {0x73,0xc0}, {0x72,0x40}, {0x72,0xc0},
- {0x2a,0x00}, {0x2e,0x00}, {0x22,0x00}, {0x26,0x00},
- {0x3a,0x00}, {0x3e,0x00}, {0x32,0x00}, {0x36,0x00},
- {0x0a,0x00}, {0x0e,0x00}, {0x02,0x00}, {0x06,0x00},
- {0x1a,0x00}, {0x1e,0x00}, {0x12,0x00}, {0x16,0x00},
- {0x55,0x00}, {0x57,0x00}, {0x51,0x00}, {0x53,0x00},
- {0x5d,0x00}, {0x5f,0x00}, {0x59,0x00}, {0x5b,0x00},
- {0x45,0x00}, {0x47,0x00}, {0x41,0x00}, {0x43,0x00},
- {0x4d,0x00}, {0x4f,0x00}, {0x49,0x00}, {0x4b,0x00},
- {0x7e,0xa8}, {0x7e,0xb8}, {0x7e,0x88}, {0x7e,0x98},
- {0x7e,0xe8}, {0x7e,0xf8}, {0x7e,0xc8}, {0x7e,0xd8},
- {0x7e,0x28}, {0x7e,0x38}, {0x7e,0x08}, {0x7e,0x18},
- {0x7e,0x68}, {0x7e,0x78}, {0x7e,0x48}, {0x7e,0x58},
- {0x7f,0xa8}, {0x7f,0xb8}, {0x7f,0x88}, {0x7f,0x98},
- {0x7f,0xe8}, {0x7f,0xf8}, {0x7f,0xc8}, {0x7f,0xd8},
- {0x7f,0x28}, {0x7f,0x38}, {0x7f,0x08}, {0x7f,0x18},
- {0x7f,0x68}, {0x7f,0x78}, {0x7f,0x48}, {0x7f,0x58},
- {0x7a,0xa0}, {0x7a,0xe0}, {0x7a,0x20}, {0x7a,0x60},
- {0x7b,0xa0}, {0x7b,0xe0}, {0x7b,0x20}, {0x7b,0x60},
- {0x78,0xa0}, {0x78,0xe0}, {0x78,0x20}, {0x78,0x60},
- {0x79,0xa0}, {0x79,0xe0}, {0x79,0x20}, {0x79,0x60},
- {0x7d,0x50}, {0x7d,0x70}, {0x7d,0x10}, {0x7d,0x30},
- {0x7d,0xd0}, {0x7d,0xf0}, {0x7d,0x90}, {0x7d,0xb0},
- {0x7c,0x50}, {0x7c,0x70}, {0x7c,0x10}, {0x7c,0x30},
- {0x7c,0xd0}, {0x7c,0xf0}, {0x7c,0x90}, {0x7c,0xb0},
- {0x95,0x80}, {0x94,0x80}, {0x97,0x80}, {0x96,0x80},
- {0x91,0x80}, {0x90,0x80}, {0x93,0x80}, {0x92,0x80},
- {0x9d,0x80}, {0x9c,0x80}, {0x9f,0x80}, {0x9e,0x80},
- {0x99,0x80}, {0x98,0x80}, {0x9b,0x80}, {0x9a,0x80},
- {0x8a,0xc0}, {0x8a,0x40}, {0x8b,0xc0}, {0x8b,0x40},
- {0x88,0xc0}, {0x88,0x40}, {0x89,0xc0}, {0x89,0x40},
- {0x8e,0xc0}, {0x8e,0x40}, {0x8f,0xc0}, {0x8f,0x40},
- {0x8c,0xc0}, {0x8c,0x40}, {0x8d,0xc0}, {0x8d,0x40},
- {0xd6,0x00}, {0xd2,0x00}, {0xde,0x00}, {0xda,0x00},
- {0xc6,0x00}, {0xc2,0x00}, {0xce,0x00}, {0xca,0x00},
- {0xf6,0x00}, {0xf2,0x00}, {0xfe,0x00}, {0xfa,0x00},
- {0xe6,0x00}, {0xe2,0x00}, {0xee,0x00}, {0xea,0x00},
- {0xab,0x00}, {0xa9,0x00}, {0xaf,0x00}, {0xad,0x00},
- {0xa3,0x00}, {0xa1,0x00}, {0xa7,0x00}, {0xa5,0x00},
- {0xbb,0x00}, {0xb9,0x00}, {0xbf,0x00}, {0xbd,0x00},
- {0xb3,0x00}, {0xb1,0x00}, {0xb7,0x00}, {0xb5,0x00},
- {0x81,0x58}, {0x81,0x48}, {0x81,0x78}, {0x81,0x68},
- {0x81,0x18}, {0x81,0x08}, {0x81,0x38}, {0x81,0x28},
- {0x81,0xd8}, {0x81,0xc8}, {0x81,0xf8}, {0x81,0xe8},
- {0x81,0x98}, {0x81,0x88}, {0x81,0xb8}, {0x81,0xa8},
- {0x80,0x58}, {0x80,0x48}, {0x80,0x78}, {0x80,0x68},
- {0x80,0x18}, {0x80,0x08}, {0x80,0x38}, {0x80,0x28},
- {0x80,0xd8}, {0x80,0xc8}, {0x80,0xf8}, {0x80,0xe8},
- {0x80,0x98}, {0x80,0x88}, {0x80,0xb8}, {0x80,0xa8},
- {0x85,0x60}, {0x85,0x20}, {0x85,0xe0}, {0x85,0xa0},
- {0x84,0x60}, {0x84,0x20}, {0x84,0xe0}, {0x84,0xa0},
- {0x87,0x60}, {0x87,0x20}, {0x87,0xe0}, {0x87,0xa0},
- {0x86,0x60}, {0x86,0x20}, {0x86,0xe0}, {0x86,0xa0},
- {0x82,0xb0}, {0x82,0x90}, {0x82,0xf0}, {0x82,0xd0},
- {0x82,0x30}, {0x82,0x10}, {0x82,0x70}, {0x82,0x50},
- {0x83,0xb0}, {0x83,0x90}, {0x83,0xf0}, {0x83,0xd0},
- {0x83,0x30}, {0x83,0x10}, {0x83,0x70}, {0x83,0x50},
-};
-
-void
-mulaw_to_slinear16_le(u_char *p, int cc)
-{
- u_char *q = p;
-
- p += cc;
- q += cc << 1;
- while (--cc >= 0) {
- --p;
- q -= 2;
- q[1] = mulawtolin16[*p][0] ^ 0x80;
- q[0] = mulawtolin16[*p][1];
- }
-}
-
-void
-alaw_to_slinear16_le(u_char *p, int cc)
-{
- u_char *q = p;
-
- p += cc;
- q += cc << 1;
- while (--cc >= 0) {
- --p;
- q -= 2;
- q[1] = alawtolin16[*p][0] ^ 0x80;
- q[0] = alawtolin16[*p][1];
- }
-}
-
-
/* headerless data files. played at /dev/audio's defaults.
*/
#define FMT_RAW 0
@@ -237,7 +67,7 @@ legacy_play(char *dev, char *aufile)
int fd, fmt = FMT_RAW;
u_int32_t pos = 0, snd_fmt = 1, rate = 8000, chan = 1;
char magic[4];
- int is_ulaw, is_alaw, wav_fmt;
+ short *map;
if ((fd = open(aufile, O_RDONLY)) < 0) {
warn("cannot open %s", aufile);
@@ -262,7 +92,7 @@ legacy_play(char *dev, char *aufile)
if (read(fd, &chan, sizeof(chan)) == sizeof(chan))
chan = ntohl(chan);
} else if (!strncmp(magic, "RIFF", 4) &&
- wav_readhdr(fd, &apar, &datasz, &wav_fmt)) {
+ wav_readhdr(fd, &apar, &datasz, &map)) {
fmt = FMT_WAV;
}
@@ -279,7 +109,6 @@ legacy_play(char *dev, char *aufile)
}
sio_initpar(&par);
- is_ulaw = is_alaw = 0;
switch(fmt) {
case FMT_WAV:
par.rate = apar.rate;
@@ -287,16 +116,6 @@ legacy_play(char *dev, char *aufile)
par.sig = apar.sig;
par.bits = apar.bits;
par.le = apar.le;
- if (wav_fmt == 6 || wav_fmt == 7) {
- if (wav_fmt == 6)
- is_alaw = 1;
- else if (wav_fmt == 7)
- is_ulaw = 1;
- par.sig = 1;
- par.bits = 16;
- par.le = 1;
- } else if (wav_fmt != 1)
- warnx("format not supported");
break;
case FMT_AU:
par.rate = rate;
@@ -304,9 +123,9 @@ legacy_play(char *dev, char *aufile)
par.sig = 1;
par.bits = 16;
par.le = 1;
- is_ulaw = 1;
+ map = wav_ulawmap;
if (snd_fmt == 27)
- is_alaw = 1;
+ map = wav_alawmap;
break;
case FMT_RAW:
default:
@@ -345,14 +164,11 @@ legacy_play(char *dev, char *aufile)
}
readsz = sizeof(buf);
- if (is_ulaw || is_alaw)
+ if (map)
readsz /= 2;
while ((rd = read(fd, buf, readsz)) > 0) {
- if (is_ulaw) {
- mulaw_to_slinear16_le(buf, rd);
- rd *= 2;
- } else if (is_alaw) {
- alaw_to_slinear16_le(buf, rd);
+ if (map) {
+ wav_conv(buf, rd, map);
rd *= 2;
}
if (sio_write(hdl, buf, rd) != rd)
diff --git a/usr.bin/aucat/wav.c b/usr.bin/aucat/wav.c
index 316f78673f0..40f22c23a8b 100644
--- a/usr.bin/aucat/wav.c
+++ b/usr.bin/aucat/wav.c
@@ -8,6 +8,76 @@
#include "conf.h"
#include "wav.h"
+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
+};
+
/*
* max data of a .wav file. The total file size must be smaller than
* 2^31, and we also have to leave some space for the headers (around 40
@@ -38,11 +108,13 @@ wav_new_in(struct fileops *ops, int fd, char *name,
if (f == NULL)
return NULL;
if (hdr == HDR_WAV) {
- if (!wav_readhdr(f->pipe.fd, par, &f->rbytes, NULL))
+ if (!wav_readhdr(f->pipe.fd, par, &f->rbytes, &f->map))
exit(1);
f->hpar = *par;
- } else
+ } else {
f->rbytes = -1;
+ f->map = NULL;
+ }
f->hdr = 0;
return f;
}
@@ -70,12 +142,30 @@ wav_new_out(struct fileops *ops, int fd, char *name,
return f;
}
+void
+wav_conv(unsigned char *data, unsigned count, short *map)
+{
+ unsigned i;
+ unsigned char *iptr;
+ short *optr;
+
+ iptr = data + count;
+ optr = (short *)data + count;
+ for (i = count; i > 0; i--) {
+ --optr;
+ --iptr;
+ *optr = map[*iptr];
+ }
+}
+
unsigned
wav_read(struct file *file, unsigned char *data, unsigned count)
{
struct wav *f = (struct wav *)file;
unsigned n;
+ if (f->map)
+ count /= sizeof(short);
if (f->rbytes >= 0 && count > f->rbytes) {
count = f->rbytes; /* file->rbytes fits in count */
if (count == 0) {
@@ -87,10 +177,13 @@ wav_read(struct file *file, unsigned char *data, unsigned count)
n = pipe_read(file, data, count);
if (f->rbytes >= 0)
f->rbytes -= n;
+ if (f->map) {
+ wav_conv(data, n, f->map);
+ n *= sizeof(short);
+ }
return n;
}
-
unsigned
wav_write(struct file *file, unsigned char *data, unsigned count)
{
diff --git a/usr.bin/aucat/wav.h b/usr.bin/aucat/wav.h
index ea4420a10cc..f7581ebe846 100644
--- a/usr.bin/aucat/wav.h
+++ b/usr.bin/aucat/wav.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: wav.h,v 1.2 2009/04/11 10:24:21 jakemsr Exp $ */
+/* $OpenBSD: wav.h,v 1.3 2009/04/22 10:57:33 ratchov Exp $ */
/*
* Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
*
@@ -31,6 +31,7 @@ struct wav {
struct aparams hpar; /* parameters to write on the header */
off_t rbytes; /* bytes to read, -1 if no limit */
off_t wbytes; /* bytes to write, -1 if no limit */
+ short *map; /* mulaw/alaw -> s16 conversion table */
};
extern struct fileops wav_ops;
@@ -42,10 +43,14 @@ struct wav *wav_new_out(struct fileops *, int, char *,
unsigned wav_read(struct file *, unsigned char *, unsigned);
unsigned wav_write(struct file *, unsigned char *, unsigned);
void wav_close(struct file *);
-int wav_readhdr(int, struct aparams *, off_t *, int *);
+int wav_readhdr(int, struct aparams *, off_t *, short **);
int wav_writehdr(int, struct aparams *);
+void wav_conv(unsigned char *, unsigned, short *);
/* legacy */
int legacy_play(char *, char *);
+extern short wav_ulawmap[256];
+extern short wav_alawmap[256];
+
#endif /* !defined(WAV_H) */