Skip to content

Commit

Permalink
Merge pull request #13 from BensonLaur/optimize-lyric-operation
Browse files Browse the repository at this point in the history
优化操作
  • Loading branch information
BensonLaur authored Sep 3, 2017
2 parents 7407910 + 17b7411 commit c42e0e3
Show file tree
Hide file tree
Showing 15 changed files with 353 additions and 12 deletions.
Binary file modified BesLyric/BesLyric.aps
Binary file not shown.
Binary file modified BesLyric/BesLyric.rc
Binary file not shown.
3 changes: 2 additions & 1 deletion BesLyric/Define.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ typedef struct _PATH_STATE
UNICODE_LITTLE_ENDIAN,
UNICODE_BIG_ENDIAN,
UTF_8,
UTF_8_NO_BOM,
OTHER
};

Expand All @@ -77,7 +78,7 @@ typedef struct _PATH_STATE
Y表示次版本,较大修改时更新
Z表示修改号,小问题时更新
*/
static const wstring VERSION_NUMBER = L"2.1.1"; //版本号(注意每次更改版本号时需要更改2处,1处是这里,1处是 BesLyric.rc 中的Version)
static const wstring VERSION_NUMBER = L"2.1.2"; //版本号(注意每次更改版本号时需要更改2处,1处是这里,1处是 BesLyric.rc 中的Version)

static const wstring LINK_VERSION_LOG= L"http://files.cnblogs.com/files/BensonLaur/versionLog.zip"; //链接,指向版本日志文件
static const wstring LINK_LAST_VERSION_INFO= L"http://files.cnblogs.com/files/BensonLaur/lastVersion.zip"; //链接,指向最后版本信息的文件
Expand Down
14 changes: 14 additions & 0 deletions BesLyric/PageMaking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,20 @@ void CPageMaking::OnBtnOpenOutput()
}
}
//打开编辑选中的歌词文件
void CPageMaking::OnEditLyric()
{
if(_tcslen(M()->maker.m_szLyricPathName)!=0)
{
//用window默认编辑器打开歌词文件
ShellExecute(NULL,L"open",L"notepad", M()->maker.m_szLyricPathName,NULL,SW_SHOWNORMAL);
}
else
{
_MessageBox(M()->m_hWnd, L"请先选择歌词文件 :)", L"提示", MB_OK|MB_ICONINFORMATION);
}
}
//第一个页面(歌词制作):回到“加载按钮”按下后的状态
void CPageMaking::backToInit_1()
{
Expand Down
3 changes: 3 additions & 0 deletions BesLyric/PageMaking.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class CPageMaking

void OnBtnPreview();
void OnBtnOpenOutput(); //打开生成的文件
void OnEditLyric(); //打开编辑选中的歌词文件

void OnBtnSoundOpen1(); //打开声音
void OnBtnSoundClose1(); //静音
Expand Down Expand Up @@ -86,6 +87,8 @@ class CPageMaking
EVENT_ID_COMMAND(R.id.btn_restart , OnBtnRestart)
EVENT_ID_COMMAND(R.id.btn_preview , OnBtnPreview)
EVENT_ID_COMMAND(R.id.btn_open_output , OnBtnOpenOutput)
EVENT_ID_COMMAND(R.id.btn_edit_origin_lyric , OnEditLyric)


EVENT_ID_COMMAND(R.id.btn_sound_open_1 , OnBtnSoundOpen1)
EVENT_ID_COMMAND(R.id.btn_sound_close_1 , OnBtnSoundClose1)
Expand Down
3 changes: 2 additions & 1 deletion BesLyric/entity/AutoUpdateThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ void AutoUpdateThread::SendLoginInfo()
{
auto beg = iter->Find(L'[')+1;
auto end = iter->Find(L']');
strIP = iter->Right(beg).Left(end);
strIP = iter->Left(end);
strIP = strIP.substr(beg);
break;
}
}
Expand Down
210 changes: 206 additions & 4 deletions BesLyric/entity/LyricPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "LyricPlayer.h"
#include "../utility/FileHelper.h"
#include "../utility/WinFile.h"
#include <algorithm>
#include <mmsystem.h>
#pragma comment (lib, "winmm.lib")

Expand Down Expand Up @@ -117,15 +118,216 @@ vector<TimeLineInfo> LyricPlayer::getLyricWithLineInfo(File& lyricFile)
{
vector<TimeLineInfo> lines;


vector<SStringW> allLines;
if(true == FileOperator::ReadAllLinesW(lyricFile, &allLines))
{
for(auto iter = allLines.begin();iter!=allLines.end(); iter++)
LrcProcessor lrcProcessor( allLines);

if(lrcProcessor.IsLrcFileValid())
{
TimeLineInfo newInfo(iter->GetBuffer(1));
lines.push_back(newInfo);
lines = lrcProcessor.GetNeteaseLrc();

//输出覆盖原来的lrc文件,因为用户可能看到可以预览LRC,就直接将网易云不支持的歌词上传上去
lrcProcessor.GenerateNeteaseLrcFile(SStringW(lyricFile.m_lpszPathFile));
}
}

//vector<SStringW> allLines;
//if(true == FileOperator::ReadAllLinesW(lyricFile, &allLines))
//{
// for(auto iter = allLines.begin();iter!=allLines.end(); iter++)
// {
// TimeLineInfo newInfo(iter->GetBuffer(1));
// lines.push_back(newInfo);
// }
//}

return lines;
}
}



