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