"Fossies" - the Fresh Open Source Software Archive 
Member "speech_tools/speech_class/EST_wave_cuts.cc" (4 Sep 2017, 6200 Bytes) of package /linux/misc/speech_tools-2.5.0-release.tar.gz:
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"
48 #include "ling_class/EST_Relation.h"
49 #include "ling_class/EST_item_aux.h"
50
51 static int wave_subwave(EST_Wave &subsig,EST_Wave &sig,
52 float offset, float length);
53
54 int wave_divide(EST_WaveList &wl, EST_Wave &sig, EST_Relation &keylab,
55 const EST_String &ext)
56 {
57 wl.clear();
58 EST_Wave a;
59 EST_Item *k;
60 EST_String filename;
61 float start = 0,end;
62
63 for (k = keylab.head(); k; k = inext(k))
64 {
65 a.clear();
66 end = k->F("end",0);
67 if (end < start)
68 continue;
69 wave_subwave(a, sig, start, end-start);
70 filename = (EST_String)k->f("file");
71 a.set_name(filename + ext);
72 wl.append(a);
73 start = end;
74 }
75
76 return 0;
77 }
78
79 int wave_extract(EST_Wave &part, EST_Wave &sig, EST_Relation &keylab,
80 const EST_String &file)
81 {
82 EST_Wave a;
83 EST_Item *k;
84 EST_String key_file_name;
85 float start=0, end;
86
87 for (k = keylab.head(); k; k = inext(k))
88 {
89 end = k->F("end",0);
90 key_file_name = (EST_String)k->f("file");
91 if (key_file_name == file)
92 {
93 wave_subwave(part, sig, start, end-start);
94 return 0;
95 }
96 start = end;
97 }
98 cerr << "Couldn't locate file fragment " << file << " in keylab file\n";
99 return -1;
100 }
101
102
103 static int wave_subwave(EST_Wave &subsig,EST_Wave &sig,
104 float offset, float length)
105 {
106 return wave_subwave(subsig, sig, (int)(offset *(float)sig.sample_rate()),
107 (int)(length *(float)sig.sample_rate()));
108 }
109
110 int wave_subwave(EST_Wave &subsig,EST_Wave &sig,int offset,int length)
111 {
112 // take out a subpart of sig and put it in subsig
113 int ns;
114
115 if (length == -1)
116 ns = sig.num_samples() - offset;
117 else
118 ns = length;
119
120 if ((offset+ns) > sig.num_samples())
121 {
122 cerr << "Subset past end of signal\n";
123 return -1;
124 }
125
126 EST_Wave subwave;
127
128 sig.sub_wave(subwave, offset, ns, 0, EST_ALL);
129
130 subsig.copy(subwave);
131
132 return 0;
133 }
134
135 int track_divide(EST_TList<EST_Track> &mtfr, EST_Track &fv, EST_Relation &key)
136 {
137 EST_Track a;
138 EST_Item *k, t;
139 float kstart, length;
140 int i, j, l, n;
141
142 mtfr.clear();
143
144 if ((key.tail())->F("end") < (fv.t(fv.num_frames() - 1)))
145 {
146 cerr << "Key file must extend beyond end of EST_Track\n";
147 cerr << "key end: " << key.tail()->F("end") << " EST_Track end: "
148 << fv.t(fv.num_frames() - 1) << endl;
149 return -1;
150 }
151
152 k = key.head();
153 a.set_name(k->name());
154 kstart = 0.0;
155
156 length = end(*k) - kstart;
157 n = (int)(length / (float) fv.shift()) + 2;
158 a.resize(n, fv.num_channels());
159
160 for (i = 0, l = 0; i < fv.num_frames(); ++i, ++l)
161 {
162 for (j = 0; j < fv.num_channels(); ++j)
163 a(l, j) = fv(i, j);
164
165 if (fv.t(i) > k->F("end"))
166 {
167 a.set_num_frames(l + 1);
168 mtfr.append(a);
169
170 kstart = k->F("end");
171 k = inext(k);
172 a.set_name(k->name());
173 length = k->F("end") - kstart;
174 n = (int)(length / (float) fv.shift()) + 2;
175 // cout << "n frames: " << n << endl;
176 a.resize(n, fv.num_channels());
177 a.fill_time(fv.shift());
178
179 // for (j = 0; j < fv.order(); ++j)
180 // a(0, j) = fv(i, j);
181 l = -1;
182 }
183 }
184 a.set_num_frames(l);
185 mtfr.append(a);
186 return 0;
187 }
188