Changeset 3179 in orxonox.OLD for orxonox/branches/sound/src/sound_control.cc
- Timestamp:
- Dec 15, 2004, 3:40:54 AM (19 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
orxonox/branches/sound/src/sound_control.cc
r2972 r3179 18 18 using namespace std; 19 19 20 // global variables 21 SoundControl* SoundControl::instance = NULL; // singleton reference 20 int sfx_channel1 = -1; 21 int sfx_channel2 = -1; 22 int finished = 0; 23 SoundControl* SoundControl::sound = SoundControl::getInstance(); 24 SoundControl* SoundControl::instance = 0; 22 25 int volume = SDL_MIX_MAXVOLUME; 23 int done = 0;24 26 int track_number = 1; 25 static Mix_Music* music = NULL; 26 int audio_rate = MIX_DEFAULT_FREQUENCY, audio_channels = MIX_DEFAULT_CHANNELS, audio_buffers = 16384, bits = 0; 27 Mix_Music* music = NULL; 28 int audio_rate = 44100, audio_channels = MIX_DEFAULT_CHANNELS, 29 audio_buffers = 16384, bits = 0; 27 30 Uint16 audio_format = MIX_DEFAULT_FORMAT; 31 SDL_Event event; 28 32 29 33 30 34 /** 31 35 \brief standard constructor 32 33 This constructor builds a SoundControl Object, which waits for callers. 36 This constructor builds a SoundControl Object and initialises it . 34 37 All sound output is handled by this singleton object. 35 38 */ 36 SoundControl::SoundControl () { 37 38 /* 39 initializing sound and calling Mix_OpenAudio 40 if(SDL_Init(SDL_INIT_AUDIO)<0){ 39 SoundControl::SoundControl() { 40 if(SDL_Init(SDL_INIT_AUDIO)<0) { 41 41 printf("SDL_Init: INIT_AUDIO error.\n"); 42 42 } 43 */ 44 45 if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)){ 43 if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) { 46 44 printf("Mix_OpenAudio: Failed to open audio!\n"); 47 45 } 48 49 46 initialise(); 50 47 } 51 48 52 53 49 /** 54 50 \brief Default destructor 55 51 */ 56 SoundControl::~SoundControl () {52 SoundControl::~SoundControl() { 57 53 } 58 54 59 55 /** 60 \brief Returns a reference to the singleton56 \brief Returns a reference to the SoundControl singleton 61 57 */ 62 58 SoundControl* SoundControl::getInstance() { 63 if (instance == NULL) {59 if (instance == 0) { 64 60 instance = new SoundControl; 65 61 } … … 68 64 69 65 void SoundControl::deleteInstance() { 70 delete instance; 71 instance = NULL; 72 } 73 74 /** 75 \brief Is called by SoundControl object to initiate all values 66 } 67 68 /** 69 \brief Is called by SoundControl object to initiate all values and to output some text 76 70 */ 77 71 void SoundControl::initialise() { 78 79 // Print some info80 72 Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); 81 73 bits=audio_format&0xFF; … … 84 76 } 85 77 86 87 /** 88 \brief Sets the number of output Channels (should not be used) 89 */ 90 void SoundControl::setNumberOfChannels (int number_of_channels) { 78 /** 79 \brief Sets the number of output Channels 80 */ 81 void SoundControl::setNumberOfChannels(int number_of_channels) { 91 82 Mix_AllocateChannels(number_of_channels); 92 83 } 93 84 94 95 /** 96 \brief May be called from any WorldEntity to play a .xm file 85 /** 86 \brief Static function to play a .xm file 97 87 \param filename: self-explanatory 98 88 */ 99 int SoundControl::playMod(char* fileName) {89 void SoundControl::playMod(char* fileName) { 100 90 Mix_Chunk* chunk = NULL; 101 91 chunk = Mix_LoadWAV(fileName); … … 105 95 } 106 96 107 108 /** 109 \brief May be called from any WorldEntity to play a .wav file 97 /** 98 \brief Static function to play a .wav file 110 99 \param filename: self-explanatory 111 100 */ 112 int SoundControl::playWav(char* fileName) {101 void SoundControl::playWav(char* fileName) { 113 102 Mix_Chunk* chunk = NULL; 114 103 chunk = Mix_LoadWAV(fileName); … … 118 107 } 119 108 120 121 /** 122 \brief May be called from any WorldEntity to play a .ogg file 109 /** 110 \brief Static function to play an .ogg file 123 111 \param filename: self-explanatory 124 112 */ 125 int SoundControl::playOgg(char* fileName) {113 void SoundControl::playOgg(char* fileName) { 126 114 Mix_Music* music = NULL; 127 115 music = Mix_LoadMUS(fileName); 128 if(Mix_PlayMusic(music, 1) == -1) {116 if(Mix_PlayMusic(music, 1) == -1) { 129 117 printf("Mix_PlayMusic: %s\n",Mix_GetError()); 130 118 } … … 132 120 } 133 121 134 135 122 /** 136 123 \brief Heightens the overall volume of output 137 124 */ 138 void SoundControl::volumeUp 125 void SoundControl::volumeUp() { 139 126 volume = (volume + 1) << 1; 140 127 if(volume > SDL_MIX_MAXVOLUME) … … 147 134 \brief Lowers the overall volume of output 148 135 */ 149 void SoundControl::volumeDown 136 void SoundControl::volumeDown() { 150 137 volume >>= 1; 151 138 Mix_VolumeMusic(volume); 152 139 } 153 140 154 155 141 /** 156 142 \brief Rewinds music to the beginning 157 143 */ 158 void SoundControl::trackRewind 144 void SoundControl::trackRewind() { 159 145 Mix_RewindMusic(); 160 146 } 161 147 162 163 148 /** 164 149 \brief Rewinds the music 5 seconds 165 150 */ 166 void SoundControl::forwardMusic 151 void SoundControl::forwardMusic() { 167 152 Mix_SetMusicPosition(+5); 168 153 } 169 170 154 171 155 /** … … 176 160 } 177 161 178 179 162 /** 180 163 \brief Pauses music output 181 164 */ 182 void SoundControl::pauseMusic 165 void SoundControl::pauseMusic() { 183 166 Mix_PauseMusic(); 184 167 } 185 168 186 187 /** 188 \brief this function pauses music output 189 */ 190 void SoundControl::resumeMusic () { 169 /** 170 \brief Pauses music output 171 */ 172 void SoundControl::resumeMusic() { 191 173 Mix_ResumeMusic(); 192 174 } 193 175 194 176 /** 195 \brief Selects the track of all orxonox tracks 196 */ 197 void SoundControl::trackSelect() { 198 switch (track_number) { 199 case 1: 200 music = Mix_LoadMUS("luke_grey_orxonox1.ogg"); 201 if(Mix_PlayMusic(music, 1) == -1){ 202 printf("Mix_PlayMusic: %s\n",Mix_GetError()); 203 } 204 Mix_HookMusicFinished(musicDone); 205 break; 206 case 2: 207 music = Mix_LoadMUS("luke_grey_orxonox2.ogg"); 208 if(Mix_PlayMusic(music, 1) == -1){ 209 printf("Mix_PlayMusic: %s\n",Mix_GetError()); 210 } 211 Mix_HookMusicFinished(musicDone); 212 break; 213 case 3: 214 music = Mix_LoadMUS("luke_grey_orxonox3.ogg"); 215 if(Mix_PlayMusic(music, 1) == -1){ 216 printf("Mix_PlayMusic: %s\n",Mix_GetError()); 217 } 218 Mix_HookMusicFinished(musicDone); 219 break; 220 case 4: 221 music = Mix_LoadMUS("luke_grey_and_aquarius_orxonox.ogg"); 222 if(Mix_PlayMusic(music, 1) == -1){ 223 printf("Mix_PlayMusic: %s\n",Mix_GetError()); 224 } 225 Mix_HookMusicFinished(musicDone); 226 break; 227 case 5: 228 music = Mix_LoadMUS("nomenes_orxonox.ogg"); 229 if(Mix_PlayMusic(music, 1) == -1){ 230 printf("Mix_PlayMusic: %s\n",Mix_GetError()); 231 } 232 Mix_HookMusicFinished(musicDone); 233 break; 234 case 6: 235 music = Mix_LoadMUS("nomenes_funkadudu.ogg"); 236 if(Mix_PlayMusic(music, 1) == -1){ 237 printf("Mix_PlayMusic: %s\n",Mix_GetError()); 238 } 239 Mix_HookMusicFinished(musicDone); 240 break; 241 } 242 } 243 177 \brief Fades in music 178 */ 179 void fadeInMusic(int time) { 180 181 } 182 183 /** 184 \brief Fades out music 185 */ 186 void SoundControl::fadeOutMusic(int time) { 187 188 } 244 189 245 190 /** … … 251 196 music = NULL; 252 197 } 198 199 /** 200 \brief Handles input events 201 */ 202 void SoundControl::handleKey(SDL_KeyboardEvent key) { 203 switch(key.keysym.sym) { 204 case SDLK_a: 205 if(key.type == SDL_KEYDOWN) { 206 if(sfx_channel1 < 0) { 207 sfx_channel1 = 1; 208 sound->playWav("sound1.wav"); 209 } 210 } else { 211 Mix_HaltChannel(sfx_channel1); 212 sfx_channel1 = -1; 213 } 214 break; 215 case SDLK_s: 216 if(key.type == SDL_KEYDOWN) { 217 if(sfx_channel2 < 0) { 218 sfx_channel2 = 1; 219 sound->playWav("sound2.wav"); 220 } 221 } else { 222 Mix_HaltChannel(sfx_channel2); 223 sfx_channel2 = -1; 224 } 225 break; 226 case SDLK_m: 227 if(key.state == SDL_PRESSED) { 228 sound->playOgg("music.ogg"); 229 } 230 break; 231 case SDLK_q: 232 finished = 1; 233 break; 234 default: 235 break; 236 } 237 } 238 239 int SoundControl::main(int argc, char* argv[]) { 240 SDL_Surface* screen; 241 SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO); 242 screen = SDL_SetVideoMode(320, 240, 0, 0); 243 while(!finished) { 244 while(SDL_PollEvent(&event)) { 245 switch(event.type) { 246 case SDL_QUIT: 247 finished = 1; 248 break; 249 case SDL_KEYDOWN: 250 case SDL_KEYUP: 251 SoundControl::handleKey(event.key); 252 break; 253 default: 254 break; 255 } 256 } 257 SDL_Delay(50); 258 } 259 deleteInstance(); 260 SDL_Quit(); 261 return 0; 262 }
Note: See TracChangeset
for help on using the changeset viewer.