forked from Milez213/CityTrial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDLSoundManager.h
175 lines (130 loc) · 3.81 KB
/
SDLSoundManager.h
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
#ifndef SDL_SOUND_MANAGER_H
#define SDL_SOUND_MANAGER_H
#include <stdio.h>
#include "SoundManager.h"
#include "SDL_mixer.h"
class SDLGameSoundSample : public GameSound {
public:
SDLGameSoundSample(const char* filename) {
m_channel = -1;
m_loops = 0; // play once
m_sound = Mix_LoadWAV(filename);
if (NULL == m_sound) {
fprintf(stderr, "*** Error loading sound \"%s\": %s\n",
filename,
Mix_GetError());
}
}
~SDLGameSoundSample() {
Mix_FreeChunk(m_sound);
Mix_HaltChannel(m_channel);
}
// Plays the sound.
// sound = ModelManager->getSound("woof.ogg");
// Sound sound("woof");
void play() {
m_channel = Mix_PlayChannel(-1, m_sound, m_loops);
}
void play(bool looped) {
m_channel = Mix_PlayChannel(-1, m_sound, looped? -1 : 0);
}
void fadeIn(int ms, int loops) {
m_channel = Mix_FadeInChannel(-1, m_sound, loops, ms);
}
void fadeOut(int ms) {
m_channel = Mix_FadeOutChannel(-1, ms);
}
void pause() {
Mix_Pause(m_channel);
}
void resume() {
Mix_Resume(m_channel);
}
void setVolume(int v){
Mix_Volume(m_channel,v);
}
// TODO - SDL_Mix can specify how many times to loop but not irrklang
private:
// a sound is pointed to by m_sound
// a sound plays on one of the channels, which SDL_Mix can choose
Mix_Chunk *m_sound;
int m_channel;
};
class SDLGameSoundMusic : public GameSound {
public:
SDLGameSoundMusic(const char* filename) {
m_loops = 0;
m_music = Mix_LoadMUS(filename);
if (NULL == m_music) {
fprintf(stderr, "*** Error loading sound \"%s\": %s\n",
filename,
Mix_GetError());
}
}
~SDLGameSoundMusic() {
Mix_HaltMusic();
Mix_FreeMusic(m_music);
}
// Plays the sound.
// sound = ModelManager->getSound("woof.ogg");
// Sound sound("woof");
virtual void play() {
Mix_PlayMusic(m_music, m_loops);
}
virtual void play(bool looped) {
Mix_PlayMusic(m_music, looped? -1 : 0);
}
virtual void pause() {
Mix_PauseMusic();
}
virtual void resume() {
Mix_ResumeMusic();
}
// TODO - SDL_Mix can specify how many times to loop but not irrklang
virtual void setLooped(bool looped) {
m_loops = looped? -1 : 0;
}
virtual bool isLooped() {
return m_loops == -1;
}
virtual void setVolume(int v){
Mix_VolumeMusic(v);
}
private:
Mix_Music *m_music;
};
class SDLSoundManager : public SoundManager {
public:
SDLSoundManager() {
printf("Initializing SDL_Mix sound\n");
int sample_rate = 22050;
Uint16 audio_format = AUDIO_S16;
int audio_channels = 2;
// smaller for more responsiveness, but needs more CPU
int audio_buffer_size = 1024;
Mix_Init(MIX_INIT_FLAC | MIX_INIT_OGG | MIX_INIT_MP3);
if (Mix_OpenAudio(sample_rate, audio_format, audio_channels,
audio_buffer_size)) {
fprintf(stderr, "SDL_Mix: Couldn't open audio device: %s\n",
Mix_GetError());
return;
}
printf("Initializing SDL_Mix sound successful\n");
}
~SDLSoundManager() {
Mix_CloseAudio();
}
// loads the sound. may cache it.
virtual GameSound* getMusic(const char* filename) {
// TODO - cache?
GameSound *sound = new SDLGameSoundMusic(filename);
return sound;
}
// loads the sound. may cache it.
virtual GameSound* getSample(const char* filename) {
// TODO - cache?
SDLGameSoundSample *sound = new SDLGameSoundSample(filename);
return sound;
}
};
#endif