Skip to content

Commit 3396e5c

Browse files
authored
Fixed wrong repeat group generation for alphaTex (#314)
1 parent b221692 commit 3396e5c

File tree

3 files changed

+92
-33
lines changed

3 files changed

+92
-33
lines changed
Lines changed: 78 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System.Diagnostics;
22
using AlphaTab.Audio.Generator;
3+
using AlphaTab.Collections;
4+
using AlphaTab.Importer;
35
using AlphaTab.Model;
46
using AlphaTab.Test.Importer;
57
using AlphaTab.Util;
@@ -13,60 +15,64 @@ public class MidiPlaybackControllerTest : GpImporterTestBase
1315
[TestMethod, AsyncTestMethod]
1416
public void TestRepeatClose()
1517
{
16-
PrepareImporterWithFile("GuitarPro5\\RepeatClose.gp5", reader =>
17-
{
18-
var score = reader.ReadScore();
19-
var expectedIndexes = new[]
18+
PrepareImporterWithFile("GuitarPro5\\RepeatClose.gp5",
19+
reader =>
2020
{
21-
0, 1, 0, 1, 2
22-
};
21+
var score = reader.ReadScore();
22+
var expectedIndexes = new[]
23+
{
24+
0, 1, 0, 1, 2
25+
};
2326

24-
TestRepeat(score, expectedIndexes);
25-
});
27+
TestRepeat(score, expectedIndexes);
28+
});
2629
}
2730

2831
[TestMethod, AsyncTestMethod]
2932
public void TestRepeatCloseMulti()
3033
{
31-
PrepareImporterWithFile("GuitarPro5\\RepeatCloseMulti.gp5", reader =>
32-
{
33-
var score = reader.ReadScore();
34-
var expectedIndexes = new[]
34+
PrepareImporterWithFile("GuitarPro5\\RepeatCloseMulti.gp5",
35+
reader =>
3536
{
36-
0,1,0,1,0,1,0,1,2
37-
};
38-
TestRepeat(score, expectedIndexes);
39-
});
37+
var score = reader.ReadScore();
38+
var expectedIndexes = new[]
39+
{
40+
0, 1, 0, 1, 0, 1, 0, 1, 2
41+
};
42+
TestRepeat(score, expectedIndexes);
43+
});
4044
}
4145

4246
[TestMethod, AsyncTestMethod]
4347
public void TestRepeatCloseWithoutStartAtBeginning()
4448
{
45-
PrepareImporterWithFile("GuitarPro5\\RepeatCloseWithoutStartAtBeginning.gp5", reader =>
46-
{
47-
var score = reader.ReadScore();
48-
var expectedIndexes = new[]
49+
PrepareImporterWithFile("GuitarPro5\\RepeatCloseWithoutStartAtBeginning.gp5",
50+
reader =>
4951
{
50-
0,1,0,1
51-
};
52+
var score = reader.ReadScore();
53+
var expectedIndexes = new[]
54+
{
55+
0, 1, 0, 1
56+
};
5257

53-
TestRepeat(score, expectedIndexes);
54-
});
58+
TestRepeat(score, expectedIndexes);
59+
});
5560
}
5661

5762
[TestMethod, AsyncTestMethod]
5863
public void TestRepeatCloseAlternateEndings()
5964
{
60-
PrepareImporterWithFile("GuitarPro5\\RepeatCloseAlternateEndings.gp5", reader =>
61-
{
62-
var score = reader.ReadScore();
63-
var expectedIndexes = new[]
65+
PrepareImporterWithFile("GuitarPro5\\RepeatCloseAlternateEndings.gp5",
66+
reader =>
6467
{
65-
0,1,0,2,3,0,1,0,4
66-
};
68+
var score = reader.ReadScore();
69+
var expectedIndexes = new[]
70+
{
71+
0, 1, 0, 2, 3, 0, 1, 0, 4
72+
};
6773

68-
TestRepeat(score, expectedIndexes);
69-
});
74+
TestRepeat(score, expectedIndexes);
75+
});
7076
}
7177

7278
private void TestRepeat(Score score, int[] expectedIndexes)
@@ -83,11 +89,51 @@ private void TestRepeat(Score score, int[] expectedIndexes)
8389
Assert.AreEqual(expectedIndexes[i], index);
8490
i++;
8591
}
92+
8693
controller.MoveNext();
8794
}
8895

8996
Assert.AreEqual(expectedIndexes.Length, i);
9097
Assert.IsTrue(controller.Finished);
9198
}
99+
100+
[TestMethod]
101+
public void TestRepeatWithAlphaTex()
102+
{
103+
var tex = @"\ro 1.3 2.3 3.3 4.3 | 5.3 6.3 7.3 8.3 | \rc 2 1.3 2.3 3.3 4.3 | \ro \rc 3 1.3 2.3 3.3 4.3";
104+
var importer = new AlphaTexImporter();
105+
importer.Init(TestPlatform.CreateStringReader(tex), new Settings());
106+
var score = importer.ReadScore();
107+
108+
var playedBars = new FastList<int>();
109+
var controller = new MidiPlaybackController(score);
110+
while (!controller.Finished)
111+
{
112+
var index = controller.Index;
113+
playedBars.Add(index);
114+
controller.ProcessCurrent();
115+
controller.MoveNext();
116+
117+
if (playedBars.Count > 50)
118+
{
119+
Assert.Fail("Too many bars generated");
120+
}
121+
}
122+
123+
var expectedBars = new FastList<int>
124+
{
125+
0,
126+
1,
127+
2,
128+
0,
129+
1,
130+
2,
131+
3,
132+
3,
133+
3
134+
};
135+
136+
Assert.AreEqual(string.Join(",", expectedBars), string.Join(",", playedBars));
137+
}
92138
}
93139
}

Source/AlphaTab.Test/Importer/AlphaTexImporterTest.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,5 +918,16 @@ public void TestRandomAnacrusis()
918918
Assert.AreEqual(1920, score.MasterBars[2].CalculateDuration());
919919
Assert.AreEqual(3840, score.MasterBars[3].CalculateDuration());
920920
}
921+
922+
[TestMethod]
923+
public void TestRepeat()
924+
{
925+
var tex = @"\ro 1.3 2.3 3.3 4.3 | 5.3 6.3 7.3 8.3 | \rc 2 1.3 2.3 3.3 4.3 | \ro \rc 3 1.3 2.3 3.3 4.3 |";
926+
var score = ParseTex(tex);
927+
Assert.AreEqual(0, score.MasterBars[0].RepeatCount);
928+
Assert.AreEqual(0, score.MasterBars[1].RepeatCount);
929+
Assert.AreEqual(2, score.MasterBars[2].RepeatCount);
930+
Assert.AreEqual(3, score.MasterBars[3].RepeatCount);
931+
}
921932
}
922933
}

Source/AlphaTab/Importer/AlphaTexImporter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public override Score ReadScore()
6161
Consolidate();
6262

6363
_score.Finish(Settings);
64+
_score.RebuildRepeatGroups();
65+
6466
foreach (var track in _lyrics)
6567
{
6668
_score.Tracks[track].ApplyLyrics(_lyrics[track]);
@@ -2023,7 +2025,7 @@ private void BarMeta(Bar bar)
20232025
Error("repeatclose", AlphaTexSymbols.Number);
20242026
}
20252027

2026-
master.RepeatCount = (int)_syData - 1;
2028+
master.RepeatCount = (int)_syData;
20272029
NewSy();
20282030
}
20292031
else if (syData == "ks")

0 commit comments

Comments
 (0)