Annotation of src/usr.bin/aucat/wav.c, Revision 1.2
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)
15: */
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);
38: if (hdr == HDR_WAV) {
39: if (!wav_readhdr(f->pipe.fd, par, &f->rbytes))
40: exit(1);
41: f->hpar = *par;
42: } else
43: f->rbytes = -1;
44: f->hdr = 0;
45: return f;
46: }
47:
48: struct wav *
49: wav_new_out(struct fileops *ops, int fd, char *name,
50: struct aparams *par, unsigned hdr)
51: {
52: struct wav *f;
53:
54: f = (struct wav *)pipe_new(ops, fd, name);
55: if (hdr == HDR_WAV) {
1.2 ! ratchov 56: par->le = 1;
! 57: par->sig = (par->bits <= 8) ? 0 : 1;
! 58: par->bps = (par->bits + 7) / 8;
1.1 ratchov 59: if (!wav_writehdr(f->pipe.fd, par))
60: exit(1);
61: f->hpar = *par;
62: f->wbytes = WAV_DATAMAX;
63: } else
64: f->wbytes = -1;
65: f->hdr = hdr;
66: return f;
67: }
68:
69: unsigned
70: wav_read(struct file *file, unsigned char *data, unsigned count)
71: {
72: struct wav *f = (struct wav *)file;
73: unsigned n;
74:
75: if (f->rbytes >= 0 && count > f->rbytes) {
76: count = f->rbytes; /* file->rbytes fits in count */
77: if (count == 0) {
78: DPRINTFN(2, "wav_read: %s: complete\n", f->pipe.file.name);
79: file_eof(&f->pipe.file);
80: return 0;
81: }
82: }
83: n = pipe_read(file, data, count);
84: if (f->rbytes >= 0)
85: f->rbytes -= n;
86: return n;
87: }
88:
89:
90: unsigned
91: wav_write(struct file *file, unsigned char *data, unsigned count)
92: {
93: struct wav *f = (struct wav *)file;
94: unsigned n;
95:
96: if (f->wbytes >= 0 && count > f->wbytes) {
97: count = f->wbytes; /* wbytes fits in count */
98: if (count == 0) {
99: DPRINTFN(2, "wav_write: %s: complete\n",
100: f->pipe.file.name);
101: file_hup(&f->pipe.file);
102: return 0;
103: }
104: }
105: n = pipe_write(file, data, count);
106: if (f->wbytes >= 0)
107: f->wbytes -= n;
108: return n;
109: }
110:
111: void
112: wav_close(struct file *file)
113: {
114: struct wav *f = (struct wav *)file;
115:
116: if (f->hdr == HDR_WAV)
117: wav_writehdr(f->pipe.fd, &f->hpar);
118: pipe_close(file);
119: }