forked from dcavar/ELAN2split
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySAX2Handler.cpp
281 lines (260 loc) · 10.8 KB
/
MySAX2Handler.cpp
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
/*
* Created by Damir Cavar on 7/28/15.
*
* (C) 2015, 2016 by Damir Cavar <damir@linguistlist.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
//#include <sox.h>
#include "MySAX2Handler.h"
using namespace std;
using namespace xercesc;
MySAX2Handler::MySAX2Handler() {
text_suffix = ".txt";
}
void MySAX2Handler::startElement(
const XMLCh *const uri,
const XMLCh *const localname,
const XMLCh *const qname,
const Attributes &attrs) {
char *message = XMLString::transcode(localname);
string tag_name = message;
XMLString::release(&message);
int attribute_name;
int tag = myTags_[tag_name];
char *temp;
switch (tag) {
case TIME_SLOT: {
string tid;
int time;
for (int i = 0; i < attrs.getLength(); i++) {
temp = XMLString::transcode(attrs.getLocalName(i));
attribute_name = myTags_[temp];
XMLString::release(&temp);
temp = XMLString::transcode(attrs.getValue(i));
if (attribute_name == TIME_SLOT_ID) {
tid = temp;
} else if (attribute_name == TIME_VALUE) {
time = atoi(temp);
}
XMLString::release(&temp);
}
time_slots[tid] = time;
break;
}
case MEDIA_DESCRIPTOR: {
for (int i = 0; i < attrs.getLength(); i++) {
temp = XMLString::transcode(attrs.getLocalName(i));
attribute_name = myTags_[temp];
XMLString::release(&temp);
temp = XMLString::transcode(attrs.getValue(i));
if (attribute_name == RELATIVE_MEDIA_URL) {
fs::path fp(file_path);
fs::path file_temp(temp);
fp = fs::canonical(fp / file_temp);
if (fs::is_regular_file(fp)) {
rel_media_urls.push_back(fp.string());
if (!quiet)
cout << "Using media file: " << rel_media_urls[0] << endl;
} else {
cerr << "Media file not fount: " << fp << endl;
// does not get to the correct exception handler!!!
throw invalid_argument("Media file " + fp.string() + " cannot be found.");
}
}
XMLString::release(&temp);
}
break;
}
case HEADER: {
// initial tag for new header: start collecting new time_slots, check for attribute MEDIA_FILE
for (int i = 0; i < attrs.getLength(); i++) {
temp = XMLString::transcode(attrs.getLocalName(i));
attribute_name = myTags_[temp];
XMLString::release(&temp);
temp = XMLString::transcode(attrs.getValue(i));
if (attribute_name == TIME_UNITS) {
time_units = temp;
if (!quiet)
cout << "Setting time units of intervals to: " << time_units << endl;
}
XMLString::release(&temp);
}
break;
}
case ANNOTATION:
break;
case REF_ANNOTATION: {
// get the reference for ANNOTATION_REF
if (parse_tier_annotation) {
for (int i = 0; i < attrs.getLength(); i++) {
temp = XMLString::transcode(attrs.getLocalName(i));
attribute_name = myTags_[temp];
XMLString::release(&temp);
temp = XMLString::transcode(attrs.getValue(i));
if (attribute_name == ANNOTATION_ID) {
myInt.id = temp;
} else if (attribute_name == ANNOTATION_REF) { // get the labels of the time boundaries
pair<string, string> idtuple = ref_slots[temp];
myInt.start_label = idtuple.first;
myInt.start = time_slots[idtuple.first];
myInt.stop_label = idtuple.second;
myInt.stop = time_slots[idtuple.second];
}
XMLString::release(&temp);
}
}
break;
}
case ALIGNABLE_ANNOTATION: {
for (int i = 0; i < attrs.getLength(); i++) {
temp = XMLString::transcode(attrs.getLocalName(i));
attribute_name = myTags_[temp];
XMLString::release(&temp);
temp = XMLString::transcode(attrs.getValue(i));
if (attribute_name == ANNOTATION_ID) {
myInt.id = temp;
} else if (attribute_name == TIME_SLOT_REF1) {
myInt.start_label = temp;
myInt.start = time_slots[temp];
} else if (attribute_name == TIME_SLOT_REF2) {
myInt.stop_label = temp;
myInt.stop = time_slots[temp];
ref_slots[myInt.id] = make_pair(myInt.start_label, myInt.stop_label);
}
XMLString::release(&temp);
}
break;
}
case ANNOTATION_VALUE: {
if (parse_tier_annotation) {
collect_annotation_value = true;
}
break;
}
case TIER: {
// Skip if the specific tier parameter given has been parsed, else use all tiers.
if (!tier_selected) {
for (int i = 0; i < attrs.getLength(); i++) {
temp = XMLString::transcode(attrs.getLocalName(i));
attribute_name = myTags_[temp];
XMLString::release(&temp);
temp = XMLString::transcode(attrs.getValue(i));
if (attribute_name == TIER_ID) {
current_tier_name = temp;
if (target_tier_name.size() > 0) {
if (target_tier_name.compare(temp) == 0) {
tier_selected = true;
if (!quiet)
cout << "Processing tier: " << temp << endl;
parse_tier_annotation = true;
}
} else {
if (!quiet)
cout << "Processing tier: " << temp << endl;
parse_tier_annotation = true;
}
}
XMLString::release(&temp);
}
}
break;
}
}
} // MySAX2Handler::startElement
void MySAX2Handler::endElement(
const XMLCh *const uri,
const XMLCh *const localname,
const XMLCh *const qname) {
char *message = XMLString::transcode(localname);
int tag = myTags_[message];
switch (tag) {
case HEADER: { // this is the closing tag for header, start collecting new time_slots
break;
}
case TIME_ORDER: // this is the end of the time slot definition
if (!quiet)
cout << "Parsed " << time_slots.size() << " time slots." << endl;
break;
case TIER: // tier closed, stop parsing, if active
parse_tier_annotation = false;
break;
case ANNOTATION_VALUE: {
if (parse_tier_annotation) {
myInt.text = buffer;
buffer.clear();
collect_annotation_value = false;
}
break;
}
case ANNOTATION: {
if (myInt.text.size() > 0) {
ostringstream convert; // stream used for the conversion
convert << std::hash<std::string>{}(current_tier_name) << "_" << myInt.id << "_" << myInt.start << "_" << myInt.stop; // << ".wav";
string ofname = convert.str();
convert.str("");
convert.clear();
// wav file is only segment id and time interval
fs::path owavfile = fs::path(output_folder) / fs::path(ofname + ".wav");
// additionally append the tier name to the text file
fs::path otextfile = fs::path(output_folder) / fs::path(ofname + text_suffix);
ofname = owavfile.string();
convert << "sox \"" << rel_media_urls[0] << "\" \"" << ofname
<< "\" trim " << (float) myInt.start / 1000.0 << " "
<< ((float) myInt.stop - (float) myInt.start) / 1000.0;
string command = convert.str();
convert.str("");
convert.clear();
// create wav file
if (fs::exists(owavfile) && !force_overwrite) {
cerr << "Will not overwrite " << owavfile.string() << endl;
} else {
if (!quiet)
cout << "Calling: " << command << endl;
system(command.c_str());
}
// create transcript file
if (fs::exists(otextfile) && !force_overwrite) {
cerr << "Will not overwrite " << otextfile.string() << endl;
} else {
if (!quiet)
cout << "Creating: " << otextfile.string() << endl;
ofstream myfile;
myfile.open(otextfile.string());
myfile << myInt.text << endl;
myfile.close();
}
myInt.stop = 0;
myInt.start = 0;
myInt.text.clear();
myInt.stop_label.clear();
myInt.start_label.clear();
}
break;
}
}
XMLString::release(&message);
} // MySAX2Handler::endElement
void MySAX2Handler::characters( const XMLCh *const chars, const XMLSize_t length) {
if (collect_annotation_value) {
char *temp = XMLString::transcode(chars);
buffer.append(temp);
XMLString::release(&temp);
}
}
void MySAX2Handler::fatalError(const SAXParseException &exception) {
char *message = XMLString::transcode(exception.getMessage());
cerr << "Fatal Error: " << message << " at line: " << exception.getLineNumber() << endl;
XMLString::release(&message);
} // MySAX2Handler::fatalError