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

Annotation of src/usr.bin/sndiod/dsp.h, Revision 1.13

1.13    ! ratchov     1: /*     $OpenBSD: dsp.h,v 1.12 2022/12/26 19:16:03 jmc Exp $    */
1.1       ratchov     2: /*
                      3:  * Copyright (c) 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 DSP_H
                     18: #define DSP_H
                     19:
                     20: #include <sys/types.h>
                     21: #include "defs.h"
                     22:
                     23: /*
                     24:  * Samples are numbers in the interval [-1, 1[, note that 1, the upper
                     25:  * boundary is excluded. We represent them as signed fixed point numbers
                     26:  * of ADATA_BITS. We also assume that 2^(ADATA_BITS - 1) fits in a int.
                     27:  */
1.11      ratchov    28: #define ADATA_BITS                     24
1.1       ratchov    29: #define ADATA_LE                       (BYTE_ORDER == LITTLE_ENDIAN)
                     30: #define ADATA_UNIT                     (1 << (ADATA_BITS - 1))
                     31:
                     32: #define ADATA_MUL(x,y)         \
                     33:        ((int)(((long long)(x) * (long long)(y)) >> (ADATA_BITS - 1)))
                     34:
                     35: typedef int adata_t;
                     36:
                     37: /*
1.8       ratchov    38:  * The FIR is sampled and stored in a table of fixed-point numbers
                     39:  * with 23 fractional bits. For convenience, we use the same fixed-point
                     40:  * numbers to represent time and to walk through the table.
                     41:  */
                     42: #define RESAMP_BITS            23
                     43: #define RESAMP_UNIT            (1 << RESAMP_BITS)
                     44:
                     45: /*
                     46:  * Filter window length (the time unit is RESAMP_UNIT)
                     47:  */
                     48: #define RESAMP_LENGTH          (8 * RESAMP_UNIT)
                     49:
                     50: /*
                     51:  * Time between samples of the FIR (the time unit is RESAMP_UNIT)
                     52:  */
                     53: #define RESAMP_STEP_BITS       (RESAMP_BITS - 6)
                     54: #define RESAMP_STEP            (1 << RESAMP_STEP_BITS)
                     55:
                     56: /*
                     57:  * Maximum downsample/upsample ratio we support, must be a power of two.
                     58:  * The ratio between the max and the min sample rates is 192kHz / 4kHz = 48,
                     59:  * so we can use 64
                     60:  */
                     61: #define RESAMP_RATIO           64
                     62:
                     63: /*
1.12      jmc        64:  * Maximum size of the encoding string (the longest possible
1.1       ratchov    65:  * encoding is ``s24le3msb'').
                     66:  */
                     67: #define ENCMAX 10
                     68:
                     69: /*
                     70:  * Default bytes per sample for the given bits per sample.
                     71:  */
                     72: #define APARAMS_BPS(bits) (((bits) <= 8) ? 1 : (((bits) <= 16) ? 2 : 4))
                     73:
                     74: struct aparams {
                     75:        unsigned int bps;               /* bytes per sample */
                     76:        unsigned int bits;              /* actually used bits */
1.5       ratchov    77:        unsigned int le;                /* 1 if little endian, else be */
1.1       ratchov    78:        unsigned int sig;               /* 1 if signed, 0 if unsigned */
1.5       ratchov    79:        unsigned int msb;               /* 1 if msb justified, else lsb */
1.1       ratchov    80: };
                     81:
                     82: struct resamp {
1.8       ratchov    83: #define RESAMP_NCTX    (RESAMP_LENGTH / RESAMP_UNIT * RESAMP_RATIO)
1.1       ratchov    84:        unsigned int ctx_start;
                     85:        adata_t ctx[NCHAN_MAX * RESAMP_NCTX];
1.8       ratchov    86:        int filt_cutoff, filt_step;
1.1       ratchov    87:        unsigned int iblksz, oblksz;
1.13    ! ratchov    88:        int diff;
1.1       ratchov    89:        int nch;
                     90: };
                     91:
                     92: struct conv {
                     93:        int bfirst;                     /* bytes to skip at startup */
                     94:        unsigned int bps;               /* bytes per sample */
                     95:        unsigned int shift;             /* shift to get 32bit MSB */
1.5       ratchov    96:        unsigned int bias;              /* bias of unsigned samples */
1.1       ratchov    97:        int bnext;                      /* to reach the next byte */
                     98:        int snext;                      /* to reach the next sample */
                     99:        int nch;
                    100: };
                    101:
                    102: struct cmap {
                    103:        int istart;
                    104:        int inext;
                    105:        int onext;
                    106:        int ostart;
                    107:        int nch;
                    108: };
                    109:
                    110: #define MIDI_TO_ADATA(m)       (aparams_ctltovol[m] << (ADATA_BITS - 16))
1.9       naddy     111: extern const int aparams_ctltovol[128];
1.1       ratchov   112:
                    113: void aparams_init(struct aparams *);
                    114: void aparams_log(struct aparams *);
                    115: int aparams_strtoenc(struct aparams *, char *);
                    116: int aparams_enctostr(struct aparams *, char *);
                    117: int aparams_native(struct aparams *);
                    118:
1.13    ! ratchov   119: void resamp_getcnt(struct resamp *, int *, int *);
        !           120: void resamp_do(struct resamp *, adata_t *, adata_t *, int, int);
1.1       ratchov   121: void resamp_init(struct resamp *, unsigned int, unsigned int, int);
                    122: void enc_do(struct conv *, unsigned char *, unsigned char *, int);
                    123: void enc_sil_do(struct conv *, unsigned char *, int);
                    124: void enc_init(struct conv *, struct aparams *, int);
                    125: void dec_do(struct conv *, unsigned char *, unsigned char *, int);
                    126: void dec_init(struct conv *, struct aparams *, int);
                    127: void cmap_add(struct cmap *, void *, void *, int, int);
                    128: void cmap_copy(struct cmap *, void *, void *, int, int);
                    129: void cmap_init(struct cmap *, int, int, int, int, int, int, int, int);
                    130:
                    131: #endif /* !defined(DSP_H) */