2011-05-21 18:17:37 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2011 Birunthan Mohanathas (www.poiru.net)
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2012-01-23 06:36:15 +00:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2011-05-21 18:17:37 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StdAfx.h"
|
|
|
|
#include "Player.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Constructor.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
CPlayer::CPlayer() :
|
2011-06-17 19:07:06 +00:00
|
|
|
m_Initialized(false),
|
2011-05-21 18:17:37 +00:00
|
|
|
m_InstanceCount(),
|
2011-06-17 19:07:06 +00:00
|
|
|
m_UpdateCount(),
|
|
|
|
m_TrackCount(),
|
2011-09-08 12:42:03 +00:00
|
|
|
m_Measures(),
|
2011-05-21 18:17:37 +00:00
|
|
|
m_State(),
|
2012-03-12 12:01:12 +00:00
|
|
|
m_Number(1),
|
|
|
|
m_Year(),
|
2011-09-11 08:22:07 +00:00
|
|
|
m_Shuffle(false),
|
|
|
|
m_Repeat(false),
|
2011-05-21 18:17:37 +00:00
|
|
|
m_Duration(),
|
|
|
|
m_Position(),
|
|
|
|
m_Rating(),
|
2011-06-19 14:58:48 +00:00
|
|
|
m_Volume(),
|
|
|
|
m_InternetThread()
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-11-05 17:52:53 +00:00
|
|
|
// Get temporary file for cover art
|
|
|
|
WCHAR buffer[MAX_PATH];
|
|
|
|
GetTempPath(MAX_PATH, buffer);
|
2011-11-19 12:06:51 +00:00
|
|
|
GetTempFileName(buffer, L"jpg", 0, buffer);
|
2011-11-05 17:52:53 +00:00
|
|
|
m_TempCoverPath = buffer;
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Destructor.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
CPlayer::~CPlayer()
|
|
|
|
{
|
2011-11-06 08:42:56 +00:00
|
|
|
DeleteFile(m_TempCoverPath.c_str());
|
|
|
|
|
2011-07-01 20:34:12 +00:00
|
|
|
if (m_InternetThread)
|
|
|
|
{
|
|
|
|
TerminateThread(m_InternetThread, 0);
|
|
|
|
}
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-06-17 19:07:06 +00:00
|
|
|
** Called during initialization of main measure.
|
2011-05-21 18:17:37 +00:00
|
|
|
**
|
|
|
|
*/
|
2011-06-17 19:07:06 +00:00
|
|
|
void CPlayer::AddInstance()
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
++m_InstanceCount;
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-06-17 19:07:06 +00:00
|
|
|
** Called during destruction of main measure.
|
2011-05-21 18:17:37 +00:00
|
|
|
**
|
|
|
|
*/
|
2011-06-17 19:07:06 +00:00
|
|
|
void CPlayer::RemoveInstance()
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-07-10 09:30:51 +00:00
|
|
|
m_UpdateCount = 0;
|
|
|
|
|
2011-06-17 19:07:06 +00:00
|
|
|
if (--m_InstanceCount == 0)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
delete this;
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-06-17 19:07:06 +00:00
|
|
|
** Called during initialization of any measure.
|
2011-05-21 18:17:37 +00:00
|
|
|
**
|
|
|
|
*/
|
2011-09-08 12:42:03 +00:00
|
|
|
void CPlayer::AddMeasure(INT type)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-09-08 12:42:03 +00:00
|
|
|
m_Measures |= type;
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-06-17 19:07:06 +00:00
|
|
|
** Called during update of main measure.
|
2011-05-21 18:17:37 +00:00
|
|
|
**
|
|
|
|
*/
|
2011-06-17 19:07:06 +00:00
|
|
|
void CPlayer::UpdateMeasure()
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
if (++m_UpdateCount == m_InstanceCount)
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2011-06-17 19:07:06 +00:00
|
|
|
UpdateData();
|
|
|
|
m_UpdateCount = 0;
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-19 14:58:48 +00:00
|
|
|
/*
|
2011-11-05 17:52:53 +00:00
|
|
|
** Default implementation for getting cover.
|
2011-06-19 14:58:48 +00:00
|
|
|
**
|
|
|
|
*/
|
2011-11-05 17:52:53 +00:00
|
|
|
void CPlayer::FindCover()
|
2011-06-19 14:58:48 +00:00
|
|
|
{
|
2011-11-10 12:09:50 +00:00
|
|
|
TagLib::FileRef fr(m_FilePath.c_str(), false);
|
2011-11-05 17:52:53 +00:00
|
|
|
if (!fr.isNull() && CCover::GetEmbedded(fr, m_TempCoverPath))
|
2011-06-19 14:58:48 +00:00
|
|
|
{
|
2011-11-05 17:52:53 +00:00
|
|
|
m_CoverPath = m_TempCoverPath;
|
2011-06-19 14:58:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-11-05 17:52:53 +00:00
|
|
|
std::wstring trackFolder = CCover::GetFileFolder(m_FilePath);
|
2011-06-19 14:58:48 +00:00
|
|
|
|
2011-11-05 17:52:53 +00:00
|
|
|
if (!CCover::GetLocal(L"cover", trackFolder, m_CoverPath) &&
|
|
|
|
!CCover::GetLocal(L"folder", trackFolder, m_CoverPath))
|
2011-06-19 14:58:48 +00:00
|
|
|
{
|
2011-11-05 17:52:53 +00:00
|
|
|
// Nothing found
|
|
|
|
m_CoverPath.clear();
|
2011-06-19 14:58:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Default implementation for getting lyrics.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void CPlayer::FindLyrics()
|
|
|
|
{
|
2011-07-01 20:34:12 +00:00
|
|
|
if (!m_InternetThread)
|
2011-06-19 14:58:48 +00:00
|
|
|
{
|
|
|
|
m_Lyrics.clear();
|
|
|
|
|
|
|
|
unsigned int id;
|
|
|
|
HANDLE thread = (HANDLE)_beginthreadex(NULL, 0, LyricsThreadProc, this, 0, &id);
|
|
|
|
if (thread)
|
|
|
|
{
|
|
|
|
m_InternetThread = thread;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
RmLog(LOG_DEBUG, L"NowPlaying.dll: Failed to start lyrics thread");
|
2011-06-19 14:58:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Thread to download lyrics.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
unsigned __stdcall CPlayer::LyricsThreadProc(void* pParam)
|
|
|
|
{
|
|
|
|
CPlayer* player = (CPlayer*)pParam;
|
|
|
|
|
|
|
|
std::wstring lyrics;
|
|
|
|
bool found;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
UINT beforeCount = player->GetTrackCount();
|
|
|
|
found = CLyrics::GetFromInternet(player->m_Artist, player->m_Title, lyrics);
|
|
|
|
UINT afterCount = player->GetTrackCount();
|
|
|
|
|
|
|
|
if (beforeCount == afterCount)
|
|
|
|
{
|
|
|
|
// We're on the same track
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-07-01 20:34:12 +00:00
|
|
|
// Track changed, try again
|
2011-06-19 14:58:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
player->m_Lyrics = lyrics;
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseHandle(player->m_InternetThread);
|
|
|
|
player->m_InternetThread = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-21 18:17:37 +00:00
|
|
|
/*
|
2011-06-17 19:07:06 +00:00
|
|
|
** Clear track information.
|
2011-05-21 18:17:37 +00:00
|
|
|
**
|
|
|
|
*/
|
2011-06-17 19:07:06 +00:00
|
|
|
void CPlayer::ClearData()
|
2011-05-21 18:17:37 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
m_State = STATE_STOPPED;
|
2011-06-17 19:07:06 +00:00
|
|
|
m_Artist.clear();
|
|
|
|
m_Album.clear();
|
|
|
|
m_Title.clear();
|
2011-06-19 14:58:48 +00:00
|
|
|
m_Lyrics.clear();
|
2011-06-17 19:07:06 +00:00
|
|
|
m_FilePath.clear();
|
|
|
|
m_CoverPath.clear();
|
2011-09-11 08:22:07 +00:00
|
|
|
m_Duration = 0;
|
|
|
|
m_Position = 0;
|
|
|
|
m_Rating = 0;
|
2012-03-12 12:01:12 +00:00
|
|
|
m_Number = 0;
|
|
|
|
m_Year = 0;
|
2011-05-21 18:17:37 +00:00
|
|
|
}
|