//使用读取的的文件的所有行初始化Lrc处理器
LrcProcessor::LrcProcessor(vector<SStringW> vecLines, bool bDealOffset)
{
m_bDealOffset = bDealOffset;
m_strTitle = L"";
m_strArtist = L"";
m_strAlbum = L"";
m_strEditor = L"";
m_nOffset = 0;

m_bIsLrcFileValid = true;

//遍历处理所有行
for(auto iter = vecLines.begin(); iter != vecLines.end(); iter++)
{
if( false == DealOneLine( *iter ) )
{
m_bIsLrcFileValid = false;
return;
}
}

//对所有歌词进行排序
sort(m_vecNeteaseLrc.begin(), m_vecNeteaseLrc.end());

//去除重复的空行,连续出现多个空行时,保留第一个空行的时间
bool isLastLineEmpty = false;
for(auto iter = m_vecNeteaseLrc.begin(); iter != m_vecNeteaseLrc.end(); )
{
if(iter->m_strLine.GetLength() == 0)
{
if(isLastLineEmpty)
{
iter = m_vecNeteaseLrc.erase(iter);
}
else
{
iter++;
isLastLineEmpty = true;
}
}
else
{
iter++;
isLastLineEmpty = false;
}
}

//对整体的时间进行偏移
if(m_bDealOffset)
{
for(auto iter = m_vecNeteaseLrc.begin(); iter != m_vecNeteaseLrc.end(); iter++)
{
iter->m_nmSesonds = iter->m_nmSesonds + m_nOffset < 0 ? 0 : iter->m_nmSesonds + m_nOffset;
}
}
}

//当前歌词文件是否有效
bool LrcProcessor::IsLrcFileValid()
{
return m_bIsLrcFileValid;
}

//获得网易云音乐支持的格式
vector<TimeLineInfo> LrcProcessor::GetNeteaseLrc()
{
return m_vecNeteaseLrc;
}

//生成Lrc文件
bool LrcProcessor::GenerateNeteaseLrcFile(SStringW strFilePath)
{
vector<SStringW> vecLines;

WCHAR szTimeBuf[MAX_BUFFER_SIZE/2];
for(auto iter = m_vecNeteaseLrc.begin(); iter != m_vecNeteaseLrc.end(); iter++)
{
int ms = iter->m_nmSesonds;

szTimeBuf[0] = L'\0';
int minutes = ms/60000;
ms = ms%60000;
double seconds = ms*1.0/1000;
_stprintf(szTimeBuf,_T("[%02d:%06.3lf]"),minutes,seconds);

SStringW newLine = szTimeBuf;
newLine.Append( iter->m_strLine );
newLine.Append(L"\n");

vecLines.push_back(newLine);
}

return FileOperator::WriteToUtf8File( strFilePath.GetBuffer(1), vecLines);
}

//处理1行
bool LrcProcessor::DealOneLine(SStringW strLine)
{
int nPos1 = -1;
int nPos2 = -1;
if( -1 != strLine.Find(L"[ti:"))//标题
{
nPos1 = strLine.Find(L":");
nPos2 = strLine.Find(L"]");
m_strTitle = strLine.Left(nPos2);
m_strTitle = m_strTitle.Right(m_strTitle.GetLength()-1 - nPos1);
}
else if( -1 != strLine.Find(L"[ar:"))//艺术家
{
nPos1 = strLine.Find(L":");
nPos2 = strLine.Find(L"]");
m_strArtist = strLine.Left(nPos2);
m_strArtist = m_strArtist.Right(m_strArtist.GetLength()-1 - nPos1);
}
else if( -1 != strLine.Find(L"[al:"))//专辑
{
nPos1 = strLine.Find(L":");
nPos2 = strLine.Find(L"]");
m_strAlbum = strLine.Left(nPos2);
m_strAlbum = m_strAlbum.Right(m_strAlbum.GetLength()-1 - nPos1 );
}
else if( -1 != strLine.Find(L"[by:"))//歌词制作者
{
nPos1 = strLine.Find(L":");
nPos2 = strLine.Find(L"]");
m_strEditor = strLine.Left(nPos2);
m_strEditor = m_strEditor.Right(m_strEditor.GetLength()-1 - nPos1);
}
else if( -1 != strLine.Find(L"[offset:"))
{
nPos1 = strLine.Find(L":");
nPos2 = strLine.Find(L"]");
SStringW m_strOffset = strLine.Left(nPos2);
m_strOffset = m_strOffset.Right(m_strOffset.GetLength()-1 - nPos1);
m_nOffset = _wtoi( m_strOffset.GetBuffer(1));
}
else //其他情况认为是标记时间的行
{
wstring sLine = strLine.GetBuffer(1);
wstring sTimeLabel = L"";
wstring sLyric = L"";
int nPosColon = -1;
sLyric = sLine.substr( sLine.find_last_of(L']') + 1);
do{
nPos1 = sLine.find_first_of(L'[');
nPosColon = sLine.find_first_of(L':');
nPos2 = sLine.find_first_of(L']');
if(nPos1 ==0 && nPos1 < nPosColon && nPosColon < nPos2) // strLine头还有时间标签
{
sTimeLabel = sLine.substr(nPos1, nPos2+1);

TimeLineInfo timeInfoLine(SStringW( (sTimeLabel + sLyric).c_str()));

m_vecNeteaseLrc.push_back(timeInfoLine); //添加一行到网易云歌词

sLine = sLine.substr(nPos2+1); //去除前面标签
}
else
break;
}
while( true);
}

return true;
}

