[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.30

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