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