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