-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModuleAudio.cpp
206 lines (169 loc) · 3.25 KB
/
ModuleAudio.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
#include "ModuleAudio.h"
#include "Application.h"
#include "SDL.h"
#include "SDL_mixer.h"
ModuleAudio::ModuleAudio(bool startEnabled) : Module(startEnabled)
{
name = "Audio";
for (uint i = 0; i < MAX_FX; ++i)
soundFx[i] = nullptr;
}
ModuleAudio::~ModuleAudio()
{
}
bool ModuleAudio::Init()
{
LOG("Loading Audio Mixer");
bool ret = true;
//Initialize audio subsystem
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0)
{
LOG("SDL_INIT_AUDIO could not initialize! SDL_Error: %s\n", SDL_GetError());
ret = false;
}
//Load support for OGG format
int flags = MIX_INIT_OGG;
int init = Mix_Init(flags);
if ((init & flags) != flags)
{
LOG("Could not initialize Mixer lib. Mix_Init: %s", Mix_GetError());
ret = false;
}
//Initialize SDL_mixer
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
{
LOG("SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError());
ret = false;
}
return ret;
}
// Called before quitting
bool ModuleAudio::CleanUp()
{
LOG("Freeing sound FX, closing Mixer and Audio subsystem");
if (music != NULL)
{
Mix_FreeMusic(music);
}
for (uint i = 0; i < MAX_FX; ++i)
{
if (soundFx[i] != nullptr) {
Mix_FreeChunk(soundFx[i]);
--fxCount;
}
}
Mix_CloseAudio();
Mix_Quit();
SDL_QuitSubSystem(SDL_INIT_AUDIO);
return true;
}
bool ModuleAudio::PlayMusic(const char* path, float fade_time)
{
bool ret = true;
if (music != NULL)
{
if (fade_time > 0.0f)
{
// Warning: This call blocks the execution until fade out is done
Mix_FadeOutMusic((int)(fade_time * 1000.0f));
}
else
{
Mix_HaltMusic();
}
Mix_FreeMusic(music);
}
music = Mix_LoadMUS(path);
if (music == NULL)
{
LOG("Cannot load music %s. Mix_GetError(): %s\n", path, Mix_GetError());
ret = false;
}
else
{
if (fade_time > 0.0f)
{
if (Mix_FadeInMusic(music, -1, (int)(fade_time * 1000.0f)) < 0)
{
LOG("Cannot fade in music %s. Mix_GetError(): %s", path, Mix_GetError());
ret = false;
}
}
else
{
if (Mix_PlayMusic(music, -1) < 0)
{
LOG("Cannot play in music %s. Mix_GetError(): %s", path, Mix_GetError());
ret = false;
}
}
}
LOG("Successfully playing %s", path);
return ret;
}
bool ModuleAudio::MuteMusic() {
bool ret = true;
//If the music is paused
if (Mix_PausedMusic() == 1)
{
//Resume the music
Mix_ResumeMusic();
}
//If the music is playing
else
{
//Pause the music
Mix_PauseMusic();
}
return ret;
}
bool ModuleAudio::StopMusic() {
Mix_FreeMusic(music);
music = nullptr;
Mix_HaltMusic();
return true;
}
uint ModuleAudio::LoadFx(const char* path)
{
uint ret = 0;
Mix_Chunk* chunk = Mix_LoadWAV(path);
if (chunk == nullptr)
{
LOG("Cannot load wav %s. Mix_GetError(): %s", path, Mix_GetError());
}
else
{
for (ret = 0; ret < MAX_FX; ++ret)
{
if (soundFx[ret] == nullptr)
{
soundFx[ret] = chunk;
++fxCount;
break;
}
}
}
return ret;
}
bool ModuleAudio::PlayFx(uint index, int repeat)
{
bool ret = false;
if (soundFx[index] != nullptr)
{
Mix_PlayChannel(-1, soundFx[index], repeat);
ret = true;
}
return ret;
}
bool ModuleAudio::UnloadFx(uint index)
{
bool ret = false;
if (soundFx[index] != nullptr)
{
Mix_FreeChunk(soundFx[index]);
soundFx[index] = nullptr;
--fxCount;
ret = true;
}
return ret;
}