AniSoundsRepo.cpp
8.55 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//
// AniSoundsRepo.cpp
// WattsenglishToyApp-mobile
//
// Created by Katarzyna Kalinowska-Górska on 26/11/2019.
//
#include <stdio.h>
#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<std::string, float> AniSoundsRepo::soundDurations = std::map<std::string, float>();
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<std::string> 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<std::string, float>& 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;
}
}