Edinburgh Speech Tools  2.1-release
pitchmark_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) 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 : 1997, 1998, 1999 */
35 /*-----------------------------------------------------------------------*/
36 /* Pitchmarking program */
37 /*************************************************************************/
38 
39 #include <cstdlib>
40 #include <iostream>
41 #include <fstream>
42 #include "EST_unix.h"
43 #include "EST_cmd_line_options.h"
44 #include "EST_cmd_line.h"
45 #include "EST_speech_class.h"
46 #include "sigpr/EST_pitchmark.h"
47 
48 using namespace std;
49 
50 void set_options(EST_Features &op, EST_Option &al);
51 
52 static EST_write_status save_msec(EST_Track &pm, EST_String filename);
53 static EST_write_status save_ogi_bin(EST_Track &pm, EST_String filename,
54  int sr);
55 void pm_to_label(EST_Track &pm, EST_Relation &lab);
56 
57 
58 /*void pm_to_label(EST_Track &pm, EST_Relation &lab);
59 void find_pm(EST_Wave &sig, EST_Track &pm);
60 
61 void pm_min_check(EST_Track &pm, float min);
62 void pm_sanity_check(EST_Track &pm, float new_end,
63  float max, float min, float def);
64 
65 void pm_fill(EST_Track &pm, float new_end, float max,
66  float min, float def);
67 
68 void pm_to_f0(EST_Track &pm, EST_Track &f0);
69 */
70 
71 
72 int main (int argc, char *argv[])
73 {
74  EST_Track pm;
75  EST_Wave lx;
76  EST_Option al;
77  EST_Features op;
78  EST_String out_file("-");
79  EST_StrList files;
80 
82  (argc, argv,
83  EST_String("[input file] -o [output file] [options]")+
84  "Summary: pitchmark laryngograph (lx) files\n"
85  "use \"-\" to make input and output files stdin/out\n"
86  "-h Options help\n\n"+
89  "-lx_lf <int> lx low frequency cutoff\n\n"
90  "-lx_lo <int> lx low order\n\n"
91  "-lx_hf <int> lx high frequency cutoff\n\n"
92  "-lx_ho <int> lx high order\n\n"
93  "-df_lf <int> df low frequeny cutoff\n\n"
94  "-df_lo <int> df low order\n\n"
95  "-med_o <int> median smoothing order\n\n"
96  "-mean_o <int> mean smoothing order\n\n"
97  "-inv Invert polarity of lx signal. Often the lx signal \n"
98  " is upside down. This option inverts the signal prior to \n"
99  " processing.\n\n"
100  "-fill Insert and remove pitchmarks according to min, max\n"
101  " and def period values. Often it is desirable to place limits\n"
102  " on the values of the pitchmarks. This option enforces a \n"
103  " minimum and maximum pitch period (specified by -man and -max).\n"
104  " If the maximum pitch setting is low enough, this will \n"
105  " esnure that unvoiced regions have evenly spaced pitchmarks \n\n"
106  "-min <float> Minimum allowed pitch period, in seconds\n\n"
107  "-max <float> Maximum allowed pitch period, in seconds\n\n"
108  "-def <float> Default pitch period in seconds, used for a guide\n"
109  " as to what length pitch periods should be in unvoiced \n"
110  " sections \n\n"
111  "-pm <ifile> Input is raw pitchmark file. This option is \n"
112  " used to perform filling operations on an already existing \n"
113  " set of pitchmarks \n\n"
114  "-f0 <ofile> Calculate F0 from pitchmarks and save to file\n\n"
115  "-end <float> Specify the end time of the last pitchmark, for use \n"
116  " with the -fill option\n\n"
117  "-wave_end Use the end of a waveform to specify when the \n"
118  " last pitchmark position should be. The waveform file is only \n"
119  " read to determine its end, no processing is performed\n\n"
120  "-inter Output intermediate waveforms. This will output the \n"
121  " signal at various stages of processing. Examination of these \n"
122  " waveforms is extremely useful in setting the parameters for \n"
123  " similar waveforms\n\n"
124  "-style <string> \"track\" or \"lab\"\n\n", files, al);
125 
126  set_options(op, al);
127 
128  out_file = al.present("-o") ? al.val("-o") : (EST_String)"-";
129 
130  if (!al.present("-pm") || (al.present("-pm") && al.present("-wave_end")))
131  if (read_wave(lx, files.first(), al) != read_ok)
132  exit(-1);
133 
134  if (al.present("-pm"))
135  pm.load(al.val("-pm"));
136  else
137  {
138  if (al.present("-inv"))
139  invert(lx);
140  pm = pitchmark(lx, op);
141  }
142 
143  // this allows the end to be aligned with the end of a waveform
144  op.set("pm_end", lx.end());
145 
146  if (al.present("-f0"))
147  {
148  EST_Track f0;
149  pm_to_f0(pm, f0);
150  f0.save(al.val("-f0"));
151  }
152 
153  // various options for filling he gaps between distant pitchmarks
154  // and removing pitchmarks that are too close together
155 
156  if (al.present("-fill"))
157  {
158  pm_fill(pm, op.F("pm_end"), op.F("max_period"),
159  op.F("min_period"), op.F("def_period"));
160  pm_fill(pm, op.F("pm_end"), op.F("max_period"),
161  op.F("min_period"), op.F("def_period"));
162  }
163  else if (al.present("-min"))
164  pm_min_check(pm, al.fval("-min"));
165 
166  if (al.present("-style"))
167  {
168  // label format
169  if (al.val("-style").contains("lab"))
170  {
171  EST_Relation lab;
172  pm_to_label(pm, lab);
173  if (lab.save(out_file + ".pm_lab") != write_ok)
174  exit(-1);
175  }
176  // save file in "traditional" milli-second format
177  if (al.val("-style").contains("msec"))
178  save_msec(pm, out_file + ".pm");
179 
180  // ogi binary integer sample point format
181  if (al.val("-style").contains("ogi_bin"))
182  save_ogi_bin(pm, out_file + ".pmv", lx.sample_rate());
183  }
184  else if (pm.save(out_file, al.val("-otype", 0)) != write_ok)
185  {
186  cerr << "pitchmark: failed to write output to \""
187  << out_file << "\"" << endl;
188  exit(-1);
189  }
190  return 0;
191 }
192 
193 static EST_write_status save_msec(EST_Track &pm, EST_String filename)
194 {
195  ostream *outf;
196 
197  if (filename == "-")
198  outf = &cout;
199  else
200  outf = new ofstream(filename);
201 
202  if (!(*outf))
203  return write_fail;
204 
205  outf->precision(5);
206  outf->setf(ios::fixed, ios::floatfield);
207  outf->width(8);
208 
209  for (int i = 0; i < pm.num_frames(); ++i)
210  *outf << pm.t(i) * 1000.0 << endl;
211 
212  return write_ok;
213 }
214 
215 static EST_write_status save_ogi_bin(EST_Track &pm, EST_String filename, int sr)
216 {
217  int *d;
218  FILE *fp;
219  int i;
220 
221  d = new int[pm.num_frames()];
222 
223  for (i = 0; i < pm.num_frames(); ++i)
224  d[i] = int(pm.t(i) * (float) sr);
225 
226  if ((fp = fopen(filename, "wb")) == NULL) {
227  delete[] d;
228  return misc_write_error;
229  }
230  if (fwrite(d, pm.num_frames(), sizeof(int), fp) != 1)
231  {
232  fclose(fp);
233  delete[] d;
234  return misc_write_error;
235  }
236  delete[] d;
237  fclose(fp);
238  return write_ok;
239 }
240 
242 {
243  op.override_ival("lx_low_frequency", 400);
244  op.override_ival("lx_low_order", 19);
245  op.override_ival("lx_high_frequency", 40);
246  op.override_ival("lx_high_order", 19);
247  op.override_ival("df_low_frequency", 1000);
248  op.override_ival("df_low_order", 19);
249  op.override_fval("min_period", 0.003);
250  op.override_fval("max_period", 0.02);
251  op.override_fval("def_period", 0.01);
252  op.override_fval("pm_end", -1.0);
253 
254  if (al.present("-lx_lf"))
255  op.override_ival("lx_low_frequency", al.ival("-lx_lf", 0));
256  if (al.present("-lx_lo"))
257  op.override_ival("lx_low_order", al.ival("-lx_lo", 0));
258  if (al.present("-lx_hf"))
259  op.override_ival("lx_high_frequency", al.ival("-lx_hf", 0));
260  if (al.present("-lx_ho"))
261  op.override_ival("lx_high_order", al.ival("-lx_ho", 0));
262  if (al.present("-med_o"))
263  op.override_ival("median_order", al.ival("-med_o", 0));
264  if (al.present("-mean_o"))
265  op.override_ival("mean_order", al.ival("-mean_o", 0));
266  if (al.present("-df_lf"))
267  op.override_ival("df_low_frequency", al.ival("-df_lf", 0));
268  if (al.present("-df_lo"))
269  op.override_ival("df_low_order", al.ival("-df_lo", 0));
270  if (al.present("-min"))
271  op.override_fval("min_period", al.fval("-min", 0));
272  if (al.present("-max"))
273  op.override_fval("max_period", al.fval("-max", 0));
274  if (al.present("-def"))
275  op.override_fval("def_period", al.fval("-def", 0));
276  if (al.present("-end"))
277  op.override_fval("pm_end", al.fval("-end", 0));
278  if (al.present("-inter"))
279  op.override_ival("pm_debug", 1);
280 }
281 
283 {
284  op.set("lx_low_frequency", LX_LOW_FREQUENCY);
285  op.set("lx_low_order", LX_LOW_ORDER);
286  op.set("lx_high_frequency", LX_HIGH_FREQUENCY);
287  op.set("lx_high_order", LX_HIGH_ORDER);
288  op.set("df_low_frequency", DF_LOW_FREQUENCY);
289  op.set("df_low_order", DF_LOW_ORDER);
290  op.set("min_period", MIN_PERIOD);
291  op.set("max_period", MAX_PERIOD);
292  op.set("def_period", DEF_PERIOD);
293  op.set("pm_end", PM_END);
294 
295  if (al.present("-lx_lf"))
296  op.set("lx_low_frequency", al.ival("-lx_lf", 0));
297  if (al.present("-lx_lo"))
298  op.set("lx_low_order", al.ival("-lx_lo", 0));
299  if (al.present("-lx_hf"))
300  op.set("lx_high_frequency", al.ival("-lx_hf", 0));
301  if (al.present("-lx_ho"))
302  op.set("lx_high_order", al.ival("-lx_ho", 0));
303  if (al.present("-med_o"))
304  op.set("median_order", al.ival("-med_o", 0));
305  if (al.present("-mean_o"))
306  op.set("mean_order", al.ival("-mean_o", 0));
307  if (al.present("-df_lf"))
308  op.set("df_low_frequency", al.ival("-df_lf", 0));
309  if (al.present("-df_lo"))
310  op.set("df_low_order", al.ival("-df_lo", 0));
311  if (al.present("-min"))
312  op.set("min_period", al.fval("-min", 0));
313  if (al.present("-max"))
314  op.set("max_period", al.fval("-max", 0));
315  if (al.present("-def"))
316  op.set("def_period", al.fval("-def", 0));
317  if (al.present("-end"))
318  op.set("pm_end", al.fval("-end", 0));
319  if (al.present("-inter"))
320  op.set("pm_debug", 1);
321 }
322 
A class for storing digital waveforms. The waveform is stored as an array of 16 bit shorts...
Definition: EST_Wave.h:64
void pm_fill(EST_Track &pm, float new_end, float max, float min, float def)
Definition: pitchmark.cc:154
int contains(const char *s, ssize_t pos=-1) const
Does it contain this substring?
Definition: EST_String.h:365
EST_write_status
The file was read in successfully.
#define DEF_PERIOD
Definition: EST_pitchmark.h:50
#define DF_LOW_FREQUENCY
Definition: EST_pitchmark.h:46
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:72
void set_options(EST_Features &op, EST_Option &al)
int ival(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:82
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
#define LX_LOW_FREQUENCY
Definition: EST_pitchmark.h:42
void override_lib_ops(EST_Option &op, EST_Option &al)
float fval(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:104
void pm_min_check(EST_Track &pm, float min)
Definition: pitchmark.cc:138
void set(const EST_String &name, int ival)
Definition: EST_Features.h:186
EST_read_status load(const EST_String name, float ishift=0.0, float startt=0.0)
Definition: EST_Track.cc:1312
float & t(ssize_t i=0)
return time position of frame i
Definition: EST_Track.h:478
#define misc_write_error
void pm_to_f0(EST_Track &pm, EST_Track &f0)
Definition: pitchmark.cc:261
The file was written successfully.
#define LX_HIGH_FREQUENCY
Definition: EST_pitchmark.h:44
EST_write_status save(const EST_String name, const EST_String EST_filetype="")
Definition: EST_Track.cc:1233
#define LX_HIGH_ORDER
Definition: EST_pitchmark.h:45
NULL
Definition: EST_WFST.cc:55
const T & first() const
return const reference to first item in list
Definition: EST_TList.h:152
ssize_t num_frames() const
return number of frames in track
Definition: EST_Track.h:651
EST_String options_wave_input(void)
getString int
Definition: EST_item_aux.cc:50
The file was not written successfully.
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
Definition: EST_TKVL.cc:145
float end()
return the time position of the last sample.
Definition: EST_Wave.h:153
EST_String options_track_output(void)
int sample_rate() const
return the sampling rate (frequency)
Definition: EST_Wave.h:147
#define PM_END
Definition: EST_pitchmark.h:51
EST_read_status read_wave(EST_Wave &sig, const EST_String &in_file, EST_Option &al)
#define DF_LOW_ORDER
Definition: EST_pitchmark.h:47
#define MIN_PERIOD
Definition: EST_pitchmark.h:48
int present(const K &rkey) const
Returns true if key is present.
Definition: EST_TKVL.cc:222
int main(int argc, char *argv[])
EST_write_status save(const EST_String &filename, bool evaluate_ff=false) const
EST_Track pitchmark(EST_Wave &lx, EST_Features &op)
Definition: pitchmark.cc:105
LISP fp
Definition: kkcompile.cc:63
EST_String
void pm_to_label(EST_Track &pm, EST_Relation &lab)
Definition: pitchmark.cc:248
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
void invert(EST_Wave &sig)
float F(const EST_String &path) const
Definition: EST_Features.h:136
#define MAX_PERIOD
Definition: EST_pitchmark.h:49
#define LX_LOW_ORDER
Definition: EST_pitchmark.h:43