-
Notifications
You must be signed in to change notification settings - Fork 0
/
lyricLRC.cpp
262 lines (231 loc) · 6.44 KB
/
lyricLRC.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "LyricJson.h"
#include "Settings.h"
#include <QFileInfo>
#include <QTime>
bool LyricJson::loadLRC(const QString& path, const QString& musicPath)
{
QFile infile(path);
if (!infile.open(QIODevice::ReadOnly | QIODevice::Text))
{
return false;
}
_song.Clear();
auto settings = Settings::getInstance();
_song.general.maxline = 2; // always 2 ?
while (!infile.atEnd())
{
QByteArray lineByte = infile.readLine();
QString line = QString::fromStdString(lineByte.toStdString()).trimmed();
if (!line.startsWith("["))
{
continue;
}
if (!line.contains("]"))
{
continue;
}
int indexOfClose = line.indexOf("]");
QString controlPart = line.mid(1, indexOfClose - 1).trimmed();
if (controlPart.startsWith("ti:"))
{
_song.info.title = controlPart.right(controlPart.length() - 3); // no trim
}
else if (controlPart.startsWith("au:"))
{
_song.info.author = controlPart.right(controlPart.length() - 3); // no trim
}
else if (controlPart.startsWith("ar:"))
{
_song.info.artist = controlPart.right(controlPart.length() - 3); // no trim
}
else if (controlPart.startsWith("al:"))
{
_song.info.album = controlPart.right(controlPart.length() - 3); // no trim
}
else if (controlPart.startsWith("by:"))
{
_song.info.by = controlPart.right(controlPart.length() - 3); // no trim
}
else if (controlPart.startsWith("offset:"))
{
_song.general.offset = controlPart.right(controlPart.length() - 7).toLongLong();
}
else if (controlPart.at(0).isDigit())
{
QTime beginTime = QTime::fromString(controlPart + "0", "mm:ss.zzz");
qint64 begin = beginTime.msecsSinceStartOfDay();
KJsonSentence sentence;
KJsonWord word;
word.begin = begin;
word.end = begin + settings->maximumDuration(); //std::numeric_limits<qint64>::max();
word.text = line.right(line.length() - indexOfClose - 1);
if (word.text.isEmpty())
{
int thisIndex = _song.lyric.sentencelist.count();
if (thisIndex % _song.general.maxline == 0)
{
// skip
continue;
}
else
{
// move up
// skip multiple whites
if (_song.lyric.sentencelist.last().wordlist.last().begin < 0)
{
continue;
}
else
{
word.end = word.begin + settings->minimumDuration();
}
}
}
if (!_song.lyric.sentencelist.empty())
{
KJsonWord& lastword = _song.lyric.sentencelist.last().wordlist.last();
if (lastword.end - lastword.begin> settings->minimumDuration())
{
lastword.end = begin;// - lastword.begin;
}
}
sentence.wordlist.append(word);
_song.lyric.sentencelist.append(sentence);
}
}
KJsonSong songCopy = _song;
// first generate weak version
treatSentecesAsControl();
QString jsonPath = Settings::getInstance()->makeJsonPath(path, false);
exportToJson(jsonPath);
_song = songCopy;
lrcWordSeparate();
exportToASS(Settings::getInstance()->makeASSPath(path), musicPath);
return loadJson(jsonPath);
}
void LyricJson::lrcWordSeparate()
{
for (QList<KJsonSentence>::iterator sentenceIt = _song.lyric.sentencelist.begin(); sentenceIt != _song.lyric.sentencelist.end(); ++sentenceIt)
{
if (sentenceIt->wordlist.count() != 1)
{
continue;
}
QString text = sentenceIt->wordlist.first().text;
if (text.trimmed().isEmpty())
{
continue;
}
qint64 firstBegin = sentenceIt->wordlist.first().begin;
qint64 totalDuration = sentenceIt->wordlist.first().end - firstBegin;
// text format
// [text|ruby]
// [AB|abc] count as three words
QString iterText = text;
// QRegExp reg("\\[[^\\[]\\|.*\\]"); prob of []
QRegExp reg("\\[.*\\|.*\\]");
int wordRubyCount = 0;
QString normalText;
QString rubiedText;
sentenceIt->wordlist.clear();
bool bControlWord = isControlOrInfoText(text);
if (bControlWord)
{
// build normal word
KJsonWord word;
word.text = text;
sentenceIt->wordlist.append(word);
wordRubyCount = 1;
normalText = text;
rubiedText = text;
}
else
{
while (true)
{
int searchedIndex = iterText.indexOf(reg);
if (searchedIndex >= 0)
{
for (int i = 0; i < searchedIndex; i++)
{
// build normal word
KJsonWord word;
word.text = iterText.at(i);
sentenceIt->wordlist.append(word);
normalText += word.text;
rubiedText += word.text;
wordRubyCount++;
}
iterText = iterText.right(iterText.length() - searchedIndex - 1);
// [ already cut
int separatorIndex = iterText.indexOf("|");
int closeIndex = iterText.indexOf("]", separatorIndex);
QString kanjiText = iterText.left(separatorIndex);
QString rubyText = iterText.mid(separatorIndex + 1, closeIndex - separatorIndex - 1);
normalText += kanjiText;
rubiedText += rubyText;
KJsonWord word;
word.text = kanjiText;
for (int i = 0; i < rubyText.length(); i++)
{
KJsonRuby ruby;
ruby.text = rubyText[i];
word.rubylist.append(ruby);
}
wordRubyCount += rubyText.length();
sentenceIt->wordlist.append(word);
iterText = iterText.right(iterText.length() - closeIndex - 1);
}
else
{
for (int i = 0; i < iterText.length(); i++)
{
// white space no adding count
// build normal word
KJsonWord word;
word.text = iterText.at(i);
sentenceIt->wordlist.append(word);
normalText += word.text;
rubiedText += word.text;
if (!word.text.trimmed().isEmpty())
{
wordRubyCount++;
}
}
break;
}
}
}
sentenceIt->normaltext = normalText;
sentenceIt->rubiedtext = rubiedText;
int curCount = 0;
for (int i = 0; i < sentenceIt->wordlist.size(); i++)
{
sentenceIt->wordlist[i].begin = firstBegin + curCount*totalDuration / wordRubyCount;
int rubyCount = sentenceIt->wordlist[i].rubylist.size();
if (rubyCount)
{
sentenceIt->wordlist[i].end = sentenceIt->wordlist[i].begin + rubyCount*totalDuration / wordRubyCount;
for (int j = 0; j < rubyCount; j++)
{
sentenceIt->wordlist[i].rubylist[j].begin = firstBegin + curCount*totalDuration / wordRubyCount;
sentenceIt->wordlist[i].rubylist[j].end = sentenceIt->wordlist[i].rubylist[j].begin + totalDuration / wordRubyCount;
curCount++;
}
continue;
}
else
{
if (!sentenceIt->wordlist[i].text.trimmed().isEmpty())
{
sentenceIt->wordlist[i].end = sentenceIt->wordlist[i].begin + totalDuration / wordRubyCount;
}
else
{
sentenceIt->wordlist[i].end = sentenceIt->wordlist[i].begin;
}
}
curCount++;
}
}
}