ToySoundsRepo.cpp
7.49 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
//
// ToySoundsRepo.cpp
// WattsenglishToyApp-mobile
//
// Created by Katarzyna Kalinowska-Górska on 26/11/2019.
//
#include <stdio.h>
#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<std::string, float> ToySoundsRepo::soundDurations = std::map<std::string, float>();
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<const std::string> 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<const std::string> 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<const std::string> 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<const std::string> 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<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));
}
}
}