Annotation of src/usr.bin/aucat/wav.c, Revision 1.5
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:
11: /*
12: * max data of a .wav file. The total file size must be smaller than
13: * 2^31, and we also have to leave some space for the headers (around 40
14: * bytes)
1.3 ratchov 15: */
1.1 ratchov 16: #define WAV_DATAMAX (0x7fff0000)
17:
18: struct fileops wav_ops = {
19: "wav",
20: sizeof(struct wav),
21: wav_close,
22: wav_read,
23: wav_write,
24: NULL, /* start */
25: NULL, /* stop */
26: pipe_nfds,
27: pipe_pollfd,
28: pipe_revents
29: };
30:
31: struct wav *
32: wav_new_in(struct fileops *ops, int fd, char *name,
33: struct aparams *par, unsigned hdr)
34: {
35: struct wav *f;
36:
37: f = (struct wav *)pipe_new(ops, fd, name);
1.4 ratchov 38: if (f == NULL)
39: return NULL;
1.1 ratchov 40: if (hdr == HDR_WAV) {
1.5 ! jakemsr 41: if (!wav_readhdr(f->pipe.fd, par, &f->rbytes, NULL))
1.1 ratchov 42: exit(1);
43: f->hpar = *par;
44: } else
45: f->rbytes = -1;
46: f->hdr = 0;
47: return f;
48: }
49:
50: struct wav *
51: wav_new_out(struct fileops *ops, int fd, char *name,
52: struct aparams *par, unsigned hdr)
53: {
54: struct wav *f;
55:
56: f = (struct wav *)pipe_new(ops, fd, name);
1.4 ratchov 57: if (f == NULL)
58: return NULL;
1.1 ratchov 59: if (hdr == HDR_WAV) {
1.2 ratchov 60: par->le = 1;
1.3 ratchov 61: par->sig = (par->bits <= 8) ? 0 : 1;
1.2 ratchov 62: par->bps = (par->bits + 7) / 8;
1.1 ratchov 63: if (!wav_writehdr(f->pipe.fd, par))
64: exit(1);
65: f->hpar = *par;
66: f->wbytes = WAV_DATAMAX;
67: } else
68: f->wbytes = -1;
69: f->hdr = hdr;
70: return f;
71: }
72:
73: unsigned
74: wav_read(struct file *file, unsigned char *data, unsigned count)
75: {
76: struct wav *f = (struct wav *)file;
77: unsigned n;
78:
79: if (f->rbytes >= 0 && count > f->rbytes) {
80: count = f->rbytes; /* file->rbytes fits in count */
81: if (count == 0) {
82: DPRINTFN(2, "wav_read: %s: complete\n", f->pipe.file.name);
83: file_eof(&f->pipe.file);
84: return 0;
85: }
86: }
87: n = pipe_read(file, data, count);
88: if (f->rbytes >= 0)
89: f->rbytes -= n;
90: return n;
91: }
92:
93:
94: unsigned
95: wav_write(struct file *file, unsigned char *data, unsigned count)
96: {
97: struct wav *f = (struct wav *)file;
98: unsigned n;
1.3 ratchov 99:
1.1 ratchov 100: if (f->wbytes >= 0 && count > f->wbytes) {
101: count = f->wbytes; /* wbytes fits in count */
102: if (count == 0) {
103: DPRINTFN(2, "wav_write: %s: complete\n",
104: f->pipe.file.name);
105: file_hup(&f->pipe.file);
106: return 0;
107: }
108: }
109: n = pipe_write(file, data, count);
110: if (f->wbytes >= 0)
111: f->wbytes -= n;
112: return n;
113: }
114:
115: void
116: wav_close(struct file *file)
117: {
118: struct wav *f = (struct wav *)file;
119:
120: if (f->hdr == HDR_WAV)
121: wav_writehdr(f->pipe.fd, &f->hpar);
122: pipe_close(file);
123: }