GNU Radio's M17 Package
codec2_internal.h
Go to the documentation of this file.
1#ifndef CODEC2_MOD_INTERNAL_H
2#define CODEC2_MOD_INTERNAL_H
3
4/*
5 This work uses code written by David Rowe VK5DGR et al.
6 https://github.com/drowe67/codec2
7*/
8
9#include <stdint.h>
10#include <math.h>
11
12#include "kiss_fft.h"
13#include "kiss_fftr.h"
14
15#ifndef M_PI
16#define M_PI 3.14159265358979323846
17#endif
18
19#define TWO_PI (2.0f * M_PI)
20
21#ifndef POW10F
22#define POW10F(x) powf(10.0f, (x))
23#endif
24
25#define BYTES_PER_FRAME 8
26#define SAMPLES_PER_FRAME (2 * N_SAMP)
27
28#define N_S 0.01
29#define MAX_AMP 80
30
31#define FFT_ENC 512
32#define FFT_DEC 512
33#define V_THRESH 6.0
34#define V_THRESH_LIN (powf(10.0f, V_THRESH * 0.1f))
35#define LPC_ORD 10
36
37#define P_MAX_S 0.0200f
38
39#define SAMP_RATE 8000
40#define N_SAMP 80
41#define M_PITCH 320
42#define P_MIN 20
43#define P_MAX 160
44#define W0_MIN ((2.0 * M_PI) / 160.0)
45#define W0_MAX ((2.0 * M_PI) / 20.0)
46#define NW 279
47#define TW 40
48
49#define PMAX_M 320
50#define COEFF 0.95
51#define PE_FFT_SIZE 512
52#define DEC 5
53#define NDEC (M_PITCH / DEC)
54#define T 0.1
55#define CNLP 0.3
56#define NLP_NTAP 48
57#define NLP_NPHASE DEC
58#define NLP_TAPS_PP ((NLP_NTAP + DEC - 1) / DEC)
59
60#define WO_BITS 7
61#define WO_LEVELS (1 << WO_BITS)
62#define E_BITS 5
63#define E_LEVELS (1 << E_BITS)
64#define E_MIN_DB -10.0
65#define E_MAX_DB 40.0
66#define LSPD_SCALAR_INDEXES 10
67
68#define LPCPF_GAMMA 0.5
69#define LPCPF_BETA 0.2
70#define LPCPF_TWO_BETA (2.0 * LPCPF_BETA)
71#define LSP_DELTA1 0.01
72#define BG_THRESH 40.0
73#define BG_BETA 0.1
74#define BG_MARGIN 6.0
75#define LPC_ENERGY_FLOOR 1e-6f
76
77#define FFT_R (TWO_PI / FFT_ENC)
78#define FFT_1_R (FFT_ENC / TWO_PI)
79
80#define CODEC2_RAND_MAX 32767
81
82#define FFT_FWD_MEM_BYTES 4360
83#define FFTR_MEM_BYTES 5408
84
85/* asserts */
86/* this is the exact Codec2 3200 bps configuration */
87/* changing any of these parameters requires DSP math modification and buffer resizing */
88_Static_assert(BYTES_PER_FRAME == 8, "Codec2 3200 only");
89_Static_assert(LPC_ORD == 10, "Codec2 3200 assumes LPC_ORD == 10");
90_Static_assert(MAX_AMP >= 80, "MAX_AMP must be >= 80 for Codec2 3200");
91_Static_assert(FFT_ENC == 512, "Codec2 3200 assumes FFT_ENC == 512");
92_Static_assert(FFT_DEC == FFT_ENC, "Encoder/decoder FFT sizes must match");
93_Static_assert(PE_FFT_SIZE == 512, "Pitch estimator FFT size must be 512");
94_Static_assert((M_PITCH % DEC) == 0, "M_PITCH must be divisible by DEC");
95_Static_assert(NDEC == (M_PITCH / DEC), "NDEC must equal M_PITCH / DEC");
96_Static_assert(NLP_NTAP == 48, "NLP FIR unrolling assumes NLP_NTAP == 48");
97_Static_assert(WO_BITS <= 8, "encode_Wo() assumes <= 8-bit fields");
98_Static_assert(E_BITS <= 8, "encode_energy() assumes <= 8-bit fields");
99_Static_assert(TW <= N_SAMP / 2, "Parzen window ramp too long");
100
101/* consts */
102extern const float nlp_fir[NLP_NTAP];
103extern const float bw_gamma[LPC_ORD + 1];
104extern const uint16_t delta_lsp_cb[LPC_ORD][32];
105
107
108typedef struct model_t
109{
110 float Wo;
111 int L;
112 float A[MAX_AMP + 1];
115
126
142
165
171
172_Static_assert(sizeof(((codec2_t *)0)->encoder.fft_buffer) >= FFT_ENC * sizeof(kiss_fft_cpx), "fft_buffer too small for FFT_ENC scratch");
173
174#endif /* CODEC2_MOD_INTERNAL_H */
#define TW
Definition codec2_internal.h:47
#define FFTR_MEM_BYTES
Definition codec2_internal.h:83
#define N_SAMP
Definition codec2_internal.h:40
#define LPC_ORD
Definition codec2_internal.h:35
const float nlp_fir[NLP_NTAP]
#define WO_BITS
Definition codec2_internal.h:60
#define PMAX_M
Definition codec2_internal.h:49
#define NDEC
Definition codec2_internal.h:53
#define PE_FFT_SIZE
Definition codec2_internal.h:51
#define E_BITS
Definition codec2_internal.h:62
#define NLP_NTAP
Definition codec2_internal.h:56
#define FFT_ENC
Definition codec2_internal.h:31
const float bw_gamma[LPC_ORD+1]
const uint16_t delta_lsp_cb[LPC_ORD][32]
#define FFT_DEC
Definition codec2_internal.h:32
#define BYTES_PER_FRAME
Definition codec2_internal.h:25
kiss_fft_cpx complex_t
Definition codec2_internal.h:106
#define MAX_AMP
Definition codec2_internal.h:29
#define M_PITCH
Definition codec2_internal.h:41
#define DEC
Definition codec2_internal.h:52
struct kiss_fftr_state * kiss_fftr_cfg
Definition kiss_fftr.h:26
Definition codec2_internal.h:144
kiss_fftr_cfg fftr_fwd_cfg
Definition codec2_internal.h:157
kiss_fftr_cfg fftr_inv_cfg
Definition codec2_internal.h:158
float prev_lsps_dec[LPC_ORD]
Definition codec2_internal.h:154
uint32_t next_rn
Definition codec2_internal.h:145
uint8_t fftr_fwd_mem[FFTR_MEM_BYTES]
Definition codec2_internal.h:162
model_t prev_model_dec
Definition codec2_internal.h:153
float Sn_[2 *N_SAMP]
Definition codec2_internal.h:147
float bg_est
Definition codec2_internal.h:151
uint8_t fftr_inv_mem[FFTR_MEM_BYTES]
Definition codec2_internal.h:163
float prev_e_dec
Definition codec2_internal.h:155
float ex_phase
Definition codec2_internal.h:150
float Pn[2 *N_SAMP]
Definition codec2_internal.h:148
kiss_fft_cpx fft_buffer[FFT_DEC]
Definition codec2_internal.h:160
Definition codec2_internal.h:128
float Sn[M_PITCH]
Definition codec2_internal.h:131
float prev_f0_enc
Definition codec2_internal.h:134
kiss_fftr_cfg fftr_fwd_cfg
Definition codec2_internal.h:136
kiss_fft_cpx fft_buffer[FFT_ENC]
Definition codec2_internal.h:138
float W[FFT_ENC]
Definition codec2_internal.h:130
float w[M_PITCH]
Definition codec2_internal.h:129
uint8_t fftr_fwd_mem[FFTR_MEM_BYTES]
Definition codec2_internal.h:140
nlp_t nlp
Definition codec2_internal.h:132
Definition codec2_internal.h:167
codec2_decoder_t decoder
Definition codec2_internal.h:169
codec2_encoder_t encoder
Definition codec2_internal.h:168
Definition kiss_fft.h:87
Definition codec2_internal.h:109
int L
Definition codec2_internal.h:111
float Wo
Definition codec2_internal.h:110
float A[MAX_AMP+1]
Definition codec2_internal.h:112
int voiced
Definition codec2_internal.h:113
Definition codec2_internal.h:117
float w[PMAX_M/DEC]
Definition codec2_internal.h:118
float sq[PMAX_M]
Definition codec2_internal.h:119
float fftr_buff[PE_FFT_SIZE]
Definition codec2_internal.h:123
complex_t Fw[PE_FFT_SIZE/2+1]
Definition codec2_internal.h:124
float sq_fir[NDEC]
Definition codec2_internal.h:120
float mem_y
Definition codec2_internal.h:121
kiss_fftr_cfg fftr_cfg
Definition codec2_internal.h:122
float mem_x
Definition codec2_internal.h:121