www.pudn.com > LECTEUR-MP3.zip > MusGestDoc.cpp


// MusGestDoc.cpp : implementation of the CMusGestDoc class 
// 
 
#include "stdafx.h" 
#include "MusGest.h" 
 
#include "MusGestDoc.h" 
#include "MainFrm.h" 
#include "math.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CMusGestDoc 
 
IMPLEMENT_DYNCREATE(CMusGestDoc, CDocument) 
 
BEGIN_MESSAGE_MAP(CMusGestDoc, CDocument) 
	//{{AFX_MSG_MAP(CMusGestDoc) 
		// NOTE - the ClassWizard will add and remove mapping macros here. 
		//    DO NOT EDIT what you see in these blocks of generated code! 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMusGestDoc construction/destruction 
//------------------------------------------------------------------------------------- 
CMusGestDoc::CMusGestDoc() 
{ 
	m_pParentView	= NULL; 
 
	//Init du lecteur 
	m_pChannel = 0; 
	m_pSound = NULL; 
	m_pSystem = NULL; 
	m_strInPlayFilename = ""; 
	m_fVol = 0.5; 
	m_fBal = 0; 
 
	FMOD_System_Create(&m_pSystem); 
	FMOD_System_Init(m_pSystem, 32, FMOD_INIT_NORMAL, NULL); 
} 
 
//------------------------------------------------------------------------------------- 
CMusGestDoc::~CMusGestDoc() 
{ 
	if (m_pSound) 
		FMOD_Sound_Release(m_pSound); 
 
	FMOD_System_Close(m_pSystem); 
    FMOD_System_Release(m_pSystem); 
} 
 
//------------------------------------------------------------------------------------- 
int CMusGestDoc::PlayFile(CRepFile * pFile) 
{ 
	if (pFile) 
	{ 
		CString strFileName = pFile->GetFileRep()->GetRepPath() + "\\" ; 
		strFileName += pFile->GetFileName(); 
 
		int paused; 
		FMOD_Channel_GetPaused(m_pChannel, &paused); 
 
		//si pause et que on fait play sur le meme fichier 
		//il faut continué de jouer 
		if ((paused == 1) && (strFileName == m_strInPlayFilename)) 
		{ 
			FMOD_Channel_SetVolume(m_pChannel, m_fVol); 
			FMOD_Channel_SetPaused(m_pChannel, 0); 
			return SOUND_IN_PLAY_AFTER_PAUSE; 
		} 
		else 
		{ 
			//On detruit m_pSound si il existe 
			if (m_pSound)  
			{ 
				FMOD_Sound_Release(m_pSound); 
				m_pSound = NULL; 
			} 
 
			//Créé et joue le son 
			if (FMOD_System_CreateStream(m_pSystem, strFileName, FMOD_SOFTWARE , 0, &m_pSound) == FMOD_OK) 
			{ 
				if (FMOD_System_PlaySound(m_pSystem, FMOD_CHANNEL_FREE, m_pSound, 0, &m_pChannel) == FMOD_OK) 
				{ 
					UINT iLength = 0; 
					 
					FMOD_Channel_SetPan(m_pChannel, m_fBal); 
					FMOD_Channel_SetVolume(m_pChannel, m_fVol); 
					FMOD_Sound_GetLength(m_pSound,&iLength,FMOD_TIMEUNIT_MS); 
 
					//Met a jour la duréé du fichier 
					//methode plus présise 
					if (iLength > 0) 
					{ 
						float iTimeTmp = (float)((float)iLength/1000); 
						int iTimeMin = (int)abs(iTimeTmp/60); 
						float iTmp = ((float)iTimeTmp/60) - iTimeMin; 
						int iTimeSec = (int)(iTmp * 60); 
 
						CAudioFile * pAudioFile = (CAudioFile*)pFile; 
						 
						if (pAudioFile) 
							pAudioFile->SetFileLenght(CTimeSpan(0,0,iTimeMin,iTimeSec)); 
					} 
 
					m_strInPlayFilename = strFileName; 
					return SOUND_IN_PLAY; 
				} 
			} 
		} 
	} 
 
	return SOUND_STOPPED; 
} 
 
//------------------------------------------------------------------------------------- 
int CMusGestDoc::PauseFile(CRepFile * pFile) 
{ 
	int paused, playing; 
 
	FMOD_Channel_IsPlaying(m_pChannel, &playing); 
	if (!playing) 
		return SOUND_STOPPED; 
 
	FMOD_Channel_GetPaused(m_pChannel, &paused); 
	 
	if (pFile) 
	{ 
		CString strFileName = pFile->GetFileRep()->GetRepPath() + "\\" ; 
		strFileName += pFile->GetFileName(); 
 
		if (strFileName == m_strInPlayFilename) 
		{ 
			FMOD_Channel_SetPaused(m_pChannel, !paused); 
 
			if (paused == 1) 
				return SOUND_IN_PLAY; 
			else 
				return SOUND_IN_PAUSE; 
		} 
	} 
 
	return SOUND_OTHER_IN_PLAY; 
} 
 
