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 | static SoundControl* instance = NULL; |
---|
24 | static SoundControl* sound = SoundControl::getInstance(); |
---|
25 | int volume = SDL_MIX_MAXVOLUME; |
---|
26 | int track_number = 1; |
---|
27 | static 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 | |
---|
37 | This constructor builds a SoundControl Object, which waits for callers. |
---|
38 | All sound output is handled by this singleton object. |
---|
39 | */ |
---|
40 | SoundControl::SoundControl () { |
---|
41 | if(SDL_Init(SDL_INIT_AUDIO)<0) { |
---|
42 | printf("SDL_Init: INIT_AUDIO error.\n"); |
---|
43 | } |
---|
44 | if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) { |
---|
45 | printf("Mix_OpenAudio: Failed to open audio!\n"); |
---|
46 | } |
---|
47 | initialise(); |
---|
48 | } |
---|
49 | |
---|
50 | /** |
---|
51 | \brief Default destructor |
---|
52 | */ |
---|
53 | SoundControl::~SoundControl () { |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | \brief Returns a reference to the singleton |
---|
58 | */ |
---|
59 | SoundControl* SoundControl::getInstance() { |
---|
60 | if (instance == NULL) { |
---|
61 | instance = new SoundControl; |
---|
62 | } |
---|
63 | return instance; |
---|
64 | } |
---|
65 | |
---|
66 | void SoundControl::deleteInstance() { |
---|
67 | delete instance; |
---|
68 | instance = NULL; |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | \brief Is called by SoundControl object to initiate all values and to output some text |
---|
73 | */ |
---|
74 | void SoundControl::initialise() { |
---|
75 | Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); |
---|
76 | bits=audio_format&0xFF; |
---|
77 | printf("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate, bits, audio_channels > 1 ? "stereo" : "mono", audio_buffers ); |
---|
78 | Mix_VolumeMusic(volume); |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | \brief Sets the number of output Channels |
---|
83 | */ |
---|
84 | void SoundControl::setNumberOfChannels (int number_of_channels) { |
---|
85 | Mix_AllocateChannels(number_of_channels); |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | \brief May be called from any WorldEntity to play a .xm file |
---|
90 | \param filename: self-explanatory |
---|
91 | */ |
---|
92 | void SoundControl::playMod (char* fileName) { |
---|
93 | Mix_Chunk* chunk = NULL; |
---|
94 | chunk = Mix_LoadWAV(fileName); |
---|
95 | if(Mix_PlayChannel(-1, chunk, 0) == -1) { |
---|
96 | printf("Mix_PlayChannel: %s\n", Mix_GetError()); |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | \brief May be called from any WorldEntity to play a .wav file |
---|
102 | \param filename: self-explanatory |
---|
103 | */ |
---|
104 | void SoundControl::playWav (char* fileName) { |
---|
105 | Mix_Chunk* chunk = NULL; |
---|
106 | chunk = Mix_LoadWAV(fileName); |
---|
107 | if(Mix_PlayChannel(-1, chunk, 0) == -1) { |
---|
108 | printf("Mix_PlayChannel: %s\n", Mix_GetError()); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | \brief May be called from any WorldEntity to play a .ogg file |
---|
114 | \param filename: self-explanatory |
---|
115 | */ |
---|
116 | void SoundControl::playOgg (char* fileName) { |
---|
117 | Mix_Music* music = NULL; |
---|
118 | music = Mix_LoadMUS(fileName); |
---|
119 | if(Mix_PlayMusic(music, 1) == -1) { |
---|
120 | printf("Mix_PlayMusic: %s\n",Mix_GetError()); |
---|
121 | } |
---|
122 | Mix_HookMusicFinished(musicDone); |
---|
123 | } |
---|
124 | |
---|
125 | /** |
---|
126 | \brief Heightens the overall volume of output |
---|
127 | */ |
---|
128 | void SoundControl::volumeUp () { |
---|
129 | volume = (volume + 1) << 1; |
---|
130 | if(volume > SDL_MIX_MAXVOLUME) |
---|
131 | volume = SDL_MIX_MAXVOLUME; |
---|
132 | Mix_VolumeMusic(volume); |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | /** |
---|
137 | \brief Lowers the overall volume of output |
---|
138 | */ |
---|
139 | void SoundControl::volumeDown () { |
---|
140 | volume >>= 1; |
---|
141 | Mix_VolumeMusic(volume); |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | \brief Rewinds music to the beginning |
---|
146 | */ |
---|
147 | void SoundControl::trackRewind () { |
---|
148 | Mix_RewindMusic(); |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | \brief Rewinds the music 5 seconds |
---|
153 | */ |
---|
154 | void SoundControl::forwardMusic () { |
---|
155 | Mix_SetMusicPosition(+5); |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | \brief Forwards the music 5 seconds |
---|
160 | */ |
---|
161 | void SoundControl::rewindMusic () { |
---|
162 | Mix_SetMusicPosition(-5); |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | \brief Pauses music output |
---|
167 | */ |
---|
168 | void SoundControl::pauseMusic () { |
---|
169 | Mix_PauseMusic(); |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | \brief this function pauses music output |
---|
174 | */ |
---|
175 | void SoundControl::resumeMusic () { |
---|
176 | Mix_ResumeMusic(); |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | \brief Hooked by playOgg at end of .ogg playback |
---|
181 | */ |
---|
182 | void SoundControl::musicDone() { |
---|
183 | Mix_HaltMusic(); |
---|
184 | Mix_FreeMusic(music); |
---|
185 | music = NULL; |
---|
186 | } |
---|
187 | |
---|
188 | /** |
---|
189 | \brief Handles input events |
---|
190 | */ |
---|
191 | void SoundControl::handleKey(SDL_KeyboardEvent key) { |
---|
192 | switch(key.keysym.sym) { |
---|
193 | case SDLK_a: |
---|
194 | if(key.type == SDL_KEYDOWN) { |
---|
195 | if(sfx_channel1 < 0) { |
---|
196 | sfx_channel1 = sound->playWav("sound1.wav"); |
---|
197 | } |
---|
198 | } else { |
---|
199 | Mix_HaltChannel(sfx_channel1); |
---|
200 | sfx_channel1 = -1; |
---|
201 | } |
---|
202 | break; |
---|
203 | case SDLK_s: |
---|
204 | if(key.type == SDL_KEYDOWN) { |
---|
205 | if(sfx_channel2 < 0) { |
---|
206 | sfx_channel2 = sound->playWav("sound2.wav"); |
---|
207 | } |
---|
208 | } else { |
---|
209 | Mix_HaltChannel(sfx_channel2); |
---|
210 | sfx_channel2 = -1; |
---|
211 | } |
---|
212 | break; |
---|
213 | case SDLK_m: |
---|
214 | if(key.state == SDL_PRESSED) { |
---|
215 | sound->playOgg("music.ogg"); |
---|
216 | } |
---|
217 | break; |
---|
218 | case SDLK_q: |
---|
219 | finished = 1; |
---|
220 | break; |
---|
221 | default: |
---|
222 | break; |
---|
223 | } |
---|
224 | } |
---|
225 | |
---|
226 | int SoundControl::main(void) { |
---|
227 | SDL_Surface* screen; |
---|
228 | SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO); |
---|
229 | screen = SDL_SetVideoMode(320, 240, 0, 0); |
---|
230 | while(!finished) { |
---|
231 | while(SDL_PollEvent(&event)) { |
---|
232 | switch(event.type) { |
---|
233 | case SDL_QUIT: |
---|
234 | finished = 1; |
---|
235 | break; |
---|
236 | case SDL_KEYDOWN: |
---|
237 | case SDL_KEYUP: |
---|
238 | SoundControl::handleKey(event.key); |
---|
239 | break; |
---|
240 | default: |
---|
241 | break; |
---|
242 | } |
---|
243 | } |
---|
244 | SDL_Delay(50); |
---|
245 | } |
---|
246 | deleteInstance(); |
---|
247 | SDL_Quit(); |
---|
248 | return 0; |
---|
249 | } |
---|