| 1 | /* | 
|---|
| 2 |  *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
| 3 |  *                    > www.orxonox.net < | 
|---|
| 4 |  * | 
|---|
| 5 |  * | 
|---|
| 6 |  *   License notice: | 
|---|
| 7 |  * | 
|---|
| 8 |  *   This program is free software; you can redistribute it and/or | 
|---|
| 9 |  *   modify it under the terms of the GNU General Public License | 
|---|
| 10 |  *   as published by the Free Software Foundation; either version 2 | 
|---|
| 11 |  *   of the License, or (at your option) any later version. | 
|---|
| 12 |  * | 
|---|
| 13 |  *   This program is distributed in the hope that it will be useful, | 
|---|
| 14 |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 16 |  *   GNU General Public License for more details. | 
|---|
| 17 |  * | 
|---|
| 18 |  *   You should have received a copy of the GNU General Public License | 
|---|
| 19 |  *   along with this program; if not, write to the Free Software | 
|---|
| 20 |  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
| 21 |  * | 
|---|
| 22 |  *   Author: | 
|---|
| 23 |  *      Reto Grieder | 
|---|
| 24 |  *   Co-authors: | 
|---|
| 25 |  *      Kevin Young | 
|---|
| 26 |  * | 
|---|
| 27 |  */ | 
|---|
| 28 |  | 
|---|
| 29 | #include "AmbientSound.h" | 
|---|
| 30 |  | 
|---|
| 31 | #include "core/CoreIncludes.h" | 
|---|
| 32 | #include "core/EventIncludes.h" | 
|---|
| 33 | #include "core/GameMode.h" | 
|---|
| 34 | #include "core/Resource.h" | 
|---|
| 35 | #include "core/XMLPort.h" | 
|---|
| 36 | #include "SoundManager.h" | 
|---|
| 37 | #include "SoundStreamer.h" | 
|---|
| 38 | #include "util/Sleep.h" | 
|---|
| 39 |  | 
|---|
| 40 | #include <AL/alut.h> | 
|---|
| 41 |  | 
|---|
| 42 | namespace orxonox | 
|---|
| 43 | { | 
|---|
| 44 |     CreateFactory(AmbientSound); | 
|---|
| 45 |      | 
|---|
| 46 |     // vorbis callbacks | 
|---|
| 47 |     size_t readVorbis(void* ptr, size_t size, size_t nmemb, void* datasource); | 
|---|
| 48 |     int seekVorbis(void* datasource, ogg_int64_t offset, int whence); | 
|---|
| 49 |     long tellVorbis(void* datasource); | 
|---|
| 50 |      | 
|---|
| 51 |     AmbientSound::AmbientSound(BaseObject* creator) | 
|---|
| 52 |         : BaseObject(creator) | 
|---|
| 53 |         , Synchronisable(creator) | 
|---|
| 54 |         , bPlayOnLoad_(false) | 
|---|
| 55 |     { | 
|---|
| 56 |         RegisterObject(AmbientSound); | 
|---|
| 57 |  | 
|---|
| 58 |         // Ambient sounds always fade in | 
|---|
| 59 |         //this->setVolume(0); | 
|---|
| 60 |         this->registerVariables(); | 
|---|
| 61 |     } | 
|---|
| 62 |  | 
|---|
| 63 |     void AmbientSound::preDestroy() | 
|---|
| 64 |     { | 
|---|
| 65 |         if (GameMode::playsSound()) | 
|---|
| 66 |         { | 
|---|
| 67 |             // Smoothly fade out by keeping a SmartPtr | 
|---|
| 68 |             SoundManager::getInstance().unregisterAmbientSound(this); | 
|---|
| 69 |             this->soundstreamthread_.interrupt(); | 
|---|
| 70 |         } | 
|---|
| 71 |     } | 
|---|
| 72 |  | 
|---|
| 73 |     void AmbientSound::registerVariables() | 
|---|
| 74 |     { | 
|---|
| 75 |         registerVariable(ambientSource_, ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::ambientSourceChanged)); | 
|---|
| 76 |         registerVariable(bLooping_,      ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::loopingChanged)); | 
|---|
| 77 |         registerVariable(pitch_,         ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::pitchChanged)); | 
|---|
| 78 |         registerVariable(bPlayOnLoad_,   ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::playOnLoadChanged)); | 
|---|
| 79 |     } | 
|---|
| 80 |  | 
|---|
| 81 |     void AmbientSound::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| 82 |     { | 
|---|
| 83 |         SUPER(AmbientSound, XMLPort, xmlelement, mode); | 
|---|
| 84 |         BaseSound::XMLPortExtern(xmlelement, mode); | 
|---|
| 85 |         XMLPortParam(AmbientSound, "ambientSource", setAmbientSource, getAmbientSource, xmlelement, mode); | 
|---|
| 86 |         XMLPortParam(AmbientSound, "playOnLoad", setPlayOnLoad, getPlayOnLoad, xmlelement, mode); | 
|---|
| 87 |     } | 
|---|
| 88 |  | 
|---|
| 89 |     void AmbientSound::XMLEventPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| 90 |     { | 
|---|
| 91 |         SUPER(AmbientSound, XMLEventPort, xmlelement, mode); | 
|---|
| 92 |         XMLPortEventState(AmbientSound, BaseObject, "play", play, xmlelement, mode); | 
|---|
| 93 |     } | 
|---|
| 94 |  | 
|---|
| 95 |     void AmbientSound::play() | 
|---|
| 96 |     { | 
|---|
| 97 |         if (GameMode::playsSound()) | 
|---|
| 98 |             SoundManager::getInstance().registerAmbientSound(this); | 
|---|
| 99 |     } | 
|---|
| 100 |  | 
|---|
| 101 |     void AmbientSound::stop() | 
|---|
| 102 |     { | 
|---|
| 103 |         if (GameMode::playsSound())  | 
|---|
| 104 |             SoundManager::getInstance().unregisterAmbientSound(this); | 
|---|
| 105 |     } | 
|---|
| 106 |  | 
|---|
| 107 |     void AmbientSound::pause() | 
|---|
| 108 |     { | 
|---|
| 109 |         if (GameMode::playsSound()) | 
|---|
| 110 |             SoundManager::getInstance().pauseAmbientSound(this); | 
|---|
| 111 |     } | 
|---|
| 112 |  | 
|---|
| 113 |     float AmbientSound::getRealVolume() | 
|---|
| 114 |     { | 
|---|
| 115 |         assert(GameMode::playsSound()); | 
|---|
| 116 |         return SoundManager::getInstance().getRealVolume(SoundType::Music); | 
|---|
| 117 |     } | 
|---|
| 118 |  | 
|---|
| 119 |     void AmbientSound::setAmbientSource(const std::string& source) | 
|---|
| 120 |     { | 
|---|
| 121 |         this->ambientSource_ = source; | 
|---|
| 122 |         this->moodChanged(this->getMood()); | 
|---|
| 123 |     } | 
|---|
| 124 |  | 
|---|
| 125 |     void AmbientSound::moodChanged(const std::string& mood) | 
|---|
| 126 |     { | 
|---|
| 127 |         if (GameMode::playsSound()) | 
|---|
| 128 |         { | 
|---|
| 129 |             const std::string& path = "ambient/" + MoodManager::getInstance().getMood() + '/' + this->ambientSource_; | 
|---|
| 130 |             shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(path); | 
|---|
| 131 |             if (fileInfo != NULL) | 
|---|
| 132 |                 this->setStreamSource(path); | 
|---|
| 133 |             else | 
|---|
| 134 |                 COUT(3) << "Sound: " << this->ambientSource_ << ": Not a valid name! Ambient sound will not change." << std::endl; | 
|---|
| 135 |         } | 
|---|
| 136 |     } | 
|---|
| 137 |  | 
|---|
| 138 |     void AmbientSound::setPlayOnLoad(bool val) | 
|---|
| 139 |     { | 
|---|
| 140 |         this->bPlayOnLoad_ = val; | 
|---|
| 141 |         if (val) | 
|---|
| 142 |             this->play(); | 
|---|
| 143 |     } | 
|---|
| 144 |  | 
|---|
| 145 |     void AmbientSound::changedActivity() | 
|---|
| 146 |     { | 
|---|
| 147 |         SUPER(AmbientSound, changedActivity); | 
|---|
| 148 |         if (this->isActive()) | 
|---|
| 149 |             this->play(); | 
|---|
| 150 |         else | 
|---|
| 151 |             this->stop(); | 
|---|
| 152 |     } | 
|---|
| 153 |  | 
|---|
| 154 |     // hacky solution for file streaming | 
|---|
| 155 |     void AmbientSound::setStreamSource(const std::string& source) | 
|---|
| 156 |     { | 
|---|
| 157 |         if (!GameMode::playsSound()) | 
|---|
| 158 |         { | 
|---|
| 159 |             this->source_ = source; | 
|---|
| 160 |             return; | 
|---|
| 161 |         } | 
|---|
| 162 |  | 
|---|
| 163 |         if(!alIsSource(this->audioSource_)) | 
|---|
| 164 |             this->audioSource_ = SoundManager::getInstance().getSoundSource(this); | 
|---|
| 165 |  | 
|---|
| 166 |         if (this->source_ == source) | 
|---|
| 167 |         { | 
|---|
| 168 |             return; | 
|---|
| 169 |         } | 
|---|
| 170 |  | 
|---|
| 171 |         this->source_ = source; | 
|---|
| 172 |         // Don't load "" | 
|---|
| 173 |         if (source_.empty()) | 
|---|
| 174 |             return; | 
|---|
| 175 |  | 
|---|
| 176 |         if (this->soundstreamthread_.get_id() != boost::thread::id()) | 
|---|
| 177 |         { | 
|---|
| 178 |             this->soundstreamthread_.interrupt(); // terminate an old thread if necessary | 
|---|
| 179 |         } | 
|---|
| 180 |  | 
|---|
| 181 |         // queue some init buffers | 
|---|
| 182 |         COUT(4) << "Sound: Creating thread for " << source << std::endl; | 
|---|
| 183 |         // Get resource info | 
|---|
| 184 |         shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(source); | 
|---|
| 185 |         if (fileInfo == NULL) | 
|---|
| 186 |         { | 
|---|
| 187 |             COUT(2) << "Sound: Warning: Sound file '" << source << "' not found" << std::endl; | 
|---|
| 188 |             return; | 
|---|
| 189 |         } | 
|---|
| 190 |         // Open data stream | 
|---|
| 191 |         DataStreamPtr dataStream = Resource::open(fileInfo); | 
|---|
| 192 |          | 
|---|
| 193 |         alSourcei(this->audioSource_, AL_BUFFER, 0); | 
|---|
| 194 |  | 
|---|
| 195 |         // Open file with custom streaming | 
|---|
| 196 |         ov_callbacks vorbisCallbacks; | 
|---|
| 197 |         vorbisCallbacks.read_func  = &readVorbis; | 
|---|
| 198 |         vorbisCallbacks.seek_func  = &seekVorbis; | 
|---|
| 199 |         vorbisCallbacks.tell_func  = &tellVorbis; | 
|---|
| 200 |         vorbisCallbacks.close_func = NULL; | 
|---|
| 201 |  | 
|---|
| 202 |         OggVorbis_File* vf = new OggVorbis_File(); | 
|---|
| 203 |         int ret = ov_open_callbacks(dataStream.get(), vf, NULL, 0, vorbisCallbacks); | 
|---|
| 204 |         if (ret < 0) | 
|---|
| 205 |         { | 
|---|
| 206 |             COUT(2) << "Sound: libvorbisfile: File does not seem to be an Ogg Vorbis bitstream" << std::endl; | 
|---|
| 207 |             ov_clear(vf); | 
|---|
| 208 |             return; | 
|---|
| 209 |         } | 
|---|
| 210 |         vorbis_info* vorbisInfo; | 
|---|
| 211 |         vorbisInfo = ov_info(vf, -1); | 
|---|
| 212 |         ALenum format; | 
|---|
| 213 |         if (vorbisInfo->channels == 1) | 
|---|
| 214 |             format = AL_FORMAT_MONO16; | 
|---|
| 215 |         else | 
|---|
| 216 |             format = AL_FORMAT_STEREO16; | 
|---|
| 217 |  | 
|---|
| 218 |         char inbuffer[4096]; | 
|---|
| 219 |         ALuint initbuffers[10]; | 
|---|
| 220 |         alGenBuffers(10, initbuffers); | 
|---|
| 221 |         if (ALint error = alGetError()) { | 
|---|
| 222 |             COUT(2) << "Sound: Streamer: Could not generate buffer:" << getALErrorString(error) << std::endl; | 
|---|
| 223 |             return; | 
|---|
| 224 |         } | 
|---|
| 225 |         int current_section; | 
|---|
| 226 |  | 
|---|
| 227 |         for(int i = 0; i < 10; i++) | 
|---|
| 228 |         { | 
|---|
| 229 |             long ret = ov_read(vf, inbuffer, sizeof(inbuffer), 0, 2, 1, ¤t_section); | 
|---|
| 230 |             if (ret == 0) | 
|---|
| 231 |             { | 
|---|
| 232 |                 break; | 
|---|
| 233 |             } | 
|---|
| 234 |             else if (ret < 0) | 
|---|
| 235 |             { | 
|---|
| 236 |                 COUT(2) << "Sound: libvorbisfile: error reading the file" << std::endl; | 
|---|
| 237 |                 ov_clear(vf); | 
|---|
| 238 |                 return; | 
|---|
| 239 |             } | 
|---|
| 240 |  | 
|---|
| 241 |             alBufferData(initbuffers[i], format, &inbuffer, ret, vorbisInfo->rate); | 
|---|
| 242 |             if(ALint error = alGetError()) { | 
|---|
| 243 |                 COUT(2) << "Sound: Could not fill buffer: " << getALErrorString(error) << std::endl; | 
|---|
| 244 |                 break; | 
|---|
| 245 |              } | 
|---|
| 246 |              alSourceQueueBuffers(this->audioSource_, 1, &initbuffers[i]); | 
|---|
| 247 |              if (ALint error = alGetError()) { | 
|---|
| 248 |                  COUT(2) << "Sound: Warning: Couldn't queue buffers: " << getALErrorString(error) << std::endl; | 
|---|
| 249 |              } | 
|---|
| 250 |         } | 
|---|
| 251 |          | 
|---|
| 252 |         this->soundstreamthread_ = boost::thread(SoundStreamer(), this->audioSource_, dataStream, vf, current_section); | 
|---|
| 253 |         if(this->soundstreamthread_ == boost::thread()) | 
|---|
| 254 |             COUT(2) << "Sound: Failed to create thread." << std::endl; | 
|---|
| 255 |         //SoundStreamer streamer; | 
|---|
| 256 |         //streamer(this->audioSource_, dataStream); | 
|---|
| 257 |  | 
|---|
| 258 |         alSource3f(this->audioSource_, AL_POSITION,  0, 0, 0); | 
|---|
| 259 |         alSource3f(this->audioSource_, AL_VELOCITY,  0, 0, 0); | 
|---|
| 260 |         alSource3f(this->audioSource_, AL_DIRECTION, 0, 0, 0); | 
|---|
| 261 |         if (ALint error = alGetError()) | 
|---|
| 262 |             COUT(2) << "Sound: Warning: Setting source parameters to 0 failed: " << getALErrorString(error) << std::endl; | 
|---|
| 263 |     } | 
|---|
| 264 |  | 
|---|
| 265 |     void AmbientSound::doStop() | 
|---|
| 266 |     { | 
|---|
| 267 |         BaseSound::doStop(); | 
|---|
| 268 |         this->soundstreamthread_.interrupt(); | 
|---|
| 269 |     } | 
|---|
| 270 |  | 
|---|
| 271 |     void AmbientSound::doPlay() | 
|---|
| 272 |     { | 
|---|
| 273 |         BaseSound::doPlay(); | 
|---|
| 274 |  | 
|---|
| 275 |         if(GameMode::playsSound() && this->getSourceState() != AL_PLAYING) | 
|---|
| 276 |         { | 
|---|
| 277 |             if(!alIsSource(this->audioSource_)) | 
|---|
| 278 |             { | 
|---|
| 279 |                 this->audioSource_ = SoundManager::getInstance().getSoundSource(this); | 
|---|
| 280 |                 if(!alIsSource(this->audioSource_)) | 
|---|
| 281 |                     return; | 
|---|
| 282 |                 this->initialiseSource(); | 
|---|
| 283 |             } | 
|---|
| 284 |  | 
|---|
| 285 |             alSourcePlay(this->audioSource_); | 
|---|
| 286 |             if(int error = alGetError()) | 
|---|
| 287 |                 COUT(2) << "Sound: Error playing sound: " << getALErrorString(error) << std::endl; | 
|---|
| 288 |         } | 
|---|
| 289 |     } | 
|---|
| 290 | } | 
|---|