//按歌词时间升序比较
bool LrcProcessor::CompareWithIncreaceTime(const TimeLineInfo & L1, const TimeLineInfo & L2)
{
return L1.m_nmSesonds < L2.m_nmSesonds;
}

//bool m_bDealOffset; /* 表示是否处理LRC文件中的时间偏移 */
//SStringW m_strTitle; /* 歌曲标题 */
//SStringW m_strArtist; /* 艺术家 */
//SStringW m_strAlbum; /* 专辑 */
//SStringW m_strEditor; /* 编辑的人 */
//int m_nOffset; /* 时间偏移量,为正数表示整体提前 */

//vector<TimeLineInfo> m_vecNeteaseLrc; /* 储存用于网易云的歌词信息 */

//bool m_bIsLrcFileValid; /* 表示歌词文件是否有效 */
42 changes: 42 additions & 0 deletions BesLyric/entity/LyricPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,49 @@ class LyricPlayer
};


class LrcProcessor{
public:
//使用读取的的文件的所有行初始化Lrc处理器
LrcProcessor(vector<SStringW> vecLines, bool bDealOffset = true);

//当前歌词文件是否有效
bool IsLrcFileValid();

//获得网易云音乐支持的格式
vector<TimeLineInfo> GetNeteaseLrc();

//生成Lrc文件
bool GenerateNeteaseLrcFile(SStringW strFilePath);

private:
//处理1行
bool DealOneLine(SStringW strLine);

//按歌词时间升序比较
bool CompareWithIncreaceTime(const TimeLineInfo & L1, const TimeLineInfo & L2);

private:
bool m_bDealOffset; /* 表示是否处理LRC文件中的时间偏移 */
SStringW m_strTitle; /* 歌曲标题 */
SStringW m_strArtist; /* 艺术家 */
SStringW m_strAlbum; /* 专辑 */
SStringW m_strEditor; /* 编辑的人 */
int m_nOffset; /* 时间偏移量,为正数表示整体提前 */

vector<TimeLineInfo> m_vecNeteaseLrc; /* 储存用于网易云的歌词信息 */

bool m_bIsLrcFileValid; /* 表示歌词文件是否有效 */
};


/*
* @brief 储存处理一行歌词文件; 处理lrc文件(带时间轴的歌词文件)的辅助类
*/
class TimeLineInfo
{
public:
TimeLineInfo(){}//无参构造函数

TimeLineInfo(SStringT timeLine)
{
//初始化类的基本成员的信息
Expand All @@ -107,6 +144,11 @@ class TimeLineInfo
else
m_bIsEmptyLine = false;
}

bool operator < (const TimeLineInfo &m)const {
return m_nmSesonds < m.m_nmSesonds;
}

private:
//从时间标签字符串得到对应的毫秒时间
int TimeStringToMSecond(LPCTSTR timeStr, int length)
Expand Down
Binary file modified BesLyric/res/resource.h
Binary file not shown.
Binary file modified BesLyric/res/soui_res.rc2
Binary file not shown.
3 changes: 2 additions & 1 deletion BesLyric/uires/xml/dlg_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ translucent="0"
<imgbtn name="btn_select_music_1" pos="[5,{0,-5,@30" skin="app_button" class="normalbtn">选择</imgbtn>

<text pos="5,[5,@80,@30">选择歌词:</text>
<edit pos="[5,{0,-60,@30" font="adding:-1" readonly="1" name="edit_lyric_1" cueText=" 拖放文件到这里 或 点击“选择”"></edit>
<edit pos="[5,{0,-115,@30" font="adding:-1" readonly="1" name="edit_lyric_1" cueText=" 拖放文件到这里 或 点击“选择”"></edit>
<imgbtn name="btn_edit_origin_lyric" pos="[5,{0,@50,@30" tip="编辑后点击“载入最新”按钮后生效" skin="app_button" class="normalbtn">编辑</imgbtn>
<imgbtn name="btn_select_lyric_1" pos="[5,{0,-5,@30" skin="app_button" class="normalbtn">选择</imgbtn>

<text pos="5,[5,@80,@30">输出目录:</text>
Expand Down
Loading

0 comments on commit c42e0e3

Please sign in to comment.