LRC歌词分析函数

来源:互联网 发布:手机淘宝怎么免费开店 编辑:程序博客网 时间:2024/05/01 06:56

特别声明:转载请用IP地址链接,严禁原文转载!

发布我的LRC歌词分析函数。这是我现在1.8版本全功能播放器所使用的最新功能。要想歌曲、歌词同步播放,其关键有2点:1、什么时候显示歌词?按照播放时间将相应的歌词快速取出来。2、歌曲、歌词还必须匹配:即3分10秒的歌曲,你就得去找3分10秒的歌词,而不是3分15秒的歌词。


'******************************************************************************************
'*                              Module1.bas                                               *
'* 调用成功(分析歌词成功)后:LyricsAlalysys=True,并返回排好序的歌词数组LRCgeci,           *
'* 以及歌曲名GeMing、歌手GeShou、专辑ZhuanJi、编者GeCiFrom;否则返回LyricsAlalysys=False     *
'* 最后一个FOR循环是对歌词排序,歌词排序是同步播放的关键!                                    *
'*                     特别声明:转载请用IP地址,严禁原文转载!                              *
'*                     作者:Chenjl1031(东方之珠)                                          *
'******************************************************************************************
Option Explicit
'处理LRC歌词
Public LRCgeci() As String 'LRC歌词按行存放在数组中
Public iDimention As Integer
Public GeMing As String, GeShou As String, ZhuanJi As String, GeCiFrom As String
Public LrcOffset As String 'LRC歌词时间偏移量

