1 /*---------------------------------------------------------------------------*\
2 
3   FILE........: codec2_internal.h
4   AUTHOR......: David Rowe
5   DATE CREATED: April 16 2012
6 
7   Header file for Codec2 internal states, exposed via this header
8   file to assist in testing.
9 
10 \*---------------------------------------------------------------------------*/
11 
12 /*
13   Copyright (C) 2012 David Rowe
14 
15   All rights reserved.
16 
17   This program is free software; you can redistribute it and/or modify
18   it under the terms of the GNU Lesser General Public License version 2.1, as
19   published by the Free Software Foundation.  This program is
20   distributed in the hope that it will be useful, but WITHOUT ANY
21   WARRANTY; without even the implied warranty of MERCHANTABILITY or
22   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
23   License for more details.
24 
25   You should have received a copy of the GNU Lesser General Public License
26   along with this program; if not, see <http://www.gnu.org/licenses/>.
27 */
28 
29 #ifndef __CODEC2_INTERNAL__
30 #define __CODEC2_INTERNAL__
31 
32 #include "codec2_fft.h"
33 #include "newamp1.h"
34 #include "newamp2.h"
35 
36 struct CODEC2 {
37     int           mode;
38     C2CONST       c2const;
39     int           Fs;
40     int           n_samp;
41     int           m_pitch;
42     codec2_fft_cfg  fft_fwd_cfg;           /* forward FFT config                        */
43     codec2_fftr_cfg fftr_fwd_cfg;          /* forward real FFT config                   */
44     float        *w;	                   /* [m_pitch] time domain hamming window      */
45     float         W[FFT_ENC];	           /* DFT of w[]                                */
46     float        *Pn;	                   /* [2*n_samp] trapezoidal synthesis window   */
47     float        *bpf_buf;                 /* buffer for band pass filter               */
48     float        *Sn;                      /* [m_pitch] input speech                    */
49     float         hpf_states[2];           /* high pass filter states                   */
50     void         *nlp;                     /* pitch predictor states                    */
51     int           gray;                    /* non-zero for gray encoding                */
52 
53     codec2_fftr_cfg  fftr_inv_cfg;         /* inverse FFT config                        */
54     float        *Sn_;	                   /* [2*n_samp] synthesised output speech      */
55     float         ex_phase;                /* excitation model phase track              */
56     float         bg_est;                  /* background noise estimate for post filter */
57     float         prev_f0_enc;             /* previous frame's f0    estimate           */
58     MODEL         prev_model_dec;          /* previous frame's model parameters         */
59     float         prev_lsps_dec[LPC_ORD];  /* previous frame's LSPs                     */
60     float         prev_e_dec;              /* previous frame's LPC energy               */
61 
62     int           lpc_pf;                  /* LPC post filter on                        */
63     int           bass_boost;              /* LPC post filter bass boost                */
64     float         beta;                    /* LPC post filter parameters                */
65     float         gamma;
66 
67     float         xq_enc[2];               /* joint pitch and energy VQ states          */
68     float         xq_dec[2];
69 
70     int           smoothing;               /* enable smoothing for channels with errors */
71     float        *softdec;                 /* optional soft decn bits from demod        */
72 
73     /* newamp1 states */
74 
75     float          rate_K_sample_freqs_kHz[NEWAMP1_K];
76     float          prev_rate_K_vec_[NEWAMP1_K];
77     float          Wo_left;
78     int            voicing_left;
79     codec2_fft_cfg phase_fft_fwd_cfg;
80     codec2_fft_cfg phase_fft_inv_cfg;
81     float          se;                       /* running sum of squared error */
82     unsigned int   nse;                      /* number of terms in sum       */
83     float         *user_rate_K_vec_no_mean_; /* optional, user supplied vector for quantisation experiments */
84     int            post_filter_en;
85     float          eq[NEWAMP1_K];            /* optional equaliser */
86     int            eq_en;
87 
88     /*newamp2 states (also uses newamp1 states )*/
89     float 	   energy_prev;
90     float          n2_rate_K_sample_freqs_kHz[NEWAMP2_K];
91     float          n2_prev_rate_K_vec_[NEWAMP2_K];
92     float          n2_pwb_rate_K_sample_freqs_kHz[NEWAMP2_16K_K];
93     float          n2_pwb_prev_rate_K_vec_[NEWAMP2_16K_K];
94 
95     /* used to dump features for deep learning experiments */
96     FILE *fmlfeat, *fmlmodel;
97 
98     /* encode/decode function pointers for the selected mode */
99     void (*encode)(struct CODEC2 *c2, unsigned char * bits, short speech[]);
100     void (*decode)(struct CODEC2 *c2, short speech[], const unsigned char * bits);
101     void (*decode_ber)(struct CODEC2 *c2, short speech[], const unsigned char * bits, float ber_est);
102 };
103 
104 // test and debug
105 void analyse_one_frame(struct CODEC2 *c2, MODEL *model, short speech[]);
106 void synthesise_one_frame(struct CODEC2 *c2, short speech[], MODEL *model,
107 			  COMP Aw[], float gain);
108 #endif
109