[BACK]Return to dev.h CVS log [TXT][DIR] Up to [local] / src / usr.bin / sndiod

Annotation of src/usr.bin/sndiod/dev.h, Revision 1.35

1.35    ! ratchov     1: /*     $OpenBSD: dev.h,v 1.34 2021/01/29 11:21:00 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)
1.29      ratchov    27: #define CTLADDR_ALT_SEL                (CTLADDR_MASTER + 1)
                     28: #define CTLADDR_END            (CTLADDR_ALT_SEL + DEV_NMAX)
1.1       ratchov    29:
                     30: /*
1.32      ratchov    31:  * preallocated audio clients
                     32:  */
                     33: #define DEV_NSLOT      8
                     34:
                     35: /*
1.33      ratchov    36:  * preallocated control clients
                     37:  */
                     38: #define DEV_NCTLSLOT 8
                     39:
                     40: /*
1.1       ratchov    41:  * audio stream state structure
                     42:  */
                     43:
                     44: struct slotops
                     45: {
1.7       ratchov    46:        void (*onmove)(void *);                 /* clock tick */
1.23      ratchov    47:        void (*onvol)(void *);                  /* tell client vol changed */
1.1       ratchov    48:        void (*fill)(void *);                   /* request to fill a play block */
                     49:        void (*flush)(void *);                  /* request to flush a rec block */
                     50:        void (*eof)(void *);                    /* notify that play drained */
                     51:        void (*exit)(void *);                   /* delete client */
                     52: };
                     53:
1.23      ratchov    54: struct ctlops
                     55: {
                     56:        void (*exit)(void *);                   /* delete client */
1.24      ratchov    57:        void (*sync)(void *);                   /* description ready */
1.23      ratchov    58: };
                     59:
1.1       ratchov    60: struct slot {
                     61:        struct slotops *ops;                    /* client callbacks */
                     62:        struct slot *next;                      /* next on the play list */
                     63:        struct dev *dev;                        /* device this belongs to */
1.13      ratchov    64:        struct opt *opt;                        /* config used */
1.1       ratchov    65:        void *arg;                              /* user data for callbacks */
                     66:        struct aparams par;                     /* socket side params */
                     67:        struct {
1.10      ratchov    68:                int weight;                     /* dynamic range */
1.1       ratchov    69:                unsigned int vol;               /* volume within the vol */
                     70:                struct abuf buf;                /* socket side buffer */
                     71:                int bpf;                        /* byte per frame */
1.19      ratchov    72:                int nch;                        /* number of play chans */
1.1       ratchov    73:                struct cmap cmap;               /* channel mapper state */
                     74:                struct resamp resamp;           /* resampler state */
                     75:                struct conv dec;                /* format decoder params */
                     76:                int join;                       /* channel join factor */
                     77:                int expand;                     /* channel expand factor */
                     78:                void *resampbuf, *decbuf;       /* tmp buffers */
                     79:        } mix;
                     80:        struct {
                     81:                struct abuf buf;                /* socket side buffer */
1.5       ratchov    82:                int prime;                      /* initial cycles to skip */
1.1       ratchov    83:                int bpf;                        /* byte per frame */
1.19      ratchov    84:                int nch;                        /* number of rec chans */
1.1       ratchov    85:                struct cmap cmap;               /* channel mapper state */
                     86:                struct resamp resamp;           /* buffer for resampling */
                     87:                struct conv enc;                /* buffer for encoding */
                     88:                int join;                       /* channel join factor */
                     89:                int expand;                     /* channel expand factor */
                     90:                void *resampbuf, *encbuf;       /* tmp buffers */
                     91:        } sub;
                     92:        int xrun;                               /* underrun policy */
1.5       ratchov    93:        int skip;                               /* cycles to skip (for xrun) */
1.1       ratchov    94: #define SLOT_BUFSZ(s) \
                     95:        ((s)->appbufsz + (s)->dev->bufsz / (s)->dev->round * (s)->round)
                     96:        int appbufsz;                           /* slot-side buffer size */
                     97:        int round;                              /* slot-side block size */
                     98:        int rate;                               /* slot-side sample rate */
                     99:        int delta;                              /* pending clock ticks */
                    100:        int delta_rem;                          /* remainder for delta */
                    101:        int mode;                               /* MODE_{PLAY,REC} */
                    102: #define SLOT_INIT      0                       /* not trying to do anything */
                    103: #define SLOT_START     1                       /* buffer allocated */
                    104: #define SLOT_READY     2                       /* buffer filled enough */
                    105: #define SLOT_RUN       3                       /* buffer attached to device */
                    106: #define SLOT_STOP      4                       /* draining */
                    107:        int pstate;
                    108:
                    109: #define SLOT_NAMEMAX   8
                    110:        char name[SLOT_NAMEMAX];                /* name matching [a-z]+ */
                    111:        unsigned int unit;                      /* instance of name */
                    112:        unsigned int serial;                    /* global unique number */
                    113:        unsigned int vol;                       /* current (midi) volume */
1.21      ratchov   114:        unsigned int id;                        /* process id */
1.1       ratchov   115: };
                    116:
                    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 */
