Skip to content

Commit d6d882a

Browse files
committed
fix(octra): PraatTableConverter loaded as plain text in URL mode
1 parent a298f74 commit d6d882a

File tree

3 files changed

+127
-130
lines changed

3 files changed

+127
-130
lines changed

apps/octra/src/app/core/store/login-mode/annotation/annotation.effects.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
import { SampleUnit } from '@octra/media';
2626
import { OctraAPIService } from '@octra/ngx-octra-api';
2727
import {
28+
appendURLQueryParams,
2829
extractFileNameFromURL,
2930
hasProperty,
3031
SubscriptionManager,
@@ -771,20 +772,29 @@ export class AnnotationEffects {
771772
this.routingService.staticQueryParams.guidelines_url &&
772773
this.routingService.staticQueryParams.functions_url
773774
) {
774-
projectConfigURL =
775-
this.routingService.staticQueryParams.project_config_url +
776-
`&v=${Date.now()}`;
775+
projectConfigURL = appendURLQueryParams(
776+
this.routingService.staticQueryParams.project_config_url,
777+
{
778+
v: Date.now().toString(),
779+
},
780+
);
777781
guidelinesURLs = [
778782
{
779-
url:
780-
this.routingService.staticQueryParams.guidelines_url +
781-
`&v=${Date.now()}`,
783+
url: appendURLQueryParams(
784+
this.routingService.staticQueryParams.guidelines_url,
785+
{
786+
v: Date.now().toString(),
787+
},
788+
),
782789
lang: this.routingService.staticQueryParams.locale ?? 'en',
783790
},
784791
];
785-
functionsURL =
786-
this.routingService.staticQueryParams.functions_url +
787-
`&v=${Date.now()}`;
792+
functionsURL = appendURLQueryParams(
793+
this.routingService.staticQueryParams.functions_url,
794+
{
795+
v: Date.now().toString(),
796+
},
797+
);
788798
}
789799
// mode is not online => load configuration for local environment
790800
return forkJoin<

libs/annotation/src/lib/converters/PraatTableConverter.ts

Lines changed: 107 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
OSegment,
88
OSegmentLevel,
99
} from '../annotjson';
10-
import { contains } from '../functions';
1110
import {
1211
Converter,
1312
ExportResult,
@@ -129,152 +128,140 @@ export class PraatTableConverter extends Converter {
129128
// check if filename is equal with audio file
130129
const filename = FileInfo.extractFileName(file.name).name;
131130

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
135150
for (let i = startWithLine; i < lines.length; i++) {
136151
if (lines[i] !== '') {
137152
const columns: string[] = lines[i].split('\t');
153+
const tmin = Number(columns[0]);
138154
const tier = columns[1];
155+
const text = columns[2];
156+
const tmax = Number(columns[3]);
139157

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;
159159

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;
161168

162-
if (isNaN(tmax)) {
169+
if (tier === tierElement) {
170+
if (isNaN(tmin)) {
163171
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`,
165173
};
166174
} 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),
206198
),
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,
220212
),
221-
);
222-
}
223-
224-
start = tmin;
213+
[new OLabel(tier, '')],
214+
),
215+
);
225216
}
226-
}
227217

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;
240219
}
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(
244225
id,
245226
Math.round(start * sampleRate),
246-
Math.round((rightBoundary - start) * sampleRate),
247-
olabels,
227+
Math.round(puffer * sampleRate),
228+
[new OLabel(tier, '')],
248229
);
249-
250-
olevel.items.push(osegment);
251-
start += rightBoundary - start;
230+
start = start + puffer;
231+
olevel.items.push(pufferItem);
232+
puffer = 0;
252233
id++;
253-
} else {
254-
puffer += rightBoundary - start;
255234
}
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;
256249
}
257250
}
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-
};
272251
}
252+
result.levels.push(olevel);
253+
}
254+
if (tiers.length > 0) {
255+
return {
256+
annotjson: result,
257+
audiofile: undefined,
258+
error: '',
259+
};
273260
} else {
274261
return {
275262
annotjson: undefined,
276263
audiofile: undefined,
277-
error: `Filenames for .Table extension does not match.`,
264+
error: `Invalid Praat table file.`,
278265
};
279266
}
280267
}

libs/utilities/src/lib/functions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ export function stringifyQueryParams(params: Record<string, any>) {
445445
if (typeof value !== 'string' && Array.isArray(value)) {
446446
value = value.join(',');
447447
}
448-
strArray.push(`${key}=${value}`);
448+
strArray.push(`${key}=${encodeURIComponent(value)}`);
449449
}
450450
}
451451

0 commit comments

Comments
 (0)