xref: /openbsd-src/usr.bin/aucat/dsp.h (revision b4d5e3c92baf7ae1b3dcb69a4c795856a3c74115)
1*b4d5e3c9Sratchov /*	$OpenBSD: dsp.h,v 1.13 2024/12/22 14:17:45 ratchov Exp $	*/
210ba9548Sratchov /*
310ba9548Sratchov  * Copyright (c) 2012 Alexandre Ratchov <alex@caoua.org>
410ba9548Sratchov  *
510ba9548Sratchov  * Permission to use, copy, modify, and distribute this software for any
610ba9548Sratchov  * purpose with or without fee is hereby granted, provided that the above
710ba9548Sratchov  * copyright notice and this permission notice appear in all copies.
810ba9548Sratchov  *
910ba9548Sratchov  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1010ba9548Sratchov  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1110ba9548Sratchov  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1210ba9548Sratchov  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1310ba9548Sratchov  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1410ba9548Sratchov  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1510ba9548Sratchov  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1610ba9548Sratchov  */
1710ba9548Sratchov #ifndef DSP_H
1810ba9548Sratchov #define DSP_H
1910ba9548Sratchov 
2010ba9548Sratchov #include <sys/types.h>
2110ba9548Sratchov #include "defs.h"
2210ba9548Sratchov 
2310ba9548Sratchov /*
2410ba9548Sratchov  * Samples are numbers in the interval [-1, 1[, note that 1, the upper
2510ba9548Sratchov  * boundary is excluded. We represent them as signed fixed point numbers
2610ba9548Sratchov  * of ADATA_BITS. We also assume that 2^(ADATA_BITS - 1) fits in a int.
2710ba9548Sratchov  */
28a2d01cbcSratchov #define ADATA_BITS			24
2910ba9548Sratchov #define ADATA_LE			(BYTE_ORDER == LITTLE_ENDIAN)
3010ba9548Sratchov #define ADATA_UNIT			(1 << (ADATA_BITS - 1))
3110ba9548Sratchov 
3210ba9548Sratchov #define ADATA_MUL(x,y)		\
3310ba9548Sratchov 	((int)(((long long)(x) * (long long)(y)) >> (ADATA_BITS - 1)))
3410ba9548Sratchov 
3510ba9548Sratchov typedef int adata_t;
3610ba9548Sratchov 
3710ba9548Sratchov /*
38da5bcb9fSratchov  * The FIR is sampled and stored in a table of fixed-point numbers
39da5bcb9fSratchov  * with 23 fractional bits. For convenience, we use the same fixed-point
40da5bcb9fSratchov  * numbers to represent time and to walk through the table.
41da5bcb9fSratchov  */
42da5bcb9fSratchov #define RESAMP_BITS		23
43da5bcb9fSratchov #define RESAMP_UNIT		(1 << RESAMP_BITS)
44da5bcb9fSratchov 
45da5bcb9fSratchov /*
46da5bcb9fSratchov  * Filter window length (the time unit is RESAMP_UNIT)
47da5bcb9fSratchov  */
48da5bcb9fSratchov #define RESAMP_LENGTH		(8 * RESAMP_UNIT)
49da5bcb9fSratchov 
50da5bcb9fSratchov /*
51da5bcb9fSratchov  * Time between samples of the FIR (the time unit is RESAMP_UNIT)
52da5bcb9fSratchov  */
53da5bcb9fSratchov #define RESAMP_STEP_BITS	(RESAMP_BITS - 6)
54da5bcb9fSratchov #define RESAMP_STEP		(1 << RESAMP_STEP_BITS)
55da5bcb9fSratchov 
56da5bcb9fSratchov /*
57da5bcb9fSratchov  * Maximum downsample/upsample ratio we support, must be a power of two.
58da5bcb9fSratchov  * The ratio between the max and the min sample rates is 192kHz / 4kHz = 48,
59da5bcb9fSratchov  * so we can use 64
60da5bcb9fSratchov  */
61da5bcb9fSratchov #define RESAMP_RATIO		64
62da5bcb9fSratchov 
63da5bcb9fSratchov /*
64d9a51c35Sjmc  * Maximum size of the encoding string (the longest possible
6510ba9548Sratchov  * encoding is ``s24le3msb'').
6610ba9548Sratchov  */
6710ba9548Sratchov #define ENCMAX	10
6810ba9548Sratchov 
6910ba9548Sratchov /*
7010ba9548Sratchov  * Default bytes per sample for the given bits per sample.
7110ba9548Sratchov  */
7210ba9548Sratchov #define APARAMS_BPS(bits) (((bits) <= 8) ? 1 : (((bits) <= 16) ? 2 : 4))
7310ba9548Sratchov 
7410ba9548Sratchov struct aparams {
7510ba9548Sratchov 	unsigned int bps;		/* bytes per sample */
7610ba9548Sratchov 	unsigned int bits;		/* actually used bits */
77a34a64ffSratchov 	unsigned int le;		/* 1 if little endian, else be */
7810ba9548Sratchov 	unsigned int sig;		/* 1 if signed, 0 if unsigned */
79a34a64ffSratchov 	unsigned int msb;		/* 1 if msb justified, else lsb */
8010ba9548Sratchov };
8110ba9548Sratchov 
8210ba9548Sratchov struct resamp {
83da5bcb9fSratchov #define RESAMP_NCTX	(RESAMP_LENGTH / RESAMP_UNIT * RESAMP_RATIO)
8410ba9548Sratchov 	unsigned int ctx_start;
8510ba9548Sratchov 	adata_t ctx[NCHAN_MAX * RESAMP_NCTX];
86da5bcb9fSratchov 	int filt_cutoff, filt_step;
8710ba9548Sratchov 	unsigned int iblksz, oblksz;
8810ba9548Sratchov 	int diff;
8910ba9548Sratchov 	int nch;
9010ba9548Sratchov };
9110ba9548Sratchov 
9210ba9548Sratchov struct conv {
9310ba9548Sratchov 	int bfirst;			/* bytes to skip at startup */
9410ba9548Sratchov 	unsigned int bps;		/* bytes per sample */
9510ba9548Sratchov 	unsigned int shift;		/* shift to get 32bit MSB */
9610ba9548Sratchov 	unsigned int bias;		/* bias of unsigned samples */
9710ba9548Sratchov 	int bnext;			/* to reach the next byte */
9810ba9548Sratchov 	int snext;			/* to reach the next sample */
9910ba9548Sratchov 	int nch;
10010ba9548Sratchov };
10110ba9548Sratchov 
10210ba9548Sratchov struct cmap {
10310ba9548Sratchov 	int istart;
10410ba9548Sratchov 	int inext;
10510ba9548Sratchov 	int onext;
10610ba9548Sratchov 	int ostart;
10710ba9548Sratchov 	int nch;
10810ba9548Sratchov };
10910ba9548Sratchov 
1107a7aa84cSratchov #define MIDI_TO_ADATA(m)	(aparams_ctltovol[m])
111b020cfe1Snaddy extern const int aparams_ctltovol[128];
11210ba9548Sratchov 
11310ba9548Sratchov void aparams_init(struct aparams *);
11410ba9548Sratchov int aparams_strtoenc(struct aparams *, char *);
11510ba9548Sratchov int aparams_enctostr(struct aparams *, char *);
11610ba9548Sratchov int aparams_native(struct aparams *);
11710ba9548Sratchov 
1181f1e15b4Sratchov void resamp_getcnt(struct resamp *, int *, int *);
119bac75139Sratchov void resamp_do(struct resamp *, adata_t *, adata_t *, int, int);
12010ba9548Sratchov void resamp_init(struct resamp *, unsigned int, unsigned int, int);
12110ba9548Sratchov void enc_do(struct conv *, unsigned char *, unsigned char *, int);
12210ba9548Sratchov void enc_sil_do(struct conv *, unsigned char *, int);
12310ba9548Sratchov void enc_init(struct conv *, struct aparams *, int);
12410ba9548Sratchov void dec_do(struct conv *, unsigned char *, unsigned char *, int);
12510ba9548Sratchov void dec_do_float(struct conv *, unsigned char *, unsigned char *, int);
12610ba9548Sratchov void dec_do_ulaw(struct conv *, unsigned char *, unsigned char *, int, int);
12710ba9548Sratchov void dec_init(struct conv *, struct aparams *, int);
12810ba9548Sratchov void cmap_add(struct cmap *, void *, void *, int, int);
12910ba9548Sratchov void cmap_copy(struct cmap *, void *, void *, int, int);
13010ba9548Sratchov void cmap_init(struct cmap *, int, int, int, int, int, int, int, int);
13110ba9548Sratchov 
13210ba9548Sratchov #endif /* !defined(DSP_H) */
133