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 | |
---|
19 | using namespace std; |
---|
20 | |
---|
21 | SoundControl* SoundControl::sound = SoundControl::getInstance(); |
---|
22 | SoundControl* SoundControl::singletonRef = NULL; |
---|
23 | |
---|
24 | |
---|
25 | /** |
---|
26 | \brief standard constructor |
---|
27 | This constructor builds a SoundControl Object and initialises it . |
---|
28 | All sound output is handled by this singleton object. |
---|
29 | */ |
---|
30 | SoundControl::SoundControl() { |
---|
31 | this->init(); |
---|
32 | |
---|
33 | } |
---|
34 | |
---|
35 | /** |
---|
36 | \brief Default destructor |
---|
37 | */ |
---|
38 | SoundControl::~SoundControl() |
---|
39 | { |
---|
40 | singletonRef = NULL; |
---|
41 | } |
---|
42 | |
---|
43 | /** |
---|
44 | \brief Returns a reference to the SoundControl singleton |
---|
45 | */ |
---|
46 | SoundControl* SoundControl::getInstance() { |
---|
47 | if (SoundControl::singletonRef) |
---|
48 | return singletonRef; |
---|
49 | else |
---|
50 | return singletonRef = new SoundControl; |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | \brief Is called by SoundControl object to initiate all values and to output some text |
---|
55 | */ |
---|
56 | void SoundControl::init(void) |
---|
57 | { |
---|
58 | this->sfx_channel1 = -1; |
---|
59 | this->sfx_channel2 = -1; |
---|
60 | this->finished = 0; |
---|
61 | this->volume = SDL_MIX_MAXVOLUME; |
---|
62 | this->track_number = 1; |
---|
63 | this->music = NULL; |
---|
64 | this->audio_rate = 44100; |
---|
65 | this->audio_channels = MIX_DEFAULT_CHANNELS; |
---|
66 | this->audio_buffers = 16384; |
---|
67 | this->bits = 0; |
---|
68 | this->audio_format = MIX_DEFAULT_FORMAT; |
---|
69 | |
---|
70 | // initialize SDL |
---|
71 | this->isInit = false; |
---|
72 | if (SDL_Init(SDL_INIT_AUDIO)) |
---|
73 | { |
---|
74 | PRINTF(1)("SDL-sound could not be initialized\n"); |
---|
75 | return; |
---|
76 | } |
---|
77 | else |
---|
78 | this->isInit = true; |
---|
79 | |
---|
80 | Mix_QuerySpec(&this->audio_rate, &this->audio_format, &this->audio_channels); |
---|
81 | this->bits=this->audio_format&0xFF; |
---|
82 | PRINTF(3)("Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", this->audio_rate, this->bits, this->audio_channels > 1 ? "stereo" : "mono", this->audio_buffers ); |
---|
83 | Mix_VolumeMusic(this->volume); |
---|
84 | |
---|
85 | if(Mix_OpenAudio(this->audio_rate, this->audio_format, this->audio_channels, this->audio_buffers)) |
---|
86 | PRINTF(1)("Mix_OpenAudio: Failed to open audio!\n"); |
---|
87 | |
---|
88 | |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | \brief Sets the number of output Channels |
---|
93 | */ |
---|
94 | void SoundControl::setNumberOfChannels(int number_of_channels) { |
---|
95 | Mix_AllocateChannels(number_of_channels); |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | \brief Static function to play a .xm file |
---|
100 | \param filename: self-explanatory |
---|
101 | */ |
---|
102 | void SoundControl::playMod(char* fileName) { |
---|
103 | Mix_Chunk* chunk = Mix_LoadWAV(fileName); |
---|
104 | if(Mix_PlayChannel(-1, chunk, 0) == -1) { |
---|
105 | printf("Mix_PlayChannel: %s\n", Mix_GetError()); |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | \brief Static function to play a .wav file |
---|
111 | \param filename: self-explanatory |
---|
112 | */ |
---|
113 | void SoundControl::playWav(char* fileName) { |
---|
114 | Mix_Chunk* 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 | \brief Static function to play an .ogg file |
---|
122 | \param filename: self-explanatory |
---|
123 | */ |
---|
124 | void SoundControl::playOgg(char* fileName) { |
---|
125 | Mix_Music* music = Mix_LoadMUS(fileName); |
---|
126 | if(Mix_PlayMusic(music, 1) == -1) { |
---|
127 | printf("Mix_PlayMusic: %s\n",Mix_GetError()); |
---|
128 | } |
---|
129 | Mix_HookMusicFinished(musicDone); |
---|
130 | } |
---|
131 | |
---|
132 | /** |
---|
133 | \brief Heightens the overall volume of output |
---|
134 | */ |
---|
135 | void SoundControl::volumeUp() { |
---|
136 | this->volume = (this->volume++) << 1; |
---|
137 | if(this->volume > SDL_MIX_MAXVOLUME) |
---|
138 | this->volume = SDL_MIX_MAXVOLUME; |
---|
139 | Mix_VolumeMusic(this->volume); |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | /** |
---|
144 | \brief Lowers the overall volume of output |
---|
145 | */ |
---|
146 | void SoundControl::volumeDown() { |
---|
147 | this->volume >>= 1; |
---|
148 | if(this->volume < 0) |
---|
149 | this->volume = 1; |
---|
150 | Mix_VolumeMusic(this->volume); |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | \brief Rewinds music to the beginning |
---|
155 | */ |
---|
156 | void SoundControl::trackRewind() { |
---|
157 | Mix_RewindMusic(); |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | \brief Rewinds the music 5 seconds |
---|
162 | */ |
---|
163 | void SoundControl::forwardMusic() { |
---|
164 | Mix_SetMusicPosition(+5); |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | \brief Forwards the music 5 seconds |
---|
169 | */ |
---|
170 | void SoundControl::rewindMusic () { |
---|
171 | Mix_SetMusicPosition(-5); |
---|
172 | } |
---|
173 | |
---|
174 | /** |
---|
175 | \brief Pauses music output |
---|
176 | */ |
---|
177 | void SoundControl::pauseMusic() { |
---|
178 | Mix_PauseMusic(); |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | \brief Pauses music output |
---|
183 | */ |
---|
184 | void SoundControl::resumeMusic() { |
---|
185 | Mix_ResumeMusic(); |
---|
186 | } |
---|
187 | |
---|
188 | /** |
---|
189 | \brief Fades in music |
---|
190 | */ |
---|
191 | void fadeInMusic(int time) { |
---|
192 | |
---|
193 | } |
---|
194 | |
---|
195 | /** |
---|
196 | \brief Fades out music |
---|
197 | */ |
---|
198 | void SoundControl::fadeOutMusic(int time) { |
---|
199 | |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | \brief Hooked by playOgg at end of .ogg playback |
---|
204 | */ |
---|
205 | void SoundControl::musicDone() |
---|
206 | { |
---|
207 | Mix_Music* music = SoundControl::getInstance()->music; |
---|
208 | Mix_HaltMusic(); |
---|
209 | Mix_FreeMusic(music); |
---|
210 | music = NULL; |
---|
211 | } |
---|
212 | |
---|