KDvoid SimpleAudioEngine::preloadBackgroundMusic ( const KDchar* szFilePath )
{
	if ( !szFilePath )
	{
		return;
	}

	std::string  sPath = FileUtils::getInstance ( )->fullPathForFilename ( szFilePath );
	KDuint		 uID   = _Hash ( sPath.c_str ( ) );

	this->stopBackgroundMusic ( true );

	SoundList::iterator it = l_aMusicList.find ( uID );
	if ( it == l_aMusicList.end ( ) )
	{
		XMSound*  pSound = xmSoundOpen ( sPath.c_str ( ), XM_SOUND_NORMAL );

		if ( pSound )
		{
			xmSoundSetCallback ( pSound, background_callBack );

			l_aMusicList.insert ( Sound ( uID, pSound ) );			
		}
	}
}
KDuint SimpleAudioEngine::playEffect ( const KDchar* szFilePath, KDbool bLoop, KDfloat fPitch, KDfloat fPan, KDfloat fGain )
{
	if (!szFilePath)
	{
		return 0;
	}

	std::string  sPath = FileUtils::getInstance()->fullPathForFilename(szFilePath);
	KDuint		 uRet = _Hash(sPath.c_str());

	preloadEffect(szFilePath);

	SoundList::iterator it = l_aEffectList.find(uRet);
	if (it != l_aEffectList.end())
	{
		xmSoundSetRepeat(it->second, bLoop);		
		xmSoundSetVolume(it->second, fGain);
		xmSoundSetPitch(it->second, fPitch);
		xmSoundSetPan(it->second, fPan);
		xmSoundRewind(it->second);
	}
	else
	{
		uRet = 0;
	}

	return uRet;
}
KDvoid SimpleAudioEngine::stopBackgroundMusic ( KDbool bReleaseData )
{
	SoundList::iterator it = l_aMusicList.find ( l_uCurrentMusic );

	if ( it != l_aMusicList.end ( ) )
	{
		if ( bReleaseData )
		{
			if ( it->second )
			{
				xmSoundClose ( it->second );
			}

			l_uCurrentMusic = 0;
			l_aEffectList.erase ( l_uCurrentMusic );
		}
		else
		{
			if ( it->second )
			{
				xmSoundStop ( it->second );
			}
		}
	}

	l_bIsBackgroundPlaying = KD_FALSE;
}
KDvoid SimpleAudioEngine::setEffectVolume ( KDuint uID, KDfloat fVolume )
{
	SoundList::iterator it = l_aEffectList.find(uID);
	if (it != l_aEffectList.end())
	{
		xmSoundSetVolume(it->second, fVolume);
	}
}
KDvoid SimpleAudioEngine::resumeEffect ( KDuint uSoundId )
{
	SoundList::iterator it = l_aEffectList.find(uSoundId);
	if (it != l_aEffectList.end())
	{
		xmSoundResume(it->second);
	}
}
KDvoid SimpleAudioEngine::setEffectPitch(KDuint uID, KDfloat fPitch)
{
	SoundList::iterator it = l_aEffectList.find(uID);
	if (it != l_aEffectList.end())
	{
		xmSoundSetPitch(it->second, fPitch);
	}
}
KDvoid SimpleAudioEngine::unloadEffect ( KDuint uSoundId )
{
	SoundList::iterator it = l_aEffectList.find(uSoundId);
	if (it != l_aEffectList.end())
	{
		xmSoundClose(it->second);
		l_aEffectList.erase(uSoundId);
	}
}
KDfloat SimpleAudioEngine::getEffectPitch ( KDuint uID )
{
	SoundList::iterator it = l_aEffectList.find(uID);
	if (it != l_aEffectList.end())
	{
		return xmSoundGetPitch(it->second);
	}

	return 0;
}
KDvoid SimpleAudioEngine::stopAllEffects ( KDvoid )
{
	for (SoundList::iterator it = l_aEffectList.begin(); it != l_aEffectList.end(); it++)
	{
		if (it->second)
		{
			xmSoundStop(it->second);
		}
	}
}
KDvoid SimpleAudioEngine::playEffect ( KDuint uSoundId, KDbool bLoop, KDfloat fPitch, KDfloat fPan, KDfloat fGain )
{
	SoundList::iterator it = l_aEffectList.find(uSoundId);
	if (it != l_aEffectList.end())
	{
		xmSoundSetRepeat(it->second, bLoop);
		xmSoundSetVolume(it->second, fGain);
		xmSoundSetPitch(it->second, fPitch);
		xmSoundSetPan(it->second, fPan);
		xmSoundRewind(it->second);
	}
}
KDvoid SimpleAudioEngine::resumeBackgroundMusic ( KDvoid )
{
	SoundList::iterator it = l_aMusicList.find ( l_uCurrentMusic );

	if ( it != l_aMusicList.end ( ) )
	{
		if ( it->second )
		{
			xmSoundResume ( it->second );
		}
	}
}
Example #12
0
SoundBuffer::~SoundBuffer()
{
    // To prevent the iterator from becoming invalid, move the entire buffer to another
    // container. Otherwise calling resetBuffer would result in detachSound being
    // called which removes the sound from the internal list.
    SoundList sounds;
    sounds.swap(m_sounds);

    // Detach the buffer from the sounds that use it (to avoid OpenAL errors)
    for (SoundList::const_iterator it = sounds.begin(); it != sounds.end(); ++it)
        (*it)->resetBuffer();
}
SimpleAudioEngine::~SimpleAudioEngine ( KDvoid )
{
	for ( SoundList::iterator it = l_aMusicList.begin ( ); it != l_aMusicList.end ( ); it++ )
	{
		if ( it->second )
		{
			xmSoundClose ( it->second );
			it->second = nullptr;
		}
	}
	l_aMusicList.clear ( );

	for ( SoundList::iterator it = l_aEffectList.begin ( ); it != l_aEffectList.end ( ); it++ )
	{
		if ( it->second )
		{
			xmSoundClose ( it->second );
			it->second = nullptr;
		}
	}
	l_aEffectList.clear ( );
}
KDvoid SimpleAudioEngine::rewindBackgroundMusic ( KDvoid )
{
	SoundList::iterator it = l_aMusicList.find ( l_uCurrentMusic );

	if ( it != l_aMusicList.end ( ) )
	{
		if ( it->second )
		{
			l_bIsBackgroundPlaying = KD_TRUE;
			xmSoundRewind ( it->second );
		}
	}
}
KDvoid SimpleAudioEngine::unloadEffect ( const KDchar* szFilePath )
{
	if (!szFilePath)
	{
		return;
	}

	std::string  sPath = FileUtils::getInstance()->fullPathForFilename(szFilePath);
	KDuint		   uID = _Hash(sPath.c_str());

	SoundList::iterator it = l_aEffectList.find(uID);
	if (it != l_aEffectList.end())
	{
		xmSoundClose(it->second);
		l_aEffectList.erase(uID);
	}
}
KDuint SimpleAudioEngine::preloadEffect(const KDchar* szFilePath, const KDchar* szKey)
{
	std::string  sPath = FileUtils::getInstance()->fullPathForFilename(szFilePath);
	KDuint		   uID = _Hash(szKey ? szKey : sPath.c_str());

	SoundList::iterator it = l_aEffectList.find(uID);
	if (it == l_aEffectList.end())
	{
		XMSound*  pSound = xmSoundOpen(sPath.c_str(), XM_SOUND_EFFECT);

		if (pSound)
		{
			l_aEffectList.insert(Sound(uID, pSound));
		}
	}

	return uID;
}
KDvoid SimpleAudioEngine::playBackgroundMusic ( const KDchar* szFilePath, bool bLoop )
{
	if ( !szFilePath )
	{
		return;
	}

	std::string  sPath = FileUtils::getInstance ( )->fullPathForFilename ( szFilePath );
	KDuint		 uRet  = _Hash ( sPath.c_str ( ) );

	preloadBackgroundMusic ( sPath.c_str ( ) );

	SoundList::iterator it = l_aMusicList.find ( uRet );
	if ( it != l_aMusicList.end ( ) )
	{
		l_uCurrentMusic = uRet;
		xmSoundSetRepeat ( it->second, bLoop );	
		this->rewindBackgroundMusic ( );
	}
}