Edinburgh Speech Tools  2.1-release
ch_track_main.cc
Go to the documentation of this file.
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1994,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 /* Author : Paul Taylor */
34 /* Date : June 1994 */
35 /*-----------------------------------------------------------------------*/
36 /* EST_Track file manipulation program */
37 /* */
38 /*=======================================================================*/
39 
40 #include "EST.h"
41 #include "EST_cmd_line_options.h"
42 
43 using namespace std;
44 
45 #define DEFAULT_TIME_SCALE 0.001
46 
48 void extract_channel(EST_Track &orig, EST_Track &nt, EST_IList &ch_list);
49 
51  EST_TrackList &inpat, EST_TrackList &outpat);
52 
54  EST_Option &al);
55 
56 void extract(EST_Track &tr, EST_Option &al);
57 
58 int main(int argc, char *argv[])
59 {
60  EST_String in_file("-"), out_file("-");
61  EST_Option al, settings;
62  EST_String fname, ftmp;
63  EST_StrList files;
64  EST_Track tr;
65  EST_TrackList trlist;
66  EST_Litem *p;
67 
69  argc, argv,
70  EST_String("[input file] -o [output file] [options]\n")+
71  "Summary: change/copy track files\n"
72  "use \"-\" to make input and output files stdin/out\n"
73  "-h Options help\n"+
74  options_track_input()+ "\n"+
75  options_track_output()+ "\n"
76  "-info Print information about file and header. \n"
77  " This option gives useful information such as file \n"
78  " length, file type, channel names. No output is produced\n\n"
79  "-track_names <string> \n"
80  " File containing new names for output channels\n\n"
81  "-diff Differentiate contour. This performs simple \n"
82  " numerical differentiation on the contour by \n"
83  " subtracting the amplitude of the current frame \n"
84  " from the amplitude of the next. Although quick, \n"
85  " this technique is crude and not recommende as the \n"
86  " estimation of the derivate is done on only one point\n\n"
87  "-delta <int> Make delta coefficients (better form of differentiate).\n"
88  " The argument to this option is the regression length of \n"
89  " of the delta calculation and can be between 2 and 4 \n\n"
90  "-sm <float> Length of smoothing window in seconds. Various types of \n"
91  " smoothing are available for tracks. This options specifies \n"
92  " length of the smooting window which effects the degree of \n"
93  " smoothing, i.e. a longer value means more smoothing \n\n"
94  "-smtype <string> Smooth type, median or mean\n"
95  "-style <string> Convert track to other form. Currently only one form \n"
96  " \"label\" is supported. This uses a specified cut off to \n"
97  " make a label file, with two labels, one for above the \n"
98  " cut off (-pos) and one for below (-neg)\n\n"
99  "-t <float> threshold for track to label conversion \n"
100  "-neg <string> Name of negative label in track to label conversion \n"
101  "-pos <string> Name of positive label in track to label conversion \n"
102  "-pc <string> Combine given tracks in parallel. If option \n"
103  " is longest, pad shorter tracks to longest, else if \n"
104  " first pad/cut to match first input track \n" +
106  files, al);
107 
108 /*redundant options
109  "-time_channel <string>\n"+
110  " Which track in track file holds pitchmark times\n"+
111  "-time_scale <float> \n"+
112  " Scale of pitchmarks (default 0.001 = milliseconds)\n"+
113 */
114 
115 
116  override_lib_ops(settings, al);
117  out_file = al.present("-o") ? al.val("-o") : (EST_String)"-";
118 
119  EST_TokenStream ts;
120 
121 // ts.open(files.first());
122 // tr.load(ts);
123 // cout << tr;
124 
125  if (read_TrackList(trlist, files, al) != read_ok)
126  exit(0);
127 
128  if (files.length() == 0)
129  {
130  cerr << argv[0] << ": no input files specified\n";
131  exit(-1);
132  }
133 
134  if (al.present("-info"))
135  {
136  for (p = trlist.head(); p; p = p->next())
137  track_info(trlist(p));
138  exit(0);
139  }
140 
141  if (al.present("-pc")) // parallelize them
142  ParallelTracks(tr, trlist, al.val("-pc"));
143 
144  else if (al.val("-otype", 0) == "snns")
145  { // sometime this will generalise for multiple input files
146  EST_TrackList inpat, outpat;
147  inpat.append(trlist.nth(0));
148  outpat.append(trlist.nth(1));
149  save_snns_pat(out_file, inpat, outpat);
150  exit(0);
151  }
152  else // concatenate them
153  {
154  tr.resize(0, tr.num_channels());
155  // Reorg -- fix += to resize to largest num_channels (with warning)
156  for (p = trlist.head(); p; p = p->next())
157  tr += trlist(p);
158  }
159 
160  if (al.present("-S"))
161  tr.sample(al.fval("-S"));
162  if (al.present("-sm"))
163  {
164  track_smooth(tr, al.fval("-sm"),al.val("-smtype"));
165  }
166 
167  if (al.present("-diff") && al.present("-delta"))
168  {
169  cerr << "Using -diff and -delta together makes no sense !\n";
170  exit(-1);
171  }
172  if (al.present("-diff"))
173  {
174  tr = differentiate(tr);
175  }
176  if (al.present("-delta"))
177  {
178  EST_Track ntr = tr; // to copy size !;
179  delta(tr,ntr,al.ival("-delta"));
180  tr = ntr;
181  }
182 
183  if (al.present("-c"))
184  {
185  EST_StrList s;
186  EST_Track ntr;
187  EST_IList il;
188  StringtoStrList(al.val("-c"), s, " ,"); // separator can be space or comma
189  StrListtoIList(s, il);
190  extract_channel(tr, ntr, il);
191  tr = ntr;
192  }
193 
194  if (al.present("-start") || al.present("-end")
195  || al.present("-to") || al.present("-from"))
196  extract(tr, al);
197 
198 // tr.assign_map(&LPCTrackMap);
199 // tr.set_space_type("VARI");
200 
201 
202  // optionally rename output tracks before saving
203 
204  if (al.present("-track_names"))
205  {
206  EST_StrList new_names;
207  if(load_StrList(al.val("-track_names"),new_names) != format_ok)
208  {
209  cerr << "Failed to load new track names file." << endl;
210  exit(-1);
211  }
212  /*
213  if (tr.num_channels() != new_names.length())
214  {
215  cerr << "Number of names in output track names file (";
216  cerr << new_names.length() << ") " << endl;
217  cerr << " does not match number of output channels (";
218  cerr << tr.num_channels() << ")" << endl;
219  exit(-1);
220  }
221 
222  EST_Litem *np;
223  int ni;
224  for (np = new_names.head(),ni=0; np; np = np->next(),ni++)
225  tr.set_channel_name(new_names(np),ni);
226  */
227  tr.resize(EST_CURRENT, new_names);
228  }
229 
230  // track_info(tr);
231 
232 /* tr.resize(EST_CURRENT, 10);
233 
234  cout << "new\n";
235  track_info(tr);
236 
237  EST_StrList x;
238  x.append("a");
239  x.append("c");
240  x.append("d");
241 
242 
243 
244  cout << "new\n";
245  track_info(tr);
246 */
247 
248 
249  // Write out file in appropriate format
250 
251  if (al.val("-style",0) == "label")
252  {
253  EST_Relation lab;
254  if (al.present("-t"))
255  track_to_label(tr, lab, al.fval("-t"));
256  else
257  track_to_label(tr, lab);
258  if (al.present("-pos"))
259  change_label(lab, "pos", al.val("-pos"));
260  if (al.present("-neg"))
261  change_label(lab, "neg", al.val("-neg"));
262  if (lab.save(out_file) != write_ok)
263  exit(-1);
264  }
265 /* else if (al.val("-style",0) == "pm")
266  {
267  EST_Relation lab;
268 
269  if (!al.present("-f"))
270  {
271  cerr << "must specify sample rate (with -f) for pm style\n";
272  exit(-1);
273  }
274  int sample_rate = al.ival("-f", 0);
275 
276  track_to_pm(tr, sample_rate, lab);
277 
278  if (lab.save(out_file) != write_ok)
279  exit(-1);
280  }
281 */
282  else
283  {
284  if (tr.save(out_file, al.val("-otype")) != write_ok)
285  exit(-1);
286  }
287 
288  return 0;
289 }
290 
292 {
293  a_list.override_val("ishift", al.val("-s", 0));
294  a_list.override_val("color", al.val("-color", 0));
295  a_list.override_val("in_track_file_type", al.val("-itype", 0));
296  a_list.override_val("out_track_file_type", al.val("-otype", 0));
297  a_list.override_val("tr_to_label_thresh", al.val("-t", 0));
298  a_list.override_fval("time_scale", DEFAULT_TIME_SCALE);
299 
300  if (al.val("-style", 0) == "label")
301  a_list.override_val("lab_file_type", al.val("-otype", 0));
302  if (al.present("-time_scale"))
303  a_list.override_fval("time_scale", al.fval("-time_scale", 1));
304  if (al.present("-time_channel"))
305  a_list.override_val("time_channel", al.sval("-time_channel", 1));
306 }
307 
308 
309 /** @name Making multiple tracks into a single track
310 
311 If multiple input files are specified, by default they are concatenated into
312 the output file.
313 <para>
314 <screen>
315 $ ch_track kdt_010.tr kdt_011.tr kdt_012.tr kdt_013.tr -o out.tr
316 </screen>
317 </para>
318 <para>
319 In the above example, 4 multi channel input files are converted to
320 one single channel output file. Multi-channel tracks can
321 concatenated provided they all have the same number of input channels.
322 
323 </para><para>
324 
325 Multiple input files can be made into a multi-channel output file by
326 using the -pc option:
327 
328 </para><para>
329 <screen>
330 $ ch_track kdt_010.tr kdt_011.tr kdt_012.tr kdt_013.tr -o -pc longest out.tr
331 </screen>
332 </para>
333 <para>
334 The argument to -pc can either be longest, in which the output
335 track is the length of the longest input file, or first in which it
336 is the length of the first input file.
337 
338 */
339 
340 //@{
341 //@}
342 
343 /** @name Extracting channels from multi-channel tracks
344 
345 The -c option is used to specify channels which should be extracted
346 from the input. If the input is a 4 channel track,
347 </para><para>
348 <screen>
349 $ ch_track kdt_m.tr -o a.tr -c "0 2"
350 </screen>
351 </para>
352 <para>
353 will extract the 0th and 2nd channel (counting starts from 0). The
354 argument to -c can be either a single number of a list of numbers
355 (wrapped in quotes).
356 
357  */
358 //@{
359 //@}
360 
361 
362 /** @name Extracting of a single region from a track
363 
364 There are several ways of extracting a region of a track. The
365 simplest way is by using the start, end, to and from commands to
366 delimit a sub portion of the input track. For example
367 </para><para>
368 <screen>
369 $ ch_track kdt_010.tr -o small.tr -start 1.45 -end 1.768
370 </screen>
371 </para>
372 <para>
373 extracts a subtrack starting at 1.45 seconds and extending to 1.768 seconds.
374 alternatively,
375 </para><para>
376 <screen>
377 $ ch_track kdt_010.tr -o small.tr -from 50 -to 100
378 </screen>
379 </para>
380 <para>
381 extracts a subtrack starting at 50 frames and extending to 100
382 frames. Times and frames can be mixed in sub-track extraction. The
383 output track will have the same number of channels as the input track.
384 
385 
386 */
387 //@{
388 //@}
389 
390 /** @name Adding headers and format conversion
391 
392 It is usually a good idea for all track files to have headers as this
393 way different files can be handled safely. ch_track provides a means
394 of adding headers to unheadered files. These files are assumed to
395 be ascii floats with one channel per line.
396 
397 The following adds a header to an ascii file.
398 </para>
399 <para>
400 <screen>
401 $ ch_track kdt_010.atr -o kdt_010.h5.tr -otype est -s 0.01
402 </screen>
403 </para>
404 <para>
405 ch_track can change the frame shift of a fixed frame file, or convert
406 a variable frame shift file into a fixed frame shift. At present this
407 is done with a very crude resampling technique and hence the output
408 file may suffer from anti-aliasing distortion.</para><para>
409 
410 
411 Change to a frame spacing of 0.02 seconds:
412 </para><para>
413 <screen>
414 $ ch_track kdt_010.tr -o kdt_010.tr2 -S 0.02
415 </screen>
416 */
417  //@{
418  //@}
419 
420 //@}
421 
void change_label(EST_Relation &seg, const EST_String &oname, const EST_String &nname)
void delta(EST_Track &tr, EST_Track &d, int regression_length=3)
Definition: delta.cc:52
EST_write_status
The file was read in successfully.
int override_val(const EST_String rkey, const EST_String rval)
add to end of list or overwrite. If rval is empty, do nothing
Definition: EST_Option.cc:54
int ival(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:82
int StrListtoIList(EST_StrList &s, EST_IList &il)
Convert a list of strings to a list of integers.
int num_channels() const
return number of channels in track
Definition: EST_Track.h:657
int override_fval(const EST_String rkey, const float rval)
add to end of list or overwrite. If rval is empty, do nothing
Definition: EST_Option.cc:62
const int EST_CURRENT
EST_write_status save_snns_pat(const EST_String filename, EST_TrackList &inpat, EST_TrackList &outpat)
float fval(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:104
void override_lib_ops(EST_Option &a_list, EST_Option &al)
T & nth(int n)
return the Nth value
Definition: EST_TList.h:145
void resize(ssize_t num_frames, int num_channels, bool preserve=1)
Definition: EST_Track.cc:214
EST_UItem * next()
Definition: EST_UList.h:55
void track_smooth(EST_Track &c, float x, EST_String stype="")
const EST_String & sval(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:93
EST_String options_track_filetypes_long(void)
void extract(EST_Track &tr, EST_Option &al)
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...
The file was written successfully.
void track_info(EST_Track &track)
EST_write_status save(const EST_String name, const EST_String EST_filetype="")
Definition: EST_Track.cc:1233
EST_String options_track_input(void)
int main(int argc, char *argv[])
void sample(float shift)
Definition: EST_Track.cc:674
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
Definition: EST_TKVL.cc:145
void append(const T &item)
add item onto end of list
Definition: EST_TList.h:196
#define DEFAULT_TIME_SCALE
EST_String options_track_output(void)
int length() const
Definition: EST_UList.cc:57
void track_to_label(const EST_Track &tr, EST_Relation &lab, float thresh=0.0)
EST_read_status
EST_read_status read_TrackList(EST_TrackList &tlist, EST_StrList &files, EST_Option &al)
#define format_ok
int present(const K &rkey) const
Returns true if key is present.
Definition: EST_TKVL.cc:222
void extract_channel(EST_Track &orig, EST_Track &nt, EST_IList &ch_list)
EST_write_status save(const EST_String &filename, bool evaluate_ff=false) const
EST_UItem * head() const
Definition: EST_UList.h:97
void ParallelTracks(EST_Track &a, EST_TrackList &list, const EST_String &style)
EST_Track differentiate(EST_Track &c, float samp_int=0.0)
EST_String
EST_read_status load_StrList(EST_String filename, EST_StrList &l)
Load tokens from a file and return them in a EST_StrList.
int parse_command_line(int argc, char *argv[], const EST_String &usage, EST_StrList &files, EST_Option &al, int make_stdio=1)
Definition: cmd_line.cc:101