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