Annotation of src/usr.bin/sndiod/dev.h, Revision 1.16
1.16 ! ratchov 1: /* $OpenBSD: dev.h,v 1.15 2018/06/26 07:22: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"
23:
24: /*
25: * audio stream state structure
26: */
27:
28: struct slotops
29: {
1.7 ratchov 30: void (*onmove)(void *); /* clock tick */
1.11 ratchov 31: void (*onvol)(void *); /* tell client vol changed */
1.1 ratchov 32: void (*fill)(void *); /* request to fill a play block */
33: void (*flush)(void *); /* request to flush a rec block */
34: void (*eof)(void *); /* notify that play drained */
35: void (*exit)(void *); /* delete client */
36: };
37:
38: struct slot {
39: struct slotops *ops; /* client callbacks */
40: struct slot *next; /* next on the play list */
41: struct dev *dev; /* device this belongs to */
1.13 ratchov 42: struct opt *opt; /* config used */
1.1 ratchov 43: void *arg; /* user data for callbacks */
44: struct aparams par; /* socket side params */
45: struct {
1.10 ratchov 46: int weight; /* dynamic range */
1.1 ratchov 47: unsigned int vol; /* volume within the vol */
48: struct abuf buf; /* socket side buffer */
49: int bpf; /* byte per frame */
50: int slot_cmin, slot_cmax; /* slot source chans */
51: int dev_cmin, dev_cmax; /* device destination chans */
52: struct cmap cmap; /* channel mapper state */
53: struct resamp resamp; /* resampler state */
54: struct conv dec; /* format decoder params */
55: int join; /* channel join factor */
56: int expand; /* channel expand factor */
57: void *resampbuf, *decbuf; /* tmp buffers */
58: } mix;
59: struct {
60: struct abuf buf; /* socket side buffer */
1.5 ratchov 61: int prime; /* initial cycles to skip */
1.1 ratchov 62: int bpf; /* byte per frame */
63: int slot_cmin, slot_cmax; /* slot destination chans */
64: int dev_cmin, dev_cmax; /* device source chans */
65: struct cmap cmap; /* channel mapper state */
66: struct resamp resamp; /* buffer for resampling */
67: struct conv enc; /* buffer for encoding */
68: int join; /* channel join factor */
69: int expand; /* channel expand factor */
70: void *resampbuf, *encbuf; /* tmp buffers */
71: } sub;
72: int xrun; /* underrun policy */
1.5 ratchov 73: int skip; /* cycles to skip (for xrun) */
1.1 ratchov 74: #define SLOT_BUFSZ(s) \
75: ((s)->appbufsz + (s)->dev->bufsz / (s)->dev->round * (s)->round)
76: int appbufsz; /* slot-side buffer size */
77: int round; /* slot-side block size */
78: int rate; /* slot-side sample rate */
79: int delta; /* pending clock ticks */
80: int delta_rem; /* remainder for delta */
81: int mode; /* MODE_{PLAY,REC} */
82: #define SLOT_INIT 0 /* not trying to do anything */
83: #define SLOT_START 1 /* buffer allocated */
84: #define SLOT_READY 2 /* buffer filled enough */
85: #define SLOT_RUN 3 /* buffer attached to device */
86: #define SLOT_STOP 4 /* draining */
87: int pstate;
88:
89: #define SLOT_NAMEMAX 8
90: char name[SLOT_NAMEMAX]; /* name matching [a-z]+ */
91: unsigned int unit; /* instance of name */
92: unsigned int serial; /* global unique number */
93: unsigned int vol; /* current (midi) volume */
94: unsigned int tstate; /* mmc state */
95: };
96:
1.12 ratchov 97: struct opt {
98: struct opt *next;
99: #define OPT_NAMEMAX 11
100: char name[OPT_NAMEMAX + 1];
101: int maxweight; /* max dynamic range for clients */
102: int pmin, pmax; /* play channels */
103: int rmin, rmax; /* recording channels */
104: int mmc; /* true if MMC control enabled */
105: int dup; /* true if join/expand enabled */
106: int mode; /* bitmap of MODE_XXX */
107: };
108:
1.1 ratchov 109: /*
110: * audio device with plenty of slots
111: */
112: struct dev {
113: struct dev *next;
114: struct slot *slot_list; /* audio streams attached */
1.12 ratchov 115: struct opt *opt_list;
1.1 ratchov 116: struct midi *midi;
117:
118: /*
119: * audio device (while opened)
1.10 ratchov 120: */
1.3 ratchov 121: struct dev_sio sio;
1.1 ratchov 122: struct aparams par; /* encoding */
123: int pchan, rchan; /* play & rec channels */
124: adata_t *rbuf; /* rec buffer */
125: adata_t *pbuf; /* array of play buffers */
126: #define DEV_PBUF(d) ((d)->pbuf + (d)->poffs * (d)->pchan)
127: int poffs; /* index of current play buf */
1.8 ratchov 128: int psize; /* size of play buffer */
1.1 ratchov 129: struct conv enc; /* native->device format */
130: struct conv dec; /* device->native format */
131: unsigned char *encbuf; /* buffer for encoding */
132: unsigned char *decbuf; /* buffer for decoding */
133:
134: /*
135: * preallocated audio sub-devices
136: */
137: #define DEV_NSLOT 8
138: struct slot slot[DEV_NSLOT];
139: unsigned int serial; /* for slot allocation */
1.5 ratchov 140:
141: /*
142: * current position, relative to the current cycle
143: */
144: int delta;
1.1 ratchov 145:
146: /*
147: * desired parameters
148: */
149: unsigned int reqmode; /* mode */
150: struct aparams reqpar; /* parameters */
151: int reqpchan, reqrchan; /* play & rec chans */
152: unsigned int reqbufsz; /* buffer size */
153: unsigned int reqround; /* block size */
154: unsigned int reqrate; /* sample rate */
155: unsigned int hold; /* hold the device open ? */
156: unsigned int autovol; /* auto adjust playvol ? */
157: unsigned int refcnt; /* number of openers */
158: #define DEV_NMAX 16 /* max number of devices */
159: unsigned int num; /* device serial number */
160: #define DEV_CFG 0 /* closed */
161: #define DEV_INIT 1 /* stopped */
1.2 ratchov 162: #define DEV_RUN 2 /* playin & recording */
1.1 ratchov 163: unsigned int pstate; /* one of above */
164: char *path; /* sio path */
165:
166: /*
167: * actual parameters and runtime state (i.e. once opened)
168: */
169: unsigned int mode; /* bitmap of MODE_xxx */
170: unsigned int bufsz, round, rate;
171: unsigned int prime;
172:
173: /*
174: * MIDI time code (MTC)
175: */
176: struct {
177: unsigned int origin; /* MTC start time */
178: unsigned int fps; /* MTC frames per second */
179: #define MTC_FPS_24 0
180: #define MTC_FPS_25 1
181: #define MTC_FPS_30 3
182: unsigned int fps_id; /* one of above */
183: unsigned int hr; /* MTC hours */
184: unsigned int min; /* MTC minutes */
185: unsigned int sec; /* MTC seconds */
186: unsigned int fr; /* MTC frames */
187: unsigned int qfr; /* MTC quarter frames */
188: int delta; /* rel. to the last MTC tick */
189: int refs;
190: } mtc;
191:
192: /*
193: * MIDI machine control (MMC)
194: */
195: #define MMC_OFF 0 /* ignore MMC messages */
196: #define MMC_STOP 1 /* stopped, can't start */
197: #define MMC_START 2 /* attempting to start */
198: #define MMC_RUN 3 /* started */
199: unsigned int tstate; /* one of above */
200: unsigned int master; /* master volume controller */
201: };
202:
203: extern struct dev *dev_list;
204:
205: void dev_log(struct dev *);
206: void dev_close(struct dev *);
207: struct dev *dev_new(char *, struct aparams *, unsigned int, unsigned int,
208: unsigned int, unsigned int, unsigned int, unsigned int);
209: struct dev *dev_bynum(int);
210: void dev_del(struct dev *);
1.11 ratchov 211: void dev_adjpar(struct dev *, int, int, int);
1.1 ratchov 212: int dev_init(struct dev *);
213: void dev_done(struct dev *);
1.9 ratchov 214: int dev_ref(struct dev *);
215: void dev_unref(struct dev *);
1.1 ratchov 216: int dev_getpos(struct dev *);
217: unsigned int dev_roundof(struct dev *, unsigned int);
218:
219: /*
220: * interface to hardware device
221: */
222: void dev_onmove(struct dev *, int);
223: void dev_cycle(struct dev *);
224:
225: /*
226: * midi & midi call-backs
227: */
228: void dev_mmcstart(struct dev *);
229: void dev_mmcstop(struct dev *);
230: void dev_mmcloc(struct dev *, unsigned int);
231: void dev_master(struct dev *, unsigned int);
232: void dev_midi_vol(struct dev *, struct slot *);
233:
234: /*
235: * sio_open(3) like interface for clients
236: */
237: void slot_log(struct slot *);
1.14 ratchov 238: struct slot *slot_new(struct dev *, struct opt *, char *,
239: struct slotops *, void *, int);
1.1 ratchov 240: void slot_del(struct slot *);
241: void slot_setvol(struct slot *, unsigned int);
242: void slot_start(struct slot *);
243: void slot_stop(struct slot *);
244: void slot_read(struct slot *);
245: void slot_write(struct slot *);
246:
247: #endif /* !defined(DEV_H) */