-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModuleAudio.cpp
179 lines (144 loc) · 3.6 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
#include "ModuleAudio.h"
#pragma comment( lib, "SDL_mixer/libx86/SDL2_mixer.lib" )
ModuleAudio::ModuleAudio()
{
for (uint i = 0; i < MAX_SFX; ++i)
sfxs[i] = nullptr;
for (uint i = 0; i < MAX_MUSIC; ++i)
musics[i] = nullptr;
}
ModuleAudio::~ModuleAudio()
{}
bool ModuleAudio::Init() {
bool ret = true;
int flags = MIX_INIT_OGG;
int initted = Mix_Init(flags);
if (initted&flags != flags) {
LOG("Mix_Init: Failed to init required ogg support! %s\n", Mix_GetError());
ret = false;
}
if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, CHUNKSIZE) == -1) { //2 is for stereo sound
LOG("Mix_OpenAudio: Failed to open audio! %s\n", Mix_GetError());
ret = false;
}
Mix_AllocateChannels(MAX_SFX);
return ret;
}
bool ModuleAudio::CleanUp()
{
LOG("Freeing sound effects and music");
Mix_HaltChannel(-1); //Stop playing all sfx to avoid problems.
for (int i = MAX_SFX - 1; i >= 0; --i) {
if (sfxs[i] != nullptr)
Mix_FreeChunk(sfxs[i]);
sfxs[i] = nullptr;
}
if (Mix_PlayingMusic())
Mix_HaltMusic(); //Stop playing music to avoid problems.
for (int i = MAX_MUSIC - 1; i >= 0; --i) {
if (musics[i] != nullptr)
Mix_FreeMusic(musics[i]);
musics[i] = nullptr;
}
Mix_CloseAudio();
return true;
}
Mix_Chunk * const ModuleAudio::LoadSFX(const char * path)
{
Mix_Chunk* ret = nullptr;
int current_sfx = 0;
for (; current_sfx < MAX_SFX; ++current_sfx)
{
if (sfxs[current_sfx] == nullptr)
break;
}
if (current_sfx == MAX_SFX) {
LOG("Overflow error: Overwriting sfx");
Mix_FreeChunk(sfxs[last_sfx]);
current_sfx = last_sfx++;
if (last_sfx == MAX_SFX)
last_sfx = 0;
}
sfxs[current_sfx] = Mix_LoadWAV(path);
if (sfxs[current_sfx] == NULL) {
LOG("MixLoadWav: Failed to load wav from path \"%s\": %s\n", path, Mix_GetError());
}
else
ret = sfxs[current_sfx];
return ret;
}
void ModuleAudio::FreeSFX(Mix_Chunk * sfx)
{
if (sfx == nullptr)
return;
for (int i = 0; i < MAX_SFX; ++i) {
if (sfxs[i] == sfx) {
Mix_FreeChunk(sfxs[i]);
sfxs[i] = nullptr;
}
}
}
Mix_Music * const ModuleAudio::LoadMusic(const char * path)
{
Mix_Music* ret = nullptr;
if (last_music == MAX_MUSIC) {
LOG("Overflow error: Overwriting music");
last_music = 0;
}
musics[last_music] = Mix_LoadMUS(path);
if (!musics[last_music]) {
LOG("Mix_LoadMUS: Could not load \"%s\": %s\n", path, Mix_GetError());
}
else
ret = musics[last_music++];
return ret;
}
void ModuleAudio::FreeMusic(Mix_Music * music)
{
if (music = nullptr)
return;
if (music == playing) {
Mix_HaltMusic();
playing = nullptr;
}
for (int i = 0; i < MAX_MUSIC; ++i) {
if (musics[i] == music) {
Mix_FreeMusic(musics[i]);
musics[i] = nullptr;
}
}
}
void const ModuleAudio::PlaySFX(Mix_Chunk * sfx)
{
uint i = 0;
for (; i < MAX_SFX; ++i)
if (sfx == sfxs[i]) break;
Mix_HaltChannel(i);
if (Mix_PlayChannel(i, sfx, 0) == -1)
LOG("Mix_PlayChannel: Could not play sfx: %s\n", Mix_GetError());
}
void const ModuleAudio::PlayMusic(Mix_Music * music)
{
if (music == playing)
return;
//This will halt previous music if StopMusic() was not called, will fade-in in 5 seconds
//Music will play on infinite-loop
if (Mix_FadeInMusic(music, -1, 5000) == -1) {
LOG("Mix_FadeInMusic: Could not play music, error: %s\n", Mix_GetError());
}
else
playing = music;
}
void const ModuleAudio::MusicVolume(int percent) {
if (percent > 100)
percent = 100;
LOG("Volume was: %d\n", Mix_VolumeMusic(MIX_MAX_VOLUME * percent / 100.0f));
LOG("Volume is now: %d\n", Mix_VolumeMusic(-1));
return;
}
void const ModuleAudio::StopMusic()
{
//Fades-out 2 seconds
Mix_FadeOutMusic(2000);
playing = nullptr;
}