Public Function LyricsAlalysys(LyricsName As String) As Boolean 'LRC歌词分析函数
      
       Dim FileNumber As Integer, FileCount As Integer, i As Integer, j As Integer, K As Long, L As Integer
       Dim TimeLabelLength As Integer '时间标签的长度
       Dim Chenjl1031 As Boolean
       Dim MyValue As String
       Dim sRow As String
       Dim TimeLabel As String
       Dim miniute As String, second As String, msecond As String 'miniute代表分,second代表秒,msecond代表毫秒
       Dim s1 As String, s2 As String
       On Error Resume Next
       GeMing = "未知"  '存放歌曲名
       GeShou = "未知" '存放歌手名字
       ZhuanJi = "未知"  '存放专辑名字
       GeCiFrom = "未知"  '存放歌词来自何处,即编者
       '打开LRC歌词文件LyricsName
       FileCount = 0: j = 0: TimeLabelLength = 0: Chenjl1031 = True
       If Dir(Player.MyPath & "Lyrics/" & LyricsName & ".lrc") <> "" Then
          LyricsAlalysys = True
          LrcOffset = "0"  '存放歌词偏移量
          FileNumber = FreeFile
          Open (Player.MyPath & "Lyrics/" & LyricsName & ".lrc") For Input As #FileNumber '打开文件,从中读取LRC歌词
          Do While Not EOF(FileNumber)
              Line Input #FileNumber, MyValue '读取一行歌词到变量MyValue
              MyValue = Trim(MyValue)
              For i = 1 To Len(MyValue)
                  sRow = Mid(MyValue, i, 1)
                  If sRow = "[" Then
                     FileCount = FileCount + 1 'FileCount计数用来确定歌词有多少行
                     L = i + 1
                     sRow = Mid(MyValue, L, 1)
                     If IsNumeric(sRow) Then
                        Do While Chenjl1031
                           sRow = Mid(MyValue, L + 1, 1)
                           If IsNumeric(sRow) Or sRow = ":" Or sRow = "." Then
                              TimeLabelLength = TimeLabelLength + 1
                           End If
                           If sRow = "]" Then TimeLabelLength = TimeLabelLength + 3: Chenjl1031 = False: Exit Do
                           L = L + 1
                        Loop
                     End If
                  End If
              Next
              '处理LRC歌词头
              If InStr(LCase(MyValue), "[ti:") > 0 Then
                 i = InStr(LCase(MyValue), "[ti:")
                 Mid(MyValue, i, 4) = "    "
                 MyValue = Mid(MyValue, 2, Len(MyValue) - 2)
                 GeMing = IIf(Trim(MyValue) <> "", Trim(MyValue), "未知") '存放歌曲名
                 j = j + 1
              End If
              If InStr(LCase(MyValue), "[ar:") > 0 Then
                 i = InStr(LCase(MyValue), "[ar:")
                 Mid(MyValue, i, 4) = "    "
                 MyValue = Mid(MyValue, 2, Len(MyValue) - 2)
                 GeShou = IIf(Trim(MyValue) <> "", Trim(MyValue), "未知") '存放歌手名字
                 j = j + 1
              End If
              If InStr(LCase(MyValue), "[al:") > 0 Then
                 i = InStr(LCase(MyValue), "[al:")
                 Mid(MyValue, i, 4) = "    "
                 MyValue = Mid(MyValue, 2, Len(MyValue) - 2)
                 ZhuanJi = IIf(Trim(MyValue) <> "", Trim(MyValue), "未知") '存放专辑名字
                 j = j + 1
              End If
              If InStr(LCase(MyValue), "[by:") > 0 Then
                 i = InStr(LCase(MyValue), "[by:")
                 Mid(MyValue, i, 4) = "    "
                 MyValue = Mid(MyValue, 2, Len(MyValue) - 2)
                 GeCiFrom = IIf(Trim(MyValue) <> "", Trim(MyValue), "未知") '存放歌词来自何处
                 j = j + 1
              End If
              If InStr(LCase(MyValue), "[offset:") > 0 Then
                 i = InStr(LCase(MyValue), "[offset:")
                 Mid(MyValue, i, 8) = Space(8)
                 MyValue = Mid(MyValue, 2, Len(MyValue) - 2)
                 LrcOffset = IIf(Trim(MyValue) <> "", Trim(MyValue), "0") '存放歌词偏移量
                 j = j + 1
              End If
          Loop
          Close #FileNumber
          ReDim LRCgeci(Trim(Str(FileCount - j - 1))) '重新定义实际歌词的行数数组,有FileCount - j个元素
         

          '处理LRC歌词文本         
          FileCount = 0
          FileNumber = FreeFile
          Open (Player.MyPath & "Lyrics/" & LyricsName & ".lrc") For Input As #FileNumber '打开文件,从中读取LRC歌词
          Do While Not EOF(FileNumber)
              Line Input #FileNumber, MyValue '读取一行歌词到变量MyValue
              MyValue = Trim(MyValue)
              sRow = Mid(MyValue, 2, 2)
              j = 0: i = 0
              If IsNumeric(sRow) Then '判断是否时间标签行
                 i = i + 1
                 Do While True
                    j = j + TimeLabelLength
                    sRow = Mid(MyValue, j + 2, 2)
                    If IsNumeric(sRow) Then  '判断是否时间标签行
                       i = i + 1 'i累加后即为每一行时间标签的个数
                    Else
                       Exit Do
                    End If
                 Loop
                
                 For j = 1 To i 'Step 1
                     TimeLabel = Mid(MyValue, (j - 1) * TimeLabelLength + 1, TimeLabelLength)
                     If TimeLabelLength > 2 Then miniute = Mid(TimeLabel, 2, 2)
                     If TimeLabelLength > 5 Then second = Mid(TimeLabel, 5, 2)
                     If TimeLabelLength > 8 Then msecond = Mid(TimeLabel, 8, 2)                    
                     K = Val(miniute) * 60000 + Val(second) * 1000 + Val(msecond) - Val(LrcOffset)
                     s1 = Trim(Str(K))
                     If Len(s1) = 1 Then s2 = "[0000000" & s1 & "]"
                     If Len(s1) = 2 Then s2 = "[000000" & s1 & "]"
                     If Len(s1) = 3 Then s2 = "[00000" & s1 & "]"
                     If Len(s1) = 4 Then s2 = "[0000" & s1 & "]"
                     If Len(s1) = 5 Then s2 = "[000" & s1 & "]"
                     If Len(s1) = 6 Then s2 = "[00" & s1 & "]"
                     If Len(s1) = 7 Then s2 = "[0" & s1 & "]"
                     If Len(s1) >= 8 Then s2 = "[" & s1 & "]"                    
                     LRCgeci(Trim(Str(FileCount))) = Trim(s2 & Mid(MyValue, i * TimeLabelLength + 1, Len(MyValue) - (i * TimeLabelLength)))  'i代表时间标签的个数                    
                     FileCount = FileCount + 1
                 Next
                
                
              End If
          Loop
          iDimention = FileCount - 1
          Close #FileNumber
         
          '冒泡排序
          For i = 0 To FileCount - 2
              For j = 0 To FileCount - 2 - i
                  If Val(Mid(LRCgeci(j), 2, 8)) > Val(Mid(LRCgeci(j + 1), 2, 8)) Then
                     sRow = LRCgeci(j)
                     LRCgeci(j) = LRCgeci(j + 1)
                     LRCgeci(j + 1) = sRow
                  End If
              Next j
          Next i
         
       Else
          LyricsAlalysys = False
       End If     
      
End Function

 
原创粉丝点击