1*7b639200Sratchov /* $OpenBSD: dsp.h,v 1.15 2024/12/20 07:35:56 ratchov Exp $ */ 287bc9f6aSratchov /* 387bc9f6aSratchov * Copyright (c) 2012 Alexandre Ratchov <alex@caoua.org> 487bc9f6aSratchov * 587bc9f6aSratchov * Permission to use, copy, modify, and distribute this software for any 687bc9f6aSratchov * purpose with or without fee is hereby granted, provided that the above 787bc9f6aSratchov * copyright notice and this permission notice appear in all copies. 887bc9f6aSratchov * 987bc9f6aSratchov * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 1087bc9f6aSratchov * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 1187bc9f6aSratchov * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 1287bc9f6aSratchov * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 1387bc9f6aSratchov * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 1487bc9f6aSratchov * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 1587bc9f6aSratchov * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1687bc9f6aSratchov */ 1787bc9f6aSratchov #ifndef DSP_H 1887bc9f6aSratchov #define DSP_H 1987bc9f6aSratchov 2087bc9f6aSratchov #include <sys/types.h> 2187bc9f6aSratchov #include "defs.h" 2287bc9f6aSratchov 2387bc9f6aSratchov /* 2487bc9f6aSratchov * Samples are numbers in the interval [-1, 1[, note that 1, the upper 2587bc9f6aSratchov * boundary is excluded. We represent them as signed fixed point numbers 2687bc9f6aSratchov * of ADATA_BITS. We also assume that 2^(ADATA_BITS - 1) fits in a int. 2787bc9f6aSratchov */ 2819e766d9Sratchov #define ADATA_BITS 24 2987bc9f6aSratchov #define ADATA_LE (BYTE_ORDER == LITTLE_ENDIAN) 3087bc9f6aSratchov #define ADATA_UNIT (1 << (ADATA_BITS - 1)) 3187bc9f6aSratchov 3287bc9f6aSratchov #define ADATA_MUL(x,y) \ 3387bc9f6aSratchov ((int)(((long long)(x) * (long long)(y)) >> (ADATA_BITS - 1))) 3487bc9f6aSratchov 3587bc9f6aSratchov typedef int adata_t; 3687bc9f6aSratchov 3787bc9f6aSratchov /* 383220ebbaSratchov * The FIR is sampled and stored in a table of fixed-point numbers 393220ebbaSratchov * with 23 fractional bits. For convenience, we use the same fixed-point 403220ebbaSratchov * numbers to represent time and to walk through the table. 413220ebbaSratchov */ 423220ebbaSratchov #define RESAMP_BITS 23 433220ebbaSratchov #define RESAMP_UNIT (1 << RESAMP_BITS) 443220ebbaSratchov 453220ebbaSratchov /* 463220ebbaSratchov * Filter window length (the time unit is RESAMP_UNIT) 473220ebbaSratchov */ 483220ebbaSratchov #define RESAMP_LENGTH (8 * RESAMP_UNIT) 493220ebbaSratchov 503220ebbaSratchov /* 513220ebbaSratchov * Time between samples of the FIR (the time unit is RESAMP_UNIT) 523220ebbaSratchov */ 533220ebbaSratchov #define RESAMP_STEP_BITS (RESAMP_BITS - 6) 543220ebbaSratchov #define RESAMP_STEP (1 << RESAMP_STEP_BITS) 553220ebbaSratchov 563220ebbaSratchov /* 573220ebbaSratchov * Maximum downsample/upsample ratio we support, must be a power of two. 583220ebbaSratchov * The ratio between the max and the min sample rates is 192kHz / 4kHz = 48, 593220ebbaSratchov * so we can use 64 603220ebbaSratchov */ 613220ebbaSratchov #define RESAMP_RATIO 64 623220ebbaSratchov 633220ebbaSratchov /* 64d9a51c35Sjmc * Maximum size of the encoding string (the longest possible 6587bc9f6aSratchov * encoding is ``s24le3msb''). 6687bc9f6aSratchov */ 6787bc9f6aSratchov #define ENCMAX 10 6887bc9f6aSratchov 6987bc9f6aSratchov /* 7087bc9f6aSratchov * Default bytes per sample for the given bits per sample. 7187bc9f6aSratchov */ 7287bc9f6aSratchov #define APARAMS_BPS(bits) (((bits) <= 8) ? 1 : (((bits) <= 16) ? 2 : 4)) 7387bc9f6aSratchov 7487bc9f6aSratchov struct aparams { 7587bc9f6aSratchov unsigned int bps; /* bytes per sample */ 7687bc9f6aSratchov unsigned int bits; /* actually used bits */ 7726308fb1Sratchov unsigned int le; /* 1 if little endian, else be */ 7887bc9f6aSratchov unsigned int sig; /* 1 if signed, 0 if unsigned */ 7926308fb1Sratchov unsigned int msb; /* 1 if msb justified, else lsb */ 8087bc9f6aSratchov }; 8187bc9f6aSratchov 8287bc9f6aSratchov struct resamp { 833220ebbaSratchov #define RESAMP_NCTX (RESAMP_LENGTH / RESAMP_UNIT * RESAMP_RATIO) 8487bc9f6aSratchov unsigned int ctx_start; 8587bc9f6aSratchov adata_t ctx[NCHAN_MAX * RESAMP_NCTX]; 863220ebbaSratchov int filt_cutoff, filt_step; 8787bc9f6aSratchov unsigned int iblksz, oblksz; 88b47fb7daSratchov int diff; 8987bc9f6aSratchov int nch; 9087bc9f6aSratchov }; 9187bc9f6aSratchov 9287bc9f6aSratchov struct conv { 9387bc9f6aSratchov int bfirst; /* bytes to skip at startup */ 9487bc9f6aSratchov unsigned int bps; /* bytes per sample */ 9587bc9f6aSratchov unsigned int shift; /* shift to get 32bit MSB */ 960569a3deSratchov unsigned int bias; /* bias of unsigned samples */ 9787bc9f6aSratchov int bnext; /* to reach the next byte */ 9887bc9f6aSratchov int snext; /* to reach the next sample */ 9987bc9f6aSratchov int nch; 10087bc9f6aSratchov }; 10187bc9f6aSratchov 10287bc9f6aSratchov struct cmap { 10387bc9f6aSratchov int istart; 10487bc9f6aSratchov int inext; 10587bc9f6aSratchov int onext; 10687bc9f6aSratchov int ostart; 10787bc9f6aSratchov int nch; 10887bc9f6aSratchov }; 10987bc9f6aSratchov 110be3fa683Sratchov #define MIDI_TO_ADATA(m) (aparams_ctltovol[m]) 111b020cfe1Snaddy extern const int aparams_ctltovol[128]; 11287bc9f6aSratchov 11387bc9f6aSratchov void aparams_init(struct aparams *); 11487bc9f6aSratchov int aparams_strtoenc(struct aparams *, char *); 11587bc9f6aSratchov int aparams_enctostr(struct aparams *, char *); 11687bc9f6aSratchov int aparams_native(struct aparams *); 11787bc9f6aSratchov 118b47fb7daSratchov void resamp_getcnt(struct resamp *, int *, int *); 119b47fb7daSratchov void resamp_do(struct resamp *, adata_t *, adata_t *, int, int); 12087bc9f6aSratchov void resamp_init(struct resamp *, unsigned int, unsigned int, int); 12187bc9f6aSratchov void enc_do(struct conv *, unsigned char *, unsigned char *, int); 12287bc9f6aSratchov void enc_sil_do(struct conv *, unsigned char *, int); 12387bc9f6aSratchov void enc_init(struct conv *, struct aparams *, int); 12487bc9f6aSratchov void dec_do(struct conv *, unsigned char *, unsigned char *, int); 12587bc9f6aSratchov void dec_init(struct conv *, struct aparams *, int); 12687bc9f6aSratchov void cmap_add(struct cmap *, void *, void *, int, int); 12787bc9f6aSratchov void cmap_copy(struct cmap *, void *, void *, int, int); 12887bc9f6aSratchov void cmap_init(struct cmap *, int, int, int, int, int, int, int, int); 12987bc9f6aSratchov 13087bc9f6aSratchov #endif /* !defined(DSP_H) */ 131