1 /* Copyright (C) 2003 Epic Games
2    Written by Jean-Marc Valin */
3 /**
4    @file speex_preprocess.h
5    @brief Speex preprocessor
6 */
7 /*
8    Redistribution and use in source and binary forms, with or without
9    modification, are permitted provided that the following conditions are
10    met:
11 
12    1. Redistributions of source code must retain the above copyright notice,
13    this list of conditions and the following disclaimer.
14 
15    2. Redistributions in binary form must reproduce the above copyright
16    notice, this list of conditions and the following disclaimer in the
17    documentation and/or other materials provided with the distribution.
18 
19    3. The name of the author may not be used to endorse or promote products
20    derived from this software without specific prior written permission.
21 
22    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25    DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
26    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30    STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32    POSSIBILITY OF SUCH DAMAGE.
33 */
34 
35 #ifndef SPEEX_PREPROCESS_H
36 #define SPEEX_PREPROCESS_H
37 
38 #include "speex_types.h"
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 struct drft_lookup;
45 
46 /** Speex pre-processor state. */
47 typedef struct SpeexPreprocessState {
48    int    frame_size;        /**< Number of samples processed each time */
49    int    ps_size;           /**< Number of points in the power spectrum */
50    int    sampling_rate;     /**< Sampling rate of the input/output */
51 
52    /* parameters */
53    int    denoise_enabled;
54    int    agc_enabled;
55    float  agc_level;
56    int    vad_enabled;
57    int    dereverb_enabled;
58    float  reverb_decay;
59    float  reverb_level;
60    float  speech_prob_start;
61    float  speech_prob_continue;
62 
63    float *frame;             /**< Processing frame (2*ps_size) */
64    float *ps;                /**< Current power spectrum */
65    float *gain2;             /**< Adjusted gains */
66    float *window;            /**< Analysis/Synthesis window */
67    float *noise;             /**< Noise estimate */
68    float *reverb_estimate;   /**< Estimate of reverb energy */
69    float *old_ps;            /**< Power spectrum for last frame */
70    float *gain;              /**< Ephraim Malah gain */
71    float *prior;             /**< A-priori SNR */
72    float *post;              /**< A-posteriori SNR */
73 
74    float *S;                 /**< Smoothed power spectrum */
75    float *Smin;              /**< See Cohen paper */
76    float *Stmp;              /**< See Cohen paper */
77    float *update_prob;       /**< Propability of speech presence for noise update */
78 
79    float *zeta;              /**< Smoothed a priori SNR */
80    float  Zpeak;
81    float  Zlast;
82 
83    float *loudness_weight;   /**< Perceptual loudness curve */
84 
85    float *echo_noise;
86 
87    float *noise_bands;
88    float *noise_bands2;
89    int    noise_bandsN;
90    float *speech_bands;
91    float *speech_bands2;
92    int    speech_bandsN;
93 
94    float *inbuf;             /**< Input buffer (overlapped analysis) */
95    float *outbuf;            /**< Output buffer (for overlap and add) */
96 
97    float  speech_prob;
98    int    last_speech;
99    float  loudness;          /**< loudness estimate */
100    float  loudness2;         /**< loudness estimate */
101    int    nb_adapt;          /**< Number of frames used for adaptation so far */
102    int    nb_loudness_adapt; /**< Number of frames used for loudness adaptation so far */
103    int    consec_noise;      /**< Number of consecutive noise frames */
104    int    nb_preprocess;     /**< Number of frames processed so far */
105    struct drft_lookup *fft_lookup;   /**< Lookup table for the FFT */
106 
107 } SpeexPreprocessState;
108 
109 /** Creates a new preprocessing state */
110 SpeexPreprocessState *speex_preprocess_state_init(int frame_size, int sampling_rate);
111 
112 /** Destroys a denoising state */
113 void speex_preprocess_state_destroy(SpeexPreprocessState *st);
114 
115 /** Preprocess a frame */
116 int speex_preprocess(SpeexPreprocessState *st, spx_int16_t *x, spx_int32_t *echo);
117 
118 /** Preprocess a frame */
119 void speex_preprocess_estimate_update(SpeexPreprocessState *st, spx_int16_t *x, spx_int32_t *echo);
120 
121 /** Used like the ioctl function to control the preprocessor parameters */
122 int speex_preprocess_ctl(SpeexPreprocessState *st, int request, void *ptr);
123 
124 
125 
126 /** Set preprocessor denoiser state */
127 #define SPEEX_PREPROCESS_SET_DENOISE 0
128 /** Get preprocessor denoiser state */
129 #define SPEEX_PREPROCESS_GET_DENOISE 1
130 
131 /** Set preprocessor Automatic Gain Control state */
132 #define SPEEX_PREPROCESS_SET_AGC 2
133 /** Get preprocessor Automatic Gain Control state */
134 #define SPEEX_PREPROCESS_GET_AGC 3
135 
136 /** Set preprocessor Voice Activity Detection state */
137 #define SPEEX_PREPROCESS_SET_VAD 4
138 /** Get preprocessor Voice Activity Detection state */
139 #define SPEEX_PREPROCESS_GET_VAD 5
140 
141 /** Set preprocessor Automatic Gain Control level */
142 #define SPEEX_PREPROCESS_SET_AGC_LEVEL 6
143 /** Get preprocessor Automatic Gain Control level */
144 #define SPEEX_PREPROCESS_GET_AGC_LEVEL 7
145 
146 /** Set preprocessor dereverb state */
147 #define SPEEX_PREPROCESS_SET_DEREVERB 8
148 /** Get preprocessor dereverb state */
149 #define SPEEX_PREPROCESS_GET_DEREVERB 9
150 
151 /** Set preprocessor dereverb level */
152 #define SPEEX_PREPROCESS_SET_DEREVERB_LEVEL 10
153 /** Get preprocessor dereverb level */
154 #define SPEEX_PREPROCESS_GET_DEREVERB_LEVEL 11
155 
156 /** Set preprocessor dereverb decay */
157 #define SPEEX_PREPROCESS_SET_DEREVERB_DECAY 12
158 /** Get preprocessor dereverb decay */
159 #define SPEEX_PREPROCESS_GET_DEREVERB_DECAY 13
160 
161 #define SPEEX_PREPROCESS_SET_PROB_START 14
162 #define SPEEX_PREPROCESS_GET_PROB_START 15
163 
164 #define SPEEX_PREPROCESS_SET_PROB_CONTINUE 16
165 #define SPEEX_PREPROCESS_GET_PROB_CONTINUE 17
166 
167 #ifdef __cplusplus
168 }
169 #endif
170 
171 #endif
172