Annotation of src/usr.bin/sndiod/dev.h, Revision 1.5
1.5 ! ratchov 1: /* $OpenBSD: dev.h,v 1.4 2014/03/05 20:24:16 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: struct abuf buf; /* socket side buffer */
52: int bpf; /* byte per frame */
53: int slot_cmin, slot_cmax; /* slot source chans */
54: int dev_cmin, dev_cmax; /* device destination chans */
55: struct cmap cmap; /* channel mapper state */
56: struct resamp resamp; /* resampler state */
57: struct conv dec; /* format decoder params */
58: int join; /* channel join factor */
59: int expand; /* channel expand factor */
60: void *resampbuf, *decbuf; /* tmp buffers */
61: } mix;
62: struct {
63: struct abuf buf; /* socket side buffer */
1.5 ! ratchov 64: int prime; /* initial cycles to skip */
1.1 ratchov 65: int bpf; /* byte per frame */
66: int slot_cmin, slot_cmax; /* slot destination chans */
67: int dev_cmin, dev_cmax; /* device source chans */
68: struct cmap cmap; /* channel mapper state */
69: struct resamp resamp; /* buffer for resampling */
70: struct conv enc; /* buffer for encoding */
71: int join; /* channel join factor */
72: int expand; /* channel expand factor */
73: void *resampbuf, *encbuf; /* tmp buffers */
74: } sub;
75: int xrun; /* underrun policy */
1.5 ! ratchov 76: int skip; /* cycles to skip (for xrun) */
1.1 ratchov 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 */
1.5 ! ratchov 130:
! 131: /*
! 132: * current position, relative to the current cycle
! 133: */
! 134: int delta;
1.1 ratchov 135:
136: /*
137: * desired parameters
138: */
139: unsigned int reqmode; /* mode */
140: struct aparams reqpar; /* parameters */
141: int reqpchan, reqrchan; /* play & rec chans */
142: unsigned int reqbufsz; /* buffer size */
143: unsigned int reqround; /* block size */
144: unsigned int reqrate; /* sample rate */
145: unsigned int hold; /* hold the device open ? */
146: unsigned int autovol; /* auto adjust playvol ? */
147: unsigned int refcnt; /* number of openers */
148: #define DEV_NMAX 16 /* max number of devices */
149: unsigned int num; /* device serial number */
150: #define DEV_CFG 0 /* closed */
151: #define DEV_INIT 1 /* stopped */
1.2 ratchov 152: #define DEV_RUN 2 /* playin & recording */
1.1 ratchov 153: unsigned int pstate; /* one of above */
154: char *path; /* sio path */
155:
156: /*
157: * actual parameters and runtime state (i.e. once opened)
158: */
159: unsigned int mode; /* bitmap of MODE_xxx */
160: unsigned int bufsz, round, rate;
161: unsigned int prime;
162:
163: /*
164: * MIDI time code (MTC)
165: */
166: struct {
167: unsigned int origin; /* MTC start time */
168: unsigned int fps; /* MTC frames per second */
169: #define MTC_FPS_24 0
170: #define MTC_FPS_25 1
171: #define MTC_FPS_30 3
172: unsigned int fps_id; /* one of above */
173: unsigned int hr; /* MTC hours */
174: unsigned int min; /* MTC minutes */
175: unsigned int sec; /* MTC seconds */
176: unsigned int fr; /* MTC frames */
177: unsigned int qfr; /* MTC quarter frames */
178: int delta; /* rel. to the last MTC tick */
179: int refs;
180: } mtc;
181:
182: /*
183: * MIDI machine control (MMC)
184: */
185: #define MMC_OFF 0 /* ignore MMC messages */
186: #define MMC_STOP 1 /* stopped, can't start */
187: #define MMC_START 2 /* attempting to start */
188: #define MMC_RUN 3 /* started */
189: unsigned int tstate; /* one of above */
190: unsigned int master; /* master volume controller */
191: };
192:
193: extern struct dev *dev_list;
194:
195: void dev_log(struct dev *);
196: void dev_close(struct dev *);
197: struct dev *dev_new(char *, struct aparams *, unsigned int, unsigned int,
198: unsigned int, unsigned int, unsigned int, unsigned int);
199: struct dev *dev_bynum(int);
200: void dev_del(struct dev *);
201: void dev_adjpar(struct dev *, int, int, int, int, int);
202: int dev_init(struct dev *);
203: void dev_done(struct dev *);
204: int dev_getpos(struct dev *);
205: unsigned int dev_roundof(struct dev *, unsigned int);
206:
207: /*
208: * interface to hardware device
209: */
210: void dev_onmove(struct dev *, int);
211: void dev_cycle(struct dev *);
212:
213: /*
214: * midi & midi call-backs
215: */
216: void dev_mmcstart(struct dev *);
217: void dev_mmcstop(struct dev *);
218: void dev_mmcloc(struct dev *, unsigned int);
219: void dev_master(struct dev *, unsigned int);
220: void dev_midi_vol(struct dev *, struct slot *);
221:
222: /*
223: * sio_open(3) like interface for clients
224: */
225: void slot_log(struct slot *);
226: struct slot *slot_new(struct dev *, char *, struct slotops *, void *, int);
227: void slot_del(struct slot *);
228: void slot_setvol(struct slot *, unsigned int);
229: void slot_start(struct slot *);
230: void slot_stop(struct slot *);
231: void slot_read(struct slot *);
232: void slot_write(struct slot *);
233:
234: #endif /* !defined(DEV_H) */