LRC歌词解析

 C++ Code 
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
现在对LRC类稍加改进,可解析如下项:
TITLE:歌名
AUTHOR:演唱者
ALBUM:专辑名称
CAPTION:歌词
BY:上传者
DELAY:延迟时间
WRONG:未定义
 

// Lyc.h: interface for the CLyc class.
//
//Author visualsan;
//Email:[email protected]\n
//////////////////////////////////////////////////////////////////////

#if  !defined(AFX_LYC_H__B0C8E9D3_8752_4910_B7BF_2F566DF05229__INCLUDED_)
#define  AFX_LYC_H__B0C8E9D3_8752_4910_B7BF_2F566DF05229__INCLUDED_

#if  _MSC_VER >  1000
#pragma  once
#endif   // _MSC_VER > 1000
#include  <afxtempl.h>
#include  <map>
#include  <algorithm>
using   namespace  std;

typedef   enum  {TITLE= 0 ,AUTHOR= 1 ,ALBUM= 2 ,CAPTION= 3 ,WRONG= 4 ,BY= 5 ,DELAY= 6 } LYCType;
class  CLyc 
{
public :

    CLyc();
    CLyc(CString filename);
    
virtual  ~CLyc();

    
//read  from file,if sucess ,retuen true.
     bool     Read(CString str);
    
//sucess to read
     bool     GetSucess(){  return  m_bLoadSuccess;}
    
//get title of the lyc file while sucessed reading
    CString GetTitle() {  return  m_Title; }
    
//get author
    CString GetAuthor(){  return  m_Author; }
    CString GetAlbum() { 
return  m_Album; }
    CString GetBy()    { 
return  m_SendBy;}
    
double   GetOffset(){  return  m_delayTime;}
    
//get length of the song
     double   GetLengthTime();
    
//get start time
     double   GetStartTime(){  return  m_beginTime; }
    
//get end  time
     double   GetEndTime()  {  return  m_endTime; }

   
   
   
    CString GetWordsFromTime(
double  curTime);
    
//Get next words of the currenttime
    CString GetNextWords( int  step= 1 );
    
//Get Previous words of current time
    CString GetPreWords( int  step= 1 );
    
//samething about.
    CString GetAbout();

private :
   
//    CString GetDataFromType(CString data,LYCType tp);
     bool  IsFileExit(CString str);
    
void  DoTitle(CString str);
    
void  DoAuthor(CString str);
    
void  DoAlbum(CString str);
    
void  DoCaption(CString str);
    
void  DoBy(CString str);
    
void  DoDelay(CString str);
    LYCType GetType(CString str);


    map<
const  CString ,CString> m_Data;
    CArray<
double  ,  double  > m_strTime;
    CString m_strFileName;
    
bool     m_bLoadSuccess;

    
int      m_Index;
    CString m_CurWords;
    
double   m_ForeTime;
    
double   m_NextTime;

    CString  m_Author;
    CString  m_Album;
    CString  m_Title;
    CString  m_SendBy;

    
double  m_beginTime;
    
double  m_endTime;

    
double  m_delayTime;

};

#endif   // !defined(AFX_LYC_H__B0C8E9D3_8752_4910_B7BF_2F566DF05229__INCLUDED_)


//////////cpp

// Lyc.cpp: implementation of the CLyc class.
//
//////////////////////////////////////////////////////////////////////

#include   "stdafx.h"
#include   "Lyc.h"

#ifdef  _DEBUG
#undef  THIS_FILE
static   char  THIS_FILE[]=__FILE__;
#define   new  DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//Author visualsan;
//Email:[email protected]\n
//////////////////////////////////////////////////////////////////////

