Skip to content

Commit 88e9107

Browse files
authored
fix(importer): Be more fault tolerant on input files (#2059)
1 parent 762e8a8 commit 88e9107

File tree

8 files changed

+332
-297
lines changed

8 files changed

+332
-297
lines changed

src/importer/AlphaTexImporter.ts

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ export class AlphaTexImporter extends ScoreImporter {
237237
}
238238
}
239239

240-
this.consolidate();
240+
ModelUtils.consolidate(this._score);
241241
this._score.finish(this.settings);
242242
this._score.rebuildRepeatGroups();
243243
for (const [track, lyrics] of this._lyrics) {
@@ -256,66 +256,6 @@ export class AlphaTexImporter extends ScoreImporter {
256256
}
257257
}
258258

259-
/**
260-
* Ensures all staffs of all tracks have the correct number of bars
261-
* (the number of bars per staff and track could be inconsistent)
262-
*/
263-
private consolidate(): void {
264-
// empty score?
265-
if (this._score.masterBars.length === 0) {
266-
const master: MasterBar = new MasterBar();
267-
this._score.addMasterBar(master);
268-
269-
const tempoAutomation: Automation = new Automation();
270-
tempoAutomation.isLinear = false;
271-
tempoAutomation.type = AutomationType.Tempo;
272-
tempoAutomation.value = this._score.tempo;
273-
master.tempoAutomations.push(tempoAutomation);
274-
275-
const bar: Bar = new Bar();
276-
this._score.tracks[0].staves[0].addBar(bar);
277-
278-
const v = new Voice();
279-
bar.addVoice(v);
280-
281-
const emptyBeat: Beat = new Beat();
282-
emptyBeat.isEmpty = true;
283-
v.addBeat(emptyBeat);
284-
return;
285-
}
286-
287-
for (const track of this._score.tracks) {
288-
for (const staff of track.staves) {
289-
// fill empty beats
290-
for (const b of staff.bars) {
291-
for (const v of b.voices) {
292-
if (v.isEmpty && v.beats.length === 0) {
293-
const emptyBeat: Beat = new Beat();
294-
emptyBeat.isEmpty = true;
295-
v.addBeat(emptyBeat);
296-
}
297-
}
298-
}
299-
300-
// fill missing bars
301-
const voiceCount = staff.bars.length === 0 ? 1 : staff.bars[0].voices.length;
302-
while (staff.bars.length < this._score.masterBars.length) {
303-
const bar: Bar = new Bar();
304-
staff.addBar(bar);
305-
306-
for (let i = 0; i < voiceCount; i++) {
307-
const v = new Voice();
308-
bar.addVoice(v);
309-
310-
const emptyBeat: Beat = new Beat();
311-
emptyBeat.isEmpty = true;
312-
v.addBeat(emptyBeat);
313-
}
314-
}
315-
}
316-
}
317-
}
318-
319259
private error(nonterm: string, expected: AlphaTexSymbols, wrongSymbol: boolean = true): void {
320260
let receivedSymbol: AlphaTexSymbols;
321261
let showSyData = false;

src/importer/CapellaParser.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -137,31 +137,7 @@ export class CapellaParser {
137137
}
138138

139139
private consolidate() {
140-
// voice counts and contents might be inconsistent
141-
// we need to ensure we have an equal amount of voices across all bars
142-
// and voices must contain an empty beat at minimum
143-
for (const track of this.score.tracks) {
144-
const trackVoiceCount = this._voiceCounts.get(track.index)!;
145-
for (const staff of track.staves) {
146-
while (staff.bars.length < this.score.masterBars.length) {
147-
this.addNewBar(staff);
148-
}
149-
150-
for (const bar of staff.bars) {
151-
while (bar.voices.length < trackVoiceCount) {
152-
bar.addVoice(new Voice());
153-
}
154-
155-
for (const voice of bar.voices) {
156-
if (voice.beats.length === 0) {
157-
const emptyBeat = new Beat();
158-
emptyBeat.isEmpty = true;
159-
voice.addBeat(emptyBeat);
160-
}
161-
}
162-
}
163-
}
164-
}
140+
ModelUtils.consolidate(this.score);
165141

166142
CapellaParser.applyEffectRange(this._slurs, (_, beat) => {
167143
beat.isLegatoOrigin = true;

src/importer/Gp3To5Importer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ export class Gp3To5Importer extends ScoreImporter {
156156
this._score.masterBars[0].tempoAutomations.push(automation);
157157
}
158158

159+
ModelUtils.consolidate(this._score);
159160
this._score.finish(this.settings);
160161
if (this._lyrics && this._lyricsTrack >= 0) {
161162
this._score.tracks[this._lyricsTrack].applyLyrics(this._lyrics);
@@ -290,10 +291,11 @@ export class Gp3To5Importer extends ScoreImporter {
290291

291292
public readPlaybackInfos(): void {
292293
this._playbackInfos = [];
294+
let channel = 0;
293295
for (let i: number = 0; i < 64; i++) {
294296
const info: PlaybackInformation = new PlaybackInformation();
295-
info.primaryChannel = i;
296-
info.secondaryChannel = i;
297+
info.primaryChannel = channel++;
298+
info.secondaryChannel = channel++;
297299
info.program = IOHelper.readInt32LE(this.data);
298300
info.volume = this.data.readByte();
299301
info.balance = this.data.readByte();

0 commit comments

Comments
 (0)