SphinxBase  5prealpha
fe_internal.h
1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3  * Copyright (c) 1996-2004 Carnegie Mellon University. All rights
4  * reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * This work was supported in part by funding from the Defense Advanced
19  * Research Projects Agency and the National Science Foundation of the
20  * United States of America, and the CMU Sphinx Speech Consortium.
21  *
22  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * ====================================================================
35  *
36  */
37 
38 #ifndef __FE_INTERNAL_H__
39 #define __FE_INTERNAL_H__
40 
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif
44 
45 #include "sphinxbase/fe.h"
46 #include "sphinxbase/fixpoint.h"
47 
48 #include "fe_noise.h"
49 #include "fe_prespch_buf.h"
50 #include "fe_type.h"
51 
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55 #if 0
56 /* Fool Emacs. */
57 }
58 #endif
59 
60 /* Values for the 'logspec' field. */
61 enum {
62  RAW_LOG_SPEC = 1,
63  SMOOTH_LOG_SPEC = 2
64 };
65 
66 /* Values for the 'transform' field. */
67 enum {
68  LEGACY_DCT = 0,
69  DCT_II = 1,
70  DCT_HTK = 2
71 };
72 
73 typedef struct melfb_s melfb_t;
75 struct melfb_s {
76  float32 sampling_rate;
77  int32 num_cepstra;
78  int32 num_filters;
79  int32 fft_size;
80  float32 lower_filt_freq;
81  float32 upper_filt_freq;
82  /* DCT coefficients. */
83  mfcc_t **mel_cosine;
84  /* Filter coefficients. */
85  mfcc_t *filt_coeffs;
86  int16 *spec_start;
87  int16 *filt_start;
88  int16 *filt_width;
89  /* Luxury mobile home. */
90  int32 doublewide;
91  char const *warp_type;
92  char const *warp_params;
93  uint32 warp_id;
94  /* Precomputed normalization constants for unitary DCT-II/DCT-III */
95  mfcc_t sqrt_inv_n, sqrt_inv_2n;
96  /* Value and coefficients for HTK-style liftering */
97  int32 lifter_val;
98  mfcc_t *lifter;
99  /* Normalize filters to unit area */
100  int32 unit_area;
101  /* Round filter frequencies to DFT points (hurts accuracy, but is
102  useful for legacy purposes) */
103  int32 round_filters;
104 };
105 
106 /* sqrt(1/2), also used for unitary DCT-II/DCT-III */
107 #define SQRT_HALF FLOAT2MFCC(0.707106781186548)
108 
109 typedef struct vad_data_s {
110  uint8 in_speech;
111  int16 pre_speech_frames;
112  int16 post_speech_frames;
113  prespch_buf_t* prespch_buf;
114 } vad_data_t;
115 
117 struct fe_s {
118  cmd_ln_t *config;
119  int refcount;
120 
121 
122  float32 sampling_rate;
123  int16 frame_rate;
124  int16 frame_shift;
125 
126  float32 window_length;
127  int16 frame_size;
128  int16 fft_size;
129 
130  uint8 fft_order;
131  uint8 feature_dimension;
132  uint8 num_cepstra;
133  uint8 remove_dc;
134  uint8 log_spec;
135  uint8 swap;
136  uint8 dither;
137  uint8 transform;
138  uint8 remove_noise;
139  uint8 remove_silence;
140 
141  float32 pre_emphasis_alpha;
142  int32 seed;
143 
144  size_t sample_counter;
145  uint8 start_flag;
146  uint8 reserved;
147 
148  /* Twiddle factors for FFT. */
149  frame_t *ccc, *sss;
150  /* Mel filter parameters. */
151  melfb_t *mel_fb;
152  /* Half of a Hamming Window. */
153  window_t *hamming_window;
154 
155  /* Noise removal */
156  noise_stats_t *noise_stats;
157 
158  /* VAD variables */
159  int16 pre_speech;
160  int16 post_speech;
161  int16 start_speech;
162  float32 vad_threshold;
163  vad_data_t *vad_data;
164 
165  /* Temporary buffers for processing. */
166  /* FIXME: too many of these. */
167  int16 *spch;
168  frame_t *frame;
169  powspec_t *spec, *mfspec;
170  int16 *overflow_samps;
171  int16 num_overflow_samps;
172  int16 prior;
173 };
174 
175 void fe_init_dither(int32 seed);
176 
177 /* Apply 1/2 bit noise to a buffer of audio. */
178 int32 fe_dither(int16 *buffer, int32 nsamps);
179 
180 /* Load a frame of data into the fe. */
181 int fe_read_frame(fe_t *fe, int16 const *in, int32 len);
182 
183 /* Shift the input buffer back and read more data. */
184 int fe_shift_frame(fe_t *fe, int16 const *in, int32 len);
185 
186 /* Process a frame of data into features. */
187 void fe_write_frame(fe_t *fe, mfcc_t *feat, int32 store_pcm);
188 
189 /* Initialization functions. */
190 int32 fe_build_melfilters(melfb_t *MEL_FB);
191 int32 fe_compute_melcosine(melfb_t *MEL_FB);
192 void fe_create_hamming(window_t *in, int32 in_len);
193 void fe_create_twiddle(fe_t *fe);
194 
195 fixed32 fe_log_add(fixed32 x, fixed32 y);
196 fixed32 fe_log_sub(fixed32 x, fixed32 y);
197 
198 /* Miscellaneous processing functions. */
199 void fe_spec2cep(fe_t * fe, const powspec_t * mflogspec, mfcc_t * mfcep);
200 void fe_dct2(fe_t *fe, const powspec_t *mflogspec, mfcc_t *mfcep, int htk);
201 void fe_dct3(fe_t *fe, const mfcc_t *mfcep, powspec_t *mflogspec);
202 
203 #ifdef __cplusplus
204 }
205 #endif
206 
207 #endif /* __FE_INTERNAL_H__ */
Base Struct to hold all structure for MFCC computation.
Definition: fe_internal.h:75
Opaque structure used to hold the results of command-line parsing.
Structure for the front-end computation.
Definition: fe_internal.h:117