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::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 ) );			
		}
	}
}
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::setEffectPitch(KDuint uID, KDfloat fPitch)
{
	SoundList::iterator it = l_aEffectList.find(uID);
	if (it != l_aEffectList.end())
	{
		xmSoundSetPitch(it->second, fPitch);
	}
}
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::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::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 );
		}
	}
}
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 ( );
	}
}