Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 52 additions & 27 deletions packages/alphatab/src/midi/MidiPlaybackController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ class Repeat {
public opening: MasterBar;
public iterations: number[];
public closingIndex: number = 0;
// Issue #2885: Unified repeat pass tracking across alternate endings
public pass: number = 0;
public isAlternateEndings: boolean = false;

public constructor(group: RepeatGroup, opening: MasterBar) {
this.group = group;
this.opening = opening;
// sort ascending according to index
group.closings = group.closings.sort((a, b) => a.index - b.index);
this.iterations = group.closings.map(_ => 0);
this.isAlternateEndings = group.masterBars.some(m => m.alternateEndings > 0);
}
}

Expand Down Expand Up @@ -104,7 +108,8 @@ export class MidiPlaybackController {
this.shouldPlay = true;
} else {
const repeat = this._repeatStack[this._repeatStack.length - 1];
const iteration = repeat.iterations[repeat.closingIndex];
// Issue #2885: For alternate endings, evaluate the bitmask against the group pass counter
const iteration = repeat.isAlternateEndings ? repeat.pass : repeat.iterations[repeat.closingIndex];
this._previousAlternateEndings = masterBarAlternateEndings;

// do we need to skip this section?
Expand Down Expand Up @@ -399,36 +404,56 @@ export class MidiPlaybackController {
const masterBarRepeatCount: number = masterBar.repeatCount - 1;
// if we encounter a repeat end...
if (this._repeatStack.length > 0 && masterBarRepeatCount > 0) {
// ...more repeats required?
const repeat = this._repeatStack[this._repeatStack.length - 1];
const iteration = repeat.iterations[repeat.closingIndex];

// -> if yes, increase the iteration and jump back to start
if (iteration < masterBarRepeatCount) {
// jump to start
this.index = repeat.opening.index;
repeat.iterations[repeat.closingIndex]++;

// clear iterations for previous closings and start over all repeats
// this ensures on scenarios like "open, bar, close, bar, close"
// that the second close will repeat again the first repeat.
for (let i = 0; i < repeat.closingIndex; i++) {
repeat.iterations[i] = 0;
// Issue #2885: Unified repeat pass tracking across alternate endings
if (repeat.isAlternateEndings) {
const isLastClosing = masterBar === repeat.group.closings[repeat.group.closings.length - 1];
if (this.shouldPlay || isLastClosing) {
if (repeat.pass < masterBarRepeatCount) {
this.index = repeat.opening.index;
repeat.pass++;
this._previousAlternateEndings = 0;
} else if (isLastClosing) {
this._repeatStack.pop();
this._groupsOnStack.delete(repeat.group);
this.index++;
} else {
this.index++;
}
} else {
this.index++;
}
repeat.closingIndex = 0;
this._previousAlternateEndings = 0;
} else {
// if we don't have further iterations left but we have additional closings in this group
// proceed heading to the next close but keep the repeat group active
if (repeat.closingIndex < repeat.group.closings.length - 1) {
repeat.closingIndex++;
this.index++; // go to next bar after current close
} else {
// if there are no further closings in the current group, we consider the current repeat done and handled
this._repeatStack.pop();
this._groupsOnStack.delete(repeat.group);
// ...more repeats required?
const iteration = repeat.iterations[repeat.closingIndex];

this.index++; // go to next bar after current close
// -> if yes, increase the iteration and jump back to start
if (iteration < masterBarRepeatCount) {
// jump to start
this.index = repeat.opening.index;
repeat.iterations[repeat.closingIndex]++;

// clear iterations for previous closings and start over all repeats
// this ensures on scenarios like "open, bar, close, bar, close"
// that the second close will repeat again the first repeat.
for (let i = 0; i < repeat.closingIndex; i++) {
repeat.iterations[i] = 0;
}
repeat.closingIndex = 0;
this._previousAlternateEndings = 0;
} else {
// if we don't have further iterations left but we have additional closings in this group
// proceed heading to the next close but keep the repeat group active
if (repeat.closingIndex < repeat.group.closings.length - 1) {
repeat.closingIndex++;
this.index++; // go to next bar after current close
} else {
// if there are no further closings in the current group, we consider the current repeat done and handled
this._repeatStack.pop();
this._groupsOnStack.delete(repeat.group);

this.index++; // go to next bar after current close
}
}
}
} else {
Expand Down
13 changes: 13 additions & 0 deletions packages/alphatab/test/audio/MidiPlaybackController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,19 @@ describe('MidiPlaybackControllerTest', () => {
testAlphaTexRepeat(tex, expectedBars, 50);
});

it('repeat-sign-in-both-alternate-endings', () => {
const tex: string = ' . \\ro :1 0.6 | \\ae 1 \\rc 2 :1 1.6 | \\ae 2 \\rc 2 :1 2.6 | :1 3.6';
const expectedBars: number[] = [0, 1, 0, 2, 3];
testAlphaTexRepeat(tex, expectedBars, 50);
});

it('repeat-sign-in-all-alternate-endings-multi', () => {
const tex: string =
' . \\ro :1 0.6 | \\ae 1 \\rc 3 :1 1.6 | \\ae 2 \\rc 3 :1 2.6 | \\ae 3 \\rc 3 :1 3.6 | :1 4.6';
const expectedBars: number[] = [0, 1, 0, 2, 0, 3, 4];
testAlphaTexRepeat(tex, expectedBars, 50);
});

// Da Capo

it('da-capo', () => {
Expand Down
Loading