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