1.28      ratchov   128: #define CTL_SEL                6               /* element of a selector */
1.23      ratchov   129:        unsigned int type;              /* one of above */
                    130:        unsigned int addr;              /* control address */
                    131: #define CTL_NAMEMAX    16              /* max name lenght */
                    132:        char func[CTL_NAMEMAX];         /* parameter function name */
                    133:        char group[CTL_NAMEMAX];        /* group aka namespace */
                    134:        struct ctl_node {
                    135:                char name[CTL_NAMEMAX]; /* stream name */
                    136:                int unit;
                    137:        } node0, node1;                 /* affected channels */
                    138: #define CTL_DEVMASK            (1 << 31)
                    139: #define CTL_SLOTMASK(i)                (1 << (i))
                    140:        unsigned int val_mask;
                    141:        unsigned int desc_mask;
                    142:        unsigned int refs_mask;
                    143:        unsigned int maxval;
                    144:        unsigned int curval;
                    145:        int dirty;
                    146: };
                    147:
                    148: struct ctlslot {
                    149:        struct ctlops *ops;
                    150:        void *arg;
                    151:        struct dev *dev;
1.35    ! ratchov   152:        unsigned int self;              /* equal to (1 << index) */
1.23      ratchov   153:        unsigned int mode;
                    154: };
                    155:
                    156: /*
1.1       ratchov   157:  * audio device with plenty of slots
                    158:  */
                    159: struct dev {
                    160:        struct dev *next;
                    161:        struct slot *slot_list;                 /* audio streams attached */
                    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:        /*
1.5       ratchov   182:         * current position, relative to the current cycle
                    183:         */
                    184:        int delta;
1.1       ratchov   185:
                    186:        /*
                    187:         * desired parameters
                    188:         */
                    189:        unsigned int reqmode;                   /* mode */
                    190:        struct aparams reqpar;                  /* parameters */
                    191:        int reqpchan, reqrchan;                 /* play & rec chans */
                    192:        unsigned int reqbufsz;                  /* buffer size */
                    193:        unsigned int reqround;                  /* block size */
                    194:        unsigned int reqrate;                   /* sample rate */
                    195:        unsigned int hold;                      /* hold the device open ? */
                    196:        unsigned int autovol;                   /* auto adjust playvol ? */
                    197:        unsigned int refcnt;                    /* number of openers */
                    198: #define DEV_NMAX       16                      /* max number of devices */
                    199:        unsigned int num;                       /* device serial number */
                    200: #define DEV_CFG                0                       /* closed */
                    201: #define DEV_INIT       1                       /* stopped */
1.2       ratchov   202: #define DEV_RUN                2                       /* playin & recording */
1.1       ratchov   203:        unsigned int pstate;                    /* one of above */
1.27      ratchov   204:        struct dev_alt {
                    205:                struct dev_alt *next;
                    206:                char *name;
                    207:                unsigned int idx;
                    208:        } *alt_list;
                    209:        int alt_num;
1.1       ratchov   210:
                    211:        /*
                    212:         * actual parameters and runtime state (i.e. once opened)
                    213:         */
                    214:        unsigned int mode;                      /* bitmap of MODE_xxx */
                    215:        unsigned int bufsz, round, rate;
                    216:        unsigned int prime;
                    217:
                    218:        /*
                    219:         * MIDI time code (MTC)
                    220:         */
                    221:        struct {
                    222:                unsigned int origin;            /* MTC start time */
                    223:                unsigned int fps;               /* MTC frames per second */
                    224: #define MTC_FPS_24     0
                    225: #define MTC_FPS_25     1
                    226: #define MTC_FPS_30     3
                    227:                unsigned int fps_id;            /* one of above */
                    228:                unsigned int hr;                /* MTC hours */
                    229:                unsigned int min;               /* MTC minutes */
                    230:                unsigned int sec;               /* MTC seconds */
                    231:                unsigned int fr;                /* MTC frames */
                    232:                unsigned int qfr;               /* MTC quarter frames */
                    233:                int delta;                      /* rel. to the last MTC tick */
                    234:                int refs;
                    235:        } mtc;
                    236:
                    237:        /*
                    238:         * MIDI machine control (MMC)
                    239:         */
                    240: #define MMC_STOP       1                       /* stopped, can't start */
                    241: #define MMC_START      2                       /* attempting to start */
                    242: #define MMC_RUN                3                       /* started */
                    243:        unsigned int tstate;                    /* one of above */
1.25      ratchov   244:
                    245:        unsigned int master;                    /* software vol. knob */
                    246:        unsigned int master_enabled;            /* 1 if h/w has no vo. knob */
1.23      ratchov   247:
                    248:        /*
                    249:         * control
                    250:         */
                    251:
                    252:        struct ctl *ctl_list;
1.1       ratchov   253: };
                    254:
                    255: extern struct dev *dev_list;
