/
SoundManager.cpp
281 lines (230 loc) · 6.55 KB
/
SoundManager.cpp
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "SoundManager.h"
#include <string>
#include <iostream>
const std::string SUCCESS = "media/sounds/Success/success5.wav";
const std::string FAILURE = "media/sounds/Failure/failure4.wav";
const std::string BOUNCE = "media/sounds/Bounce/bounce1.wav";
const std::string BACKGROUND = "media/sounds/Background/background1.wav";
//sound effect ids
int successId;
int failureId;
int bounceId;
std::vector<Mix_Chunk*> SoundList;
Mix_Music* backgroundMusic;
static bool mute = false;
bool init = false;
bool debug = false;
SoundManager::SoundManager() {
if(debug){ std::cout<<"Entered SoundManager Constructor"<<std::endl;}
init();
if(debug){ std::cout<<"Exiting SoundManager Constructor"<<std::endl;}
}
SoundManager::~SoundManager() {
if(debug){std::cout<<"Entering SoundManager Destructor"<<std::endl;}
Cleanup();
Mix_CloseAudio();
if(debug){std::cout<<"Exiting SoundManager Destructor"<<std::endl;}
}
void SoundManager::resetIDs(void){
successId=-1;
failureId=-1;
bounceId=-1;
}
void SoundManager::init(void){
int errorCode = Mix_OpenAudio(22050, AUDIO_S16SYS, 2, 4096);
if(errorCode != 0){
std::cout<<"Error: Could not open SDL Mixer.\n"<<Mix_GetError()<<std::endl;
}
resetIDs();
backgroundMusic=NULL;
loadSounds();
}
void SoundManager::loadSounds(void){
//load default sound effects
if(debug){
std::cout<<"Entered loadSounds()"<<std::endl;
std::cout<<"success:"<<successId<<
", failure:"<<failureId<<
",bounce:"<<bounceId<<
std::endl;
}
if(successId<0){
successId = LoadChunk(SUCCESS.c_str());
}
if(failureId<0){
failureId = LoadChunk(FAILURE.c_str());
}
if(bounceId<0){
bounceId = LoadChunk(BOUNCE.c_str());
}
if(backgroundMusic==NULL){
backgroundMusic=Mix_LoadMUS(BACKGROUND.c_str());
}
if(debug){
std::cout<<"Exiting loadSounds()"<<std::endl;
std::cout<<"success:"<<successId<<
", failure:"<<failureId<<
",bounce:"<<bounceId<<
std::endl;
}
}
Mix_Music* SoundManager::LoadMusic(const char* File){
//File type can be wav, mod, midi, ogg, mp3, or flac
if(debug){std::cout<<"Entered LoadMusic()"<<std::endl;}
if(File==NULL){
std::cout<<"Error in LoadMusic(), File==NULL"<<std::endl;
return NULL;
}
Mix_Music* musicPtr = Mix_LoadMUS(File);
if(musicPtr==NULL){
std::cout<<"Error in MixLoadMUS(), could not load file."<<std::endl;
}
if(debug){ std::cout<<"Exiting LoadMusic()"<<std::endl;}
return musicPtr;
}
int SoundManager::LoadChunk(const char* File) {
//File type can be wav, mod, midi, ogg, or mp3
if(debug){ std::cout<<"Entered LoadChunk()"<<std::endl;}
Mix_Chunk* TempSound = NULL;
if(File==NULL){
std::cout<<"Error: File=NULL."<<std::endl;
if(debug){ std::cout<<"Exiting LoadChunk()"<<std::endl;}
return -1;
}
//Mix_LoadWav name is misleading, it also supports other filetypes
if((TempSound = Mix_LoadWAV(File)) == NULL) {
std::cout<<"Error: "<<File<<" cound not be loaded."<<std::endl;
if(debug){ std::cout<<"Exiting LoadChunk()"<<std::endl;}
return -1;
}
if(debug){std::cout<<"SoundList.size() Before:"<<SoundList.size()<<std::endl;}
SoundList.push_back(TempSound);
if(debug){std::cout<<"SoundList.size() After:"<<SoundList.size()<<std::endl;}
if(debug){ std::cout<<"Exiting LoadChunk()"<<std::endl;}
//chunkID
return (SoundList.size() - 1);
}
void SoundManager::Cleanup() {
for(int i = 0;i < SoundList.size();i++) {
Mix_FreeChunk(SoundList[i]);
}
SoundList.clear();
}
void SoundManager::fadeInLoopMusic(Mix_Music* music, int loops, int ms){
//ms= milliseconds it takes to fade in
if(music==NULL){
std::cout<<"Error in LoopMusic(), *music=NULL."<<std::endl;
return;
}
if(ms<0){
std::cout<<"Error in LoopMusic(), ms < 0."<<std::endl;
return;
}
int errorCode = Mix_FadeInMusic(music, loops, ms);
if(errorCode!=0){
std::cout<<"Error in Mix_FadeInMusic(): "<< Mix_GetError() << std::endl;
return;
}
}
void SoundManager::LoopMusic(Mix_Music* music, int loops){
//loops==-1 plays music infinitely
if(music==NULL){
std::cout<<"Error in LoopMusic(), *music=NULL."<<std::endl;
return;
}
int errorCode = Mix_PlayMusic(music, loops);
if(errorCode!=0){
std::cout<<"Error in Mix_PlayMusic(): "<< Mix_GetError() << std::endl;
return;
}
}
void SoundManager::PlaySound(int chunkID) {
if(debug){std::cout<<"Entered PlaySound("<<chunkID<<")"<<std::endl;}
if(chunkID < 0){
std::cout<<"Invalid. ID < 0"<<std::endl;
return;
}
if(chunkID >= SoundList.size()){
std::cout<<"Invalid. ID >= SoundList.size() = "<<SoundList.size()<<std::endl;
return;
}
if(SoundList[chunkID] == NULL){
std::cout<<"Empty SoundList."<<std::endl;
return;
}
Mix_PlayChannel(-1, SoundList[chunkID], 0);
if(debug){std::cout<<"Exiting PlaySound()"<<std::endl;}
}
void SoundManager::toggleMute(void){
setMute(!mute);
}
void SoundManager::setMute(bool val){
if(val==true){
stopAllAudio();
}
if(val==false){
playBackground(-1);
}
mute=val;
}
void SoundManager::playFailure(void){
if(!mute){
if(debug){std::cout<<"entered playFailure()"<<std::endl;}
PlaySound(failureId);
if(debug){std::cout<<"exiting playFailure()"<<std::endl;}
}
}
void SoundManager::playSuccess(void){
if(!mute){
if(debug){std::cout<<"entered playSuccess("<<successId<<")"<<std::endl;}
PlaySound(successId);
if(debug){std::cout<<"Exiting playSuccess()"<<std::endl;}
}
}
void SoundManager::playBounce(void){
if(!mute){
if(debug){
std::cout<<"entered playBounce()"<<std::endl;
}
PlaySound(bounceId);
if(debug){std::cout<<"exiting playBounce()"<<std::endl;}
}
}
void SoundManager::playBackground(int numLoops){
playBackground(numLoops, 0);
}
void SoundManager::playBackground(int numLoops, int ms){
if(!mute){
if(debug){std::cout<<"entered playBackground()"<<std::endl;}
if(backgroundMusic==NULL){
backgroundMusic=Mix_LoadMUS(BACKGROUND.c_str());
}
fadeInLoopMusic(backgroundMusic, numLoops, ms);
if(debug){std::cout<<"exiting playBackground()"<<std::endl;}
}
}
void SoundManager::pauseSounds(void){
//can't pause background music, only play and stop.
//pause sound effects
Mix_Pause(-1);
}
void SoundManager::resumeSounds(void){
//resume sound effects
Mix_Resume(-1);
}
void SoundManager::stopMusic(void)
{ //stop background music
if(backgroundMusic!=NULL){
Mix_FreeMusic(backgroundMusic);
}
backgroundMusic=NULL; //backgroundMusic now points to garbage, so set it to null
}
void SoundManager::stopSounds(void)
{ //stop sound effects
Mix_HaltChannel(-1);
}
void SoundManager::stopAllAudio(void)
{
stopMusic();
stopSounds();
}