我正在开发一个可以读取 lilypond 和 midi 文件的音乐应用程序。两种文件类型都需要变成我们自己的存储。
对于 lilypond,您需要阅读重复及其替代方法。但是,计数可能会有所不同。如果有三个重复但有两个备选方案,则前两个重复获得第一个备选方案,第三个备选方案获得最后一个备选方案。
由于重用从前面开始,我不知道该怎么做。我当前的代码看起来像这样,所以唯一缺少的部分是将repeatList 和altList 结合起来。
我希望有一个数学解决方案,因为翻转数组对于性能来说是可悲的。
private List<Note> readRepeat(List<string> repeat, List<string> alt, int repeatCount)
{
List<Note> noteList = new List<Note>();
List<Note> repeatList = new List<Note>();
List<List<Note>> altList = new List<List<Note>>();
foreach (string line in repeat)
{
repeatList.AddRange(readNoteLine(line));
}
foreach (string line in alt)
{
altList.Add(readNoteLine(line));
}
while (repeatCount > 0)
{
List<Note> toAdd = repeatList.ToList(); // Clone the list, to destroy the reference
if (altList.Count() != 0)
{
// logic to add the right alt
}
noteList.AddRange(toAdd);
repeatCount--;
}
return noteList;
}
在上面的代码中,两个列表都填充了注释。它们的功能如下: RepeatList:播放 x 次的基本音符列表 AltList:要添加到重复列表的可能性列表。
一些示例 I/O
- 重复次数 = 4
- AltList.Count() = 3
- 重复 1 得到:Alt 1
- 重复 2 得到:Alt 1
- 重复 3 得到:Alt 2
- 重复 4 得到:Alt 3
视觉风格示例