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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
|
/*
* Copyright (C) 2005-2013 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
// C++ Implementation: karaokelyricstextlrc
#include <math.h>
#include "filesystem/File.h"
#include "settings/AdvancedSettings.h"
#include "utils/MathUtils.h"
#include "utils/log.h"
#include "utils/URIUtils.h"
#include "karaokelyricstextlrc.h"
enum ParserState
{
PARSER_INIT, // looking for time
PARSER_IN_TIME, // processing time
PARSER_IN_LYRICS // processing lyrics
};
// Used in multi-time lyric loader
typedef struct
{
CStdString text;
unsigned int timing;
unsigned int flags;
} MtLyric;
CKaraokeLyricsTextLRC::CKaraokeLyricsTextLRC( const CStdString & lyricsFile )
: CKaraokeLyricsText()
{
m_lyricsFile = lyricsFile;
}
CKaraokeLyricsTextLRC::~CKaraokeLyricsTextLRC()
{
}
bool CKaraokeLyricsTextLRC::Load()
{
XFILE::CFile file;
// Clear the lyrics array
clearLyrics();
XFILE::auto_buffer buf;
if (file.LoadFile(m_lyricsFile, buf) <= 0)
{
CLog::Log(LOGERROR, "%s: can't load \"%s\" file", __FUNCTION__, m_lyricsFile.c_str());
return false;
}
file.Close();
// Parse the correction value
int timing_correction = MathUtils::round_int( g_advancedSettings.m_karaokeSyncDelayLRC * 10 );
unsigned int offset = 0;
CStdString songfilename = getSongFile();
// Skip windoze UTF8 file prefix, if any, and reject UTF16 files
if (buf.size() > 3)
{
if ((unsigned char)buf.get()[0] == 0xFF && (unsigned char)buf.get()[1] == 0xFE)
{
CLog::Log( LOGERROR, "LRC lyric loader: lyrics file is in UTF16 encoding, must be in UTF8" );
return false;
}
// UTF8 prefix added by some windoze apps
if ((unsigned char)buf.get()[0] == 0xEF && (unsigned char)buf.get()[1] == 0xBB && (unsigned char)buf.get()[2] == 0xBF)
offset = 3;
}
if (checkMultiTime(buf.get() + offset, buf.size() - offset))
return ParserMultiTime(buf.get() + offset, buf.size() - offset, timing_correction);
else
return ParserNormal(buf.get() + offset, buf.size() - offset, timing_correction);
}
bool CKaraokeLyricsTextLRC::checkMultiTime(char *lyricData, unsigned int lyricSize)
{
// return true only when find lines like:
// [02:24][01:40][00:51][00:05]I'm a big big girl
// but not like:
// [00:01.10]I [00:01.09]just [00:01.50]call
bool inTime = false;
bool newLine = true;
bool maybeMultiTime = false;
unsigned int i = 0;
for ( char * p = lyricData; i < lyricSize; i++, p++ )
{
if (inTime)
{
if (*p == ']')
inTime = false;
}
else
{
if (*p == '[')
{
inTime = true;
if (newLine)
{
newLine = false;
}
else
{
if (*(p - 1) != ']')
return false;
else
maybeMultiTime = true;
}
}
if (*p == '\n')
newLine = true;
}
}
return maybeMultiTime;
}
bool CKaraokeLyricsTextLRC::ParserNormal(char *lyricData, unsigned int lyricSize, int timing_correction)
{
CLog::Log( LOGDEBUG, "LRC lyric loader: parser normal lyrics file" );
//
// A simple state machine to parse the file
//
ParserState state = PARSER_INIT;
unsigned int state_offset = 0;
unsigned int lyric_flags = 0;
int lyric_time = -1;
int start_offset = 0;
unsigned int offset = 0;
for ( char * p = lyricData; offset < lyricSize; offset++, p++ )
{
// Skip \r
if ( *p == 0x0D )
continue;
if ( state == PARSER_IN_LYRICS )
{
// Lyrics are terminated either by \n or by [
if ( *p == '\n' || *p == '[' || *p == '<' )
{
// Time must be there
if ( lyric_time == -1 )
{
CLog::Log( LOGERROR, "LRC lyric loader: lyrics file has no time before lyrics" );
return false;
}
// Add existing lyrics
char current = *p;
CStdString text;
if ( offset > state_offset )
{
// null-terminate string, we saved current char anyway
*p = '\0';
text = &lyricData[0] + state_offset;
}
else
text = " "; // add a single space for empty lyric
// If this was end of line, set the flags accordingly
if ( current == '\n' )
{
// Add space after the trailing lyric in lrc
text += " ";
addLyrics( text, lyric_time, lyric_flags | LYRICS_CONVERT_UTF8 );
state_offset = -1;
lyric_flags = CKaraokeLyricsText::LYRICS_NEW_LINE;
state = PARSER_INIT;
}
else
{
// No conversion needed as the file should be in UTF8 already
addLyrics( text, lyric_time, lyric_flags | LYRICS_CONVERT_UTF8 );
lyric_flags = 0;
state_offset = offset + 1;
state = PARSER_IN_TIME;
}
lyric_time = -1;
}
}
else if ( state == PARSER_IN_TIME )
{
// Time is terminated by ] or >
if ( *p == ']' || *p == '>' )
{
int mins, secs, htenths, ltenths = 0;
if ( offset == state_offset )
{
CLog::Log( LOGERROR, "LRC lyric loader: empty time" );
return false; // [] - empty time
}
// null-terminate string
char * timestr = &lyricData[0] + state_offset;
*p = '\0';
// Now check if this is time field or info tag. Info tags are like [ar:Pink Floyd]
char * fieldptr = strchr( timestr, ':' );
if ( timestr[0] >= 'a' && timestr[0] <= 'z' && timestr[1] >= 'a' && timestr[1] <= 'z' && fieldptr )
{
// Null-terminate the field name and switch to the field value
*fieldptr = '\0';
fieldptr++;
while ( isspace( *fieldptr ) )
fieldptr++;
// Check the info field
if ( !strcmp( timestr, "ar" ) )
m_artist += fieldptr;
else if ( !strcmp( timestr, "sr" ) )
{
// m_artist += "[CR]" + CStdString( fieldptr ); // Add source to the artist name as a separate line
}
else if ( !strcmp( timestr, "ti" ) )
m_songName = fieldptr;
else if ( !strcmp( timestr, "offset" ) )
{
if ( sscanf( fieldptr, "%d", &start_offset ) != 1 )
{
CLog::Log( LOGERROR, "LRC lyric loader: invalid [offset:] value '%s'", fieldptr );
return false; // [] - empty time
}
// Offset is in milliseconds; convert to 1/10 seconds
start_offset /= 100;
}
state_offset = -1;
state = PARSER_INIT;
continue;
}
else if ( sscanf( timestr, "%d:%d.%1d%1d", &mins, &secs, &htenths, <enths ) == 4 )
lyric_time = mins * 600 + secs * 10 + htenths + MathUtils::round_int( ltenths / 10 );
else if ( sscanf( timestr, "%d:%d.%1d", &mins, &secs, &htenths ) == 3 )
lyric_time = mins * 600 + secs * 10 + htenths;
else if ( sscanf( timestr, "%d:%d", &mins, &secs ) == 2 )
lyric_time = mins * 600 + secs * 10;
else
{
// bad time
CLog::Log( LOGERROR, "LRC lyric loader: lyrics file has no proper time field: '%s'", timestr );
return false;
}
// Correct timing if necessary
lyric_time += start_offset;
lyric_time += timing_correction;
if ( lyric_time < 0 )
lyric_time = 0;
// Set to next char
state_offset = offset + 1;
state = PARSER_IN_LYRICS;
}
}
else if ( state == PARSER_INIT )
{
// Ignore spaces
if ( *p == ' ' || *p == '\t' )
continue;
// We're looking for [ or <
if ( *p == '[' || *p == '<' )
{
// Set to next char
state_offset = offset + 1;
state = PARSER_IN_TIME;
lyric_time = -1;
}
else if ( *p == '\n' )
{
// If we get a newline and we're not paragraph, set it
if ( lyric_flags & CKaraokeLyricsText::LYRICS_NEW_LINE )
lyric_flags = CKaraokeLyricsText::LYRICS_NEW_PARAGRAPH;
}
else
{
// Everything else is error
CLog::Log( LOGERROR, "LRC lyric loader: lyrics file does not start from time" );
return false;
}
}
}
return true;
}
bool CKaraokeLyricsTextLRC::ParserMultiTime(char *lyricData, unsigned int lyricSize, int timing_correction)
{
CLog::Log( LOGDEBUG, "LRC lyric loader: parser mult-time lyrics file" );
ParserState state = PARSER_INIT;
unsigned int state_offset = 0;
unsigned int lyric_flags = 0;
std::vector<int> lyric_time(1, -1);
int time_num = 0;
std::vector<MtLyric> mtline;
MtLyric line;
int start_offset = 0;
unsigned int offset = 0;
for ( char * p = lyricData; offset < lyricSize; offset++, p++ )
{
// Skip \r
if ( *p == 0x0D )
continue;
if ( state == PARSER_IN_LYRICS )
{
// Lyrics are terminated either by \n or by [
if ( *p == '\n' || *p == '[' )
{
// Time must be there
if ( lyric_time[0] == -1 )
{
CLog::Log( LOGERROR, "LRC lyric loader: lyrics file has no time before lyrics" );
return false;
}
// Add existing lyrics
char current = *p;
CStdString text;
if ( offset > state_offset )
{
// null-terminate string, we saved current char anyway
*p = '\0';
text = &lyricData[0] + state_offset;
}
else
text = " "; // add a single space for empty lyric
// If this was end of line, set the flags accordingly
if ( current == '\n' )
{
// Add space after the trailing lyric in lrc
text += " ";
for ( int i = 0; i <= time_num; i++ )
{
line.text = text;
line.flags = lyric_flags | LYRICS_CONVERT_UTF8;
line.timing = lyric_time[i];
mtline.push_back( line );
}
state_offset = -1;
lyric_flags = CKaraokeLyricsText::LYRICS_NEW_LINE;
state = PARSER_INIT;
}
else
{
// No conversion needed as the file should be in UTF8 already
for ( int i = 0; i <= time_num; i++ )
{
line.text = text;
line.flags = lyric_flags | LYRICS_CONVERT_UTF8;
line.timing = lyric_time[i];
mtline.push_back( line );
}
lyric_flags = 0;
state_offset = offset + 1;
state = PARSER_IN_TIME;
}
time_num = 0;
lyric_time.resize(1);
lyric_time[0] = -1;
}
}
else if ( state == PARSER_IN_TIME )
{
// Time is terminated by ] or >
if ( *p == ']' || *p == '>' )
{
int mins, secs, htenths, ltenths = 0;
if ( offset == state_offset )
{
CLog::Log( LOGERROR, "LRC lyric loader: empty time" );
return false; // [] - empty time
}
// null-terminate string
char * timestr = &lyricData[0] + state_offset;
*p = '\0';
// Now check if this is time field or info tag. Info tags are like [ar:Pink Floyd]
char * fieldptr = strchr( timestr, ':' );
if ( timestr[0] >= 'a' && timestr[0] <= 'z' && timestr[1] >= 'a' && timestr[1] <= 'z' && fieldptr )
{
// Null-terminate the field name and switch to the field value
*fieldptr = '\0';
fieldptr++;
while ( isspace( *fieldptr ) )
fieldptr++;
// Check the info field
if ( !strcmp( timestr, "ar" ) )
m_artist += fieldptr;
else if ( !strcmp( timestr, "sr" ) )
{
// m_artist += "[CR]" + CStdString( fieldptr ); // Add source to the artist name as a separate line
}
else if ( !strcmp( timestr, "ti" ) )
m_songName = fieldptr;
else if ( !strcmp( timestr, "offset" ) )
{
if ( sscanf( fieldptr, "%d", &start_offset ) != 1 )
{
CLog::Log( LOGERROR, "LRC lyric loader: invalid [offset:] value '%s'", fieldptr );
return false; // [] - empty time
}
// Offset is in milliseconds; convert to 1/10 seconds
start_offset /= 100;
}
state_offset = -1;
state = PARSER_INIT;
continue;
}
else if ( sscanf( timestr, "%d:%d.%1d%1d", &mins, &secs, &htenths, <enths ) == 4 )
lyric_time[time_num] = mins * 600 + secs * 10 + htenths + MathUtils::round_int( ltenths / 10 );
else if ( sscanf( timestr, "%d:%d.%1d", &mins, &secs, &htenths ) == 3 )
lyric_time[time_num] = mins * 600 + secs * 10 + htenths;
else if ( sscanf( timestr, "%d:%d", &mins, &secs ) == 2 )
lyric_time[time_num] = mins * 600 + secs * 10;
else
{
// bad time
CLog::Log( LOGERROR, "LRC lyric loader: lyrics file has no proper time field: '%s'", timestr );
return false;
}
// Correct timing if necessary
lyric_time[time_num] += start_offset;
lyric_time[time_num] += timing_correction;
if ( lyric_time[time_num] < 0 )
lyric_time[time_num] = 0;
// Multi-time line
if ( *(p + 1) == '[' )
{
offset++;
p++;
state_offset = offset + 1;
state = PARSER_IN_TIME;
time_num++;
lyric_time.push_back(-1);
}
else
{
// Set to next char
state_offset = offset + 1;
state = PARSER_IN_LYRICS;
}
}
}
else if ( state == PARSER_INIT )
{
// Ignore spaces
if ( *p == ' ' || *p == '\t' )
continue;
// We're looking for [ or <
if ( *p == '[' || *p == '<' )
{
// Set to next char
state_offset = offset + 1;
state = PARSER_IN_TIME;
time_num = 0;
lyric_time.resize(1);
lyric_time[0] = -1;
}
else if ( *p == '\n' )
{
// If we get a newline and we're not paragraph, set it
if ( lyric_flags & CKaraokeLyricsText::LYRICS_NEW_LINE )
lyric_flags = CKaraokeLyricsText::LYRICS_NEW_PARAGRAPH;
}
else
{
// Everything else is error
CLog::Log( LOGERROR, "LRC lyric loader: lyrics file does not start from time" );
return false;
}
}
}
unsigned int lyricsNum = mtline.size();
if ( lyricsNum >= 2 )
{
for ( unsigned int i = 0; i < lyricsNum - 1; i++ )
{
for ( unsigned int j = i + 1; j < lyricsNum; j++ )
{
if ( mtline[i].timing > mtline[j].timing )
{
line = mtline[i];
mtline[i] = mtline[j];
mtline[j] = line;
}
}
}
}
for ( unsigned int i=0; i < lyricsNum; i++ )
addLyrics( mtline[i].text, mtline[i].timing, mtline[i].flags );
return true;
}
|