1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Simon Hofmann |
---|
13 | co-programmer: |
---|
14 | */ |
---|
15 | |
---|
16 | #include "sound_control.h" |
---|
17 | |
---|
18 | using namespace std; |
---|
19 | |
---|
20 | int sfx_channel1 = -1; |
---|
21 | int sfx_channel2 = -1; |
---|
22 | int finished = 0; |
---|
23 | SoundControl* SoundControl::sound = SoundControl::getInstance(); |
---|
24 | SoundControl* SoundControl::singletonRef = 0; |
---|
25 | int volume = SDL_MIX_MAXVOLUME; |
---|
26 | int track_number = 1; |
---|
27 | Mix_Music* music = NULL; |
---|
28 | int audio_rate = 44100, audio_channels = MIX_DEFAULT_CHANNELS, |
---|
29 | audio_buffers = 16384, bits = 0; |
---|
30 | Uint16 audio_format = MIX_DEFAULT_FORMAT; |
---|
31 | SDL_Event event; |
---|
32 | |
---|
33 | |
---|
34 | /** |
---|
35 | \brief standard constructor |
---|
36 | This constructor builds a SoundControl Object and initialises it . |
---|
37 | All sound output is handled by this singleton object. |
---|
38 | */ |
---|
39 | SoundControl::SoundControl() { |
---|
40 | if(SDL_Init(SDL_INIT_AUDIO)<0) { |
---|
41 | printf("SDL_Init: INIT_AUDIO error.\n"); |
---|
42 | } |
---|
43 | if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) { |
---|
44 | printf("Mix_OpenAudio: Failed to open audio!\n"); |
---|
45 | } |
---|
46 | initialise(); |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | \brief Default destructor |
---|
51 | */ |
---|
52 | SoundControl::~SoundControl() { |
---|
53 | } |
---|
54 | |
---|
55 | /** |
---|
56 | \brief Returns a reference to the SoundControl singleton |
---|
57 | */ |
---|
58 | SoundControl* SoundControl::getInstance() { |
---|
59 | if (!SoundControl::singletonRef) |
---|
60 | return singletonRef; |
---|
61 | else |
---|
62 | return singletonRef = new SoundControl; |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | \brief Is called by SoundControl object to initiate all values and to output some text |
---|
67 | */ |
---|
68 | void SoundControl::initialise() { |
---|
69 | Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); |
---|
70 | bits=audio_format&0xFF; |
---|
71 | printf("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate, bits, audio_channels > 1 ? "stereo" : "mono", audio_buffers ); |
---|
72 | Mix_VolumeMusic(volume); |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | \brief Sets the number of output Channels |
---|
77 | */ |
---|
78 | void SoundControl::setNumberOfChannels(int number_of_channels) { |
---|
79 | Mix_AllocateChannels(number_of_channels); |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | \brief Static function to play a .xm file |
---|
84 | \param filename: self-explanatory |
---|
85 | */ |
---|
86 | void SoundControl::playMod(char* fileName) { |
---|
87 | Mix_Chunk* chunk = NULL; |
---|
88 | chunk = Mix_LoadWAV(fileName); |
---|
89 | if(Mix_PlayChannel(-1, chunk, 0) == -1) { |
---|
90 | printf("Mix_PlayChannel: %s\n", Mix_GetError()); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | \brief Static function to play a .wav file |
---|
96 | \param filename: self-explanatory |
---|
97 | */ |
---|
98 | void SoundControl::playWav(char* fileName) { |
---|
99 | Mix_Chunk* chunk = NULL; |
---|
100 | chunk = Mix_LoadWAV(fileName); |
---|
101 | if(Mix_PlayChannel(-1, chunk, 0) == -1) { |
---|
102 | printf("Mix_PlayChannel: %s\n", Mix_GetError()); |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | \brief Static function to play an .ogg file |
---|
108 | \param filename: self-explanatory |
---|
109 | */ |
---|
110 | void SoundControl::playOgg(char* fileName) { |
---|
111 | Mix_Music* music = NULL; |
---|
112 | music = Mix_LoadMUS(fileName); |
---|
113 | if(Mix_PlayMusic(music, 1) == -1) { |
---|
114 | printf("Mix_PlayMusic: %s\n",Mix_GetError()); |
---|
115 | } |
---|
116 | Mix_HookMusicFinished(musicDone); |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | \brief Heightens the overall volume of output |
---|
121 | */ |
---|
122 | void SoundControl::volumeUp() { |
---|
123 | volume = (volume + 1) << 1; |
---|
124 | if(volume > SDL_MIX_MAXVOLUME) |
---|
125 | volume = SDL_MIX_MAXVOLUME; |
---|
126 | Mix_VolumeMusic(volume); |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | /** |
---|
131 | \brief Lowers the overall volume of output |
---|
132 | */ |
---|
133 | void SoundControl::volumeDown() { |
---|
134 | volume >>= 1; |
---|
135 | Mix_VolumeMusic(volume); |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | \brief Rewinds music to the beginning |
---|
140 | */ |
---|
141 | void SoundControl::trackRewind() { |
---|
142 | Mix_RewindMusic(); |
---|
143 | } |
---|
144 | |
---|
145 | /** |
---|
146 | \brief Rewinds the music 5 seconds |
---|
147 | */ |
---|
148 | void SoundControl::forwardMusic() { |
---|
149 | Mix_SetMusicPosition(+5); |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | \brief Forwards the music 5 seconds |
---|
154 | */ |
---|
155 | void SoundControl::rewindMusic () { |
---|
156 | Mix_SetMusicPosition(-5); |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | \brief Pauses music output |
---|
161 | */ |
---|
162 | void SoundControl::pauseMusic() { |
---|
163 | Mix_PauseMusic(); |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | \brief Pauses music output |
---|
168 | */ |
---|
169 | void SoundControl::resumeMusic() { |
---|
170 | Mix_ResumeMusic(); |
---|
171 | } |
---|
172 | |
---|
173 | /** |
---|
174 | \brief Fades in music |
---|
175 | */ |
---|
176 | void fadeInMusic(int time) { |
---|
177 | |
---|
178 | } |
---|
179 | |
---|
180 | /** |
---|
181 | \brief Fades out music |
---|
182 | */ |
---|
183 | void SoundControl::fadeOutMusic(int time) { |
---|
184 | |
---|
185 | } |
---|
186 | |
---|
187 | /** |
---|
188 | \brief Hooked by playOgg at end of .ogg playback |
---|
189 | */ |
---|
190 | void SoundControl::musicDone() { |
---|
191 | Mix_HaltMusic(); |
---|
192 | Mix_FreeMusic(music); |
---|
193 | music = NULL; |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | \brief Handles input events |
---|
198 | */ |
---|
199 | void SoundControl::handleKey(SDL_KeyboardEvent key) { |
---|
200 | SoundControl* sound = SoundControl::getInstance(); |
---|
201 | |
---|
202 | switch(key.keysym.sym) { |
---|
203 | case SDLK_a: |
---|
204 | if(key.type == SDL_KEYDOWN) { |
---|
205 | if(sound->sfx_channel1 < 0) { |
---|
206 | sound->sfx_channel1 = 1; |
---|
207 | sound->playWav("sound1.wav"); |
---|
208 | } |
---|
209 | } else { |
---|
210 | Mix_HaltChannel(sound->sfx_channel1); |
---|
211 | sound->sfx_channel1 = -1; |
---|
212 | } |
---|
213 | break; |
---|
214 | case SDLK_s: |
---|
215 | if(key.type == SDL_KEYDOWN) { |
---|
216 | if(sound->sfx_channel2 < 0) { |
---|
217 | sound->sfx_channel2 = 1; |
---|
218 | sound->playWav("sound2.wav"); |
---|
219 | } |
---|
220 | } else { |
---|
221 | Mix_HaltChannel(sound->sfx_channel2); |
---|
222 | sound->sfx_channel2 = -1; |
---|
223 | } |
---|
224 | break; |
---|
225 | case SDLK_m: |
---|
226 | if(key.state == SDL_PRESSED) { |
---|
227 | sound->playOgg("music.ogg"); |
---|
228 | } |
---|
229 | break; |
---|
230 | case SDLK_q: |
---|
231 | sound->finished = 1; |
---|
232 | break; |
---|
233 | default: |
---|
234 | break; |
---|
235 | } |
---|
236 | } |
---|
237 | |
---|
238 | int main(int argc, char* argv[]) { |
---|
239 | SDL_Surface* screen; |
---|
240 | SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO); |
---|
241 | screen = SDL_SetVideoMode(320, 240, 0, 0); |
---|
242 | while(!finished) { |
---|
243 | while(SDL_PollEvent(&event)) { |
---|
244 | switch(event.type) { |
---|
245 | case SDL_QUIT: |
---|
246 | finished = 1; |
---|
247 | break; |
---|
248 | case SDL_KEYDOWN: |
---|
249 | case SDL_KEYUP: |
---|
250 | SoundControl::handleKey(event.key); |
---|
251 | break; |
---|
252 | default: |
---|
253 | break; |
---|
254 | } |
---|
255 | } |
---|
256 | SDL_Delay(50); |
---|
257 | } |
---|
258 | delete SoundControl::getInstance(); |
---|
259 | SDL_Quit(); |
---|
260 | return 0; |
---|
261 | } |
---|