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