#include "fsCAudioManager.h"
#include <algorithm>
#include <audio/AudioEngine.h>
#include "fsCCachedSoundEffect.h"
#include "fsCSoundEffectInstance.h"
void fsCAudioManager::effectDoneCB(int pId, const std::string_view pError)
{
auto trash = std::find_if(mLiveEffects.begin(), mLiveEffects.end(),
[&](std::pair<fsISoundEffectInstance*, fsICachedSoundEffect*>& entry) -> fsBool<--- Parameter 'entry' can be declared as reference to const
{
fsCSoundEffectInstance* const sfxInstance = static_cast<fsCSoundEffectInstance*>(entry<--- Variable 'sfxInstance' can be declared as pointer to const
.first);
return sfxInstance->idMatches(pId);
});
if (trash != mLiveEffects.end())
{
delete trash->first;
mLiveEffects.erase(trash);
}
}
void fsCAudioManager::effectsCacheFlushDo()
{
std::for_each(mLiveEffects.begin(), mLiveEffects.end(),
[](std::pair<fsISoundEffectInstance*, fsICachedSoundEffect*> liveEffect)
{
liveEffect.first->stop();
});
}
void fsCAudioManager::updateDo(fsS32 pDelta)
{
}
void fsCAudioManager::effectPlayDo(fsICachedSoundEffect* const pSoundEffect, fsBool pLoop)
{
if (fsCSoundEffectInstance* const soundInst = static_cast<fsCSoundEffectInstance*>(pSoundEffect->play(pLoop)))
{
mLiveEffects.push_back(std::pair<fsISoundEffectInstance*, fsICachedSoundEffect*>(soundInst, pSoundEffect));
soundInst->volumeSet(globalVolumeGet() * effectsVolumeGet());
soundInst->callbackSet(std::function<void(int, std::string_view)>(
[&](int id, const std::string_view error) -> void
{
effectDoneCB(id, error);
}));
}
}
void fsCAudioManager::effectsVolumeSetDo(fsF32 pEffectsVolume)
{
std::for_each(mLiveEffects.begin(), mLiveEffects.end(),
[pEffectsVolume](std::pair<fsISoundEffectInstance*, fsICachedSoundEffect*> liveEffect)
{
liveEffect.first->volumeSet(pEffectsVolume);
});
}
fsCAudioManager::fsCAudioManager()
{
}
fsCAudioManager::~fsCAudioManager()
{
axmol::AudioEngine::end();
}