// // ToySoundsRepo.cpp // WattsenglishToyApp-mobile // // Created by Katarzyna Kalinowska-Górska on 26/11/2019. // #include #include "ToySoundsRepo.h" #include "ToyMathUtils.h" #include "json/document.h" static float SOUND_EFFECTS_VOLUME = 0.75; std::string ToySoundsRepo::musicBgFilePath = "common/sounds/menu_bg_music.mp3"; std::string ToySoundsRepo::soundsFolder = "common/sounds/"; std::map ToySoundsRepo::soundDurations = std::map(); int ToySoundsRepo::currentMusicSoundId = -1; ToySoundsRepo::SoundInfo ToySoundsRepo::introSound(){ auto fp = soundsFolder + "g_intro.mp3"; return SoundInfo(fp, soundDuration(fp)); } ToySoundsRepo::SoundInfo ToySoundsRepo::gameTNCIntroSound(){ auto fp = soundsFolder + "games/g_tnc_intro.mp3"; return SoundInfo(fp, soundDuration(fp)); } ToySoundsRepo::SoundInfo ToySoundsRepo::gameLetsStartIntroSound(){ auto fp = soundsFolder + "games/g_letsstart.mp3"; return SoundInfo(fp, soundDuration(fp)); } ToySoundsRepo::SoundInfo ToySoundsRepo::hooraySound(){ std::vector hooraySounds = {/*"games/g_yes.mp3",*/ "games/g_whoo_hoo.mp3", "games/g_yeah.mp3"}; auto soundFilename = ToyMathUtils::getRandomElement(hooraySounds); auto fp = soundsFolder + soundFilename; return SoundInfo(fp, soundDuration(fp)); } ToySoundsRepo::SoundInfo ToySoundsRepo::oopsSound(){ std::vector oopsSounds = {"games/g_oops.mp3", "games/g_no.mp3", "games/g_uh_oh.mp3"}; auto soundFilename = ToyMathUtils::getRandomElement(oopsSounds); auto fp = soundsFolder + soundFilename; return SoundInfo(fp, soundDuration(fp)); } ToySoundsRepo::SoundInfo ToySoundsRepo::pureOopsSound(){ std::vector oopsSounds = {"games/g_oops.mp3", "games/g_uh_oh.mp3"}; auto soundFilename = ToyMathUtils::getRandomElement(oopsSounds); auto fp = soundsFolder + soundFilename; return SoundInfo(fp, soundDuration(fp)); } ToySoundsRepo::SoundInfo ToySoundsRepo::noSound(){ auto fp = soundsFolder + "games/g_no.mp3"; return SoundInfo(fp, soundDuration(fp)); } ToySoundsRepo::SoundInfo ToySoundsRepo::pickLevelSound(){ auto fp = soundsFolder + "level_picking/g_pick_level.mp3"; return SoundInfo(fp, soundDuration(fp)); } ToySoundsRepo::SoundInfo ToySoundsRepo::changeLevelSound(){ auto fp = soundsFolder + "games/g_next_level.mp3"; return SoundInfo(fp, soundDuration(fp)); } ToySoundsRepo::SoundInfo ToySoundsRepo::gameFinishedSound(){ auto fp = soundsFolder + "games/g_finished.mp3"; return SoundInfo(fp, soundDuration(fp)); } ToySoundsRepo::SoundInfo ToySoundsRepo::scoreSound(){ auto fp = soundsFolder + "games/g_your_score.mp3"; return SoundInfo(fp, soundDuration(fp)); } ToySoundsRepo::SoundInfo ToySoundsRepo::hurryUpSound(){ auto fp = soundsFolder + "games/g_hurry_up.mp3"; return SoundInfo(fp, soundDuration(fp)); } ToySoundsRepo::SoundInfo ToySoundsRepo::tooSlowSound(){ std::vector hooraySounds = {"games/g_slow.mp3", "games/g_slow2.mp3"}; auto soundFilename = ToyMathUtils::getRandomElement(hooraySounds); auto fp = soundsFolder + soundFilename; return SoundInfo(fp, soundDuration(fp)); } ToySoundsRepo::SoundInfo ToySoundsRepo::wrongThreeTimesSound(){ auto fp = soundsFolder + "games/g_wrong_3_times.mp3"; return SoundInfo(fp, soundDuration(fp)); } ToySoundsRepo::SoundInfo ToySoundsRepo::wrongItemEffectSound(){ std::string soundPath = soundsFolder + "games/g_buzzer.mp3"; return SoundInfo(soundPath, 0); } ToySoundsRepo::SoundInfo ToySoundsRepo::rightItemEffectSound(){ std::string soundPath = soundsFolder + "games/g_stars.mp3"; return SoundInfo(soundPath, 0); } ToySoundsRepo::SoundInfo ToySoundsRepo::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]); } ToySoundsRepo::SoundInfo ToySoundsRepo::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 ToySoundsRepo::stopAllSounds(){ cocos2d::AudioEngine::stopAll(); } void ToySoundsRepo::stopSoundById(int audioId){ cocos2d::AudioEngine::stop(audioId); } int ToySoundsRepo::playSound(const std::string soundFilepath, bool stopOtherEffects){ if(stopOtherEffects){ cocos2d::AudioEngine::stopAll(); } auto soundId = cocos2d::AudioEngine::play2d(soundFilepath); cocos2d::AudioEngine::setVolume(soundId, SOUND_EFFECTS_VOLUME); return soundId; } int ToySoundsRepo::playMusic(const std::string musicFilepath){ currentMusicSoundId = cocos2d::AudioEngine::play2d(musicFilepath, true); return currentMusicSoundId; } void ToySoundsRepo::stopMusic(){ cocos2d::AudioEngine::stop(currentMusicSoundId); } float ToySoundsRepo::soundDuration(const std::string soundFilepath){ if(soundDurations.find(soundFilepath) != soundDurations.end()){ return soundDurations[soundFilepath]; } else { return 0; } } void ToySoundsRepo::preloadSoundEffect(std::string soundPath){ cocos2d::AudioEngine::preload(soundPath); } void ToySoundsRepo::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); } } void ToySoundsRepo::pauseAllSounds(){ cocos2d::AudioEngine::pauseAll(); } void ToySoundsRepo::resumeAllSounds(){ cocos2d::AudioEngine::resumeAll(); } void ToySoundsRepo::loadSoundsList(){ parseSoundInfoFile(ToySoundsRepo::soundsFolder, ToySoundsRepo::soundsFolder + "sounds_info.si", ToySoundsRepo::soundDurations); } void ToySoundsRepo::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)); } } }