//------------------------------------------------------------------------------------- 
int CMusGestDoc::StopFile(CRepFile * pFile) 
{ 
	int iPlay; 
	FMOD_Channel_IsPlaying(m_pChannel, &iPlay); 
 
	if (pFile) 
	{ 
		if ((iPlay) && (m_pSound)) 
		{ 
			CString strFileName = pFile->GetFileRep()->GetRepPath() + "\\" ; 
			strFileName += pFile->GetFileName(); 
 
			if (strFileName == m_strInPlayFilename) 
			{ 
				FMOD_Sound_Release(m_pSound); 
				m_pSound = NULL; 
				m_strInPlayFilename = ""; 
				return SOUND_STOPPED; 
			} 
 
			return SOUND_OTHER_IN_PLAY; 
		} 
		else 
			return SOUND_STOPPED; 
	} 
 
	return SOUND_STOPPED; 
} 
 
//------------------------------------------------------------------------------------- 
int CMusGestDoc::DownVolume() 
{ 
	float fVol = 0; 
 
	FMOD_Channel_GetVolume(m_pChannel, &fVol); 
 
	if (fVol > 0.1) 
		fVol = (float)(fVol - 0.1); 
	else 
		fVol = 0; 
 
	return ChangeVolume(fVol); 
} 
 
//------------------------------------------------------------------------------------- 
int CMusGestDoc::UpVolume() 
{	 
	float fVol = 0; 
 
	FMOD_Channel_GetVolume(m_pChannel, &fVol); 
 
	if (fVol < 0.9) 
		fVol = (float)(fVol + 0.1); 
	else  
		fVol = 1; 
 
	return ChangeVolume(fVol); 
} 
 
//------------------------------------------------------------------------------------- 
int CMusGestDoc::ChangeVolume(float fVol) 
{ 
	FMOD_Channel_SetVolume(m_pChannel, fVol); 
	int iVol = (int)(fVol*100); 
	m_fVol = fVol; 
 
	return iVol; 
} 
 
//-------------------------------------------------------------------------------------- 
int CMusGestDoc::GetVolume() 
{ 
	float fVol = -1; 
 
	if (m_pChannel) 
		FMOD_Channel_GetVolume(m_pChannel, &fVol); 
	else  
		return (int)(m_fVol *100); 
 
	return (int)(fVol * 100); 
} 
 
//-------------------------------------------------------------------------------------- 
int CMusGestDoc::ChangeBalance(int iBal)//entre -1 et 1 
{ 
	m_fBal = ((float)iBal/100); 
 
	if (m_pChannel) 
		FMOD_Channel_SetPan(m_pChannel, m_fBal); 
 
	return iBal; 
} 
 
//-------------------------------------------------------------------------------------- 
int CMusGestDoc::GetBalance() 
{ 
	if (m_pChannel) 
		FMOD_Channel_GetPan(m_pChannel, &m_fBal); 
 
	return (int)(m_fBal*100); 
} 
 
//-------------------------------------------------------------------------------------- 
void CMusGestDoc::SetMute(bool bMute) 
{ 
	if (m_pChannel) 
		FMOD_Channel_SetMute(m_pChannel, bMute); 
} 
 
//-------------------------------------------------------------------------------------- 
bool CMusGestDoc::AvancePos() 
{ 
	if (m_pChannel) 
	{ 
		unsigned int iPosMs = 0; 
 
		FMOD_Channel_GetPosition(m_pChannel,&iPosMs,FMOD_TIMEUNIT_MS); 
		FMOD_Channel_SetPosition(m_pChannel,iPosMs+5000,FMOD_TIMEUNIT_MS);//ajoute 5s 
		return true; 
	} 
 
	return false; 
} 
 
//-------------------------------------------------------------------------------------- 
bool CMusGestDoc::ReculePos() 
{ 
	if (m_pChannel) 
	{ 
		unsigned int iPosMs = 0; 
 
		FMOD_Channel_GetPosition(m_pChannel,&iPosMs,FMOD_TIMEUNIT_MS); 
		FMOD_Channel_SetPosition(m_pChannel,iPosMs-5000,FMOD_TIMEUNIT_MS);//enlève 5s 
		return true; 
	} 
 
	return false; 
} 
 
//-------------------------------------------------------------------------------------- 
bool CMusGestDoc::ChangePos(int iPosMs) 
{ 
	if (m_pChannel) 
	{ 
		FMOD_Channel_SetPosition(m_pChannel,iPosMs,FMOD_TIMEUNIT_MS); 
		return true; 
	} 
 
	return false; 
} 
 
//-------------------------------------------------------------------------------------- 
BOOL CMusGestDoc::OnNewDocument() 
{ 
	if (!CDocument::OnNewDocument()) 
		return FALSE; 
 
	// TODO: add reinitialization code here 
	// (SDI documents will reuse this document) 
 
	return TRUE; 
} 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CMusGestDoc serialization 
 
void CMusGestDoc::Serialize(CArchive& ar) 
{ 
	if (ar.IsStoring()) 
	{ 
		// TODO: add storing code here 
	} 
	else 
	{ 
		// TODO: add loading code here 
	} 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMusGestDoc diagnostics 
 
#ifdef _DEBUG 
void CMusGestDoc::AssertValid() const 
{ 
	CDocument::AssertValid(); 
} 
 
void CMusGestDoc::Dump(CDumpContext& dc) const 
{ 
	CDocument::Dump(dc); 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CMusGestDoc commands 
/////////////////////////////////////////////////////////////////////////////