1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "fsCAudioManager.h"

#include <algorithm>

#include <audio/include/AudioEngine.h>

#include "fsCCachedSoundEffect.h"
#include "fsCSoundEffectInstance.h"

#if defined fsFRAMEWORK_CX_V3
	#define cocosPrefix cocos2d::experimental
#else
#define cocosPrefix cocos2d
#endif

void fsCAudioManager::effectDoneCB(int pId, const std::string 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)>(
			[&](int id, const std::string 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()
{
	cocosPrefix::AudioEngine::end();
}