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