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