// // AniSoundsRepo.cpp // WattsenglishToyApp-mobile // // Created by Katarzyna Kalinowska-Górska on 26/11/2019. // #include #include "AniSoundsRepo.h" #include "AniMathUtils.h" #include "json/document.h" static float SOUND_EFFECTS_VOLUME = 0.75f; //static float MUSIC_VOLUME_BASE = 0.1f; //static float MUSIC_VOLUME_QUIET = 0.05f; std::string AniSoundsRepo::musicBgFilePath = "common/sounds/menu_bg_music.mp3"; std::string AniSoundsRepo::soundsFolder = "common/sounds/"; std::map AniSoundsRepo::soundDurations = std::map(); int AniSoundsRepo::currentMusicSoundId = cocos2d::AudioEngine::INVALID_AUDIO_ID; bool AniSoundsRepo::musicPreloaded = false; const char * AniSoundsRepo::MAP_GAME_SOUND_INTRO = "common/sounds/games/game_map/intro.mp3"; const char * AniSoundsRepo::MAP_GAME_SOUND_ALL_DONE = "common/sounds/games/game_map/end.mp3"; const std::vector AniSoundsRepo::TUTORIAL_SOUNDS = { "common/sounds/games/game_map/tutorial/1_movesteve.mp3", "common/sounds/games/game_map/tutorial/2_tapobject.mp3", "common/sounds/games/game_map/tutorial/3_jumpsteve.mp3", "common/sounds/games/game_map/tutorial/swipe_down.mp3", "common/sounds/games/game_map/tutorial/swipe_left.mp3", "common/sounds/games/game_map/tutorial/swipe_right.mp3", "common/sounds/games/game_map/tutorial/4_ticklesteve.mp3", "common/sounds/games/game_map/tutorial/5_movemap.mp3", // "common/sounds/games/game_map/tutorial/cancel_zoom.mp3", "common/sounds/games/game_map/tutorial/tap_corner2.mp3", "common/sounds/games/game_map/tutorial/finished_tutorial.mp3" }; void AniSoundsRepo::preloadAllTutorialGameSounds(){ for(int i = 0; i < TUTORIAL_SOUNDS.size(); ++i){ preloadSoundEffect(TUTORIAL_SOUNDS[i]); } } AniSoundsRepo::SoundInfo AniSoundsRepo::gameTutorialSound(int phase){ auto fp = /*soundsFolder +*/ TUTORIAL_SOUNDS[phase]; return SoundInfo(fp, soundDuration(fp)); } AniSoundsRepo::SoundInfo AniSoundsRepo::gameTNCIntroSound(){ auto fp = soundsFolder + "games/g_tnc_intro.mp3"; return SoundInfo(fp, soundDuration(fp)); } AniSoundsRepo::SoundInfo AniSoundsRepo::hooraySound(){ std::vector< std::string> hooraySounds = {/*"games/g_yes.mp3",*/ "../g_whoo_hoo.mp3", "../g_yeah.mp3", "../g_super.mp3", "../g_well_done.mp3", "../g_fantastic.mp3"}; auto soundFilename = AniMathUtils::getRandomElement(hooraySounds); auto fp = soundsFolder + soundFilename; return SoundInfo(fp, soundDuration(fp)); } AniSoundsRepo::SoundInfo AniSoundsRepo::pickLevelSound(){ auto fp = soundsFolder + "level_picking/g_pick_level.mp3"; return SoundInfo(fp, soundDuration(fp)); } AniSoundsRepo::SoundInfo AniSoundsRepo::scoreSound(){ auto fp = "sounds/games/g_your_score.mp3"; return SoundInfo(fp, soundDuration(fp)); } AniSoundsRepo::SoundInfo AniSoundsRepo::tooSlowSound(){ std::vector< std::string> hooraySounds = {"games/g_slow.mp3"}; auto soundFilename = AniMathUtils::getRandomElement(hooraySounds); auto fp = soundsFolder + soundFilename; return SoundInfo(fp, soundDuration(fp)); } AniSoundsRepo::SoundInfo AniSoundsRepo::wrongItemEffectSound(){ std::string soundPath = soundsFolder + "../g_buzzer.mp3"; return SoundInfo(soundPath, 0); } AniSoundsRepo::SoundInfo AniSoundsRepo::rightItemEffectSound(){ std::string soundPath = soundsFolder + "../g_stars.mp3"; return SoundInfo(soundPath, 0); } AniSoundsRepo::SoundInfo AniSoundsRepo::typeConfirmSound(int game, int itemType){ std::string soundPath = soundsFolder; switch(itemType){ case 1: soundPath += "games/game" + std::to_string(game) + "/g_conf_1.mp3"; break; case 2: soundPath += "games/game" + std::to_string(game) + "/g_conf_2.mp3"; break; case 3: soundPath += "games/game" + std::to_string(game) + "/g_conf_3.mp3"; break; case 4: soundPath += "games/game" + std::to_string(game) + "/g_conf_4.mp3"; break; default: soundPath = ""; break; } return SoundInfo(soundPath, soundDurations[soundPath]); } AniSoundsRepo::SoundInfo AniSoundsRepo::typeRequestSound(int game, int itemType){ std::string soundPath = soundsFolder; switch(itemType){ case 1: soundPath += "games/game" + std::to_string(game) + "/g_request_1.mp3"; break; case 2: soundPath += "games/game" + std::to_string(game) + "/g_request_2.mp3"; break; case 3: soundPath += "games/game" + std::to_string(game) + "/g_request_3.mp3"; break; case 4: soundPath += "games/game" + std::to_string(game) + "/g_request_4.mp3"; break; default: soundPath = ""; break; } return SoundInfo(soundPath, soundDurations[soundPath]); } void AniSoundsRepo::stopAllSounds(bool musicToo){ if(musicToo){ cocos2d::AudioEngine::stopAll(); } else { cocos2d::AudioEngine::stopAllBut(currentMusicSoundId); } } void AniSoundsRepo::stopSoundById(int audioId){ cocos2d::AudioEngine::stop(audioId); } int AniSoundsRepo::playSound(const std::string soundFilepath, bool stopOtherEffects){ if(stopOtherEffects){ // cocos2d::AudioEngine::stopAll(); cocos2d::AudioEngine::stopAllBut(currentMusicSoundId); } auto soundId = cocos2d::AudioEngine::play2d(soundFilepath); cocos2d::AudioEngine::setVolume(soundId, SOUND_EFFECTS_VOLUME); return soundId; //return 0; } int AniSoundsRepo::playMusic(const std::string musicFilepath){ // CocosDenshion::SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(MUSIC_VOLUME_BASE); // CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(musicFilepath.c_str(), true); currentMusicSoundId = cocos2d::AudioEngine::play2d(musicFilepath, true); // cocos2d::AudioEngine::setVolume(currentMusicSoundId, MUSIC_VOLUME_BASE); return currentMusicSoundId; } void AniSoundsRepo::stopMusic(){ cocos2d::AudioEngine::stop(currentMusicSoundId); currentMusicSoundId = cocos2d::AudioEngine::INVALID_AUDIO_ID; } void AniSoundsRepo::pauseMusic(){ cocos2d::AudioEngine::pause(currentMusicSoundId); } void AniSoundsRepo::resumeMusic(){ cocos2d::AudioEngine::resume(currentMusicSoundId); } //void AniSoundsRepo::turnDownMusic(){ // cocos2d::AudioEngine::setVolume(currentMusicSoundId, MUSIC_VOLUME_QUIET); //} // //void AniSoundsRepo::turnUpMusic(){ // cocos2d::AudioEngine::setVolume(currentMusicSoundId, MUSIC_VOLUME_BASE); //} float AniSoundsRepo::soundDuration(const std::string soundFilepath){ if(soundDurations.find(soundFilepath) != soundDurations.end()){ return soundDurations[soundFilepath]; } else { return 0; } } void AniSoundsRepo::preloadMusic(std::string musicFilePath){ cocos2d::AudioEngine::preload(musicFilePath, [&](bool success){ musicPreloaded = success; }); } void AniSoundsRepo::preloadSoundEffect(std::string soundPath){ cocos2d::AudioEngine::preload(soundPath); } void AniSoundsRepo::preloadAllLoadedSoundEffects(){ for(auto soundIt = soundDurations.begin(); soundIt != soundDurations.end(); soundIt++){ std::string fullSoundPath = /*commonConfig.soundConfig.soundsFolder + */soundIt->first; // soundEngine->preloadEffect(fullSoundPath.c_str()); cocos2d::AudioEngine::preload(fullSoundPath); } } bool AniSoundsRepo::isPlayingSound(){ auto allSoundsCount = cocos2d::AudioEngine::getPlayingAudioCount(); if(isMusicPlaying()){ --allSoundsCount; } return allSoundsCount > 0; } void AniSoundsRepo::pauseAllSounds(){ cocos2d::AudioEngine::pauseAll(); } void AniSoundsRepo::resumeAllSounds(){ cocos2d::AudioEngine::resumeAll(); } void AniSoundsRepo::loadSoundsList(){ parseSoundInfoFile(AniSoundsRepo::soundsFolder, AniSoundsRepo::soundsFolder + "sounds_info.si", AniSoundsRepo::soundDurations); } void AniSoundsRepo::parseSoundInfoFile(std::string soundFolderPath, std::string filePath, std::map& mapToFill){ if(cocos2d::FileUtils::getInstance()->isFileExist(filePath)){ auto soundInfoJson = new rapidjson::Document(); auto jsonString = cocos2d::FileUtils::getInstance()->getStringFromFile(filePath); soundInfoJson->Parse(jsonString.c_str()); auto soundsList = (*soundInfoJson)["sounds"].GetArray(); for(const auto& sound : soundsList){ auto soundFilename = sound["filename"].GetString(); auto soundDuration = sound["duration_seconds"].GetFloat(); mapToFill.insert(std::make_pair(soundFolderPath + soundFilename, soundDuration)); } delete soundInfoJson; } }