-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtgRead.m
More file actions
274 lines (241 loc) · 8.83 KB
/
Copy pathtgRead.m
File metadata and controls
274 lines (241 loc) · 8.83 KB
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
function [textgrid, fid] = tgRead(fileName, encoding)
% function textgrid = tgRead(fileName)
%
% Loads TextGrid from Praat in Text or Short text format,
% it handles both Interval and Point tiers.
% Labels may contain quotation marks and new lines.
%
% fileName ... file name of TextGrid
% encoding ... [optional, default: 'UTF-8'] file encoding, 'auto' for Unicode
% standard autodetection
%
% Tomas Boril, borilt@gmail.com + Pol van Rijn
%
% Example
% tg = tgRead('demo/H.TextGrid');
% tgPlot(tg);
textgrid = [];
if ~isnumeric(fileName)
% means it is not a collection!
if nargin < 2
encoding = 'UTF-8';
end
if strcmp(encoding, 'auto')
encoding = tgDetectEncoding(fileName);
end
[fid, message] = fopen(fileName, 'r', 'n', encoding);
if fid == -1
error(['cannot open file [' fileName ']: ' message]);
end
for I = 1: 3
r = fgetl(fid); % ignore
end
else
% encoding is never used
fid = fileName;
end
xminStr = fgetl(fid); % xmin
xmaxStr = fgetl(fid); % xmax
r = fgetl(fid); % either '<exists>' -> shorttext or 'tiers? <exists> ' -> full text format
if ~isempty(strfind(r, 'tiers?'))
shortFormat = false;
elseif ~isempty(strfind(r, '<exists>'))
shortFormat = true;
else
fclose(fid);
error('Unknown textgrid format.');
end
xmin = getNumberInLine(xminStr, shortFormat);
xmax = getNumberInLine(xmaxStr, shortFormat);
if shortFormat
numberOfTiers = str2double(fgetl(fid));
else
r = fgetl(fid);
if r(end) ~= ' '
sppasFormat = true;
else
sppasFormat = false;
end
r = textscan(r, 'size = %d');
numberOfTiers = r{1};
end
for tier = 1: numberOfTiers
if shortFormat
typ = fgetl(fid);
else
r = strtrim(fgetl(fid));
while strcmp(r(1:4), 'item') == 1
r = strtrim(fgetl(fid));
end
if strcmp(r(1:9), 'class = "') ~= 1
fclose(fid); error('unknown textgrid format');
end
typ = r(9:end);
end
if strcmp(typ, '"IntervalTier"') == 1 % IntervalTier
r = fgetl(fid); % name
if shortFormat
textgrid.tier{tier}.name = r(2: end-1);
else
r = strtrim(r);
textgrid.tier{tier}.name = r(9: end-1);
end
textgrid.tier{tier}.type = 'interval';
r = fgetl(fid); % ignore xmin and xmax
r = fgetl(fid);
if shortFormat
numberOfIntervals = str2double(fgetl(fid));
else
r = strtrim(fgetl(fid));
numberOfIntervals = str2double(r(19:end));
end
textgrid.tier{tier}.T1 = [];
textgrid.tier{tier}.T2 = [];
textgrid.tier{tier}.Label = {};
for I = 1: numberOfIntervals
if ~shortFormat
r = fgetl(fid); % ignore line intervals [..]:
end
if shortFormat
t = str2double(fgetl(fid));
t2 = str2double(fgetl(fid));
else
r1 = strtrim(fgetl(fid));
r2 = strtrim(fgetl(fid));
if strcmp(r1(1:7), 'xmin = ') ~= 1 || strcmp(r2(1:7), 'xmax = ') ~= 1
fclose(fid); error('unknown textgrid format');
end
t = str2double(r1(8:end));
t2 = str2double(r2(8:end));
end
r = fgetl(fid);
if ~shortFormat
if isempty(strfind(r, 'text = "'))
fclose(fid); error('unknown textgrid format');
end
rind = strfind(r, '"');
numberOfQuotationMarks = sum(r == '"');
if mod(numberOfQuotationMarks, 2) ~= 1 % remove whitespace at the end of line, it is only in the case of even number of quotation marks
if sppasFormat ~= true
r = r(rind(1): end-1);
else
r = r(rind(1): end);
end
else
r = r(rind(1): end);
end
end
numberOfQuotationMarks = sum(r == '"');
label = r(2:end);
if mod(numberOfQuotationMarks, 2) == 1
label = [label sprintf('\n')];
while 1
r = fgetl(fid);
numberOfQuotationMarks = sum(r == '"');
if ~shortFormat && mod(numberOfQuotationMarks, 2) == 1 && ~sppasFormat % remove whitespace at the end of line, it is only in the case of odd number of quotation marks
r = r(1: end-1);
end
if mod(numberOfQuotationMarks, 2) == 1 && r(end) == '"'
label = [label r(1:end-1) '"'];
break
else
label = [label sprintf([r '\n'])];
end
end
end
label = label(1: end-1);
if isempty(label)
label = ''; % unification: '' and Empty string: 1-by-0
end
textgrid.tier{tier}.T1 = [textgrid.tier{tier}.T1 t];
textgrid.tier{tier}.T2 = [textgrid.tier{tier}.T2 t2];
textgrid.tier{tier}.Label{1, I} = label; % trim quotation marks
xmin = min(t, xmin); xmin = min(t2, xmin);
xmax = max(t, xmax); xmax = max(t2, xmax);
end
elseif strcmp(typ, '"TextTier"') == 1 % PointTier
r = fgetl(fid); % name
if shortFormat
textgrid.tier{tier}.name = r(2: end-1);
else
r = strtrim(r);
textgrid.tier{tier}.name = r(9: end-1);
end
textgrid.tier{tier}.type = 'point';
r = fgetl(fid); % ignore xmin and xmax
r = fgetl(fid);
if shortFormat
numberOfIntervals = str2double(fgetl(fid));
else
r = strtrim(fgetl(fid));
numberOfIntervals = str2double(r(16:end));
end
textgrid.tier{tier}.T = [];
textgrid.tier{tier}.Label = {};
for I = 1: numberOfIntervals
if ~shortFormat
r = fgetl(fid); % ignore line points [..]:
end
if shortFormat
t = str2double(fgetl(fid));
else
r = strtrim(fgetl(fid));
if strcmp(r(1:9), 'number = ') ~= 1
fclose(fid); error('unknown textgrid format');
end
t = str2double(r(10:end));
end
r = fgetl(fid);
if ~shortFormat
if isempty(strfind(r, 'mark = "'))
fclose(fid); error('unknown textgrid format');
end
rind = strfind(r, '"');
numberOfQuotationMarks = sum(r == '"');
if mod(numberOfQuotationMarks, 2) ~= 1 % remove whitespace at the end of line, it is only in the case of even number of quotation marks
if sppasFormat ~= true
r = r(rind(1): end-1);
else
r = r(rind(1): end);
end
else
r = r(rind(1): end);
end
end
numberOfQuotationMarks = sum(r == '"');
label = r(2:end);
if mod(numberOfQuotationMarks, 2) == 1
label = [label sprintf('\n')];
while 1
r = fgetl(fid);
numberOfQuotationMarks = sum(r == '"');
if ~shortFormat && mod(numberOfQuotationMarks, 2) == 1 && ~sppasFormat % remove whitespace at the end of line, it is only in the case of odd number of quotation marks
r = r(1: end-1);
end
if mod(numberOfQuotationMarks, 2) == 1 && r(end) == '"'
label = [label r(1:end-1) '"'];
break
else
label = [label sprintf([r '\n'])];
end
end
end
label = label(1: end-1);
if isempty(label)
label = ''; % unification: '' and Empty string: 1-by-0
end
textgrid.tier{tier}.T = [textgrid.tier{tier}.T t];
textgrid.tier{tier}.Label{1, I} = label; % trim quotation marks
xmin = min(t, xmin);
xmax = max(t, xmax);
end
else % unknown tier
fclose(fid);
error('Unknown tier type - IntervalTier and PointTier are supported only.');
end
end
if ~isnumeric(fileName)
fclose(fid);
end
textgrid.tmin = xmin;
textgrid.tmax = xmax;