-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrcWriter.cs
More file actions
84 lines (83 loc) · 2.17 KB
/
BrcWriter.cs
File metadata and controls
84 lines (83 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#nullable enable
using System.Text;
using static Brc.BrcGlobal;
namespace Brc
{
public class BrcWriter
{
private readonly StringBuilder sb = new();
private readonly Brc brc;
private BrcSentence pre;
private BrcWriter(Brc brc) => this.brc = brc;
private void Write(BrcSentence sentence)
{
if (sentence.lines?.Length > 0) { sb.AppendLine(); }
int notEqual = BrcSentence.PropertyEquals(pre, sentence);
if (notEqual >= 0)
{
sb.Append(_leftbucket);
sb.Append(sentence.BeatsPerBar);
if (notEqual >= 1)
{
sb.Append(_comma);
sb.Append(sentence.NotePerBeat);
if (notEqual >= 2)
{
sb.Append(_comma);
sb.Append(sentence.BeatsPerMinute.ToString("F2"));
if (notEqual >= 3)
{
sb.Append(_comma);
sb.Append(sentence.Offset);
}
}
}
sb.Append(_rightbucket);
}
pre = sentence;
for (int lineIndex = 0; lineIndex < sentence.lines!.Length; lineIndex++)
{
var line = sentence.lines[lineIndex];
float totalLength = 0;
for (int wordIndex = 0; wordIndex < line.words.Length; wordIndex++)
{
var word = line.words[wordIndex];
WriteWord(word, wordIndex, line.words.Length);
totalLength += word.Duration;
if (totalLength >= pre.BeatsPerBar && wordIndex < line.words.Length - 1)
{
sb.AppendLine();
totalLength -= pre.BeatsPerBar;
}
}
if (lineIndex < sentence.lines.Length - 1)
sb.Append(_newLine);
}
sb.Append(_sentence);
}
private void WriteWord(BrcWord word, int index, int all)
{
foreach (char c in word.Word)
{
if (c is _escape or _separator or _sentence or _leftbucket or _rightbucket or _connect or _newLine)
sb.Append(_escape);
sb.Append(c);
}
if (word.Duration * pre.NotePerBeat > 1)
for (int j = 0; j < word.Duration * pre.NotePerBeat - 1; j++)
sb.Append(_connect);
else if (index < all - 1)
sb.Append(_separator);
}
private void Write(string filepath)
{
foreach (BrcSentence sentence in brc)
Write(sentence);
System.IO.File.WriteAllText(filepath, sb.ToString());
}
public static void Write(string filepath, Brc brc)
{
new BrcWriter(brc).Write(filepath);
}
}
}