1.32      ratchov   256: extern struct slot slot_array[DEV_NSLOT];
1.33      ratchov   257: extern struct ctlslot ctlslot_array[DEV_NCTLSLOT];
1.32      ratchov   258:
                    259: void slot_array_init(void);
1.1       ratchov   260:
                    261: void dev_log(struct dev *);
1.26      ratchov   262: void dev_abort(struct dev *);
1.22      ratchov   263: int dev_reopen(struct dev *);
1.1       ratchov   264: struct dev *dev_new(char *, struct aparams *, unsigned int, unsigned int,
                    265:     unsigned int, unsigned int, unsigned int, unsigned int);
                    266: struct dev *dev_bynum(int);
1.27      ratchov   267: int dev_addname(struct dev *, char *);
1.1       ratchov   268: void dev_del(struct dev *);
1.11      ratchov   269: void dev_adjpar(struct dev *, int, int, int);
1.1       ratchov   270: int  dev_init(struct dev *);
                    271: void dev_done(struct dev *);
1.9       ratchov   272: int dev_ref(struct dev *);
                    273: void dev_unref(struct dev *);
1.1       ratchov   274: int  dev_getpos(struct dev *);
                    275: unsigned int dev_roundof(struct dev *, unsigned int);
                    276:
                    277: /*
                    278:  * interface to hardware device
                    279:  */
                    280: void dev_onmove(struct dev *, int);
                    281: void dev_cycle(struct dev *);
                    282:
                    283: /*
                    284:  * midi & midi call-backs
                    285:  */
                    286: void dev_mmcstart(struct dev *);
                    287: void dev_mmcstop(struct dev *);
                    288: void dev_mmcloc(struct dev *, unsigned int);
                    289: void dev_master(struct dev *, unsigned int);
                    290: void dev_midi_vol(struct dev *, struct slot *);
                    291:
                    292: /*
                    293:  * sio_open(3) like interface for clients
                    294:  */
                    295: void slot_log(struct slot *);
1.21      ratchov   296: struct slot *slot_new(struct dev *, struct opt *, unsigned int, char *,
1.14      ratchov   297:     struct slotops *, void *, int);
1.1       ratchov   298: void slot_del(struct slot *);
                    299: void slot_setvol(struct slot *, unsigned int);
                    300: void slot_start(struct slot *);
1.31      ratchov   301: void slot_stop(struct slot *, int);
1.1       ratchov   302: void slot_read(struct slot *);
                    303: void slot_write(struct slot *);
1.30      ratchov   304: void slot_initconv(struct slot *);
                    305: void slot_attach(struct slot *);
                    306: void slot_detach(struct slot *);
1.23      ratchov   307:
                    308: /*
                    309:  * control related functions
                    310:  */
                    311: void ctl_log(struct ctl *);
                    312: struct ctlslot *ctlslot_new(struct dev *, struct ctlops *, void *);
                    313: void ctlslot_del(struct ctlslot *);
                    314: int dev_setctl(struct dev *, int, int);
                    315: int dev_onval(struct dev *, int, int);
                    316: int dev_nctl(struct dev *);
                    317: void dev_label(struct dev *, int);
                    318: struct ctl *dev_addctl(struct dev *, char *, int, int,
                    319:     char *, int, char *, char *, int, int, int);
                    320: void dev_rmctl(struct dev *, int);
                    321: int dev_makeunit(struct dev *, char *);
1.24      ratchov   322: void dev_ctlsync(struct dev *);
1.1       ratchov   323:
                    324: #endif /* !defined(DEV_H) */