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