-
Notifications
You must be signed in to change notification settings - Fork 55
/
LyricMaker.cpp
358 lines (279 loc) · 7.46 KB
/
LyricMaker.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include "LyricMaker.h"
#include "global.h"
#include <QTextStream>
#include <QStringList>
#include <UnicodeReader.h>
#include "SettingManager.h"
//载入原始的歌词数据
bool LyricMaker::loadRawLyric(QString lyricPath)
{
rawLines.clear();
rawLyricPath.clear();
QString content;
UnicodeReader unicodeReader;
if(!unicodeReader.ReadFromFile(lyricPath,content))
return false;
rawLyricPath =lyricPath;
isLyricChanged = false;
QRegExp sepRegExp = QRegExp("\n|\r"); //linux\mac\windows 换行符号
QStringList lineList = content.split(sepRegExp);
for(auto& line: lineList)
{
line = line.trimmed();
if(!line.isEmpty())
rawLines.push_back(line.trimmed());
}
return true;
}
//歌词是否为空
bool LyricMaker::isRawLyricEmpty()
{
return rawLines.empty();
}
bool LyricMaker::saveLyrc(QString savePath)
{
QFile file(savePath);
if(!file.open(QIODevice::WriteOnly | QIODevice::Text |QIODevice::Truncate))
{
return false;
}
QTextStream streamFileOut(&file);
streamFileOut.setCodec("UTF-8");
streamFileOut << lrcContent;
streamFileOut.flush();
streamFileOut.setGenerateByteOrderMark(false);
file.close();
return true;
}
bool LyricMaker::saveToRawLyric()
{
if(rawLyricPath.isEmpty())
return false;
//收集源歌词,保存回之前的路径
QFile file(rawLyricPath);
if(!file.open(QIODevice::WriteOnly | QIODevice::Text |QIODevice::Truncate))
{
return false;
}
QString rawLyricContent;
for(auto &line:rawLines)
{
rawLyricContent += line;
rawLyricContent += "\n";
}
QTextStream streamFileOut(&file);
streamFileOut.setCodec("UTF-8");
streamFileOut << rawLyricContent;
streamFileOut.flush();
streamFileOut.setGenerateByteOrderMark(false);
file.close();
return true;
}
//开始制作歌词
void LyricMaker::startMaking()
{
insertOffsetTime = SettingManager::GetInstance().data().shiftTime; //现在开始制作的时候读取偏移设置
rawCurrent = 0;
lrcNext = 0;
//初始化lrc歌词内容
lrcLines.clear();
lrcContent.clear();
}
//是否有前一个LRC行
bool LyricMaker::hasPreLrcLine()
{
if(isRawLyricEmpty())
return false;
if(lrcNext <= 1)
return false;
return true;
}
//是否有前二个LRC行
bool LyricMaker::hasPPreLrcLine()
{
if(isRawLyricEmpty())
return false;
if(lrcNext <= 2)
return false;
return true;
}
//是否有下一个原歌词行
bool LyricMaker::hasNextRawLine()
{
if(isRawLyricEmpty())
return false;
if(rawCurrent + 1 > uint(rawLines.size()))
return false;
return true;
}
//是否有后一个原歌词行
bool LyricMaker::hasNNextRawLine()
{
if(isRawLyricEmpty())
return false;
if(rawLines.size()<2)
return false;
if(rawCurrent + 2 > uint(rawLines.size()))
return false;
return true;
}
//当前行为第一行时,将返回 false
bool LyricMaker::getPreLrcLineText(QString& line)
{
if(!hasPreLrcLine())
return false;
line = lrcLines[lrcNext-2].second;
return true;
}
//当前行为第一二行时,将返回 false
bool LyricMaker::getPPreLrcLineText(QString& line)
{
if(!hasPPreLrcLine())
return false;
line = lrcLines[lrcNext-3].second;
return true;
}
//文档为空时,将返回 false
bool LyricMaker::getCurrentLrcLineText(QString& line)
{
if(lrcNext <= 0)
return false;
line = lrcLines[lrcNext-1].second;
return true;
}
//文档为空时,将返回 false
bool LyricMaker::getCurrentRawLineText(QString& line)
{
if(isRawLyricEmpty())
return false;
if(rawCurrent == 0)
return false;
line = rawLines[rawCurrent-1];
return true;
}
//当前行为最后一行时,将返回 false
bool LyricMaker::getNextRawLineText(QString& line)
{
if(!hasNextRawLine())
return false;
line = rawLines[rawCurrent];
return true;
}
//当前行为最后二行时,将返回 false
bool LyricMaker::getNNextRawLineText(QString& line)
{
if(!hasNNextRawLine())
return false;
line = rawLines[rawCurrent+1];
return true;
}
//更新当前行的歌词内容
bool LyricMaker::updateCurrentLineText(QString& line)
{
QString text;
if(getCurrentLrcLineText(text))
{
isLyricChanged = true; //标记歌词已经发生改变
lrcLines[lrcNext-1].second = line;
rawLines[rawCurrent-1] = line;
return true;
}
else
return false; //没有当前行可更新
}
//标记当前行为 time
bool LyricMaker::markCurrentRawLine(quint64 time)
{
time -= insertOffsetTime;
lrcLines.push_back(QPair<quint64, QString>(time,rawLines[rawCurrent]));
rawCurrent++;
lrcNext++;
return true;
}
//标记时间为 time 的空行
bool LyricMaker::markEmptyLine(quint64 time)
{
//只有当lrc 歌词为空,或者不为空时最后一个元素的内容不为空时,才插入一个空的时间行
if(lrcLines.isEmpty() || lrcLines.last().second != "")
{
time -= insertOffsetTime;
lrcLines.push_back(QPair<quint64, QString>(time,""));
lrcNext++;
}
return true;
}
//回退到 time
bool LyricMaker::stepBackToTime(quint64 time)
{
//从下标为 0 开始遍历到 lrcNext, 知道找到 比 time 大的,其下标即为跳转后下标
for(uint i = 0; i< lrcNext; i++)
{
if(lrcLines[i].first > time)
{
lrcNext = i; // i 对应的时间比 指定时间大,所以 i 为跳转后的当前下标
break;
}
}
//删除 lrcNext 后面可能有的 lrcLines 的项
uint index = 0;
for(auto iter = lrcLines.begin(); iter != lrcLines.end(); iter++, index++)
{
if(index == lrcNext)//index 到达 lrcNext,则 接下来的 的元素都是要删除的
{
while(iter != lrcLines.end())
{
iter = lrcLines.erase(iter);
}
break; //删除后跳出
}
}
//重新纠正 rawCurrent 的 值,计算当前 lrcLines 有多少非空行,即为 rawCurrent的值
uint count = 0;
for(auto& line:lrcLines)
{
if(line.second != "")
count++;
}
rawCurrent = count;
return true;
}
//获得上一行LRC歌词的时间,没有上一行时返回-1
int LyricMaker::getLastLrcLineTime()
{
if(!hasPreLrcLine())
return -1;
return int(lrcLines[lrcNext-2].first);
}
//结束制作
void LyricMaker::finishMaking()
{
lrcContent.clear();
for(auto& line:lrcLines)
{
quint64 time = line.first;
int ms = time % 1000;
time = time/1000;
int s = time % 60;
int m = time/60;
QString timeLabel;
timeLabel.sprintf("%.2d:%.2d.%.3d",m, s, ms);
QString oneline = "["+timeLabel+"]"+line.second + "\n";
lrcContent.append(oneline);
}
}
//制作结束的lrc是否为空
bool LyricMaker::isResultLrcEmpty()
{
return lrcContent.size() == 0;
}
void LyricMaker::getLyricData(QVector<QString>& _rawLines, QVector<QPair<quint64, QString>>& _lrcLines)
{
_rawLines = rawLines;
_lrcLines = lrcLines;
}
void LyricMaker::updateLyricData(QVector<QString>& _rawLines, QVector<QPair<quint64, QString>>& _lrcLines)
{
isLyricChanged = true; //标记歌词已经发生改变
rawLines = _rawLines;
lrcLines = _lrcLines;
}