CLyc::CLyc()
{

    m_bLoadSuccess = FALSE;
    m_Data.clear();
    m_strTime.RemoveAll();
    m_Author=
"" ;
    m_Album=
"" ;   
    m_Title=
"" ;
    m_beginTime = 
0 . 0 ;
    m_endTime = 
0 . 0 ;
    m_Index = 
0 ;
    m_CurWords = 
"" ;
    m_ForeTime =-
1 ;
    m_NextTime = -
1 ;
    m_delayTime = 
0 . 0 ;
}
CLyc::CLyc(CString filename)
{
   
    m_bLoadSuccess = FALSE;
    m_Data.clear();
    m_strTime.RemoveAll();
    m_Author=
"" ;
    m_Album=
"" ;   
    m_Title=
"" ;
    m_beginTime = 
0 . 0 ;
    m_endTime = 
0 . 0 ;
    m_Index = 
0 ;
    m_CurWords = 
"" ;
    m_SendBy = 
"" ;
    Read(filename);
}
CLyc::~CLyc()
{
    m_Data.clear();
    m_strTime.RemoveAll();

}

bool  CLyc::Read(CString str)
{
    m_bLoadSuccess = 
false ;
    
if  (IsFileExit(str) ==  false  )
    {   
        
return   false ;
    }
   
    CString data,data1;
    m_Data.clear();
    m_strTime.RemoveAll();
    m_Author=
"UnKnow" ;
    m_Album=
"UnKnow" ;   
    m_Title=
"UnKnow" ;
    m_beginTime = 
0 . 0 ;
    m_endTime = 
0 . 0 ;
    m_Index = 
0 ;
    m_CurWords = 
"UnKnow" ;
    m_ForeTime =-
1 ;
    m_NextTime = -
1 ;
    m_delayTime = 
0 . 0 ;
    m_SendBy = 
"UnKnow" ;
    CStdioFile f(str,CFile::modeRead);
    
do
    {
        f.ReadString(data);
        data1 = data;
        data1.Replace(
" " , "" );
        
if  ( !data1.IsEmpty() )
        {
            
while (str.GetAt( 0 ) ==  ' ' ) str = str.Right(str.GetLength()- 1 );
            
//cout << (LPCTSTR)data << endl;
            LYCType tp=GetType(data);

           
            
switch  ( tp )
            {
            
case  TITLE:
                DoTitle(data);
                
break ;
            
case  AUTHOR:
                DoAuthor(data);
                
break ;
            
case  ALBUM:
                DoAlbum(data);
                
break ;
            
case  CAPTION:
                DoCaption(data);
                
break ;
            
case  BY:
                DoBy(data);
            
case  DELAY:
                DoDelay(data);
            
case  WRONG:
                
break ;
            }
           
        }
    } 
while  (!data.IsEmpty());
       
    f.Close();
    m_bLoadSuccess = 
true ;

    
int  l=m_strTime.GetSize();
    
double  *dt= new   double [m_strTime.GetSize()];
    
for  ( int  i= 0 ;i<l;i++)
    {
        dt[i] = m_strTime.GetAt(i);
    }
    m_strTime.RemoveAll();
    sort(dt,dt+l);
    
for  (i= 0 ;i<l;i++)
    {
        m_strTime.Add(dt[i]);
    }
    
delete  dt;
    m_beginTime = m_strTime.GetAt(
0 );
    m_endTime = m_strTime.GetAt(m_strTime.GetSize()-
1 );
    
return   true ;

}
double   CLyc::GetLengthTime()
{
    
if  ( !m_bLoadSuccess )
    {
        
return   0 . 0 ;
    }
    
return  m_endTime-m_beginTime;
}

