Annotation of src/usr.bin/aucat/wav.c, Revision 1.6
1.1 ratchov 1: #include <sys/types.h>
2: #include <err.h>
3: #include <fcntl.h>
4: #include <stdio.h>
5: #include <stdlib.h>
6: #include <unistd.h>
7:
8: #include "conf.h"
9: #include "wav.h"
10:
1.6 ! ratchov 11: short wav_ulawmap[256] = {
! 12: -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956,
! 13: -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764,
! 14: -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412,
! 15: -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316,
! 16: -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
! 17: -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
! 18: -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
! 19: -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
! 20: -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
! 21: -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
! 22: -876, -844, -812, -780, -748, -716, -684, -652,
! 23: -620, -588, -556, -524, -492, -460, -428, -396,
! 24: -372, -356, -340, -324, -308, -292, -276, -260,
! 25: -244, -228, -212, -196, -180, -164, -148, -132,
! 26: -120, -112, -104, -96, -88, -80, -72, -64,
! 27: -56, -48, -40, -32, -24, -16, -8, 0,
! 28: 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
! 29: 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
! 30: 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
! 31: 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
! 32: 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
! 33: 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
! 34: 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
! 35: 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
! 36: 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
! 37: 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
! 38: 876, 844, 812, 780, 748, 716, 684, 652,
! 39: 620, 588, 556, 524, 492, 460, 428, 396,
! 40: 372, 356, 340, 324, 308, 292, 276, 260,
! 41: 244, 228, 212, 196, 180, 164, 148, 132,
! 42: 120, 112, 104, 96, 88, 80, 72, 64,
! 43: 56, 48, 40, 32, 24, 16, 8, 0
! 44: };
! 45:
! 46: short wav_alawmap[256] = {
! 47: -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
! 48: -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
! 49: -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
! 50: -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
! 51: -22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944,
! 52: -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136,
! 53: -11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472,
! 54: -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568,
! 55: -344, -328, -376, -360, -280, -264, -312, -296,
! 56: -472, -456, -504, -488, -408, -392, -440, -424,
! 57: -88, -72, -120, -104, -24, -8, -56, -40,
! 58: -216, -200, -248, -232, -152, -136, -184, -168,
! 59: -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
! 60: -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
! 61: -688, -656, -752, -720, -560, -528, -624, -592,
! 62: -944, -912, -1008, -976, -816, -784, -880, -848,
! 63: 5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
! 64: 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
! 65: 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
! 66: 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
! 67: 22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
! 68: 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
! 69: 11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
! 70: 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
! 71: 344, 328, 376, 360, 280, 264, 312, 296,
! 72: 472, 456, 504, 488, 408, 392, 440, 424,
! 73: 88, 72, 120, 104, 24, 8, 56, 40,
! 74: 216, 200, 248, 232, 152, 136, 184, 168,
! 75: 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
! 76: 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
! 77: 688, 656, 752, 720, 560, 528, 624, 592,
! 78: 944, 912, 1008, 976, 816, 784, 880, 848
! 79: };
! 80:
1.1 ratchov 81: /*
82: * max data of a .wav file. The total file size must be smaller than
83: * 2^31, and we also have to leave some space for the headers (around 40
84: * bytes)
1.3 ratchov 85: */
1.1 ratchov 86: #define WAV_DATAMAX (0x7fff0000)
87:
88: struct fileops wav_ops = {
89: "wav",
90: sizeof(struct wav),
91: wav_close,
92: wav_read,
93: wav_write,
94: NULL, /* start */
95: NULL, /* stop */
96: pipe_nfds,
97: pipe_pollfd,
98: pipe_revents
99: };
100:
101: struct wav *
102: wav_new_in(struct fileops *ops, int fd, char *name,
103: struct aparams *par, unsigned hdr)
104: {
105: struct wav *f;
106:
107: f = (struct wav *)pipe_new(ops, fd, name);
1.4 ratchov 108: if (f == NULL)
109: return NULL;
1.1 ratchov 110: if (hdr == HDR_WAV) {
1.6 ! ratchov 111: if (!wav_readhdr(f->pipe.fd, par, &f->rbytes, &f->map))
1.1 ratchov 112: exit(1);
113: f->hpar = *par;
1.6 ! ratchov 114: } else {
1.1 ratchov 115: f->rbytes = -1;
1.6 ! ratchov 116: f->map = NULL;
! 117: }
1.1 ratchov 118: f->hdr = 0;
119: return f;
120: }
121:
122: struct wav *
123: wav_new_out(struct fileops *ops, int fd, char *name,
124: struct aparams *par, unsigned hdr)
125: {
126: struct wav *f;
127:
128: f = (struct wav *)pipe_new(ops, fd, name);
1.4 ratchov 129: if (f == NULL)
130: return NULL;
1.1 ratchov 131: if (hdr == HDR_WAV) {
1.2 ratchov 132: par->le = 1;
1.3 ratchov 133: par->sig = (par->bits <= 8) ? 0 : 1;
1.2 ratchov 134: par->bps = (par->bits + 7) / 8;
1.1 ratchov 135: if (!wav_writehdr(f->pipe.fd, par))
136: exit(1);
137: f->hpar = *par;
138: f->wbytes = WAV_DATAMAX;
139: } else
140: f->wbytes = -1;
141: f->hdr = hdr;
142: return f;
143: }
144:
1.6 ! ratchov 145: void
! 146: wav_conv(unsigned char *data, unsigned count, short *map)
! 147: {
! 148: unsigned i;
! 149: unsigned char *iptr;
! 150: short *optr;
! 151:
! 152: iptr = data + count;
! 153: optr = (short *)data + count;
! 154: for (i = count; i > 0; i--) {
! 155: --optr;
! 156: --iptr;
! 157: *optr = map[*iptr];
! 158: }
! 159: }
! 160:
1.1 ratchov 161: unsigned
162: wav_read(struct file *file, unsigned char *data, unsigned count)
163: {
164: struct wav *f = (struct wav *)file;
165: unsigned n;
166:
1.6 ! ratchov 167: if (f->map)
! 168: count /= sizeof(short);
1.1 ratchov 169: if (f->rbytes >= 0 && count > f->rbytes) {
170: count = f->rbytes; /* file->rbytes fits in count */
171: if (count == 0) {
172: DPRINTFN(2, "wav_read: %s: complete\n", f->pipe.file.name);
173: file_eof(&f->pipe.file);
174: return 0;
175: }
176: }
177: n = pipe_read(file, data, count);
178: if (f->rbytes >= 0)
179: f->rbytes -= n;
1.6 ! ratchov 180: if (f->map) {
! 181: wav_conv(data, n, f->map);
! 182: n *= sizeof(short);
! 183: }
1.1 ratchov 184: return n;
185: }
186:
187: unsigned
188: wav_write(struct file *file, unsigned char *data, unsigned count)
189: {
190: struct wav *f = (struct wav *)file;
191: unsigned n;
1.3 ratchov 192:
1.1 ratchov 193: if (f->wbytes >= 0 && count > f->wbytes) {
194: count = f->wbytes; /* wbytes fits in count */
195: if (count == 0) {
196: DPRINTFN(2, "wav_write: %s: complete\n",
197: f->pipe.file.name);
198: file_hup(&f->pipe.file);
199: return 0;
200: }
201: }
202: n = pipe_write(file, data, count);
203: if (f->wbytes >= 0)
204: f->wbytes -= n;
205: return n;
206: }
207:
208: void
209: wav_close(struct file *file)
210: {
211: struct wav *f = (struct wav *)file;
212:
213: if (f->hdr == HDR_WAV)
214: wav_writehdr(f->pipe.fd, &f->hpar);
215: pipe_close(file);
216: }