Sign in to follow this  
d000hg

Loading .wav to DirectSoundBuffer using DirectMusicLoader

Recommended Posts

IDirectSoundBuffer8 *DirectSoundWaveFileLoader::LoadWaveFile(WCHAR *path)
{
	IDirectSoundBuffer8 *buffer=0;
	HRESULT hr=E_FAIL;
	if(!mLoader)
		return 0;
	//mLoader is a DirectMusicLoader8 object
	hr=mLoader->LoadObjectFromFile(CLSID_DirectSoundWave,IID_IDirectSoundBuffer,path,(void **)(&buffer));
	if(FAILED(hr))
	{
		return 0;
	}
	return buffer;
}


I've recently been told something like this should work - that it's possible to load a .wav file into a DirectSound(8) buffer instead of a DirectMusic segment. This would let me use DirectSound without having to parse/load wave files myself, as well as supporting compressed .wav formats. GetObjectFromFile returns E_NOINTERFACE (or whatever the exact error is to say the interface isn't supported) so what's wrong with my code?

Share this post


Link to post
Share on other sites
If you're going to use DirectMusic to load the file, why not use DirectMusic to play it, as well?

If you need to change the sound buffer parameters, you can create an audio path and obtain the sound buffer from the audio path. Check out the second DirectMusic tutorial in the SDK docs for an example.

Share this post


Link to post
Share on other sites
why not just do something like this?


bool Audio_Init(HWND hWnd) {
//Create the DirectAudio performance object
if (CoCreateInstance(CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC,
IID_IDirectMusicPerformance8, (void**) &m_pDirectAudioPerformance) != S_OK) {
Error("DirectAudio: Failed to create the DirectAudio perfomance object.");
return false;
}

//Create the DirectAudio loader object
if (CoCreateInstance(CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC,
IID_IDirectMusicLoader8, (void**) &m_pDirectAudioLoader) != S_OK) {
Error("DirectAudio: Failed to create the DirectAudio loader object.");
return false;
}

//Initialise the performance object
if (FAILED(m_pDirectAudioPerformance->InitAudio(NULL, NULL, hWnd, DMUS_APATH_SHARED_STEREOPLUSREVERB,
64, DMUS_AUDIOF_ALL, NULL))) {
Error("DirectAudio: Failed to initialise the DirectAudio perfomance object.");
return false;
}

//Get the our applications "sounds" directory.
CHAR strSoundPath[MAX_PATH];
GetCurrentDirectory(MAX_PATH, strSoundPath);

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

//Set the search directory.
if (FAILED(m_pDirectAudioLoader->SetSearchDirectory(GUID_DirectMusicAllTypes, wstrSoundPath, FALSE))) {
Error("DirectAudio: Failed to set the search directory %s", strSoundPath);
return false;
}

return true;
}

/*==================
CSound
==================*/

CSound::CSound() {
m_pAudioPerformance = NULL;
m_pAudioLoader = NULL;
m_pSegment = NULL;
m_pGraph = NULL;
m_pMediaControl = NULL;
m_pMediaPosition = NULL;
m_enumFormat = Unknown;
}

void CSound::InitialiseForWavMidi(IDirectMusicPerformance8* pDirectAudioPerformance, IDirectMusicLoader8* pDirectAudioLoader) {
m_pAudioPerformance = pDirectAudioPerformance;
m_pAudioLoader = pDirectAudioLoader;

m_enumFormat = WavMidi;
}

void CSound::InitialiseForMP3() {
CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IGraphBuilder, (void**)&m_pGraph);

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

m_enumFormat = MP3;
}


CSound::~CSound() {
Stop();
}

bool CSound::LoadSound(const char* szSoundFileName) {
WCHAR wstrSoundPath[MAX_PATH];
CHAR strSoundPath[MAX_PATH];

switch(m_enumFormat) {
case MP3:
//Get the our applications "sounds" directory.
GetCurrentDirectory(MAX_PATH, strSoundPath);
strcat(strSoundPath, "/");
strcat(strSoundPath, szSoundFileName);

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

m_pGraph->RenderFile(wstrSoundPath, NULL);
break;
case WavMidi:
GetCurrentDirectory(MAX_PATH, strSoundPath);
strcat(strSoundPath, "/");
strcat(strSoundPath, szSoundFileName);

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

//Load a sound
m_pAudioLoader->LoadObjectFromFile(CLSID_DirectMusicSegment, IID_IDirectMusicSegment8, wstrSoundPath, (void**) &m_pSegment);

m_pSegment->Download(m_pAudioPerformance);
break;
default:
return false;
}

return true;
}

bool CSound::Play(DWORD dwNumOfRepeats) {
switch(m_enumFormat) {
case MP3:
//Make sure that we are at the start of the stream
m_pMediaPosition->put_CurrentPosition(0);

//Play mp3
m_pMediaControl->Run();
break;
case WavMidi:
//Set the number of times the sound repeats
m_pSegment->SetRepeats(dwNumOfRepeats); //To loop the sound forever, pass in DMUS_SEG_REPEAT_INFINITE

//Play the loaded sound
m_pAudioPerformance->PlaySegmentEx(m_pSegment, NULL, NULL, 0, 0, NULL, NULL, NULL);
break;
default:
return false;
}

return true;
}

bool CSound::Stop()
{
switch(m_enumFormat)
{
case MP3:
m_pMediaControl->Stop();
break;
case WavMidi:
//Stop the loaded sound
m_pAudioPerformance->StopEx(m_pSegment, 0, 0);
break;
default:
return false;
}

return true;
}

bool CSound::IsPlaying()
{
switch(m_enumFormat)
{
case MP3:
REFTIME refPosition;
REFTIME refDuration;

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

if(refPosition < refDuration)
{
return true;
}
else
{
return false;
}
break;
case WavMidi:
if(m_pAudioPerformance->IsPlaying(m_pSegment, NULL) == S_OK)
{
return true;
}
else
{
return false;
}
break;
default:
return false;
}
}


Share this post


Link to post
Share on other sites
DirectMusic is geared towards music - it can play wave files but you have to buy into this audiopath methodology which isn't useful to me and makes it more complicated. I want to use DirectSound; I was just hoping I could avoid writing a .wav loader by using DirectMusic functions...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this