-
Notifications
You must be signed in to change notification settings - Fork 1
/
Audio.cpp
163 lines (133 loc) · 2.57 KB
/
Audio.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
#include "Audio.h"
Audio::Audio()
{
for (int i = 0; i < MAX_FX; ++i)
fx[i] = nullptr;
}
Audio::~Audio()
{
}
bool Audio::Init()
{
SDL_Log("Loading Audio Mixer");
bool ret = true;
//todo 2: Init SDL audio subsystem.
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0)
{
SDL_Log("SDL EVENTS Could not initialize. SDL_Error %s\n", Mix_GetError());
ret = false;
}
// load support for the OGG format
int flags = MIX_INIT_OGG;
int init = Mix_Init(flags);
if ((init & flags) != flags)
{
SDL_Log("Could not initialize Mixer lib. Mix_Init: %s", Mix_GetError());
ret = false;
}
//todo 3: Take a look at this code.
if (Mix_OpenAudio(48000, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
{
SDL_Log("SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError());
ret = false;
}
return ret;
}
bool Audio::PlayMusic(const char* path, float fade_time)
{
bool ret = true;
if (music != NULL)
{
if (fade_time > 0.0f)
{
Mix_FadeOutMusic((int)(fade_time * 1000.0f));
}
else
{
Mix_HaltMusic();
}
// this call blocks until fade out is done
Mix_FreeMusic(music);
}
music = Mix_LoadMUS(path);
if (music == NULL)
{
SDL_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)
{
SDL_Log("Cannot fade in music %s. Mix_GetError(): %s", path, Mix_GetError());
ret = false;
}
}
else
{
if (Mix_PlayMusic(music, -1) < 0)
{
SDL_Log("Cannot play in music %s. Mix_GetError(): %s", path, Mix_GetError());
ret = false;
}
}
}
SDL_Log("Successfully playing %s", path);
return ret;
}
bool Audio::CleanUp()
{
SDL_Log("Freeing sound FX, closing Mixer and Audio subsystem");
if (music != NULL)
{
Mix_FreeMusic(music);
}
for (int i = 0; i < MAX_FX; ++i)
if (fx[i] != nullptr)
Mix_FreeChunk(fx[i]);
Mix_CloseAudio();
Mix_Quit();
SDL_QuitSubSystem(SDL_INIT_AUDIO);
return true;
}
// Load WAV
int Audio::LoadFx(const char* path)
{
int ret = 0;
Mix_Chunk* chunk = Mix_LoadWAV(path);
if (chunk == nullptr)
{
SDL_Log("Cannot load wav %s. Mix_GetError(): %s", path, Mix_GetError());
}
else
{
fx[last_fx] = chunk;
ret = last_fx++;
}
return ret;
}
// UnLoad WAV
bool Audio::UnLoadFx(int id)
{
bool ret = false;
if (fx[id] != nullptr)
{
Mix_FreeChunk(fx[id]);
fx[id] = nullptr;
ret = true;
}
return ret;
}
// Play WAV
bool Audio::PlayFx(unsigned int id, int repeat)
{
bool ret = false;
if (fx[id] != nullptr)
{
Mix_PlayChannel(-1, fx[id], repeat);
ret = true;
}
return ret;
}