⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sound.cpp

📁 泡泡堂单机版源码 内容: 模拟泡泡堂的一个小作品
💻 CPP
字号:
#include "Sound.h"

#define	SAFE_RELEASE(p)			{if (p) { p->Release();	p = NULL;}}

CSound::CSound()
{
		CoInitialize(NULL);
		m_pDirectAudioPerformance = NULL;
		m_pDirectAudioLoader = NULL;
}

CSound::~CSound()
{
	//	stop
	m_pDirectAudioPerformance->Stop(NULL, NULL, 0, 0);
	//	close
	m_pDirectAudioPerformance->CloseDown();

	SAFE_RELEASE(m_pDirectAudioPerformance);
	SAFE_RELEASE(m_pDirectAudioLoader);
	CoUninitialize();
}

bool	CSound::Initialize(HWND	hWnd)
{
	//Create the DirectAudio performance object
	if(CoCreateInstance(CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC, 
		IID_IDirectMusicPerformance8, 
		(void**) &m_pDirectAudioPerformance) != S_OK)
	{
		return false;
	}

	//Create the DirectAudio loader object
	if(CoCreateInstance(CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC, 
		IID_IDirectMusicLoader8, 
		(void**) &m_pDirectAudioLoader) != S_OK)
	{
		return false;
	}

	//Initialise the performance object
	if(FAILED(m_pDirectAudioPerformance->InitAudio(NULL, NULL, hWnd,
		DMUS_APATH_SHARED_STEREOPLUSREVERB,
		64, DMUS_AUDIOF_ALL, NULL)))
	{
		return false;
	}

	//Get the our applications "CSounds" directory.
	CHAR strCSoundPath[MAX_PATH];
	GetCurrentDirectory(MAX_PATH, strCSoundPath);
	strcat(strCSoundPath, "\\Sound");

	//Convert the path to unicode.
	WCHAR wstrCSoundPath[MAX_PATH];
	MultiByteToWideChar(CP_ACP, 0, strCSoundPath, -1, wstrCSoundPath, MAX_PATH);

	//Set the search directory.
	if(FAILED(m_pDirectAudioLoader->SetSearchDirectory(GUID_DirectMusicAllTypes,
		wstrCSoundPath, FALSE)))
	{
		return false;
	}

	CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, (void**)&m_pGraph);

	return true;
}

void CSound::InitialiseWavMidi(CWavMidi** pWavMidi)
{
	(*pWavMidi) = new	CWavMidi(m_pDirectAudioPerformance,m_pDirectAudioLoader);
}


void	CSound::InitialiseMP3(CMP3** pMp3)
{
	(*pMp3) = new	CMP3(m_pGraph);
}


CWavMidi::CWavMidi(IDirectMusicPerformance8* pDirectAudioPerformance, IDirectMusicLoader8* pDirectAudioLoader)
{
	m_pDirectAudioPerformance = pDirectAudioPerformance;
	m_pDirectAudioLoader = pDirectAudioLoader;
	m_pSegment = NULL;
}

CWavMidi::~CWavMidi(void)
{
	Stop();

	SAFE_RELEASE(m_pSegment);
	//	不能删
	//	SAFE_RELEASE(m_pDirectAudioPerformance);
	//	SAFE_RELEASE(m_pDirectAudioLoader);
}

void CWavMidi::Load(const char* szCSoundFileName)
{
	WCHAR wstrCSoundPath[MAX_PATH];
	MultiByteToWideChar(CP_ACP, 0, szCSoundFileName, -1, wstrCSoundPath, MAX_PATH);
	m_pDirectAudioLoader->LoadObjectFromFile(CLSID_DirectMusicSegment, IID_IDirectMusicSegment8,
											wstrCSoundPath, (void**) &m_pSegment);
	m_pSegment->Download(m_pDirectAudioPerformance);
}

void CWavMidi::Play(DWORD dwNumOfRepeats)
{
	m_pSegment->SetRepeats(dwNumOfRepeats);
	m_pDirectAudioPerformance->PlaySegmentEx(m_pSegment, NULL, NULL, 0, 0, NULL, NULL, NULL);
}

void CWavMidi::Stop()
{
	m_pDirectAudioPerformance->StopEx(m_pSegment, 0, 0);
}

bool CWavMidi::IsPlaying()
{
	if(m_pDirectAudioPerformance->IsPlaying(m_pSegment, NULL) == S_OK)
	{
		return true;
	}
	else
	{
		return false;
	}
}

CMP3::CMP3(IGraphBuilder*	pGraph)
{
	m_pGraph = pGraph;

	m_pGraph->QueryInterface(IID_IMediaControl, (void**)&m_pMediaControl);

	m_pGraph->QueryInterface(IID_IMediaPosition, (void**)&m_pMediaPosition);
}

CMP3::~CMP3()
{
	SAFE_RELEASE(m_pMediaPosition);
	SAFE_RELEASE(m_pMediaControl);
//	不能删
//	SAFE_RELEASE(m_pGraph);
}

void	CMP3::Load(const char* szSoundFileName)
{
	CHAR strSoundPath[MAX_PATH];
	WCHAR wstrSoundPath[MAX_PATH];

	GetCurrentDirectory(MAX_PATH, strSoundPath);
	strcat(strSoundPath, "\\Sound\\");
	strcat(strSoundPath, szSoundFileName);

	//Convert the path to unicode.
	MultiByteToWideChar(CP_ACP, 0, strSoundPath, -1, wstrSoundPath, MAX_PATH);

	m_pGraph->RenderFile(wstrSoundPath, NULL);

}

void	CMP3::Play()
{
	//Make sure that we are at the start of the stream
	m_pMediaPosition->put_CurrentPosition(0);
	//Play mp3
	m_pMediaControl->Run();
}

void	CMP3::Stop()
{
	m_pMediaControl->Stop();
}

bool	CMP3::IsPlaying()
{
	REFTIME refPosition;
	REFTIME refDuration;

	m_pMediaPosition->get_CurrentPosition(&refPosition);
	m_pMediaPosition->get_Duration(&refDuration);

	if(refPosition < refDuration)
	{
		return true;
	}
	else
	{
		return false;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -