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