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

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