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