LRC Lyrics Parser 0.0.1.0

Source: Internet
Author: User
1. Class Diagram

2. Source code List
/**
* FileName: Lrcanalyst.java
* Environment: Gnu/linux Ubuntu 7.04 + Eclipse 3.2 + JDK 1.6
* Function: Parse LRC lyrics file, this is just a demo under the CLI
* Version: 0.0.1.0
* Author: 88250
* Date: 2007.4.22
* e-Mail & mdn:dl88250@gmail.com
* qq:845765
*/

Import java.io. * ;
Import Java.util. * ;


/**
* Lyrics Interpretation class, for the LRC lyrics file parsing
*/
public class Lrcanalyst
{
/**
* LRC File Buffer
*/
private static Vector lyrics;

/**
* Formatted lyrics time and index, format reference Timeandindex class
*/
private static Vector Lrctimevalandindex;

/**
* The actual content of the lyrics
*/
private static Vector lyrciscontent;

/**
* Length of the time label, used to differentiate the time label format
*
* @see Private float Computetimetag (String timestr) method
*/
private int timetaglength;

/**
* True lyrics content begins with the offset of the beginning of the LRC text
*/
private int reallyrcisstartoffset;

/**
* Return the True lyrics content start to the offset of the beginning of the LRC text
* @return Offset
*/
public int Getreallrcstartoffset ()
{
return reallyrcisstartoffset;
}

/**
* Whether or not to determine the format of the lyrics tag, determine the words are <code>true</code>, otherwise it is <code>false</code>
* @see Private float Computetimetag (String timestr) method
*/
Private Boolean Isconfirmtimetagleng;

/**
* Default builder for lyric parser <br>
* Initialize lyrics file buffer fields
*/
Public Lrcanalyst ()
{
Lyrics = new Vector ();
Lrctimevalandindex = new Vector ();
Lyrciscontent = new Vector ();
timetaglength = 0;
Isconfirmtimetagleng = false;
Reallyrcisstartoffset = 0;
}

/**
* Read text file content by line
* @param fileName
* File path and filename
* @throws IOException
*/
public void ReadFile (String fileName)
{
Try
{
InputStream r = new FileInputStream (fileName);
Bytearrayoutputstream byteout = new Bytearrayoutputstream ();
byte tmp[] = new byte [1024];
BYTE context[];

R.read (TMP);
Byteout.write (TMP);
context = Byteout.tobytearray ();
String str = new String (Context, "GBK");
String lyrcistext[] = Str.split ("");

for (int i = 0; i < lyrcistext.length i + +)
{
Lyrics.add (Lyrcistext);
}
}
catch (IOException E)
{
E.printstacktrace ();
}
}

/**
* Show read all contents of LRC to console
*/
public void Displaylrccontent ()
{
for (int i = 0; i < lyrics.size (); i + +)
{
System.out.println (Lyrics.get (i));
}
}

/**
* Parse the lyrics file contents from the file buffer lyrics vector and save the results of the parsing <br>
* Resolution Method: <br>
* Step0. Split one line of content with '] ' to string[]
* Step1. See if each row in the 3rd char is ': ' <br>
* Step2. See if the 6th char in each row is '. ' <br>
* Step3. Determines the line of lyrics, determines the format used for the LRC <br>
* <b> Note: At present, the format of the time tag is two, respectively, the length of 8 and 5 of the </b> <br>
* Step4. Loop until the lyrics buffer is over <br>
* <b> NOTE: This algorithm may have serious defects <b>
* @see about the time label format, refer to Timetag class
*/
public void Parselyrics ()
{
for (int i = 0; i < lyrics.size (); i + +)
{
String alinelyrics = (string) lyrics.get (i);
string[] PARTSLRC = Alinelyrics.split ("]");

Char Flag1 = 0;
Char flag2 = 0;
if (! Alinelyrics.isempty ())
{//Many people who make LRC files are not responsible for the format!!!!
Flag1 = Alinelyrics.charat (1);
Flag2 = Alinelyrics.charat (3);
}

if ((Alinelyrics.length () > 3)
&& (Flag1 >= && Flag1 <= 57)
&& (': ' = = Flag2)]
{//OK for lyrics content line
if (! Isconfirmtimetagleng && alinelyrics.length () > 9)
{
if (Alinelyrics.charat (9) = = '])
{//Time label format with length 8
Timetaglength = 8;
}
else if (Alinelyrics.charat (6) = = '])
{//Time label format with length 5
Timetaglength = 5;
}
Reallyrcisstartoffset = i;
Isconfirmtimetagleng = true;
}

for (int j = 0; J < Partslrc.length; J + +)
{
if (Partslrc[j].charat (0) = = ' [])
{//OK for the time label part, not the lyric content part
String timetagstr = partslrc[j].substring (1, timetaglength + 1);
float timevalue = Computetimetag (TIMETAGSTR);
Add lyrics time to the corresponding lyrics index
Lrctimevalandindex.add (New Lrctimeandindex (TimeValue, i));
}
else//If the content at the beginning of the lyrics is also ' [' will appear fatal error
{//identified as part of the lyrics content
Lyrciscontent.add (Partslrc[j]);
}
}
}
}
Sortbytimetag ();
}

/**
* @param timestr
* A string <br> that shows the time of the lyrics
* For example:<br>
* "02:03.30" means 2*60s+3.3s=123.3s=1233ms time <br>
* <b> Note: Considering the length of the song will not be very long, so the algorithm directly take the first two bits as minutes, <br>
* and from ': ' After the unit is seconds. However, the LRC file format should now be not very uniform, for example: <br>
* "02:03" means 2*60s+3s=123s=1230ms, which is a short form representation, also to be considered. <br>
* Currently, the time tag is divided into two formats, the length of which is 8,5
* @return Calculated time value
*/
Private float

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.