xref: /openbsd/usr.bin/aucat/abuf.h (revision 404b540a)
1 /*	$OpenBSD: abuf.h,v 1.19 2009/10/09 16:49:48 ratchov Exp $	*/
2 /*
3  * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 #ifndef ABUF_H
18 #define ABUF_H
19 
20 #include <sys/queue.h>
21 
22 #define XRUN_IGNORE	0	/* on xrun silently insert/discard samples */
23 #define XRUN_SYNC	1	/* catchup to sync to the mix/sub */
24 #define XRUN_ERROR	2	/* xruns are errors, eof/hup buffer */
25 #define MIDI_MSGMAX	16	/* max size of MIDI messaage */
26 
27 struct aproc;
28 struct aparams;
29 
30 struct abuf {
31 	LIST_ENTRY(abuf) ient;	/* reader's list of inputs entry */
32 	LIST_ENTRY(abuf) oent;	/* writer's list of outputs entry */
33 
34 	/*
35 	 * fifo parameters
36 	 */
37 	unsigned bpf;		/* bytes per frame */
38 	unsigned cmin, cmax;	/* channel range of this buf */
39 	unsigned start;		/* offset where data starts */
40 	unsigned used;		/* valid data */
41 	unsigned len;		/* size of the ring */
42 	unsigned abspos;	/* frame number of the start position */
43 	unsigned silence;	/* silence to insert on next write */
44 	unsigned drop;		/* bytes to drop on next read */
45 	struct aproc *rproc;	/* reader */
46 	struct aproc *wproc;	/* writer */
47 	struct abuf *duplex;	/* link to buffer of the other direction */
48 	unsigned inuse;		/* in abuf_{flush,fill,run}() */
49 	unsigned tickets;	/* max data to (if throttling) */
50 
51 	/*
52 	 * Misc reader aproc-specific per-buffer parameters.
53 	 */
54 	union {
55 		struct {
56 			int weight;	/* dynamic range */
57 			int maxweight;	/* max dynamic range allowed */
58 			unsigned vol;	/* volume within the dynamic range */
59 			unsigned done;	/* bytes ready */
60 			unsigned xrun;	/* underrun policy */
61 		} mix;
62 		struct {
63 			unsigned st;	/* MIDI running status */
64 			unsigned used;	/* bytes used from ``msg'' */
65 			unsigned idx;	/* actual MIDI message size */
66 			unsigned len;	/* MIDI message length */
67 			unsigned char msg[MIDI_MSGMAX];
68 		} midi;
69 	} r;
70 
71 	/*
72 	 * Misc reader aproc-specific per-buffer parameters.
73 	 */
74 	union {
75 		struct {
76 			unsigned todo;	/* bytes to process */
77 		} mix;
78 		struct {
79 			unsigned done;	/* bytes copied */
80 			unsigned xrun;	/* overrun policy */
81 		} sub;
82 	} w;
83 };
84 
85 /*
86  * the buffer contains at least one frame. This macro should
87  * be used to check if the buffer can be flushed
88  */
89 #define ABUF_ROK(b) ((b)->used >= (b)->bpf)
90 
91 /*
92  * there's room for at least one frame
93  */
94 #define ABUF_WOK(b) ((b)->len - (b)->used >= (b)->bpf)
95 
96 /*
97  * the buffer is empty and has no writer anymore
98  */
99 #define ABUF_EOF(b) (!ABUF_ROK(b) && (b)->wproc == NULL)
100 
101 /*
102  * the buffer has no reader anymore, note that it's not
103  * enough the buffer to be disconnected, because it can
104  * be not yet connected buffer (eg. socket play buffer)
105  */
106 #define ABUF_HUP(b) (!ABUF_WOK(b) && (b)->rproc == NULL)
107 
108 /*
109  * similar to !ABUF_WOK, but is used for file i/o, where
110  * operation may not involve an integer number of frames
111  */
112 #define ABUF_FULL(b) ((b)->used == (b)->len)
113 
114 /*
115  * same as !ABUF_ROK, but used for files, where
116  * operations are byte orientated, not frame-oriented
117  */
118 #define ABUF_EMPTY(b) ((b)->used == 0)
119 
120 struct abuf *abuf_new(unsigned, struct aparams *);
121 void abuf_del(struct abuf *);
122 void abuf_dbg(struct abuf *);
123 void abuf_clear(struct abuf *);
124 unsigned char *abuf_rgetblk(struct abuf *, unsigned *, unsigned);
125 unsigned char *abuf_wgetblk(struct abuf *, unsigned *, unsigned);
126 void abuf_rdiscard(struct abuf *, unsigned);
127 void abuf_wcommit(struct abuf *, unsigned);
128 int abuf_fill(struct abuf *);
129 int abuf_flush(struct abuf *);
130 void abuf_eof(struct abuf *);
131 void abuf_hup(struct abuf *);
132 void abuf_run(struct abuf *);
133 void abuf_ipos(struct abuf *, int);
134 void abuf_opos(struct abuf *, int);
135 
136 #endif /* !defined(ABUF_H) */
137