Edinburgh Speech Tools  2.1-release
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
sig2fv_main.cc
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1995,1996 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Authors: Paul Taylor and Simon King */
34 /* Date : April 1995 */
35 /*-----------------------------------------------------------------------*/
36 /* Generate feature vectors */
37 /* */
38 /*=======================================================================*/
39 
40 #include <cstdlib>
41 #include "EST_speech_class.h"
42 #include "EST_string_aux.h"
43 #include "EST_cmd_line.h"
44 #include "EST_cmd_line_options.h"
45 #include "sigpr/EST_sigpr_utt.h"
46 #include "sigpr/EST_filter.h"
47 
48 #define EPSILON (0.0001)
49 
50 #define DEFAULT_FRAME_SIZE 0.01
51 #define DEFAULT_FRAME_FACTOR 2.0
52 #define DEFAULT_LPC_ORDER 16
53 #define DEFAULT_REF_ORDER 16
54 #define DEFAULT_CEP_ORDER 12
55 #define DEFAULT_FBANK_ORDER 20
56 #define DEFAULT_MELCEP_ORDER 12
57 #define DEFAULT_WINDOW "hamming"
58 #define DEFAULT_PREEMPH 0
59 #define DEFAULT_LIFTER 0
60 
61 
62 // sane values for pitchmarks (in seconds)
63 
64 #define MINIMUM_PITCH_PERIOD (0.0033) // 300 hz
65 #define MAXIMUM_PITCH_PERIOD (0.02) // 50 Hz
66 #define DEFAULT_PITCH_PERIOD (0.01) // 100 Hz
67 
68 void calculate_orders(EST_StrList &clist, EST_IList &olist,
69  EST_Option &op);
70 
71 void add_channels_to_map(EST_StrList &map, EST_StrList &types,
72  EST_Features &op, int order);
73 
74 void set_options(EST_Features &op, EST_Option &al);
75 
76 EST_String sigpr_options_supported(void)
77 {
78  return
79  EST_String("")+
80  " lpc linear predictive coding\n"
81  " cep cepstrum coding from lpc coefficients\n"
82  " melcep Mel scale cepstrum coding via fbank\n"
83  " fbank Mel scale log filterbank analysis\n"
84  " lsf line spectral frequencies\n"
85  " ref Linear prediction reflection coefficients\n"
86  " power\n"
87  " f0\n"
88  " energy: root mean square energy\n";
89 };
90 
91 
92 
93 int main(int argc, char *argv[])
94 {
95  EST_String out_file("-");
96  EST_StrList files;
97  EST_Option al;
98  EST_Features op;
99  EST_Wave sig;
100  EST_Track full;
101  EST_StrList coef_list, delta_list, acc_list, tlist, map;
102  EST_IList olist;
103 
104  parse_command_line
105  (argc, argv,
106  EST_String("[input file] -o [output file]\n")+
107  "Summary: generate acoustic feature vectors for a waveform file \n"
108  "use \"-\" to make input and output files stdin/out \n"
109  "-h Options help \n\n" +
110  options_wave_input() +
111  options_track_output() + " \n"
112  "-shift <float> frame spacing in seconds for fixed frame analysis. This \n"
113  " doesn't have to be the same as the output file spacing - the \n"
114  " S option can be used to resample the track before saving \n"
115  " default: "+ftoString(DEFAULT_FRAME_SIZE) +"\n\n"
116  "-factor <float> Frames lengths will be FACTOR times the \n"
117  " local pitch period. \n"
118  " default: "+ftoString(DEFAULT_FRAME_FACTOR) +"\n\n"
119  "-pm <ifile> Pitch mark file name. This is used to \n"
120  " specify the positions of the analysis frames for pitch \n"
121  " synchronous analysis. Pitchmark files are just standard \n"
122  " track files, but the channel information is ignored and \n"
123  " only the time positions are used\n"
124  "-size <float> If specified with pm, size is used as the \n"
125  " fixed window size (times factor) rather than size within \n"
126  " each the pms.\n\n"
127 
128  "-coefs <string> list of basic types of processing required. \n"
129  " Permissable types are: \n" + sigpr_options_supported()+" \n"
130  "-delta <string> list of delta types of processing required. Basic \n"
131  " processing does not need to be specified for this option to work. \n"
132  " Permissable types are: \n" + sigpr_options_supported()+" \n"
133  "-acc <string> list of acceleration (delta delta) processing \n"
134  " required. Basic processing does not need to be specified for \n"
135  " this option to work. \n"
136  " Permissable types are: \n"
137  + sigpr_options_supported()+"\n"
138  "-window_type <string> Type of window used on waveform. \n"
139  " Permissable types are: \n" +
141  " default: "DEFAULT_WINDOW"\n\n"
142  "-lpc_order <int> Order of lpc analysis. \n\n"
143  "-ref_order <int> Order of lpc reflection coefficient analysis. \n\n"
144  "-cep_order <int> Order of lpc cepstral analysis.\n\n"
145  "-melcep_order <int> Order of Mel cepstral analysis.\n\n"
146  "-fbank_order <int> Order of filter bank analysis.\n\n"
147  "-preemph <float> Perform pre-emphasis with this factor.\n\n"
148  "-lifter <float> lifter coefficient.\n\n"
149  "-usepower use power rather than energy in filter bank \n"
150  " analysis\n\n"+
151  "-include_c0 include cepstral coefficient 0\n\n"
152  "-order <string> order of analyses\n", files, al);
153 
154  out_file = al.present("-o") ? al.val("-o") : (EST_String)"-";
155  set_options(op, al);
156 
157  StringtoStrList(al.val("-coefs"), coef_list);
158  StringtoStrList(al.val("-delta"), delta_list);
159  StringtoStrList(al.val("-acc"), acc_list);
160 
161  StringtoStrList(al.val("-order"), tlist);
162  StrListtoIList(tlist, olist);
163 
164  if (read_wave(sig, files.first(), al) != read_ok)
165  exit(-1);
166 
167  // allocate and fill time axis
168  if (al.present("-pm"))
169  {
170  if (read_track(full, al.val("-pm"), al))
171  exit(1);
172  }
173  else
174  {
175  full.resize((int)ceil(sig.end() / op.F("frame_shift")), 0);
176  full.fill_time(op.F("frame_shift"));
177  }
178 
179  // allocate channels
180  add_channels_to_map(map, coef_list, op, 0);
181  add_channels_to_map(map, delta_list, op, 1);
182  add_channels_to_map(map, acc_list, op, 2);
183 
184  //cerr << "MAP " << map << endl;
185 
186  full.resize(EST_CURRENT, map);
187 
188  if (al.present("-preemph"))
189  pre_emphasis(sig, al.fval("-preemph"));
190 
191  if(al.present("-usepower"))
192  cerr << "sig2fv: -usepower currently not supported" << endl;
193 
194  sigpr_base(sig, full, op, coef_list);
195  sigpr_delta(sig, full, op, delta_list);
196  sigpr_acc(sig, full, op, acc_list);
197 
198  if (al.present("-S"))
199  {
200  cout << "-S " << al.fval("-S") << endl;
201  full.sample(al.fval("-S"));
202  }
203 
204  if (full.save(out_file, al.val("-otype", 0)) != write_ok)
205  {
206  cerr << "sig2fv: failed to write output to \"" << out_file
207  << "\"" << endl;
208  exit(-1);
209  }
210  return 0;
211 }
212 
213 
214 
215 void calculate_orders(EST_StrList &clist, EST_IList &olist,
216  EST_Option &op)
217 {
218  EST_Litem *c, *o;
219  EST_String k;
220  int v;
221 
222  for (c = clist.head(), o = olist.head(); c && o; c= c->next(), o = o->next())
223  {
224  k = clist(c) + "_order";
225  v = olist(o);
226  op.override_ival(k, v);
227  }
228 }
229 
230 void set_options(EST_Features &op, EST_Option &al)
231 {
232  op.set("frame_shift", DEFAULT_FRAME_SIZE);
233  op.set("frame_factor", DEFAULT_FRAME_FACTOR);
234  op.set("window_type", DEFAULT_WINDOW);
235 
236  op.set("preemph", DEFAULT_PREEMPH);
237  op.set("lifter", DEFAULT_LIFTER);
238 
239  op.set("lpc_order", DEFAULT_LPC_ORDER);
240  op.set("ref_order", DEFAULT_REF_ORDER);
241  op.set("cep_order", DEFAULT_CEP_ORDER);
242  op.set("fbank_order", DEFAULT_FBANK_ORDER);
243  op.set("melcep_order", DEFAULT_MELCEP_ORDER);
244 
245  op.set("max_period", MAXIMUM_PITCH_PERIOD);
246  op.set("min_period", MINIMUM_PITCH_PERIOD);
247  op.set("def_period", DEFAULT_PITCH_PERIOD);
248 
249  if (al.present("-max_period"))
250  op.set("max_period", al.fval("-max_period", 0));
251  if (al.present("-min_period"))
252  op.set("min_period", al.fval("-min_period", 0));
253  if (al.present("-def_period"))
254  op.set("def_period", al.fval("-def_period", 0));
255 
256  if (al.present("-window_type"))
257  op.set("window_type", al.sval("-window_type", 1));
258 
259  if (al.present("-shift"))
260  op.set("frame_shift", al.fval("-shift", 1));
261  if (al.present("-factor"))
262  op.set("frame_factor", al.fval("-factor", 1));
263  if (al.present("-size"))
264  op.set("frame_factor", op.F("frame_factor")*-1.0*al.fval("-size"));
265  if (al.present("-length"))
266  op.set("frame_factor",
267  al.fval("-length", est_errors_allowed)/op.F("frame_shift",est_errors_allowed));
268 
269  if (al.present("-preemph"))
270  op.set("preemph", al.fval("-preemph", 1));
271  if (al.present("-lifter"))
272  op.set("lifter", al.fval("-lifter", 1));
273 
274  if (al.present("-lpc_order"))
275  op.set("lpc_order", al.ival("-lpc_order", 1));
276  if (al.present("-ref_order"))
277  op.set("ref_order", al.ival("-ref_order", 1));
278  if (al.present("-cep_order"))
279  op.set("cep_order", al.ival("-cep_order", 1));
280  if (al.present("-fbank_order"))
281  op.set("fbank_order", al.ival("-fbank_order", 1));
282  if (al.present("-melcep_order"))
283  op.set("melcep_order", al.ival("-melcep_order", 1));
284 
285  if (al.present("-usepower"))
286  op.set("usepower", al.val("-usepower", 1));
287 
288  if (al.present("-include_c0"))
289  op.set("include_c0", al.val("-include_c0", 1));
290 
291 }
292 
293 
A class for storing digital waveforms. The waveform is stored as an array of 16 bit shorts...
Definition: EST_Wave.h:64
const T & first() const
return const reference to first item in list
Definition: EST_TList.h:154
void pre_emphasis(EST_Wave &sig, float a=DEFAULT_PRE_EMPH_FACTOR)
Definition: filter.cc:254
int override_ival(const EST_String rkey, const int rval)
add to end of list or overwrite. If rval is empty, do nothing
Definition: EST_Option.cc:66
int ival(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:76
int StrListtoIList(EST_StrList &s, EST_IList &il)
Convert a list of strings to a list of integers.
float fval(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:98
static EST_String options_supported(void)
Return a paragraph describing the available windows.
Definition: EST_Window.cc:381
void set(const EST_String &name, int ival)
Definition: EST_Features.h:186
EST_String ftoString(float n, int pres=3, int width=0, int l=0)
Make a EST_String object from an float, with variable precision.
Definition: util_io.cc:148
const float F(const EST_String &path) const
Definition: EST_Features.h:136
const EST_String & sval(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:87
void resize(int num_frames, int num_channels, bool preserve=1)
Definition: EST_Track.cc:211
void StringtoStrList(EST_String s, EST_StrList &l, EST_String sep)
Convert a EST_String to a EST_StrList by separating tokens in s delimited by the separator sep...
void sigpr_delta(EST_Wave &sig, EST_Track &fv, EST_Features &op, const EST_StrList &slist)
Definition: sigpr_utt.cc:307
EST_write_status save(const EST_String name, const EST_String EST_filetype="")
Definition: EST_Track.cc:1230
const int present(const K &rkey) const
Returns true if key is present.
Definition: EST_TKVL.cc:222
void sample(float shift)
Definition: EST_Track.cc:671
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
Definition: EST_TKVL.cc:145
void sigpr_acc(EST_Wave &sig, EST_Track &fv, EST_Features &op, const EST_StrList &slist)
Definition: sigpr_utt.cc:300
float end()
return the time position of the last sample.
Definition: EST_Wave.h:153
void fill_time(float t, int start=1)
Definition: EST_Track.cc:786
void sigpr_base(EST_Wave &sig, EST_Track &fv, EST_Features &op, const EST_StrList &slist)
Definition: sigpr_utt.cc:136
Utility EST_String Functions header file.