See how to parse the lyrics in the LRC format in C language (top)

Source: Internet
Author: User

Well, it was supposed to have been written last night, and it's been dragged to this morning because of some problems with the system. All right, no more nonsense.

Always listen to the song, must have seen the lyrics it. Of course, the lyrics we're going to talk about here are the words with the LRC suffix. In addition, there are lyrics in QRC,KRC format. But in contrast, the LRC format lyrics are more common. Let's start by looking at what the LRC format lyrics look like:

Well, how do we read the lyrics in this format? Recommended to see Baidu Encyclopedia of its introduction, in this will not nonsense.

Believe that you have written music or want to write music player will be involved in the interpretation of the lyrics file. Naturally, it may be more convenient to use a high-level language. For example, we can use a regular match to get the time and lyrics lines we need and so on. But today we only use C language, rely on C language library functions and some of their own written functions to achieve the above lyrics to parse. Well, the following may be a bit cumbersome, in addition, the algorithm is not the best. However, one of the biggest benefits for individuals is being able to familiarize themselves with the use of C library functions. For example, we will use the malloc ()/free () function to dynamically manage memory , and use the function of string processing in <string.h> and so on.


Ok, since we have to use the most Earth method to resolve, we will come to the rationale.

First of all, the idea is that we can set up a few global variables or global arrays to hold the name of the song , the record of the song, the singer of the song , and offset . So, we can declare so many global variables in the header file lyric.h:

extern  char* lrc_title;extern  char* lrc_artist;extern char* lrc_album;extern char* lrc_singer  ; extern  char* lrc_filename;extern  double  lrc_offset;//milliseconds.

As you can see, I am not directly declared as extern char lrc_title[100] in this form, because it is going to be stored in a way that dynamically requests memory at the time of real parsing. Then release the storage space when we don't need it.

Well, take a look at how to deal with the text of the lyrics. We find that each line has a corresponding time in front of the lyrics. Of course, the player is also based on the time to display the corresponding line of lyrics. So how can we match the time to the corresponding lyrics line? Learn Java, C #, C + + and other high-level languages must have used a similar "container"map<t1 Key, T2 value>, of course, may not be the name, but the functions are similar. map<t1 Key, T2 value> allows us to store key-value pairs. Just like a dictionary, each word has a corresponding interpretation. But how do we implement a similar function in C language? Show that we can not do like map<t1 Key, T2 value> such advanced and usable containers. However, the C-language structure can help us implement a similar "container" with weaker functionality. First look at my structure definition in the header file lyric.h:

struct  textline{    double time;    milliseconds.    Char Text[maxlrclinelen];};

As I can see, I'm trying to convert all the time in front of the lyrics into milliseconds before storing them in time, and then storing a line of lyrics in the Text[maxlrclinelen] array. Here, the reason is not used char* text form, mainly for the convenience of the later processing. Because the lrc_title and the like above will demonstrate this dynamic allocation of memory usage, so it is no longer duplicated. In addition, I would like to mention that C99 also added a feature called " Flex " Array. That is, in the structure above can be replaced by the following way:

struct  textline{    double time;    milliseconds.    Char text[];};

We can then allocate a certain amount of space at the time of parsing to hold the text of the lyrics. However, this usage is no longer demonstrated in the following code. Do not know the "flexible" array usage can be Google for yourself.

Okay, move on to the next step. Now that we've figured out how to store it, it's natural to design some important functions related to lyrics parsing. First look at the following global functions I declared in lyric.h:

Declaration of some global functions, which can be called in other files. file*   getlrcfilestream (const char* fileName); STATUS  analyzelrcfile (file* stream); void    destroylrc (void);

These three global functions are responsible for a different task, respectively.

1. Function:file* getlrcfilestream (1)

Used to attempt to open a file as read-only and return a file stream, based on the file name entered by the user.

2. Function:STATUS analyzelrcfile (1)

The function of parsing lyrics, which is used to receive a "file stream", and to store the parsed lyrics information in the global variables we declared above, or in a struct array. Finally, a flag is returned that resolves a successful or failed state.

3. Function: void Destroylrc ()

This is the Destroy function. The main reason for designing such a function is that we can release the previously requested heap memory space at the end to reflect the essence of dynamic memory management. In fact, the implementation of the DESTROYLRC () function has to be careful, because we have to remember which application storage space is to be released at the end, and can be properly released. At the same time, we have to avoid the "lost pointer" appearance. These specific details will be given in the next article to give a concrete implementation.

Finally, before looking at how to implement the functions declared above, let's take a look at the file lyric.h:

1 /*2 *filename lyric.h3 *author Leomon.4 *date 2014.4.14 to 2014.4.15.5 *version 0.1.06 *changelog nothing.7  */8 #ifndef Lyric_h9 #defineLyric_hTen  One#include <stdio.h> A#include <stdlib.h> -#include <string.h> -#include <assert.h> the#include <ctype.h> -  - #defineMaxtitlelen 50 - #defineMaxtaglen 50 + #defineMaxlrclinelen 128 - #defineMAXTEXTLINECNT 128 +  A structTextLine at { -     DoubleTime////milliseconds. -     CharText[maxlrclinelen]; - }; -  - enumSTATUS in { - successful, to FAILED + }; -  thetypedefenumstatus status; *typedefstructTextLine TextLine; $ Panax Notoginseng //declaration of some global variables. - extern  Char*Lrc_title; the extern  Char*lrc_artist; + extern  Char*Lrc_album; A extern  Char*Lrc_singer; the extern  Char*Lrc_filename; + extern  DoubleLrc_offset;//milliseconds. - externTextLine *(lrc_textline[maxtextlinecnt]); $  $ //declaration of some global functions, which can be called in other files. -file* Getlrcfilestream (Const Char*fileName); -STATUS Analyzelrcfile (file*stream); the voidDESTROYLRC (void); - #endif //Lyric_h
View Code

The next article will give the implementation details of all the functions. It was meant to be done in one article, but it was too long and it looked uncomfortable. OK, we have to go to class again. ^_^


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

See how to parse the lyrics in the LRC format in C language (top)

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.