Archived

This topic is now archived and is closed to further replies.

timmay314

Playing multiple wav files with DirectMusic

Recommended Posts

timmay314    138
I am working on a game where I need to have multiple explosion sounds playing at once. I have background music playing as the primary segment and I have one explosion playing as the secondary segment, but for some reason it refuses to play additional secondary segments. The problem has been traced to IsPlaying() returning S_OK for sounds that shouldn''t be playing. I have a class CSound that''s doing this stuff - here''s some code:
void CSound::LoadFromFile(char *strFile)
{
	SAFE_RELEASE(m_Segment);
	WCHAR wszFile[MAX_PATH];

	MultiByteToWideChar(CP_ACP, 0, strFile, -1, wszFile, MAX_PATH);

	CoCreateInstance(CLSID_DirectMusicSegment, NULL, CLSCTX_INPROC, 
	  IID_IDirectMusicSegment8, (void**)&m_Segment);

	g_pAudioLoader->LoadObjectFromFile(CLSID_DirectMusicSegment,
		IID_IDirectMusicSegment8, wszFile, (void**) &m_Segment);

	m_Segment->Download(g_pAudioPerformance);
}

void CSound::Play(DWORD dwNumOfRepeats)
{
	m_Segment->SetRepeats(dwNumOfRepeats);
	DWORD dwFlags = 0;
	if(!m_bPrimary)
		dwFlags |= DMUS_SEGF_SECONDARY;
	g_pAudioPerformance->PlaySegmentEx(m_Segment, NULL, NULL, dwFlags, 0, NULL, NULL, NULL);
}

// This is returning true if ANY of the explosion sounds are playing

bool CSound::IsPlaying()
{
	return (g_pAudioPerformance->IsPlaying(m_Segment, NULL) == S_OK);
}

// Global functions

void InitExplosionSounds()
{
	CSound* itSound = g_aExpSounds;
	for(; itSound != g_aExpSounds + EXP_SOUND_COUNT; itSound++)
	{
		itSound->LoadFromFile("explosion.wav");
	}
}

// Called to add another explosion sound

void AddExplosionSound()
{
	CSound* itSound = g_aExpSounds;
	for(; itSound != g_aExpSounds + EXP_SOUND_COUNT; itSound++)
	{
 		if(!itSound->IsPlaying())
		{
			itSound->Play(0);
			return;
		}
	}
}
This isn''t very complicated, and I can''t see why it wouldn''t be working. What is going wrong?

Share this post


Link to post
Share on other sites
AndyTX    806
Not entirely sure about your code, but you don''t have to keep multiple buffers of the same thing around in DirectMusic. It will let you play secondary segments over top of one another (rather than in DirectSound where you had to "Duplicate Buffers").

You may need to check the creation flags on your secondary segment for the applicable setting, but it should be fairly obvious.

Share this post


Link to post
Share on other sites