-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAudioManager.cpp
More file actions
167 lines (128 loc) · 3.91 KB
/
AudioManager.cpp
File metadata and controls
167 lines (128 loc) · 3.91 KB
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
#include "AudioManager.hpp"
AudioHandler::AudioHandler() {
}
bool AudioHandler::init(uint8_t channels, int frequency, int chunksize, uint16_t format) {
if (channels > MIX_CHANNELS) {
printf("Too many channels for SDL_mixer (%u), defaulting to maximum (%u).\n", channels, MIX_CHANNELS);
channels = MIX_CHANNELS;
}
// We want mp3 and ogg files:
// For some reason, works for me without this. Breaks with it.
//int flags = MIX_INIT_OGG | MIX_INIT_MP3;
/*if (Mix_Init(flags) & flags != flags) {
printf("Mix_Init: Failed to init required .ogg and .mp3 support!\n");
printf("Mix_Init: %s\n", Mix_GetError());
return false;
}*/
// Initialize SDL_mixer
if (Mix_OpenAudio(frequency, format, channels, chunksize) < 0) {
printf("SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError());
return false;
}
this->frequency = frequency;
this->format = format;
this->channels = channels;
this->chunksize = chunksize;
return true;
}
void AudioHandler::quit() {
// Quit mixer
Mix_Quit();
}
uint8_t AudioHandler::play_sound(Sound sample, int loops, int channel) {
return Mix_PlayChannel(channel, sample, loops);
}
void AudioHandler::set_sound_volume(float volume) {
Mix_Volume(-1, convert_volume_float_to_int(volume));
}
void AudioHandler::set_sound_volume(uint8_t channel, float volume) {
if (channel < channels) {
Mix_Volume(channel, convert_volume_float_to_int(volume));
}
else {
printf("Failed to set sound volume! Channel number supplied does not exist.");
}
}
float AudioHandler::get_sound_volume(uint8_t channel) {
return convert_volume_int_to_float(Mix_Volume(channel, -1));
}
Sound AudioHandler::load_sound(std::string path) {
// Load sample
Sound sample = Mix_LoadWAV(path.c_str());
if (sample == NULL) {
// Failed to load
printf("Failed to load sound sample, path %s\nSDL_mixer Error: %s\n", path.c_str(), Mix_GetError());
}
return sample;
}
void AudioHandler::free_sound(Sound* sample) {
// Free sample
Mix_FreeChunk(*sample);
*sample = NULL;
}
void AudioHandler::play_music(Music music, int loops) {
Mix_PlayMusic(music, loops);
}
void AudioHandler::halt_music() {
Mix_HaltMusic();
}
bool AudioHandler::is_music_playing() {
return Mix_PlayingMusic();
}
bool AudioHandler::is_music_fading_in() {
return Mix_FadingMusic() == MIX_FADING_IN;
}
bool AudioHandler::is_music_fading_out() {
return Mix_FadingMusic() == MIX_FADING_OUT;
}
void AudioHandler::fade_music_in(Music music, float fade_time, int loops) {
Mix_FadeInMusic(music, loops, convert_s_to_ms_float_to_int(fade_time));
}
void AudioHandler::fade_music_out(float fade_time) {
Mix_FadeOutMusic(convert_s_to_ms_float_to_int(fade_time));
}
void AudioHandler::set_music_volume(float volume) {
Mix_VolumeMusic(convert_volume_float_to_int(volume));
}
float AudioHandler::get_music_volume() {
return convert_volume_int_to_float(Mix_VolumeMusic(-1));
}
Music AudioHandler::load_music(std::string path) {
// Load music
Music music = Mix_LoadMUS(path.c_str());
if (music == NULL) {
// Failed to load
printf("Failed to load music, path %s\nSDL_mixer Error: %s\n", path.c_str(), Mix_GetError());
}
return music;
}
void AudioHandler::free_music(Music* sample) {
// Free sample
Mix_FreeMusic(*sample);
*sample = NULL;
}
void AudioHandler::free_all_sounds() {
for (uint8_t i = 0; i < sound_samples.size(); i++) {
free_sound(&sound_samples.at(i));
}
sound_samples.clear();
}
void AudioHandler::free_all_music() {
for (uint8_t i = 0; i < music_samples.size(); i++) {
free_music(&music_samples.at(i));
}
music_samples.clear();
}
void AudioHandler::free_all() {
free_all_sounds();
free_all_music();
}
int AudioHandler::convert_volume_float_to_int(float volume) {
return static_cast<int>(volume * MIX_MAX_VOLUME);
}
float AudioHandler::convert_volume_int_to_float(int volume) {
return static_cast<float>(volume) / MIX_MAX_VOLUME;
}
int AudioHandler::convert_s_to_ms_float_to_int(float s) {
return static_cast<int>(s * 1000);
}