-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModuleAudio.cpp
149 lines (132 loc) · 3.06 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
#include "Globals.h"
#include "Application.h"
#include "ModuleAudio.h"
#include"SDL_Mixer/include/SDL_mixer.h"
#pragma comment(lib, "SDL_Mixer/libx86/SDL2_mixer.lib")
ModuleAudio::ModuleAudio() {
for (int i = 0; i < MAX_CHUNK; i++) {
chunks[i] = nullptr;
}
for (int i = 0; i < MAX_MUSIC; i++) {
musics[i] = nullptr;
}
}
ModuleAudio::~ModuleAudio(){}
bool ModuleAudio::Init() {
LOG("Init Mixer library");
bool ret = true;
int flags = MIX_INIT_OGG;
int init = Mix_Init(flags);
if ((init & flags) != flags)
{
LOG("Could not initialize Mixer lib.\n\n Mix_Init: %s", Mix_GetError());
ret = false;
}
if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 4096) == -1) {
LOG("Could not open audio.\n\n Mix_OpenAudio: %s", Mix_GetError());
ret = false;
}
("Could initilialze\n\n");
return ret;
}
bool ModuleAudio::CleanUp(){
LOG("Freeing chunks, music and Mixer library");
for (int i = MAX_CHUNK - 1; i >= 0; i--) {
if (chunks[i] != nullptr) {
Mix_FreeChunk(chunks[i]);
chunks[i] = nullptr;
}
}
for (int i = MAX_MUSIC - 1; i >= 0; i--) {
if (musics[i] != nullptr) {
Mix_FreeMusic(musics[i]);
musics[i] = nullptr;
}
}
Mix_CloseAudio();
while (Mix_Init(0)) {
Mix_Quit();
}
return true;
}
Mix_Chunk* const ModuleAudio::LoadChunk(const char* path) {
Mix_Chunk *chunk;
chunk = Mix_LoadWAV(path);
if (chunk == NULL) {
LOG("Chunk not correct\n\n\n\n\n");
LOG(Mix_GetError());
return false;
}
LOG("Chunk correct\n\n\n\n\n");
for (int i = 0; i < MAX_CHUNK; i++) {
if (chunks[i] == nullptr) {
chunks[i] = chunk;
chunks[i]->volume = MIX_MAX_VOLUME;
return chunk;
}
}
return nullptr;
}
Mix_Music* const ModuleAudio::LoadMusic(const char* path) {
Mix_Music *musicload;
musicload = Mix_LoadMUS(path);
if (musicload == NULL) {
LOG("Music not correct\n\n\n\n\n");
LOG(Mix_GetError());
return false;
}
LOG("Music correct\n\n\n\n\n");
for (int i = 0; i < MAX_CHUNK; i++) {
if (musics[i] == nullptr) {
musics[i] = musicload;
return musicload;
}
}
return nullptr;
}
bool const ModuleAudio::UnLoadChunk(Mix_Chunk* chunk) {
if (chunk != nullptr)
{
for (int i = 0; i < MAX_CHUNK; ++i)
{
if (chunks[i] == chunk)
{
chunks[i] = nullptr;
Mix_FreeChunk(chunk);
break;
}
}
}
return true;
}
bool const ModuleAudio::UnLoadMusic(_Mix_Music* music) {
if (music != nullptr)
{
for (int i = 0; i < MAX_MUSIC; ++i)
{
if (musics[i] == music)
{
musics[i] = nullptr;
Mix_FreeMusic(music);
break;
}
}
}
return true;
}
void ModuleAudio::PlayChunk(Mix_Chunk* chunk, int loops, int channel) {
Mix_PlayChannel(channel, chunk, loops);
}
void ModuleAudio::StopChunk() {
Mix_HaltChannel(-1);
}
void ModuleAudio::PlayMusic(_Mix_Music* music) {
if (Mix_FadeInMusic(music, -1, 1000) != 0) LOG("Music not playing correctly: Mix_FadeInMusic: %s", Mix_GetError());
}
void ModuleAudio::StopMusic() {
if (Mix_FadeOutMusic(1000) != 1) LOG("Music not stoping correctly: Mix_FadeOutMusic: %s", Mix_GetError());
}
int ModuleAudio::Check_Playing(int channel) {
int n = Mix_Playing(channel);
return n;
}