Edinburgh Speech Tools  2.1-release
EST_wave_cuts.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 and Alan Black */
34 /* Date : June 1996 */
35 /*-----------------------------------------------------------------------*/
36 /* EST_Wave class methods for cutting, and extracting */
37 /* */
38 /*=======================================================================*/
39 
40 #include <cstring>
41 #include "EST_unix.h"
42 #include <cstdlib>
43 #include "EST_cutils.h"
44 #include "EST_string_aux.h"
45 #include "EST_Wave.h"
46 #include "EST_wave_aux.h"
47 #include "EST_Track.h"
50 
51 using namespace std;
52 
53 static int wave_subwave(EST_Wave &subsig,EST_Wave &sig,
54  float offset, float length);
55 
57  const EST_String &ext)
58 {
59  wl.clear();
60  EST_Wave a;
61  EST_Item *k;
62  EST_String filename;
63  float start = 0,end;
64 
65  for (k = keylab.head(); k; k = k->next())
66  {
67  a.clear();
68  end = k->F("end",0);
69  if (end < start)
70  continue;
71  wave_subwave(a, sig, start, end-start);
72  filename = (EST_String)k->f("file");
73  a.set_name(filename + ext);
74  wl.append(a);
75  start = end;
76  }
77 
78  return 0;
79 }
80 
81 int wave_extract(EST_Wave &part, EST_Wave &sig, EST_Relation &keylab,
82  const EST_String &file)
83 {
84  EST_Wave a;
85  EST_Item *k;
86  EST_String key_file_name;
87  float start=0, end;
88 
89  for (k = keylab.head(); k; k = k->next())
90  {
91  end = k->F("end",0);
92  key_file_name = (EST_String)k->f("file");
93  if (key_file_name == file)
94  {
95  wave_subwave(part, sig, start, end-start);
96  return 0;
97  }
98  start = end;
99  }
100  cerr << "Couldn't locate file fragment " << file << " in keylab file\n";
101  return -1;
102 }
103 
104 
105 static int wave_subwave(EST_Wave &subsig,EST_Wave &sig,
106  float offset, float length)
107 {
108  return wave_subwave(subsig, sig, (int)(offset *(float)sig.sample_rate()),
109  (int)(length *(float)sig.sample_rate()));
110 }
111 
112 int wave_subwave(EST_Wave &subsig,EST_Wave &sig,int offset,int length)
113 {
114  // take out a subpart of sig and put it in subsig
115  int ns;
116 
117  if (length == -1)
118  ns = sig.num_samples() - offset;
119  else
120  ns = length;
121 
122  if ((offset+ns) > sig.num_samples())
123  {
124  cerr << "Subset past end of signal\n";
125  return -1;
126  }
127 
128  EST_Wave subwave;
129 
130  sig.sub_wave(subwave, offset, ns, 0, EST_ALL);
131 
132  subsig.copy(subwave);
133 
134  return 0;
135 }
136 
138 {
139  EST_Track a;
140  EST_Item *k, t;
141  float kstart, length;
142  ssize_t i, j, l, n;
143 
144  mtfr.clear();
145 
146  if ((key.tail())->F("end") < (fv.t(fv.num_frames() - 1)))
147  {
148  cerr << "Key file must extend beyond end of EST_Track\n";
149  cerr << "key end: " << key.tail()->F("end") << " EST_Track end: "
150  << fv.t(fv.num_frames() - 1) << endl;
151  return -1;
152  }
153 
154  k = key.head();
155  a.set_name(k->name());
156  kstart = 0.0;
157 
158  length = end(*k) - kstart;
159  n = (ssize_t)(length / (float) fv.shift()) + 2;
160  a.resize(n, fv.num_channels());
161 
162  for (i = 0, l = 0; i < fv.num_frames(); ++i, ++l)
163  {
164  for (j = 0; j < fv.num_channels(); ++j)
165  a(l, j) = fv(i, j);
166 
167  if (fv.t(i) > k->F("end"))
168  {
169  a.set_num_frames(l + 1);
170  mtfr.append(a);
171 
172  kstart = k->F("end");
173  k = k->next();
174  a.set_name(k->name());
175  length = k->F("end") - kstart;
176  n = (ssize_t)(length / (float) fv.shift()) + 2;
177  // cout << "n frames: " << n << endl;
178  a.resize(n, fv.num_channels());
179  a.fill_time(fv.shift());
180 
181  // for (j = 0; j < fv.order(); ++j)
182  // a(0, j) = fv(i, j);
183  l = -1;
184  }
185  }
186  a.set_num_frames(l);
187  mtfr.append(a);
188  return 0;
189 }
190 
A class for storing digital waveforms. The waveform is stored as an array of 16 bit shorts...
Definition: EST_Wave.h:64
EST_Item * head() const
Definition: EST_Relation.h:121
float end(const EST_Item &item)
Definition: EST_item_aux.cc:96
const EST_String name() const
Definition: EST_Item.h:250
EST_Item * tail() const
Definition: EST_Relation.h:127
int track_divide(EST_TList< EST_Track > &mtfr, EST_Track &fv, EST_Relation &key)
int num_channels() const
return number of channels in track
Definition: EST_Track.h:657
int wave_subwave(EST_Wave &subsig, EST_Wave &sig, int offset, int length)
int wave_divide(EST_WaveList &wl, EST_Wave &sig, EST_Relation &keylab, const EST_String &ext)
void copy(const EST_Wave &from)
Definition: EST_Wave.cc:121
void set_name(const EST_String n)
Sets name.
Definition: EST_Wave.h:173
ssize_t num_samples() const
return the number of samples in the waveform
Definition: EST_Wave.h:143
int ssize_t
const int EST_ALL
void resize(ssize_t num_frames, int num_channels, bool preserve=1)
Definition: EST_Track.cc:214
void sub_wave(EST_Wave &sw, int offset=0, ssize_t num=EST_ALL, ssize_t start_c=0, ssize_t nchan=EST_ALL)
Definition: EST_Wave.cc:342
const EST_Val f(const EST_String &name) const
Definition: EST_Item.h:260
void set_num_frames(ssize_t n, bool preserve=1)
Definition: EST_Track.h:178
float & t(ssize_t i=0)
return time position of frame i
Definition: EST_Track.h:478
float F(const EST_String &name) const
Definition: EST_Item.h:135
void set_name(const EST_String &n)
set name of track - redundant use access to features
Definition: EST_Track.h:199
ssize_t num_frames() const
return number of frames in track
Definition: EST_Track.h:651
void append(const T &item)
add item onto end of list
Definition: EST_TList.h:196
EST_Item * next() const
Definition: EST_Item.h:348
int sample_rate() const
return the sampling rate (frequency)
Definition: EST_Wave.h:147
float start(const EST_Item &item)
Definition: EST_item_aux.cc:52
float shift() const
Definition: EST_Track.cc:602
int wave_extract(EST_Wave &part, EST_Wave &sig, EST_Relation &keylab, const EST_String &file)
void clear()
clear waveform and set size to 0.
Definition: EST_Wave.h:203
EST_String
void fill_time(float t, int start=1)
Definition: EST_Track.cc:789
void clear(void)
remove all items in list
Definition: EST_TList.h:244
Utility EST_String Functions header file.