File: pitchmark_main.cc

package info (click to toggle)
speech-tools 1%3A2.5.0-11
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 9,988 kB
  • sloc: cpp: 67,350; ansic: 12,174; sh: 4,055; java: 3,748; makefile: 1,111; lisp: 711; perl: 396; awk: 85; xml: 9
file content (427 lines) | stat: -rw-r--r-- 15,323 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*************************************************************************/
/*                                                                       */
/*                Centre for Speech Technology Research                  */
/*                     University of Edinburgh, UK                       */
/*                         Copyright (c) 1996                            */
/*                        All Rights Reserved.                           */
/*                                                                       */
/*  Permission is hereby granted, free of charge, to use and distribute  */
/*  this software and its documentation without restriction, including   */
/*  without limitation the rights to use, copy, modify, merge, publish,  */
/*  distribute, sublicense, and/or sell copies of this work, and to      */
/*  permit persons to whom this work is furnished to do so, subject to   */
/*  the following conditions:                                            */
/*   1. The code must retain the above copyright notice, this list of    */
/*      conditions and the following disclaimer.                         */
/*   2. Any modifications must be clearly marked as such.                */
/*   3. Original authors' names are not deleted.                         */
/*   4. The authors' names are not used to endorse or promote products   */
/*      derived from this software without specific prior written        */
/*      permission.                                                      */
/*                                                                       */
/*  THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK        */
/*  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING      */
/*  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT   */
/*  SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE     */
/*  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    */
/*  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   */
/*  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          */
/*  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       */
/*  THIS SOFTWARE.                                                       */
/*                                                                       */
/*************************************************************************/
/*                    Author :  Paul Taylor                              */
/*                    Date   :  1997, 1998, 1999                         */
/*-----------------------------------------------------------------------*/
/*                    Pitchmarking program                               */
/*************************************************************************/

#include <cstdlib>
#include <iostream>
#include <fstream>
#include "EST_unix.h"
#include "EST_cmd_line_options.h"
#include "EST_cmd_line.h"
#include "EST_speech_class.h"
#include "sigpr/EST_pitchmark.h"


void set_options(EST_Features &op, EST_Option &al);

static EST_write_status save_msec(EST_Track &pm, EST_String filename);
static EST_write_status save_ogi_bin(EST_Track &pm, EST_String filename, 
				     int sr);
void pm_to_label(EST_Track &pm, EST_Relation &lab);


/*void pm_to_label(EST_Track &pm, EST_Relation &lab);
void find_pm(EST_Wave &sig, EST_Track &pm);

void pm_min_check(EST_Track &pm, float min);
void pm_sanity_check(EST_Track &pm, float new_end,
		     float max, float min, float def);

void pm_fill(EST_Track &pm, float new_end, float max, 
	     float min, float def);

void pm_to_f0(EST_Track &pm, EST_Track &f0);
*/


/** @name <command> pitchmark </command> <emphasis> Find instants of glottal closure in Laryngograph file</emphasis>

  * @id pitchmark-manual
  * @toc */

//@{


/**@name Synopsis
  */
//@{

//@synopsis

