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 | #include <string.h> |
---|
18 | #include "SDL_mixer.h" |
---|
19 | |
---|
20 | using namespace std; |
---|
21 | |
---|
22 | |
---|
23 | /** |
---|
24 | \brief standard constructor |
---|
25 | |
---|
26 | This constructor builds a SoundControl Object, which waits for callers |
---|
27 | calling its functions to output sound. All sound output is handles by this |
---|
28 | object, which must only be constructed once. |
---|
29 | */ |
---|
30 | SoundControl::SoundControl () { |
---|
31 | |
---|
32 | //setup parameters |
---|
33 | int audio_rate = MIX_DEFAULT_FREQUENCY, audio_channels = MIX_DEFAULT_CHANNELS, audio_buffers = 8192, bits = 0; |
---|
34 | Uint16 audio_format = MIX_DEFAULT_FORMAT; |
---|
35 | Mix_Music* music = NULL; |
---|
36 | |
---|
37 | //initializing sound and calling Mix_OpenAudio |
---|
38 | if(SDL_Init(SDL_INIT_AUDIO)<0){ |
---|
39 | printf("SDL_Init: \n"); |
---|
40 | exit(1); |
---|
41 | } |
---|
42 | |
---|
43 | if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)){ |
---|
44 | printf("Mix_OpenAudio: \n"); |
---|
45 | exit(1); |
---|
46 | } |
---|
47 | |
---|
48 | postConstruction(); |
---|
49 | } |
---|
50 | |
---|
51 | SoundControl::SoundControl (int audio_rate audiorate, Uint16 audio_format audioformat, int audio_channels audiochannels, int audio_buffers audiobuffers) { |
---|
52 | |
---|
53 | //setup parameters |
---|
54 | int audio_rate = audiorate, audio_channels = audiochannels, audio_buffers = audiobuffers, bits = 0; |
---|
55 | Uint16 audio_format = audioformat; |
---|
56 | Mix_Music* music = NULL; |
---|
57 | |
---|
58 | //initializing sound and calling Mix_OpenAudio |
---|
59 | if(SDL_Init(SDL_INIT_AUDIO)<0){ |
---|
60 | printf("SDL_Init: \n"); |
---|
61 | exit(1); |
---|
62 | } |
---|
63 | |
---|
64 | if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)){ |
---|
65 | printf("Mix_OpenAudio: \n"); |
---|
66 | exit(1); |
---|
67 | } |
---|
68 | |
---|
69 | postConstruction(); |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | /** |
---|
74 | \brief standard destructor |
---|
75 | */ |
---|
76 | SoundControl::~SoundControl () { |
---|
77 | SoundControl* SoundControl::singleton = NULL; |
---|
78 | } |
---|
79 | |
---|
80 | SoundControl* SoundControl::singleton = NULL; |
---|
81 | |
---|
82 | |
---|
83 | /** |
---|
84 | \brief Is called by SoundControl object to initiate all values |
---|
85 | */ |
---|
86 | void SoundControl::postConstruction () { |
---|
87 | |
---|
88 | // Print some info |
---|
89 | Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); |
---|
90 | bits=audio_format&0xFF; |
---|
91 | printf("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate, bits, audio_channels > 1 ? "stereo" : "mono", audio_buffers ); |
---|
92 | |
---|
93 | int volume = SDL_MIX_MAXVOLUME; |
---|
94 | Mix_VolumeMusic(volume); |
---|
95 | int track_number = 1; |
---|
96 | int done = 0; |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | /** |
---|
101 | \brief Sets the number of output Channels |
---|
102 | */ |
---|
103 | void SoundControl::setNumberOfChannels (int number_of_channels) { |
---|
104 | Mix_AllocateChannels(number_of_channels); |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | /** |
---|
109 | \brief May be called from any WorldEntity to play a .xm file |
---|
110 | \param filename: self-explanatory |
---|
111 | */ |
---|
112 | int SoundControl::playMod (char* fileName) { |
---|
113 | Mix_Chunk* chunk = NULL; |
---|
114 | chunk = Mix_LoadWAV(fileName); |
---|
115 | if(Mix_PlayChannel(-1, chunk, 0) == -1) { |
---|
116 | printf("Mix_PlayChannel: %s\n", Mix_GetError()); |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | /** |
---|
122 | \brief May be called from any WorldEntity to play a .wav file |
---|
123 | \param filename: self-explanatory |
---|
124 | */ |
---|
125 | int SoundControl::playWav (char* fileName) { |
---|
126 | Mix_Chunk* chunk = NULL; |
---|
127 | chunk = Mix_LoadWAV(fileName); |
---|
128 | if(Mix_PlayChannel(-1, chunk, 0) == -1) { |
---|
129 | printf("Mix_PlayChannel: %s\n", Mix_GetError()); |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | /** |
---|
135 | \brief May be called from any WorldEntity to play a .ogg file |
---|
136 | \param filename: self-explanatory |
---|
137 | */ |
---|
138 | int SoundControl::playOgg (char* fileName) { |
---|
139 | Mix_Music* music = NULL; |
---|
140 | music = Mix_LoadMUS(fileName); |
---|
141 | if(Mix_PlayMusic(music, 1) == -1){ |
---|
142 | printf("Mix_PlayMusic: %s\n",Mix_GetError()); |
---|
143 | } |
---|
144 | Mix_HookMusicFinished(musicDone); |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | /** |
---|
149 | \brief Heightens the overall volume of output |
---|
150 | */ |
---|
151 | void SoundControl::volumeUp () { |
---|
152 | volume = (volume + 1) << 1; |
---|
153 | if(volume > SDL_MIX_MAXVOLUME) |
---|
154 | volume = SDL_MIX_MAXVOLUME; |
---|
155 | Mix_VolumeMusic(volume); |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | /** |
---|
160 | \brief Lowers the overall volume of output |
---|
161 | */ |
---|
162 | void SoundControl::volumeDown () { |
---|
163 | volume >>= 1; |
---|
164 | Mix_VolumeMusic(volume); |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | /** |
---|
169 | \brief Rewinds music to the beginning |
---|
170 | */ |
---|
171 | void SoundControl::trackRewind () { |
---|
172 | Mix_RewindMusic(); |
---|
173 | } |
---|
174 | |
---|
175 | |
---|
176 | /** |
---|
177 | \brief Rewinds the music 5 seconds |
---|
178 | */ |
---|
179 | void SoundControl::forwardMusic () { |
---|
180 | Mix_SetMusicPosition(+5); |
---|
181 | } |
---|
182 | |
---|
183 | |
---|
184 | /** |
---|
185 | \brief Forwards the music 5 seconds |
---|
186 | */ |
---|
187 | void SoundControl::rewindMusic () { |
---|
188 | Mix_SetMusicPosition(-5); |
---|
189 | } |
---|
190 | |
---|
191 | |
---|
192 | /** |
---|
193 | \brief Pauses music output |
---|
194 | */ |
---|
195 | void SoundControl::pauseMusic () { |
---|
196 | Mix_PauseMusic() |
---|
197 | } |
---|
198 | |
---|
199 | |
---|
200 | /** |
---|
201 | \brief this function pauses music output |
---|
202 | */ |
---|
203 | void SoundControl::resumeMusic () { |
---|
204 | Mix_ResumeMusic() |
---|
205 | } |
---|
206 | |
---|
207 | /** |
---|
208 | \brief Selects the track of all orxonox tracks |
---|
209 | */ |
---|
210 | void SoundControl::trackSelect () { |
---|
211 | switch (track_number) { |
---|
212 | case 1: |
---|
213 | music = Mix_LoadMUS("luke_grey_orxonox1.ogg"); |
---|
214 | break; |
---|
215 | case 2: |
---|
216 | music = Mix_LoadMUS("luke_grey_orxonox2.ogg"); |
---|
217 | break; |
---|
218 | case 3: |
---|
219 | music = Mix_LoadMUS("luke_grey_orxonox3.ogg"); |
---|
220 | break; |
---|
221 | case 4: |
---|
222 | music = Mix_LoadMUS("luke_grey_and aquarius_orxonox.ogg"); |
---|
223 | break; |
---|
224 | case 5: |
---|
225 | music = Mix_LoadMUS("nomenes_orxonox.ogg"); |
---|
226 | break; |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | |
---|
231 | /** |
---|
232 | \brief Hooked by playOgg at end of .ogg playback |
---|
233 | */ |
---|
234 | void SoundControl::musicDone () { |
---|
235 | track_number++; |
---|
236 | Mix_HaltMusic(); |
---|
237 | Mix_FreeMusic(music); |
---|
238 | music = NULL; |
---|
239 | } |
---|