LYCType CLyc::GetType(CString str)
{

    
//wrong case
     if  (str.IsEmpty())
    {
        
return  WRONG;
    }
    
if  (str.GetAt( 0 ) !=  '[' )
    {
        
return  WRONG;
    }
    
if  (str.Find( ']' ) == - 1 )
    {
        
return  WRONG;
    }
    
/////////////////
   
    
//title case
     if  ((str.GetAt( 1 ) ==  't' || str.GetAt( 1 ) ==  'T' )&&
        (str.GetAt(
2 ) ==  'i' || str.GetAt( 1 ) ==  'I' ))
    {
        
return  TITLE;
    }
    
/////////////

    
//Author
     //Author case
     if  ((str.GetAt( 1 ) ==  'a' || str.GetAt( 1 ) ==  'A' )&&
        (str.GetAt(
2 ) ==  'r' || str.GetAt( 1 ) ==  'R' ))
    {
        
return  AUTHOR;
    }
    
/////////////

    
//ALBUM
     //ALBUM case
     if  ((str.GetAt( 1 ) ==  'a' || str.GetAt( 1 ) ==  'A' )&&
        (str.GetAt(
2 ) ==  'l' || str.GetAt( 1 ) ==  'L' ))
    {
        
return  ALBUM;
    }
    
if  ((str.GetAt( 1 ) ==  'b' || str.GetAt( 1 ) ==  'B' )&&
        (str.GetAt(
2 ) ==  'y' || str.GetAt( 1 ) ==  'Y' ))
    {
        
return  BY;
    }

/////////////////////////////
    CString str1(str);
    str1.MakeLower();
    
if ( str1.Find( "offset" ) != - 1 )
        
if  ((str.GetAt( 1 ) ==  'o' || str.GetAt( 1 ) ==  'O' )&&
            (str.GetAt(
2 ) ==  'f' || str.GetAt( 2 ) ==  'F' )&&
            (str.GetAt(
3 ) ==  'f' || str.GetAt( 3 ) ==  'F' )&&
            (str.GetAt(
4 ) ==  's' || str.GetAt( 4 ) ==  'S' )&&
            (str.GetAt(
5 ) ==  'e' || str.GetAt( 5 ) ==  'E' )&&
            (str.GetAt(
6 ) ==  't' || str.GetAt( 6 ) ==  'T' )
            )
            
return  DELAY;
   
    
/////////////


    
if  (str.Find( ':' ) == - 1 )
    {
        
return  WRONG;
    }
    
return  CAPTION;
}
bool  CLyc::IsFileExit(CString str)
{

    FILE *f;
    f = fopen(str,
"r" );
    
if  ( f ==  NULL  )
    {
        
return   false ;
    }
    fclose( f );
    
return   true ;
}
void  CLyc::DoDelay(CString str)
{
    
char  c[ 500 ]={ '\0' };
    
for  ( int  i= 8 ;i<str.GetLength()- 1 ;i++)
    {
        c[i-
8 ] = str.GetAt(i);
    }

    m_delayTime = atof( c );
    m_delayTime /= 
1000 . 0 ;
}
void  CLyc::DoTitle(CString str)
{

    
char  c[ 500 ]={ '\0' };
    
for  ( int  i= 4 ;i<str.GetLength()- 1 ;i++)
    {
        c[i-
4 ] = str.GetAt(i);
    }
    m_Title = c;
}
void  CLyc::DoAuthor(CString str)
{

    
char  c[ 500 ]={ '\0' };
    
for  ( int  i= 4 ;i<str.GetLength()- 1 ;i++)
    {
        c[i-
4 ] = str.GetAt(i);
    }
    m_Author = c;
}
void  CLyc::DoAlbum(CString str)
{
    
char  c[ 500 ]={ '\0' };
    
for  ( int  i= 4 ;i<str.GetLength()- 1 ;i++)
    {
        c[i-
4 ] = str.GetAt(i);
    }
    m_Album = c;
}
void  CLyc::DoBy(CString str)
{
    
char  c[ 500 ]={ '\0' };
    
for  ( int  i= 4 ;i<str.GetLength()- 1 ;i++)
    {
        c[i-
4 ] = str.GetAt(i);
    }
    m_SendBy = c;
}
void  CLyc::DoCaption(CString str)
{
    CString st;
    CString outstr;
    st=str.Left(str.Find(
']' ));
    st=st.Right(st.GetLength()-
1 );
    str=str.Right(str.GetLength()-
1 );
    str=str.Right(str.GetLength()-str.Find(
']' )- 1 );
    outstr = str.Right(str.GetLength()-str.ReverseFind(
']' )- 1 );
   
    st.Format(
"%.2f" ,atof(st.Left(st.Find( ':' )+ 1 ))* 60 . 0 +atof(st.Right(st.GetLength()-st.ReverseFind( ':' )- 1 )));
   
//    cout << (LPCTSTR)st << endl;   
//    cout << (LPCTSTR)outstr << endl;
    m_strTime.Add(atof(st));
    m_Data[st] = outstr;
    
if  (str.Find( '[' )  != - 1 )
    {
        DoCaption(str);
    }
   
}
CString CLyc::GetWordsFromTime(
double  curTime)
{

    
if  ( !m_bLoadSuccess )
    {
        
return   " " ;
    }
    
if  (curTime>m_ForeTime && curTime<m_NextTime)
    {
        
return  m_CurWords;
    }

    CString str;
    curTime += m_delayTime;
    
if  (curTime < m_beginTime )
    {
        curTime = m_beginTime;
        str.Format(
"%.2f" ,m_strTime.GetAt( 0 ));
        
return  m_Data[str];
    }
    
if  (curTime > m_endTime )
    {
        curTime = m_endTime;
        str.Format(
"%.2f" , m_strTime.GetAt(m_strTime.GetSize()- 1 ) );
        
return  m_Data[str];
    }

    
for  ( int  i= 0 ; i<m_strTime.GetSize(); i++ )
    {
        
if  (curTime < m_strTime.GetAt(i) )
        {
            
break ;
        }
    }
    str.Format(
"%.2f" , m_strTime.GetAt(i- 1 ) );

    m_Index = i-
1 ;
    m_CurWords = m_Data[str];
    
if  (i- 2 >= 0 )
    {
        m_ForeTime=m_strTime.GetAt(i-
2 );
    }
    
if  (i- 1 <m_strTime.GetSize())
    {
        m_NextTime=m_strTime.GetAt(i-
1 );
    }
    
return  m_Data[str];
}
CString CLyc::GetNextWords(
int  step)
{
    
if  ( !m_bLoadSuccess )
    {
        
return   " " ;
    }
    CString str;
    
if  (m_Index+step< 0 )
    {
        str.Format(
"%.2f" ,m_strTime.GetAt( 0 ));
        
return   " " ;
    }
    
else   if (m_Index+step>=m_strTime.GetSize())
    {
        str.Format(
"%.2f" ,m_strTime.GetAt(m_strTime.GetSize()- 1 ));
        
return   " " ;
    }
    
else
        str.Format(
"%.2f" ,m_strTime.GetAt(m_Index+step));
           
    
return  m_Data[str];
}   
CString CLyc::GetPreWords(
int  step)
{
    
if  ( !m_bLoadSuccess )
    {
        
return   " " ;
    }
    CString str;
    
if  (m_Index-step< 0 )
    {
        str.Format(
"%.2f" ,m_strTime.GetAt( 0 ));
        
return   " " ;
    }
    
else   if (m_Index-step>=m_strTime.GetSize())
    {
        str.Format(
"%.2f" ,m_strTime.GetAt(m_strTime.GetSize()- 1 ));
        
return   " " ;
    }
    
else
        str.Format(
"%.2f" ,m_strTime.GetAt(m_Index-step));
   
    
return  m_Data[str];
}
CString CLyc::GetAbout()
{
    CString str=
        
"Author Email:[email protected]\n" ;
    
return  str;
}
 C++ Code 
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
关于LRC歌词文件的格式介绍,参见百度或者谷歌搜索结果。

以下是5个实例。

代码达到极简:

#include  <iostream>
#include   "lrc_parser.hpp"
#include   "toolbox.hpp" //UTF2GBK function

using   namespace  music;

int  main( int , char **)
{
    lrc_parser lrc(
"lrc_file(.lrc)" ); //创建对象
  lrc.parser(); //解析
   //输出
    std::cout<<UTF2GBK(lrc.get_title().c_str()) //歌名
            << "  "
            <<UTF2GBK(lrc.get_author().c_str())
//歌者
            << "  "
            <<UTF2GBK(lrc.get_album().c_str())
//专辑
            <<std::endl<<std::endl;
    std::cout<<
"=============================" <<std::endl;
    
for (size_t i= 0 ;i<lrc.size();++i)
    {
         
for (size_t j= 0 ;j<lrc[i].size();++j)
         {
             std::cout<<lrc[i][j]<<
"   " ;
         }
         std::cout<<UTF2GBK(lrc.get_lrc_body(i).c_str())<<std::endl;
     }
    
return   0 ;
}

猜你喜欢

转载自blog.csdn.net/damon1118/article/details/9670323