Пример #1
0
inline bool KNMusicLyricsManager::loadLocalLyrics(
        const KNMusicDetailInfo &detailInfo)
{
    //Generate the same file name as the music.
    QFileInfo musicFileInfo(detailInfo.filePath);
    QString sameNameLyrics=musicFileInfo.completeBaseName()+".lrc";
    //Search the lyrics according the lyrics finding policy.
    for(int i=0; i<m_policyList.size(); i++)
    {
        //Load the lyrics according to the policy index in the policy list.
        switch(m_policyList.at(i))
        {
        case SameNameInLyricsDir:
            if(loadLyricsFile(m_lyricsDir+"/"+sameNameLyrics))
            {
                return true;
            }
            break;
        case RelateNameInLyricsDir:
            if(loadRelatedLyrics(m_lyricsDir, detailInfo))
            {
                return true;
            }
            break;
        case SameNameInMusicDir:
            if(loadLyricsFile(musicFileInfo.absolutePath()+"/"+
                              sameNameLyrics))
            {
                return true;
            }
            break;
        case RelateNameInMusicDir:
            if(loadRelatedLyrics(musicFileInfo.absolutePath(), detailInfo))
            {
                return true;
            }
            break;
        }
    }
    return false;
}
Пример #2
0
bool KNMusicCueParser::parseList(QFile &listFile,
                                 KNMusicListDetailInfo &listDetailInfo)
{
    //Initial the file as a text file.
    QTextStream trackStream(&listFile);
    //Read the first text line.
    QString rawLine=trackStream.readLine().simplified();
    //Read every line until no text line.
    while(!rawLine.isNull())
    {
        QString rawCommand, rawData;
        parseCommand(rawLine, rawCommand, rawData);
        if(rawCommand=="FILE")
        {
            //Get the file name.
            QString musicFilePath=
                    rawLine.left(rawLine.lastIndexOf('\"'));
            musicFilePath.remove(0, musicFilePath.indexOf('\"')+1);
            //Because the cue file must be the same directory of the music file,
            //so we need to combine the music file path.
            QFileInfo listFileInfo(listFile);
            musicFilePath=listFileInfo.absolutePath()+"/"+musicFilePath;
            //Check is the music file exist.
            QFileInfo musicFileInfo(musicFilePath);
            if(musicFileInfo.exists())
            {
                musicFilePath=musicFileInfo.absoluteFilePath();
            }
            else
            {
                //If there's no music file, try to find the music file using the
                //same name.
                musicFilePath=listFileInfo.absolutePath()+"/"+
                        listFileInfo.completeBaseName()+"."+musicFileInfo.suffix();
                QFileInfo preferMusicFileInfo(musicFilePath);
                if(!preferMusicFileInfo.exists())
                {
                    //If we still can't find this, then is real failed.
                    return false;
                }
                musicFilePath=preferMusicFileInfo.absoluteFilePath();
            }
            //Save the path to listDetailInfo.
            listDetailInfo.musicFilePath=musicFilePath;

            //Now there must all be track data.
            QString trackLine=trackStream.readLine().simplified();
            parseCommand(trackLine, rawCommand, rawData);
            //Do parse until the rawCommand is not "TRACK".
            while(rawCommand=="TRACK" && !trackLine.isNull())
            {
                //Get the current Track Index.
                int trackIndex=rawData.left(rawData.indexOf(' ')).toInt();
                if(trackIndex==0)
                {
                    trackLine=trackStream.readLine();
                    parseCommand(trackLine, rawCommand, rawData);
                    //Read line until the next track.
                    while(rawCommand!="TRACK")
                    {
                        trackLine=trackStream.readLine();
                        parseCommand(trackLine, rawCommand, rawData);
                    }
                    continue;
                }
                //Here must be at least track 01.
                KNMusicListTrackDetailInfo currentTrack;
                //Parse all the data in the track.
                currentTrack.index=trackIndex;
                trackLine=trackStream.readLine().simplified();
                parseCommand(trackLine, rawCommand, rawData);
                while(rawCommand!="TRACK" && !trackLine.isNull())
                {
                    if(rawCommand=="INDEX")
                    {
                        //Here's the important part, INDEX command record the
                        //start time(INDEX 01) and stop time for last track
                        //(INDEX 00).
                        int indexSplitter=rawData.indexOf(' '),
                                indexID=rawData.left(indexSplitter).toInt();
                        QString indexRecordTime=rawData.mid(indexSplitter+1);
                        //Set the track index.
                        switch(indexID)
                        {
                        case 0:
                            //Check is there a track in the list.
                            if(!listDetailInfo.trackList.isEmpty())
                            {
                                listDetailInfo.trackList.last().trackDuration
                                        =timeTextToPosition(indexRecordTime)-
                                         listDetailInfo.trackList.last().startPosition;
                            }
                            break;
                        case 1:
                            //Set the start position.
                            currentTrack.startPosition=
                                    timeTextToPosition(indexRecordTime);
                            //Check is there a track in the list.
                            if(!listDetailInfo.trackList.isEmpty())
                            {
                                if(listDetailInfo.trackList.last().trackDuration==-1)
                                {
                                    //Check the is the last track's track duration
                                    //-1. If so, using the current start as it's
                                    //stop.
                                    listDetailInfo.trackList.last().trackDuration
                                            =currentTrack.startPosition-
                                            listDetailInfo.trackList.last().startPosition;
                                }
                            }
                            //Check is the start position available.
                            if(currentTrack.startPosition==-1)
                            {
                                return false;
                            }
                            break;
                        }
                    }
                    else
                    {
                        int commandIndex;
                        QString commandData;
                        //It must be a metadata.
                        parseMetaCommand(rawCommand,
                                         rawData,
                                         commandIndex,
                                         commandData,
                                         true);
                        //If there's a command we can't parse, that means parse failed.
                        if(commandIndex!=-1)
                        {
                            currentTrack.metaData[commandIndex]=
                                    commandIndex==Comments?
                                        currentTrack.metaData[commandIndex]+commandData:
                                        commandData;
                        }
                    }
                    //Parse next line.
                    trackLine=trackStream.readLine().simplified();
                    parseCommand(trackLine, rawCommand, rawData);
                }
                //Add current track to track list.
                listDetailInfo.trackList.append(currentTrack);
            }
            //If there's no track in the list, means parse failed.
            return listDetailInfo.trackList.size()!=0;
        }
        //This command must be a meta data.
        int commandIndex;
        QString commandData;
        //Parse the current command.
        parseMetaCommand(rawCommand,
                         rawData,
                         commandIndex,
                         commandData,
                         false);
        //If there's a command we can't parse, that means parse failed.
        if(commandIndex!=-1)
        {
            //Add current data to list detail info's data.
            listDetailInfo.metaData[commandIndex]=
                    commandIndex==Comments?
                        listDetailInfo.metaData[commandIndex]+commandData:
                        commandData;
        }
        //Read the next line.
        rawLine=trackStream.readLine().simplified();
    }
    return true;
}