Annotation of src/usr.bin/sndiod/dev.h, Revision 1.28
1.28 ! ratchov 1: /* $OpenBSD: dev.h,v 1.27 2020/06/18 05:11:13 ratchov Exp $ */
1.1 ratchov 2: /*
3: * Copyright (c) 2008-2012 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 DEV_H
18: #define DEV_H
19:
20: #include "abuf.h"
21: #include "dsp.h"
22: #include "siofile.h"
1.23 ratchov 23: #include "dev_sioctl.h"
24:
25: #define CTLADDR_SLOT_LEVEL(n) (n)
26: #define CTLADDR_MASTER (DEV_NSLOT)
27: #define CTLADDR_END (DEV_NSLOT + 1)
1.1 ratchov 28:
29: /*
30: * audio stream state structure
31: */
32:
33: struct slotops
34: {
1.7 ratchov 35: void (*onmove)(void *); /* clock tick */
1.23 ratchov 36: void (*onvol)(void *); /* tell client vol changed */
1.1 ratchov 37: void (*fill)(void *); /* request to fill a play block */
38: void (*flush)(void *); /* request to flush a rec block */
39: void (*eof)(void *); /* notify that play drained */
40: void (*exit)(void *); /* delete client */
41: };
42:
1.23 ratchov 43: struct ctlops
44: {
45: void (*exit)(void *); /* delete client */
1.24 ratchov 46: void (*sync)(void *); /* description ready */
1.23 ratchov 47: };
48:
1.1 ratchov 49: struct slot {
50: struct slotops *ops; /* client callbacks */
51: struct slot *next; /* next on the play list */
52: struct dev *dev; /* device this belongs to */
1.13 ratchov 53: struct opt *opt; /* config used */
1.1 ratchov 54: void *arg; /* user data for callbacks */
55: struct aparams par; /* socket side params */
56: struct {
1.10 ratchov 57: int weight; /* dynamic range */
1.1 ratchov 58: unsigned int vol; /* volume within the vol */
59: struct abuf buf; /* socket side buffer */
60: int bpf; /* byte per frame */
1.19 ratchov 61: int nch; /* number of play chans */
1.1 ratchov 62: struct cmap cmap; /* channel mapper state */
63: struct resamp resamp; /* resampler state */
64: struct conv dec; /* format decoder params */
65: int join; /* channel join factor */
66: int expand; /* channel expand factor */
67: void *resampbuf, *decbuf; /* tmp buffers */
68: } mix;
69: struct {
70: struct abuf buf; /* socket side buffer */
1.5 ratchov 71: int prime; /* initial cycles to skip */
1.1 ratchov 72: int bpf; /* byte per frame */
1.19 ratchov 73: int nch; /* number of rec chans */
1.1 ratchov 74: struct cmap cmap; /* channel mapper state */
75: struct resamp resamp; /* buffer for resampling */
76: struct conv enc; /* buffer for encoding */
77: int join; /* channel join factor */
78: int expand; /* channel expand factor */
79: void *resampbuf, *encbuf; /* tmp buffers */
80: } sub;
81: int xrun; /* underrun policy */
1.5 ratchov 82: int skip; /* cycles to skip (for xrun) */
1.1 ratchov 83: #define SLOT_BUFSZ(s) \
84: ((s)->appbufsz + (s)->dev->bufsz / (s)->dev->round * (s)->round)
85: int appbufsz; /* slot-side buffer size */
86: int round; /* slot-side block size */
87: int rate; /* slot-side sample rate */
88: int delta; /* pending clock ticks */
89: int delta_rem; /* remainder for delta */
90: int mode; /* MODE_{PLAY,REC} */
91: #define SLOT_INIT 0 /* not trying to do anything */
92: #define SLOT_START 1 /* buffer allocated */
93: #define SLOT_READY 2 /* buffer filled enough */
94: #define SLOT_RUN 3 /* buffer attached to device */
95: #define SLOT_STOP 4 /* draining */
96: int pstate;
97:
98: #define SLOT_NAMEMAX 8
99: char name[SLOT_NAMEMAX]; /* name matching [a-z]+ */
100: unsigned int unit; /* instance of name */
101: unsigned int serial; /* global unique number */
102: unsigned int vol; /* current (midi) volume */
1.21 ratchov 103: unsigned int id; /* process id */
1.1 ratchov 104: };
105:
1.12 ratchov 106: struct opt {
107: struct opt *next;
108: #define OPT_NAMEMAX 11
109: char name[OPT_NAMEMAX + 1];
110: int maxweight; /* max dynamic range for clients */
111: int pmin, pmax; /* play channels */
112: int rmin, rmax; /* recording channels */
113: int mmc; /* true if MMC control enabled */
114: int dup; /* true if join/expand enabled */
115: int mode; /* bitmap of MODE_XXX */
116: };
117:
1.1 ratchov 118: /*
1.23 ratchov 119: * subset of channels of a stream
120: */
121:
122: struct ctl {
123: struct ctl *next;
124: #define CTL_NONE 0 /* deleted */
125: #define CTL_NUM 2 /* number (aka integer value) */
126: #define CTL_SW 3 /* on/off switch, only bit 7 counts */
127: #define CTL_VEC 4 /* number, element of vector */
128: #define CTL_LIST 5 /* switch, element of a list */
1.28 ! ratchov 129: #define CTL_SEL 6 /* element of a selector */
1.23 ratchov 130: unsigned int type; /* one of above */
131: unsigned int addr; /* control address */
132: #define CTL_NAMEMAX 16 /* max name lenght */
133: char func[CTL_NAMEMAX]; /* parameter function name */
134: char group[CTL_NAMEMAX]; /* group aka namespace */
135: struct ctl_node {
136: char name[CTL_NAMEMAX]; /* stream name */
137: int unit;
138: } node0, node1; /* affected channels */
139: #define CTL_DEVMASK (1 << 31)
140: #define CTL_SLOTMASK(i) (1 << (i))
141: unsigned int val_mask;
142: unsigned int desc_mask;
143: unsigned int refs_mask;
144: unsigned int maxval;
145: unsigned int curval;
146: int dirty;
147: };
148:
149: struct ctlslot {
150: struct ctlops *ops;
151: void *arg;
152: struct dev *dev;
153: unsigned int mask;
154: unsigned int mode;
155: };
156:
157: /*
1.1 ratchov 158: * audio device with plenty of slots
159: */
160: struct dev {
161: struct dev *next;
162: struct slot *slot_list; /* audio streams attached */
1.12 ratchov 163: struct opt *opt_list;
1.1 ratchov 164: struct midi *midi;
165:
166: /*
167: * audio device (while opened)
1.10 ratchov 168: */
1.3 ratchov 169: struct dev_sio sio;
1.23 ratchov 170: struct dev_sioctl sioctl;
1.1 ratchov 171: struct aparams par; /* encoding */
172: int pchan, rchan; /* play & rec channels */
173: adata_t *rbuf; /* rec buffer */
174: adata_t *pbuf; /* array of play buffers */
175: #define DEV_PBUF(d) ((d)->pbuf + (d)->poffs * (d)->pchan)
176: int poffs; /* index of current play buf */
1.8 ratchov 177: int psize; /* size of play buffer */
1.1 ratchov 178: struct conv enc; /* native->device format */
179: struct conv dec; /* device->native format */
180: unsigned char *encbuf; /* buffer for encoding */
181: unsigned char *decbuf; /* buffer for decoding */
182:
183: /*
184: * preallocated audio sub-devices
185: */
186: #define DEV_NSLOT 8
187: struct slot slot[DEV_NSLOT];
188: unsigned int serial; /* for slot allocation */
1.5 ratchov 189:
190: /*
191: * current position, relative to the current cycle
192: */
193: int delta;
1.1 ratchov 194:
195: /*
196: * desired parameters
197: */
198: unsigned int reqmode; /* mode */
199: struct aparams reqpar; /* parameters */
200: int reqpchan, reqrchan; /* play & rec chans */
201: unsigned int reqbufsz; /* buffer size */
202: unsigned int reqround; /* block size */
203: unsigned int reqrate; /* sample rate */
204: unsigned int hold; /* hold the device open ? */
205: unsigned int autovol; /* auto adjust playvol ? */
206: unsigned int refcnt; /* number of openers */
207: #define DEV_NMAX 16 /* max number of devices */
208: unsigned int num; /* device serial number */
209: #define DEV_CFG 0 /* closed */
210: #define DEV_INIT 1 /* stopped */
1.2 ratchov 211: #define DEV_RUN 2 /* playin & recording */
1.1 ratchov 212: unsigned int pstate; /* one of above */
1.27 ratchov 213: struct dev_alt {
214: struct dev_alt *next;
215: char *name;
216: unsigned int idx;
217: } *alt_list;
218: int alt_num;
1.1 ratchov 219:
220: /*
221: * actual parameters and runtime state (i.e. once opened)
222: */
223: unsigned int mode; /* bitmap of MODE_xxx */
224: unsigned int bufsz, round, rate;
225: unsigned int prime;
226:
227: /*
228: * MIDI time code (MTC)
229: */
230: struct {
231: unsigned int origin; /* MTC start time */
232: unsigned int fps; /* MTC frames per second */
233: #define MTC_FPS_24 0
234: #define MTC_FPS_25 1
235: #define MTC_FPS_30 3
236: unsigned int fps_id; /* one of above */
237: unsigned int hr; /* MTC hours */
238: unsigned int min; /* MTC minutes */
239: unsigned int sec; /* MTC seconds */
240: unsigned int fr; /* MTC frames */
241: unsigned int qfr; /* MTC quarter frames */
242: int delta; /* rel. to the last MTC tick */
243: int refs;
244: } mtc;
245:
246: /*
247: * MIDI machine control (MMC)
248: */
249: #define MMC_STOP 1 /* stopped, can't start */
250: #define MMC_START 2 /* attempting to start */
251: #define MMC_RUN 3 /* started */
252: unsigned int tstate; /* one of above */
1.25 ratchov 253:
254: unsigned int master; /* software vol. knob */
255: unsigned int master_enabled; /* 1 if h/w has no vo. knob */
1.23 ratchov 256:
257: /*
258: * control
259: */
260:
261: struct ctl *ctl_list;
262: #define DEV_NCTLSLOT 8
263: struct ctlslot ctlslot[DEV_NCTLSLOT];
1.1 ratchov 264: };
265:
266: extern struct dev *dev_list;
267:
268: void dev_log(struct dev *);
1.26 ratchov 269: void dev_abort(struct dev *);
1.22 ratchov 270: int dev_reopen(struct dev *);
1.1 ratchov 271: struct dev *dev_new(char *, struct aparams *, unsigned int, unsigned int,
272: unsigned int, unsigned int, unsigned int, unsigned int);
273: struct dev *dev_bynum(int);
1.27 ratchov 274: int dev_addname(struct dev *, char *);
1.1 ratchov 275: void dev_del(struct dev *);
1.11 ratchov 276: void dev_adjpar(struct dev *, int, int, int);
1.1 ratchov 277: int dev_init(struct dev *);
278: void dev_done(struct dev *);
1.9 ratchov 279: int dev_ref(struct dev *);
280: void dev_unref(struct dev *);
1.1 ratchov 281: int dev_getpos(struct dev *);
282: unsigned int dev_roundof(struct dev *, unsigned int);
283:
284: /*
285: * interface to hardware device
286: */
287: void dev_onmove(struct dev *, int);
288: void dev_cycle(struct dev *);
289:
290: /*
291: * midi & midi call-backs
292: */
293: void dev_mmcstart(struct dev *);
294: void dev_mmcstop(struct dev *);
295: void dev_mmcloc(struct dev *, unsigned int);
296: void dev_master(struct dev *, unsigned int);
297: void dev_midi_vol(struct dev *, struct slot *);
298:
299: /*
300: * sio_open(3) like interface for clients
301: */
302: void slot_log(struct slot *);
1.21 ratchov 303: struct slot *slot_new(struct dev *, struct opt *, unsigned int, char *,
1.14 ratchov 304: struct slotops *, void *, int);
1.1 ratchov 305: void slot_del(struct slot *);
306: void slot_setvol(struct slot *, unsigned int);
307: void slot_start(struct slot *);
308: void slot_stop(struct slot *);
309: void slot_read(struct slot *);
310: void slot_write(struct slot *);
1.23 ratchov 311:
312: /*
313: * control related functions
314: */
315: void ctl_log(struct ctl *);
316: struct ctlslot *ctlslot_new(struct dev *, struct ctlops *, void *);
317: void ctlslot_del(struct ctlslot *);
318: int dev_setctl(struct dev *, int, int);
319: int dev_onval(struct dev *, int, int);
320: int dev_nctl(struct dev *);
321: void dev_label(struct dev *, int);
322: struct ctl *dev_addctl(struct dev *, char *, int, int,
323: char *, int, char *, char *, int, int, int);
324: void dev_rmctl(struct dev *, int);
325: int dev_makeunit(struct dev *, char *);
1.24 ratchov 326: void dev_ctlsync(struct dev *);
1.1 ratchov 327:
328: #endif /* !defined(DEV_H) */