Annotation of src/usr.bin/sndiod/dev.h, Revision 1.26
1.26 ! ratchov 1: /* $OpenBSD: dev.h,v 1.25 2020/04/16 12:26:55 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 */
129: unsigned int type; /* one of above */
130: unsigned int addr; /* control address */
131: #define CTL_NAMEMAX 16 /* max name lenght */
132: char func[CTL_NAMEMAX]; /* parameter function name */
133: char group[CTL_NAMEMAX]; /* group aka namespace */
134: struct ctl_node {
135: char name[CTL_NAMEMAX]; /* stream name */
136: int unit;
137: } node0, node1; /* affected channels */
138: #define CTL_DEVMASK (1 << 31)
139: #define CTL_SLOTMASK(i) (1 << (i))
140: unsigned int val_mask;
141: unsigned int desc_mask;
142: unsigned int refs_mask;
143: unsigned int maxval;
144: unsigned int curval;
145: int dirty;
146: };
147:
148: struct ctlslot {
149: struct ctlops *ops;
150: void *arg;
151: struct dev *dev;
152: unsigned int mask;
153: unsigned int mode;
154: };
155:
156: /*
1.1 ratchov 157: * audio device with plenty of slots
158: */
159: struct dev {
160: struct dev *next;
161: struct slot *slot_list; /* audio streams attached */
1.12 ratchov 162: struct opt *opt_list;
1.1 ratchov 163: struct midi *midi;
164:
165: /*
166: * audio device (while opened)
1.10 ratchov 167: */
1.3 ratchov 168: struct dev_sio sio;
1.23 ratchov 169: struct dev_sioctl sioctl;
1.1 ratchov 170: struct aparams par; /* encoding */
171: int pchan, rchan; /* play & rec channels */
172: adata_t *rbuf; /* rec buffer */
173: adata_t *pbuf; /* array of play buffers */
174: #define DEV_PBUF(d) ((d)->pbuf + (d)->poffs * (d)->pchan)
175: int poffs; /* index of current play buf */
1.8 ratchov 176: int psize; /* size of play buffer */
1.1 ratchov 177: struct conv enc; /* native->device format */
178: struct conv dec; /* device->native format */
179: unsigned char *encbuf; /* buffer for encoding */
180: unsigned char *decbuf; /* buffer for decoding */
181:
182: /*
183: * preallocated audio sub-devices
184: */
185: #define DEV_NSLOT 8
186: struct slot slot[DEV_NSLOT];
187: unsigned int serial; /* for slot allocation */
1.5 ratchov 188:
189: /*
190: * current position, relative to the current cycle
191: */
192: int delta;
1.1 ratchov 193:
194: /*
195: * desired parameters
196: */
197: unsigned int reqmode; /* mode */
198: struct aparams reqpar; /* parameters */
199: int reqpchan, reqrchan; /* play & rec chans */
200: unsigned int reqbufsz; /* buffer size */
201: unsigned int reqround; /* block size */
202: unsigned int reqrate; /* sample rate */
203: unsigned int hold; /* hold the device open ? */
204: unsigned int autovol; /* auto adjust playvol ? */
205: unsigned int refcnt; /* number of openers */
206: #define DEV_NMAX 16 /* max number of devices */
207: unsigned int num; /* device serial number */
208: #define DEV_CFG 0 /* closed */
209: #define DEV_INIT 1 /* stopped */
1.2 ratchov 210: #define DEV_RUN 2 /* playin & recording */
1.1 ratchov 211: unsigned int pstate; /* one of above */
1.22 ratchov 212: struct name *path_list;
1.1 ratchov 213:
214: /*
215: * actual parameters and runtime state (i.e. once opened)
216: */
217: unsigned int mode; /* bitmap of MODE_xxx */
218: unsigned int bufsz, round, rate;
219: unsigned int prime;
220:
221: /*
222: * MIDI time code (MTC)
223: */
224: struct {
225: unsigned int origin; /* MTC start time */
226: unsigned int fps; /* MTC frames per second */
227: #define MTC_FPS_24 0
228: #define MTC_FPS_25 1
229: #define MTC_FPS_30 3
230: unsigned int fps_id; /* one of above */
231: unsigned int hr; /* MTC hours */
232: unsigned int min; /* MTC minutes */
233: unsigned int sec; /* MTC seconds */
234: unsigned int fr; /* MTC frames */
235: unsigned int qfr; /* MTC quarter frames */
236: int delta; /* rel. to the last MTC tick */
237: int refs;
238: } mtc;
239:
240: /*
241: * MIDI machine control (MMC)
242: */
243: #define MMC_STOP 1 /* stopped, can't start */
244: #define MMC_START 2 /* attempting to start */
245: #define MMC_RUN 3 /* started */
246: unsigned int tstate; /* one of above */
1.25 ratchov 247:
248: unsigned int master; /* software vol. knob */
249: unsigned int master_enabled; /* 1 if h/w has no vo. knob */
1.23 ratchov 250:
251: /*
252: * control
253: */
254:
255: struct ctl *ctl_list;
256: #define DEV_NCTLSLOT 8
257: struct ctlslot ctlslot[DEV_NCTLSLOT];
1.1 ratchov 258: };
259:
260: extern struct dev *dev_list;
261:
262: void dev_log(struct dev *);
1.26 ! ratchov 263: void dev_abort(struct dev *);
1.22 ratchov 264: int dev_reopen(struct dev *);
1.1 ratchov 265: struct dev *dev_new(char *, struct aparams *, unsigned int, unsigned int,
266: unsigned int, unsigned int, unsigned int, unsigned int);
267: struct dev *dev_bynum(int);
268: void dev_del(struct dev *);
1.11 ratchov 269: void dev_adjpar(struct dev *, int, int, int);
1.1 ratchov 270: int dev_init(struct dev *);
271: void dev_done(struct dev *);
1.9 ratchov 272: int dev_ref(struct dev *);
273: void dev_unref(struct dev *);
1.1 ratchov 274: int dev_getpos(struct dev *);
275: unsigned int dev_roundof(struct dev *, unsigned int);
276:
277: /*
278: * interface to hardware device
279: */
280: void dev_onmove(struct dev *, int);
281: void dev_cycle(struct dev *);
282:
283: /*
284: * midi & midi call-backs
285: */
286: void dev_mmcstart(struct dev *);
287: void dev_mmcstop(struct dev *);
288: void dev_mmcloc(struct dev *, unsigned int);
289: void dev_master(struct dev *, unsigned int);
290: void dev_midi_vol(struct dev *, struct slot *);
291:
292: /*
293: * sio_open(3) like interface for clients
294: */
295: void slot_log(struct slot *);
1.21 ratchov 296: struct slot *slot_new(struct dev *, struct opt *, unsigned int, char *,
1.14 ratchov 297: struct slotops *, void *, int);
1.1 ratchov 298: void slot_del(struct slot *);
299: void slot_setvol(struct slot *, unsigned int);
300: void slot_start(struct slot *);
301: void slot_stop(struct slot *);
302: void slot_read(struct slot *);
303: void slot_write(struct slot *);
1.23 ratchov 304:
305: /*
306: * control related functions
307: */
308: void ctl_log(struct ctl *);
309: struct ctlslot *ctlslot_new(struct dev *, struct ctlops *, void *);
310: void ctlslot_del(struct ctlslot *);
311: int dev_setctl(struct dev *, int, int);
312: int dev_onval(struct dev *, int, int);
313: int dev_nctl(struct dev *);
314: void dev_label(struct dev *, int);
315: struct ctl *dev_addctl(struct dev *, char *, int, int,
316: char *, int, char *, char *, int, int, int);
317: void dev_rmctl(struct dev *, int);
318: int dev_makeunit(struct dev *, char *);
1.24 ratchov 319: void dev_ctlsync(struct dev *);
1.1 ratchov 320:
321: #endif /* !defined(DEV_H) */