Edinburgh Speech Tools  2.1-release
EST_relation_track.cc
Go to the documentation of this file.
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 /* Author : Paul Taylor and Simon King */
34 /* Date : June 1995 */
35 /*-----------------------------------------------------------------------*/
36 /* Stream class auxiliary routines that refer to tracks */
37 /* */
38 /*=======================================================================*/
39 
40 #include <cstdlib>
41 #include <iostream>
42 #include <fstream>
43 #include <cmath>
44 #include "EST_types.h"
47 #include "EST_track_aux.h"
48 #include "EST_string_aux.h"
49 #include "EST_io_aux.h"
50 #include "EST_Option.h"
51 
52 static int pos_phone(const EST_Relation &seg, float x, float shift);
53 
54 void track_to_label(const EST_Track &tr, EST_Relation &lab, float thresh)
55 {
56  ssize_t i;
57  EST_Item *tmp_seg;
58  int p_pos = FALSE;
59  int c_pos = FALSE;
60 
61  for (i = 0; i < tr.num_frames(); ++i)
62  {
63  if (tr.a(i) > thresh)
64  c_pos = TRUE;
65  else
66  c_pos = FALSE;
67 
68  if (c_pos == p_pos)
69  {
70  p_pos = c_pos;
71  continue;
72  }
73 
74  tmp_seg = lab.append();
75 
76  if (c_pos == TRUE)
77  tmp_seg->set_name("neg");
78  else
79  tmp_seg->set_name("pos");
80 
81  tmp_seg->set("end", tr.t(i - 1));
82 
83  p_pos = c_pos;
84  }
85 
86  tmp_seg = lab.append();
87  if (c_pos)
88  tmp_seg->set_name("pos");
89  else
90  tmp_seg->set_name("neg");
91 
92  tmp_seg->set("end", tr.t(i - 1));
93 }
94 
95 void track_to_pm(const EST_Track &tr, int sample_rate, EST_Relation &lab)
96 {
97  int i;
98  EST_Item *tmp_seg;
99 
100  bool have_offset = tr.has_channel(channel_offset);
101  bool have_length = tr.has_channel(channel_length);
102 
103  for (i = 0; i < tr.num_frames(); ++i)
104  {
105  float c, b, e=0.0;
106  if (have_length)
107  if (have_offset)
108  get_frame_o(tr, sample_rate, i, b, c, e);
109  else
110  get_frame(tr, sample_rate, i, b, c, e);
111  else
112  c = tr.t(i);
113 
114  if (have_length)
115  {
116  tmp_seg = lab.append();
117  tmp_seg->set_name("b");
118  tmp_seg->set("end", b);
119  }
120 
121  tmp_seg = lab.append();
122  tmp_seg->set_name("pm");
123  tmp_seg->set("end", c);
124 
125  if (have_length)
126  {
127  tmp_seg = lab.append();
128  tmp_seg->set_name("e");
129  tmp_seg->set("end", e);
130  }
131  }
132 }
133 
134 void label_to_track(const EST_Relation &lab, EST_Track &tr,
135  float shift, float offset, float
136  range, float req_l, const EST_String &pad)
137 {
138  EST_Item tmp_seg;
139  ssize_t i;
140  int n, endn;
141 
142  n = (int)ceil(lab.tail()->F("end")/shift);
143  endn = (req_l > 0.0) ? (int)(req_l /shift) : n;
144 
145  // cout << req_l << endl;
146  // cout << "shift " << shift << endl;
147  // cout << "endn is " << endn << endl;
148  // cout << lab.tail()->f.F("end") << " " << shift << endl;
149 
150  tr.resize(endn, 1);
151  tr.fill_time(shift);
152 
153  for (i = 0; i < n; ++i)
154  {
155  tr.a(i) = (pos_phone(lab, tr.t(i), shift) * range) + offset;
156  tr.set_value(i);
157  }
158  for (; i < endn; ++i)
159  {
160  tr.a(i) = (pad == "high") ? range + offset : offset;
161  tr.set_value(i);
162  }
163 }
164 
165 void label_to_track(const EST_Relation &lab,
166  const EST_Option &al,
167  const EST_Option &op,
168  EST_Track &tr)
169 {
170  float shift = op.present("frame_shift") ? op.fval("frame_shift"): 0.01;
171  float offset = op.present("label_offset")? op.fval("label_offset"):0.0;
172 
173  float range = op.present("label_range") ? op.fval("label_range"): 1.0;
174  float length = al.present("-length") ? al.fval("-length") : -1.0;
175 
176  label_to_track(lab, tr, shift, offset, range, length, al.val("-pad", 0));
177  // tr.amin = 0.0;
178  // tr.amax = 3.0;
179 
180 }
181 
182 static int pos_phone(const EST_Relation &seg, float x, float shift)
183 {
184  // returns true if x is in a positive segment. The decision is
185  // slightly biased towards positive inclusion when x is near
186  // a boundary.
187  EST_Item *p;
188 
189  for (p = seg.head(); p != 0; p = p->next())
190  if (p->f("pos") == 1)
191  if ((x < (p->F("end") + (shift / 2.0))) &&
192  (x > (start(p) - (shift / 2.0))))
193  return 1;
194  return 0;
195 }
196 
EST_Item * head() const
Definition: EST_Relation.h:121
Utility IO Function header file.
EST_Item * tail() const
Definition: EST_Relation.h:127
void label_to_track(const EST_Relation &lab, EST_Track &tr, float shift, float offset, float range, float req_l, const EST_String &pad)
void set_value(ssize_t i)
set frame i to be a value
Definition: EST_Track.cc:133
void set(const EST_String &name, ssize_t ival)
Definition: EST_Item.h:185
float fval(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:104
int ssize_t
void resize(ssize_t num_frames, int num_channels, bool preserve=1)
Definition: EST_Track.cc:214
const EST_Val f(const EST_String &name) const
Definition: EST_Item.h:260
void track_to_label(const EST_Track &tr, EST_Relation &lab, float thresh)
void set_name(const EST_String &name) const
Definition: EST_Item.h:254
float & t(ssize_t i=0)
return time position of frame i
Definition: EST_Track.h:478
float & a(ssize_t i, int c=0)
Definition: EST_Track.cc:1025
float F(const EST_String &name) const
Definition: EST_Item.h:135
#define FALSE
Definition: EST_bool.h:119
ssize_t num_frames() const
return number of frames in track
Definition: EST_Track.h:651
Length of section in samples.
getString int
Definition: EST_item_aux.cc:50
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
Definition: EST_TKVL.cc:145
EST_Item * next() const
Definition: EST_Item.h:348
Offset from frame center to center of window.
float start(const EST_Item &item)
Definition: EST_item_aux.cc:52
int present(const K &rkey) const
Returns true if key is present.
Definition: EST_TKVL.cc:222
EST_Item * append(EST_Item *si)
Definition: EST_Relation.cc:88
void track_to_pm(const EST_Track &tr, int sample_rate, EST_Relation &lab)
bool has_channel(const char *name) const
Definition: EST_Track.h:385
#define TRUE
Definition: EST_bool.h:118
void fill_time(float t, int start=1)
Definition: EST_Track.cc:789
Utility EST_String Functions header file.