comparison Code/Descriptors/Matlab/MPEG7/FromWeb/VoiceSauce/func_readTextgrid.m @ 4:92ca03a8fa99 tip

Update to ICASSP 2013 benchmark
author Dawn Black
date Wed, 13 Feb 2013 11:02:39 +0000
parents
children
comparison
equal deleted inserted replaced
3:e1cfa7765647 4:92ca03a8fa99
1 function [labels, start, stop] = func_readTextgrid(filename)
2 % [labels, start, stop] = func_readTextgrid(filename)
3 % Input: filename - textgrid file
4 % Output: labels, start, stop - vectors containing textgrid data
5 % Notes: Functions seeks out the "xmin", "xmax", and "text" labels within
6 % a textgrid file.
7 %
8 % Author: Yen-Liang Shue, Speech Processing and Auditory Perception Laboratory, UCLA
9 % Copyright UCLA SPAPL 2009
10
11 POINT_BUFFER = 0.025; % 25 ms buffer on either side of the point
12
13 if (exist(filename, 'file') == 0)
14 fprintf('Error: %s not found\n', filename);
15 labels = NaN; start = NaN; stop = NaN;
16 return;
17 end
18
19 fid = fopen(filename, 'rt');
20 C = textscan(fid, '%s', 'delimiter', '\n');
21 fclose(fid);
22
23 C = C{1};
24 tiers = 0;
25 proceed_int = 0; % proceed with intervals
26 proceed_pnt = 0; % proceed with point-sources
27 xmin = 0;
28 xmax = 1;
29
30 for k=1:length(C)
31 % try to read a string in the format: %s = %s
32 if (isempty(C{k}))
33 continue;
34 end
35
36 A = textscan(C{k}, '%[^=] = %s', 'delimiter', '\n');
37
38 if (~isempty(A{1}{1}))
39 switch A{1}{1}
40 % intervals
41 case {'intervals: size '} % we found the start of a new tier, now allocate mem
42 tiers = tiers + 1;
43 tier_len = str2num(A{2}{1});
44 labels{tiers} = cell(tier_len, 1);
45 start{tiers} = zeros(tier_len, 1);
46 stop{tiers} = zeros(tier_len, 1);
47 cnt = 1;
48 proceed_int = 1;
49 case {'xmin '}
50 if (proceed_int)
51 start{tiers}(cnt) = str2num(A{2}{1});
52 else
53 xmin = str2num(A{2}{1});
54 end
55 case {'xmax '}
56 if (proceed_int)
57 stop{tiers}(cnt) = str2num(A{2}{1});
58 else
59 xmax = str2num(A{2}{1});
60 end
61 case {'text '}
62 if (proceed_int)
63 lab = A{2}{1};
64 if (lab(end) ~= '"')
65 lab = lab(1:end-1);
66 end
67
68 labels{tiers}{cnt} = lab;
69 cnt = cnt + 1;
70 if (cnt > tier_len)
71 proceed_int = 0;
72 end
73 end
74 % point-sources
75 case {'points: size '} % we found the start of a new tier, now allocate mem
76 tiers = tiers + 1;
77 tier_len = str2num(A{2}{1});
78 labels{tiers} = cell(tier_len, 1);
79 start{tiers} = zeros(tier_len, 1);
80 stop{tiers} = zeros(tier_len, 1);
81 cnt = 1;
82 proceed_pnt = 1;
83 case {'time '}
84 if (proceed_pnt)
85 start{tiers}(cnt) = str2num(A{2}{1}) - POINT_BUFFER;
86 stop{tiers}(cnt) = str2num(A{2}{1}) + POINT_BUFFER;
87
88 if (start{tiers}(cnt) < xmin)
89 start{tiers}(cnt) = xmin;
90 end
91
92 if (stop{tiers}(cnt) > xmax)
93 stop{tiers}(cnt) = xmas;
94 end
95 end
96 case {'mark '}
97 if (proceed_pnt)
98 lab = A{2}{1};
99 if (lab(end) ~= '"')
100 lab = lab(1:end-1);
101 end
102
103 labels{tiers}{cnt} = lab;
104 cnt = cnt + 1;
105 if (cnt > tier_len)
106 proceed_pnt = 0;
107 end
108 end
109
110
111 end
112 end
113
114 end
115
116
117
118