|
7 | 7 | OSegment, |
8 | 8 | OSegmentLevel, |
9 | 9 | } from '../annotjson'; |
10 | | -import { contains } from '../functions'; |
11 | 10 | import { |
12 | 11 | Converter, |
13 | 12 | ExportResult, |
@@ -129,152 +128,140 @@ export class PraatTableConverter extends Converter { |
129 | 128 | // check if filename is equal with audio file |
130 | 129 | const filename = FileInfo.extractFileName(file.name).name; |
131 | 130 |
|
132 | | - if (contains(audiofile.name, filename)) { |
133 | | - const tiers: string[] = []; |
134 | | - // get tiers |
| 131 | + const tiers: string[] = []; |
| 132 | + // get tiers |
| 133 | + for (let i = startWithLine; i < lines.length; i++) { |
| 134 | + if (lines[i] !== '') { |
| 135 | + const columns: string[] = lines[i].split('\t'); |
| 136 | + const tier = columns[1]; |
| 137 | + |
| 138 | + if (tiers.filter((a) => a === tier).length === 0) { |
| 139 | + tiers.push(tier); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + for (const tierElement of tiers) { |
| 145 | + const olevel = new OSegmentLevel(tierElement); |
| 146 | + let start = 0; |
| 147 | + let puffer = 0; |
| 148 | + let id = 1; |
| 149 | + // start at line 0 |
135 | 150 | for (let i = startWithLine; i < lines.length; i++) { |
136 | 151 | if (lines[i] !== '') { |
137 | 152 | const columns: string[] = lines[i].split('\t'); |
| 153 | + const tmin = Number(columns[0]); |
138 | 154 | const tier = columns[1]; |
| 155 | + const text = columns[2]; |
| 156 | + const tmax = Number(columns[3]); |
139 | 157 |
|
140 | | - if (tiers.filter((a) => a === tier).length === 0) { |
141 | | - tiers.push(tier); |
142 | | - } |
143 | | - } |
144 | | - } |
145 | | - |
146 | | - for (const tierElement of tiers) { |
147 | | - const olevel = new OSegmentLevel(tierElement); |
148 | | - let start = 0; |
149 | | - let puffer = 0; |
150 | | - let id = 1; |
151 | | - // start at line 0 |
152 | | - for (let i = startWithLine; i < lines.length; i++) { |
153 | | - if (lines[i] !== '') { |
154 | | - const columns: string[] = lines[i].split('\t'); |
155 | | - const tmin = Number(columns[0]); |
156 | | - const tier = columns[1]; |
157 | | - const text = columns[2]; |
158 | | - const tmax = Number(columns[3]); |
| 158 | + let rightBoundary = 0; |
159 | 159 |
|
160 | | - let rightBoundary = 0; |
| 160 | + if (isNaN(tmax)) { |
| 161 | + return { |
| 162 | + error: `Parsing error at line ${i + 1} column 4: Not a number`, |
| 163 | + }; |
| 164 | + } else { |
| 165 | + rightBoundary = Number(tmax); |
| 166 | + } |
| 167 | + const sampleRate = audiofile.sampleRate; |
161 | 168 |
|
162 | | - if (isNaN(tmax)) { |
| 169 | + if (tier === tierElement) { |
| 170 | + if (isNaN(tmin)) { |
163 | 171 | return { |
164 | | - error: `Parsing error at line ${i + 1} column 4: Not a number`, |
| 172 | + error: `Parsing error at line ${i + 1} column 1: Not a number`, |
165 | 173 | }; |
166 | 174 | } else { |
167 | | - rightBoundary = Number(tmax); |
168 | | - } |
169 | | - const sampleRate = audiofile.sampleRate; |
170 | | - |
171 | | - if (tier === tierElement) { |
172 | | - if (isNaN(tmin)) { |
173 | | - return { |
174 | | - error: `Parsing error at line ${ |
175 | | - i + 1 |
176 | | - } column 1: Not a number`, |
177 | | - }; |
178 | | - } else { |
179 | | - const last = ( |
180 | | - olevel.items.length > 0 && |
181 | | - !( |
182 | | - olevel.items[olevel.items.length - 1] === undefined || |
183 | | - olevel.items[olevel.items.length - 1] === undefined |
184 | | - ) |
185 | | - ? olevel.items[olevel.items.length - 1] |
186 | | - : undefined |
187 | | - ) as OSegment; |
188 | | - if ( |
189 | | - (last !== undefined && |
190 | | - Math.round( |
191 | | - (last.sampleStart + last.sampleDur) / sampleRate, |
192 | | - ) < Math.round(Number(tmin))) || |
193 | | - (!last && tmin > start) |
194 | | - ) { |
195 | | - if (last) { |
196 | | - // add empty segment |
197 | | - olevel.items.push( |
198 | | - new OSegment( |
199 | | - id++, |
200 | | - last.sampleStart + last.sampleDur, |
201 | | - Math.round( |
202 | | - tmin * sampleRate - |
203 | | - (last.sampleStart + last.sampleDur), |
204 | | - ), |
205 | | - [new OLabel(tier, '')], |
| 175 | + const last = ( |
| 176 | + olevel.items.length > 0 && |
| 177 | + !( |
| 178 | + olevel.items[olevel.items.length - 1] === undefined || |
| 179 | + olevel.items[olevel.items.length - 1] === undefined |
| 180 | + ) |
| 181 | + ? olevel.items[olevel.items.length - 1] |
| 182 | + : undefined |
| 183 | + ) as OSegment; |
| 184 | + if ( |
| 185 | + (last !== undefined && |
| 186 | + Math.round((last.sampleStart + last.sampleDur) / sampleRate) < |
| 187 | + Math.round(Number(tmin))) || |
| 188 | + (!last && tmin > start) |
| 189 | + ) { |
| 190 | + if (last) { |
| 191 | + // add empty segment |
| 192 | + olevel.items.push( |
| 193 | + new OSegment( |
| 194 | + id++, |
| 195 | + last.sampleStart + last.sampleDur, |
| 196 | + Math.round( |
| 197 | + tmin * sampleRate - (last.sampleStart + last.sampleDur), |
206 | 198 | ), |
207 | | - ); |
208 | | - } else { |
209 | | - // add empty segment |
210 | | - olevel.items.push( |
211 | | - new OSegment( |
212 | | - id++, |
213 | | - start * sampleRate, |
214 | | - Math.round( |
215 | | - tmin * sampleRate - |
216 | | - start * sampleRate - |
217 | | - tmin * sampleRate, |
218 | | - ), |
219 | | - [new OLabel(tier, '')], |
| 199 | + [new OLabel(tier, '')], |
| 200 | + ), |
| 201 | + ); |
| 202 | + } else { |
| 203 | + // add empty segment |
| 204 | + olevel.items.push( |
| 205 | + new OSegment( |
| 206 | + id++, |
| 207 | + start * sampleRate, |
| 208 | + Math.round( |
| 209 | + tmin * sampleRate - |
| 210 | + start * sampleRate - |
| 211 | + tmin * sampleRate, |
220 | 212 | ), |
221 | | - ); |
222 | | - } |
223 | | - |
224 | | - start = tmin; |
| 213 | + [new OLabel(tier, '')], |
| 214 | + ), |
| 215 | + ); |
225 | 216 | } |
226 | | - } |
227 | 217 |
|
228 | | - if (puffer > 0) { |
229 | | - // fill |
230 | | - const pufferItem = new OSegment( |
231 | | - id, |
232 | | - Math.round(start * sampleRate), |
233 | | - Math.round(puffer * sampleRate), |
234 | | - [new OLabel(tier, '')], |
235 | | - ); |
236 | | - start = start + puffer; |
237 | | - olevel.items.push(pufferItem); |
238 | | - puffer = 0; |
239 | | - id++; |
| 218 | + start = tmin; |
240 | 219 | } |
241 | | - const olabels: OLabel[] = []; |
242 | | - olabels.push(new OLabel(tier, text)); |
243 | | - const osegment = new OSegment( |
| 220 | + } |
| 221 | + |
| 222 | + if (puffer > 0) { |
| 223 | + // fill |
| 224 | + const pufferItem = new OSegment( |
244 | 225 | id, |
245 | 226 | Math.round(start * sampleRate), |
246 | | - Math.round((rightBoundary - start) * sampleRate), |
247 | | - olabels, |
| 227 | + Math.round(puffer * sampleRate), |
| 228 | + [new OLabel(tier, '')], |
248 | 229 | ); |
249 | | - |
250 | | - olevel.items.push(osegment); |
251 | | - start += rightBoundary - start; |
| 230 | + start = start + puffer; |
| 231 | + olevel.items.push(pufferItem); |
| 232 | + puffer = 0; |
252 | 233 | id++; |
253 | | - } else { |
254 | | - puffer += rightBoundary - start; |
255 | 234 | } |
| 235 | + const olabels: OLabel[] = []; |
| 236 | + olabels.push(new OLabel(tier, text)); |
| 237 | + const osegment = new OSegment( |
| 238 | + id, |
| 239 | + Math.round(start * sampleRate), |
| 240 | + Math.round((rightBoundary - start) * sampleRate), |
| 241 | + olabels, |
| 242 | + ); |
| 243 | + |
| 244 | + olevel.items.push(osegment); |
| 245 | + start += rightBoundary - start; |
| 246 | + id++; |
| 247 | + } else { |
| 248 | + puffer += rightBoundary - start; |
256 | 249 | } |
257 | 250 | } |
258 | | - result.levels.push(olevel); |
259 | | - } |
260 | | - if (tiers.length > 0) { |
261 | | - return { |
262 | | - annotjson: result, |
263 | | - audiofile: undefined, |
264 | | - error: '', |
265 | | - }; |
266 | | - } else { |
267 | | - return { |
268 | | - annotjson: undefined, |
269 | | - audiofile: undefined, |
270 | | - error: `Invalid Praat table file.`, |
271 | | - }; |
272 | 251 | } |
| 252 | + result.levels.push(olevel); |
| 253 | + } |
| 254 | + if (tiers.length > 0) { |
| 255 | + return { |
| 256 | + annotjson: result, |
| 257 | + audiofile: undefined, |
| 258 | + error: '', |
| 259 | + }; |
273 | 260 | } else { |
274 | 261 | return { |
275 | 262 | annotjson: undefined, |
276 | 263 | audiofile: undefined, |
277 | | - error: `Filenames for .Table extension does not match.`, |
| 264 | + error: `Invalid Praat table file.`, |
278 | 265 | }; |
279 | 266 | } |
280 | 267 | } |
|
0 commit comments