-
Notifications
You must be signed in to change notification settings - Fork 0
/
SongFile.cpp
214 lines (167 loc) · 4.92 KB
/
SongFile.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "SongFile.h"
#include "ASSFile.h"
#include "ENLyricFile.h"
#include <iostream>
#define DIVIDER_STYLE "-- DIVIDER --"
#define METADATA_STYLE "Title"
const ASSTime ZERO("0:00:00.00");
const ASSTime METADATA_DURATION("0:00:05.00");
SongFile::SongFile(std::string styleFileName, std::string karaokeFileName, std::string ENFileName, std::string metadataFileName)
{
try
{
ASSFileWithSwitch withK(karaokeFileName);
ASSFileWithSwitch withStyle(styleFileName);
ASSFileWithSwitch merged = mergeStylesIntoFile(withK, withStyle);
configureStyles(merged);
ENLyricFile EN(ENFileName);
checkNumberOfLines(merged, EN);
ASSFileWithSwitch output;
output.readPreprocessLines(metadataFileName);
output.insertLine(createDividerLine(EN.getTitle(), ZERO, merged.getEndTime()));
output.insertLine(createMetadataLine(EN.getMetadata(), ZERO, merged.getEndTime()));
output.insertLine(createDividerLine("English", ZERO, merged.getEndTime()));
for (int i = 0; i < merged.getNumLines(); i++)
output.insertLine(createENLine(merged.getLine(i), EN.getLine(i)));
output.insertLine(createDividerLine("Romaji", ZERO, merged.getEndTime()));
for (ASSLineWithSwitch currLine : merged.getLines())
{
ASSLineWithSwitch toAdd = currLine;
output.insertLine(toAdd);
if (toAdd.isUp())
output.swapLines(output.getNumLines() - 1, output.getNumLines() - merged.getNumLines() - 2);
}
this->data = output;
}
catch (const char* exception)
{
throw exception;
}
}
void SongFile::configureStyles(ASSFileWithSwitch& input)
{
for (int i = 0; i < input.getNumLines(); i++)
{
if (input.getLine(i).isComment())
{
ASSLineWithSwitch toSet = input.getLine(i);
toSet.unsetComment();
toSet.setDialogue();
input.setLine(toSet, i);
}
if (input.getLine(i).getStyle()[0] == 'U')
{
ASSLineWithSwitch toSet = input.getLine(i);
toSet.setStyle(toSet.getStyle().substr(1, toSet.getStyle().length() - 1));
toSet.setUp();
input.setLine(toSet, i);
}
}
}
ASSFileWithSwitch SongFile::mergeStylesIntoFile(ASSFileWithSwitch& original, ASSFileWithSwitch& toMerge)
{
if (!canFindUnmatchedLine(original, toMerge))
{
try
{
ASSFileWithSwitch output = toMerge;
output.clearLines();
for (int i = 0; i < original.getNumLines(); i++)
output.insertLine(original.getLine(i).mergeStyles(toMerge.getLine(i)));
return output;
}
catch (const char* exception)
{
throw exception;
}
}
else
{
throw "Cannot merge styles!";
}
}
bool SongFile::canFindUnmatchedLine(ASSFileWithSwitch& file1, ASSFileWithSwitch& file2)
{
for (int i = 0; i < std::min(file1.getNumLines(), file2.getNumLines()); i++)
{
if (file1.getLine(i).getText() != file2.getLine(i).getText())
{
std::cout << "Unmatched line at line " << i + 1<< std::endl;
std::cout << file1.getLine(i).getText() << std::endl;
std::cout << file2.getLine(i).getText() << std::endl;
return true;
}
}
return false;
}
void SongFile::checkNumberOfLines(ASSFileWithSwitch& ASS, ENLyricFile& EN)
{
if (ASS.getNumLines() != EN.getNumLines())
{
std::cerr << "ASS file has " << ASS.getNumLines() << " lines." << std::endl;
std::cerr << "EN file has " << EN.getNumLines() << " lines." << std::endl;
throw "Mismatch in number of lines!";
}
}
ASSLineWithSwitch SongFile::createDividerLine(std::string input, ASSTime startTime, ASSTime endTime)
{
ASSLineWithSwitch result;
result.setStart(startTime);
result.setEnd(endTime);
result.setStyle(DIVIDER_STYLE);
result.setText(input);
result.setComment();
return result;
}
ASSLineWithSwitch SongFile::createMetadataLine(std::vector <std::string> input, ASSTime startTime, ASSTime endTime)
{
ASSLineWithSwitch result;
result.setStart(startTime);
result.setEnd(startTime + METADATA_DURATION);
result.setStyle(METADATA_STYLE);
std::string text = "";
for (std::string currLine : input)
{
text += currLine;
text += "\\N";
}
result.setText(text);
return result;
}
ASSLineWithSwitch SongFile::createENLine(ASSLineWithSwitch JPline, std::string ENtext)
{
ASSLineWithSwitch result = JPline;
result.clearKaraokeSwitch();
result.setStyle("EN" + JPline.getStyle().substr(2, JPline.getStyle().length() - 2));
std::vector <StyleSwitchCode> styleCodes;
for (StyleSwitchCode currCode : JPline.getStyleSwitchCodes())
{
StyleSwitchCode currStyleCode(currCode.getDuration(), "EN" + currCode.getStyle().substr(2, currCode.getStyle().length() - 2));
styleCodes.push_back(currStyleCode);
}
result.setStyleSwitchCodes(styleCodes);
result.setText(ENtext);
if (JPline.getText() == ENtext)
result.setComment();
return result;
}
void SongFile::printFile(std::string outFileName)
{
this->data.printASSFile(outFileName);
}
ASSFileWithSwitch SongFile::getFile()
{
return this->data;
}
ASSTime SongFile::getStart()
{
return this->data.getLine(3).getStart();
}
ASSTime SongFile::getEnd()
{
return this->data.getLine(data.getNumLines() - 1).getEnd();
}
void SongFile::offsetFile(ASSTime offset)
{
this->data.offsetFile(offset);
}