/**
<command>pitchmark</command> locates instants of glottal closure in a
laryngograph waveform, and performs post-processing to produce even
pitchmarks. EST does not currently provide any means of pitchmarking a
speech waveform.

Pitchmarking is performed by calling the
<function>pitchmark()</function> function, which carries out the
following operations: 

<orderedlist> <listitem><para>Double low pass filter the signal. This
removes noise in the signal. The parameter
<parameter>lx_lf</parameter> specifies the low pass cutoff frequency,
and <parameter>lx_lo</parameter> specifies the order. Double filtering
(feeding the waveform through the filter, then reversing the waveform
and feeding it through again) is performed to reduce any phase shift
between the input and output of the filtering operation.
</para></listitem>

<listitem><para>Double high pass filter the signal. This removes the
very low frequency swell that is often observed in laryngograph
waveforms.  The parameter <parameter>lx_hf</parameter> specifies the high pass cutoff frequency,
and <parameter>lx_ho</parameter> specifies the order.
Double filtering is performed to reduce any phase shift
between the input and output of the filtering operation.
</para></listitem>

<listitem><para>Calculate the delta signal. The filtered waveform is
differentiated using the <function>delta()</function>
function.</para></listitem>

<listitem><para>Low pass filter the delta signal. Some noise may still
be present in the signal, and this is removed by further low pass
filtering. Experimentation has shown that simple mean smoothing is
often more effective than FIR smoothing at this point.  The parameter
<parameter>mo</parameter> is used to specify the size of the mean
smoothing window.  If FIR smoothing is chosen, the parameter
<parameter>df_lf</parameter> specifies the low pass cutoff frequency,
and <parameter>df_lo</parameter> specifies the order. Double filtering
is again used to avoid phase distortion.

</para></listitem>

<listitem><para>Pick zero crossings. Now simple zero-crossing is used
to find the pitchmarks themselves.  </para></listitem>

</orderedlist>

<command>pitchmark</command> also performs post-processing on the pitchmarks. 
This can be used to eliminate pitchmarks which occur too closely together, 
or to provide estimated evenly spaced pitchmarks during unvoiced regions.
The -fill option switches <action>this facility on</action>, 
and -min, -max, -def, 
-end and -wave_end control its operation.

*/

//@}

/**@name OPTIONS
  */
//@{

//@options

//@}


int main (int argc, char *argv[])
{
    EST_Track pm;
    EST_Wave lx;
    EST_Option al;
    EST_Features op;
    EST_String out_file("-");
    EST_StrList files;

    parse_command_line
	(argc, argv, 
       EST_String("[input file] -o [output file] [options]")+
       "Summary: pitchmark laryngograph (lx) files\n"
       "use \"-\" to make input and output files stdin/out\n"
       "-h               Options help\n\n"+
       options_wave_input()+
       options_track_output()+
	 "-lx_lf <int>     lx low frequency cutoff\n\n"
	 "-lx_lo <int>     lx low order\n\n"
	 "-lx_hf <int>     lx high frequency cutoff\n\n"
	 "-lx_ho <int>     lx high order\n\n"
	 "-df_lf <int>     df low frequeny cutoff\n\n"
	 "-df_lo <int>     df low order\n\n"
	 "-med_o <int>     median smoothing order\n\n"
	 "-mean_o <int>    mean smoothing order\n\n"
	 "-inv   Invert polarity of lx signal. Often the lx signal \n"
	 "    is upside down. This option inverts the signal prior  to \n"
	 "    processing.\n\n"
	 "-fill Insert and remove pitchmarks according to min, max\n"
	 "    and def period values. Often it is desirable to place limits\n"
	 "    on the values of the pitchmarks. This option enforces a \n"
	 "    minimum and maximum pitch period (specified by -man and -max).\n"
	 "    If the maximum pitch setting is low enough, this will \n"
	 "    esnure that unvoiced regions have evenly spaced pitchmarks \n\n"
	 "-min <float>  Minimum allowed pitch period, in seconds\n\n"
	 "-max <float>  Maximum allowed pitch period, in seconds\n\n"
	 "-def <float>  Default pitch period in seconds, used for a guide\n"
	 "    as to what length pitch periods should be in unvoiced \n"
	 "    sections \n\n" 
	 "-pm <ifile>   Input is raw pitchmark file. This option is \n"
	 "    used to perform filling operations on an already existing \n"
	 "    set of pitchmarks \n\n"
	 "-f0 <ofile> Calculate F0 from pitchmarks and save to file\n\n"
	 "-end <float> Specify the end time of the last pitchmark, for use \n"
	 "    with the -fill option\n\n"
	 "-wave_end Use the end of a waveform to specify when the \n"
	 "    last pitchmark position should be. The waveform file is only \n"
	 "    read to determine its end, no  processing is performed\n\n"
	 "-inter  Output intermediate waveforms. This will output the \n"
	 "    signal at various stages of processing. Examination of these \n"
	 "    waveforms is extremely useful in setting the parameters for \n"
	 "    similar waveforms\n\n"
	 "-style <string>  \"track\" or \"lab\"\n\n", files, al);

    set_options(op, al);

    out_file = al.present("-o") ? al.val("-o") : (EST_String)"-";

    if (!al.present("-pm") || (al.present("-pm") && al.present("-wave_end")))
	if (read_wave(lx, files.first(), al) != read_ok)
	    exit(-1);

    if (al.present("-pm"))
	pm.load(al.val("-pm"));
    else
    {
	if (al.present("-inv"))
	    invert(lx);
	pm = pitchmark(lx, op);
    }

    // this allows the end to be aligned with the end of a waveform
    op.set("pm_end", lx.end());
    
    if (al.present("-f0"))
    {
	EST_Track f0;
	pm_to_f0(pm, f0);
	f0.save(al.val("-f0"));
    }

    // various options for filling he gaps between distant pitchmarks
    // and removing pitchmarks that are too close together

    if (al.present("-fill"))
    {
	pm_fill(pm, op.F("pm_end"), op.F("max_period"), 
			op.F("min_period"), op.F("def_period"));
	pm_fill(pm, op.F("pm_end"), op.F("max_period"), 
			op.F("min_period"), op.F("def_period"));
    }
    else if (al.present("-min"))
	pm_min_check(pm, al.fval("-min"));

    if (al.present("-style"))
    {
	// label format
	if (al.val("-style").contains("lab"))
	{
	    EST_Relation lab;
	    pm_to_label(pm, lab);
	    if (lab.save(out_file + ".pm_lab") != write_ok)
		exit(-1);
	}
	// save file in "traditional" milli-second format
	if (al.val("-style").contains("msec"))
	    save_msec(pm, out_file + ".pm");

	// ogi binary integer sample point format
	if (al.val("-style").contains("ogi_bin"))
	    save_ogi_bin(pm, out_file + ".pmv", lx.sample_rate());
    }
    else if (pm.save(out_file, al.val("-otype", 0)) != write_ok)
    {
	cerr << "pitchmark: failed to write output to \"" 
	    << out_file << "\"" << endl;
	exit(-1);
    }
    return 0;
}

