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::setEffectVolume ( KDuint uID, KDfloat fVolume ) { SoundList::iterator it = l_aEffectList.find(uID); if (it != l_aEffectList.end()) { xmSoundSetVolume(it->second, fVolume); } }
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::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); } }
KDvoid SimpleAudioEngine::stopAllEffects ( KDvoid ) { for (SoundList::iterator it = l_aEffectList.begin(); it != l_aEffectList.end(); it++) { if (it->second) { xmSoundStop(it->second); } } }
KDfloat SimpleAudioEngine::getEffectPitch ( KDuint uID ) { SoundList::iterator it = l_aEffectList.find(uID); if (it != l_aEffectList.end()) { return xmSoundGetPitch(it->second); } return 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(); }
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; }
// Detach and unlock all sounds void detachAll() { ManagedSound *s = list.getHead(); while(s) { ManagedSound *cur = s; s = s->next; cur->detach(); } }
// Update all sounds void updateAll() { ManagedSound *s = list.getHead(); while(s) { ManagedSound *cur = s; // Propagate first, since update() may delete object s = s->next; cur->update(); } }
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 ( ); } }
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 ( ); }
// Number of sounds in the list int numSounds() { return list.getNum(); }
namespace CocosDenshion { typedef std::map <KDuint, XMSound*> SoundList; typedef std::pair<KDuint, XMSound*> Sound; static SoundList l_aMusicList; static SoundList l_aEffectList; static KDuint l_uCurrentMusic = 0; static SimpleAudioEngine* l_pSharedEngine = nullptr; static KDbool l_bIsBackgroundPlaying = KD_FALSE; static std::function<KDvoid()> l_pBackgroundMusicMusicCompletionListener = nullptr; ////////////////////////////////////////////////////////////////////////// // static function ////////////////////////////////////////////////////////////////////////// static KDuint _Hash(const KDchar *key) { unsigned int len = strlen(key); const char *end = key + len; unsigned int hash; for (hash = 0; key < end; key++) { hash *= 16777619; hash ^= (unsigned int)(unsigned char)toupper(*key); } return (hash); } static KDvoid background_callBack ( const KDEvent* pEvent ) { l_bIsBackgroundPlaying = KD_FALSE; if ( l_pBackgroundMusicMusicCompletionListener ) { l_pBackgroundMusicMusicCompletionListener ( ); } } SimpleAudioEngine::SimpleAudioEngine ( KDvoid ) { } 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 ( ); } SimpleAudioEngine* SimpleAudioEngine::getInstance ( KDvoid ) { if ( !l_pSharedEngine ) { l_pSharedEngine = new SimpleAudioEngine ( ); } return l_pSharedEngine; } KDvoid SimpleAudioEngine::end ( KDvoid ) { CC_SAFE_DELETE ( l_pSharedEngine ); return; } ////////////////////////////////////////////////////////////////////////// // BackgroundMusic ////////////////////////////////////////////////////////////////////////// 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 ( ); } } 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::pauseBackgroundMusic ( KDvoid ) { SoundList::iterator it = l_aMusicList.find ( l_uCurrentMusic ); if ( it != l_aMusicList.end ( ) ) { if ( it->second ) { xmSoundPause ( 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 ); } } } KDbool SimpleAudioEngine::willPlayBackgroundMusic ( KDvoid ) { return KD_FALSE; } KDbool SimpleAudioEngine::isBackgroundMusicPlaying ( KDvoid ) { return l_bIsBackgroundPlaying; } ////////////////////////////////////////////////////////////////////////// // effect function ////////////////////////////////////////////////////////////////////////// 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::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::stopEffect ( KDuint uSoundId ) { SoundList::iterator it = l_aEffectList.find(uSoundId); if (it != l_aEffectList.end()) { xmSoundStop(it->second); } } 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::pauseEffect ( KDuint uSoundId ) { SoundList::iterator it = l_aEffectList.find(uSoundId); if (it != l_aEffectList.end()) { xmSoundPause(it->second); } } KDvoid SimpleAudioEngine::pauseAllEffects ( KDvoid ) { for (SoundList::iterator it = l_aEffectList.begin(); it != l_aEffectList.end(); it++) { if (it->second) { xmSoundPause(it->second); } } } KDvoid SimpleAudioEngine::resumeEffect ( KDuint uSoundId ) { SoundList::iterator it = l_aEffectList.find(uSoundId); if (it != l_aEffectList.end()) { xmSoundResume(it->second); } } KDvoid SimpleAudioEngine::resumeAllEffects ( KDvoid ) { for (SoundList::iterator it = l_aEffectList.begin(); it != l_aEffectList.end(); it++) { if (it->second) { xmSoundResume(it->second); } } } KDvoid SimpleAudioEngine::stopAllEffects ( KDvoid ) { for (SoundList::iterator it = l_aEffectList.begin(); it != l_aEffectList.end(); it++) { if (it->second) { xmSoundStop(it->second); } } } 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::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); } } KDvoid SimpleAudioEngine::unloadEffect ( KDuint uSoundId ) { SoundList::iterator it = l_aEffectList.find(uSoundId); if (it != l_aEffectList.end()) { xmSoundClose(it->second); l_aEffectList.erase(uSoundId); } } ////////////////////////////////////////////////////////////////////////// // volume interface ////////////////////////////////////////////////////////////////////////// KDfloat SimpleAudioEngine::getBackgroundMusicVolume ( KDvoid ) { return xmSoundGetVolumes(XM_SOUND_NORMAL); } KDvoid SimpleAudioEngine::setBackgroundMusicVolume ( KDfloat fVolume ) { xmSoundSetVolumes(XM_SOUND_NORMAL, fVolume); } KDfloat SimpleAudioEngine::getEffectsVolume ( KDvoid ) { return xmSoundGetVolumes(XM_SOUND_EFFECT); } KDvoid SimpleAudioEngine::setEffectsVolume ( KDfloat fVolume ) { xmSoundSetVolumes(XM_SOUND_EFFECT, fVolume); } KDfloat SimpleAudioEngine::getEffectVolume ( KDuint uID ) { SoundList::iterator it = l_aEffectList.find(uID); if (it != l_aEffectList.end()) { return xmSoundGetVolume(it->second); } return 0; } KDvoid SimpleAudioEngine::setEffectVolume ( KDuint uID, KDfloat fVolume ) { SoundList::iterator it = l_aEffectList.find(uID); if (it != l_aEffectList.end()) { xmSoundSetVolume(it->second, fVolume); } } KDfloat SimpleAudioEngine::getEffectsPitch ( KDvoid ) { return xmSoundGetPitches(XM_SOUND_EFFECT); } KDvoid SimpleAudioEngine::setEffectsPitch ( KDfloat fPitch ) { xmSoundSetPitches ( XM_SOUND_EFFECT, fPitch ); } 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::setEffectPitch(KDuint uID, KDfloat fPitch) { SoundList::iterator it = l_aEffectList.find(uID); if (it != l_aEffectList.end()) { xmSoundSetPitch(it->second, fPitch); } } KDvoid SimpleAudioEngine::unloadMusic ( const KDchar* szFilePath ) { if ( !szFilePath ) { return; } std::string sPath = FileUtils::getInstance()->fullPathForFilename ( szFilePath ); KDuint uID = _Hash ( sPath.c_str ( ) ); SoundList::iterator it = l_aMusicList.find ( uID ); if ( it != l_aMusicList.end ( ) ) { xmSoundClose ( it->second ); l_aMusicList.erase ( uID ); } if ( l_uCurrentMusic == uID ) { l_uCurrentMusic = 0; } } KDvoid SimpleAudioEngine::setBackgroundMusicMusicCompletionListener ( const std::function<KDvoid()>& pFunc ) { l_pBackgroundMusicMusicCompletionListener = pFunc; } } // end of namespace CocosDenshion
// Remove a sound from the list void remove(ManagedSound *snd) { list.remove(snd); }
int global_messageHandler2(ExCommand *cmd) { if (cmd->_messageKind != 17) return 0; int res = 0; StaticANIObject *ani; switch (cmd->_messageNum) { case 0x44c8: error("0x44c8"); // Unk3_sub_4477A0(&unk3, _parentId, _field_14 != 0); break; case 28: ani = g_fp->_currentScene->getStaticANIObject1ById(cmd->_parentId, cmd->_param); if (ani) ani->_priority = cmd->_field_14; break; case 25: ani = g_fp->_currentScene->getStaticANIObject1ById(cmd->_parentId, cmd->_param); if (ani) { if (cmd->_field_14) { ani->setFlags40(true); ani->_callback2 = staticANIObjectCallback; } else { ani->setFlags40(false); ani->_callback2 = 0; // Really NULL } } break; case 26: ani = g_fp->_currentScene->getStaticANIObject1ById(cmd->_parentId, cmd->_param); if (ani) { Movement *mov = ani->_movement; if (mov) mov->_currDynamicPhase->_field_68 = 0; } break; default: #if 0 // We never put anything into _defMsgArray while (::iterator it = g_fp->_defMsgArray.begin(); it != g_fp->_defMsgArray.end(); ++it) if (((ExCommand *)*it)->_field_24 == _messageNum) { ((ExCommand *)*it)->firef34(v13); res = 1; } #endif //debug_msg(_messageNum); if (!g_fp->_soundEnabled || cmd->_messageNum != 33 || g_fp->_currSoundListCount <= 0) return res; for (int snd = 0; snd < g_fp->_currSoundListCount; snd++) { SoundList *s = g_fp->_currSoundList1[snd]; int ms = s->getCount(); for (int i = 0; i < ms; i++) { s->getSoundByIndex(i)->setPanAndVolumeByStaticAni(); } } } return res; }
// Add a new sound to the list void addNew(ManagedSound* snd) { list.insert(snd); }