static EST_write_status save_msec(EST_Track &pm, EST_String filename)
{
    ostream *outf;
    
    if (filename == "-")
	outf = &cout;
    else
	outf = new ofstream(filename);
    
    if (!(*outf))
	return write_fail;
    
    outf->precision(5);
    outf->setf(ios::fixed, ios::floatfield);
    outf->width(8);
    
    for (int i = 0; i < pm.num_frames(); ++i)
	*outf << pm.t(i)  * 1000.0 << endl;
    
    return write_ok;
}

static EST_write_status save_ogi_bin(EST_Track &pm, EST_String filename, int sr)
{
    int *d;
    FILE *fp;
    int i;
    
    d = new int[pm.num_frames()];
    
    for (i = 0; i < pm.num_frames(); ++i)
	d[i] = int(pm.t(i) * (float) sr);
    
    if ((fp = fopen(filename, "wb")) == NULL)
	return misc_write_error;
    
    if (fwrite(d, pm.num_frames(), sizeof(int), fp) != 1)
    {
	fclose(fp);
	return misc_write_error;
    }
    delete d;
    
    return write_ok;
}

void override_lib_ops(EST_Option &op, EST_Option &al)
{
    op.override_ival("lx_low_frequency", 400);
    op.override_ival("lx_low_order", 19);
    op.override_ival("lx_high_frequency", 40);
    op.override_ival("lx_high_order", 19);
    op.override_ival("df_low_frequency", 1000);
    op.override_ival("df_low_order", 19);
    op.override_fval("min_period", 0.003);
    op.override_fval("max_period", 0.02);
    op.override_fval("def_period", 0.01);
    op.override_fval("pm_end", -1.0);
    
    if (al.present("-lx_lf"))
	op.override_ival("lx_low_frequency", al.ival("-lx_lf", 0));
    if (al.present("-lx_lo"))
	op.override_ival("lx_low_order", al.ival("-lx_lo", 0));
    if (al.present("-lx_hf"))
	op.override_ival("lx_high_frequency", al.ival("-lx_hf", 0));
    if (al.present("-lx_ho"))
	op.override_ival("lx_high_order", al.ival("-lx_ho", 0));
    if (al.present("-med_o"))
	op.override_ival("median_order", al.ival("-med_o", 0));
    if (al.present("-mean_o"))
	op.override_ival("mean_order", al.ival("-mean_o", 0));
    if (al.present("-df_lf"))
	op.override_ival("df_low_frequency", al.ival("-df_lf", 0));
    if (al.present("-df_lo"))
	op.override_ival("df_low_order", al.ival("-df_lo", 0));
    if (al.present("-min"))
	op.override_fval("min_period", al.fval("-min", 0));
    if (al.present("-max"))
	op.override_fval("max_period", al.fval("-max", 0));
    if (al.present("-def"))
	op.override_fval("def_period", al.fval("-def", 0));
    if (al.present("-end"))
	op.override_fval("pm_end", al.fval("-end", 0));
    if (al.present("-inter"))
	op.override_ival("pm_debug", 1);
}

void set_options(EST_Features &op, EST_Option &al)
{
    op.set("lx_low_frequency", LX_LOW_FREQUENCY);
    op.set("lx_low_order", LX_LOW_ORDER);
    op.set("lx_high_frequency", LX_HIGH_FREQUENCY);
    op.set("lx_high_order", LX_HIGH_ORDER);
    op.set("df_low_frequency", DF_LOW_FREQUENCY);
    op.set("df_low_order", DF_LOW_ORDER);
    op.set("min_period", MIN_PERIOD);
    op.set("max_period", MAX_PERIOD);
    op.set("def_period", DEF_PERIOD);
    op.set("pm_end", PM_END);
    
    if (al.present("-lx_lf"))
	op.set("lx_low_frequency", al.ival("-lx_lf", 0));
    if (al.present("-lx_lo"))
	op.set("lx_low_order", al.ival("-lx_lo", 0));
    if (al.present("-lx_hf"))
	op.set("lx_high_frequency", al.ival("-lx_hf", 0));
    if (al.present("-lx_ho"))
	op.set("lx_high_order", al.ival("-lx_ho", 0));
    if (al.present("-med_o"))
	op.set("median_order", al.ival("-med_o", 0));
    if (al.present("-mean_o"))
	op.set("mean_order", al.ival("-mean_o", 0));
    if (al.present("-df_lf"))
	op.set("df_low_frequency", al.ival("-df_lf", 0));
    if (al.present("-df_lo"))
	op.set("df_low_order", al.ival("-df_lo", 0));
    if (al.present("-min"))
	op.set("min_period", al.fval("-min", 0));
    if (al.present("-max"))
	op.set("max_period", al.fval("-max", 0));
    if (al.present("-def"))
	op.set("def_period", al.fval("-def", 0));
    if (al.present("-end"))
	op.set("pm_end", al.fval("-end", 0));
    if (al.present("-inter"))
	op.set("pm_debug", 1);
}

/** @name Examples
</para>
<formalpara><title>Basic Pitchmarking</title>
<para>
<screen>
$ pitchmark kdt_010.lar -o kdt_010.pm -otype est
</screen>
</para> 
</formalpara>

<formalpara><title>Pitchmarking with unvoiced regions
filled</title> <para> The following fills unvoiced regions with pitch
periods that are about 0.01 seconds long. It also post-processes the
set of pitchmarks and ensures that noe are above 0.02 seconds long and
none below 0.003. A final unvoiced region extending to the end of the
wave is specified by using the -wave_end option.
</para> </formalpara><para>
<screen>
$ pitchmark kdt_010.lar -o kdt_010.pm -otype est -fill -min 0.003  \
       -max 0.02 -def 0.01 -wave_end
</screen>

*/

//@{
//@}