| [3060] | 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: |
|---|
| [5896] | 23 | * Reto Grieder |
|---|
| [3060] | 24 | * Co-authors: |
|---|
| [6767] | 25 | * Kevin Young |
|---|
| [3060] | 26 | * |
|---|
| 27 | */ |
|---|
| [3196] | 28 | |
|---|
| [5896] | 29 | #include "AmbientSound.h" |
|---|
| [3196] | 30 | |
|---|
| [5914] | 31 | #include "core/CoreIncludes.h" |
|---|
| [5896] | 32 | #include "core/EventIncludes.h" |
|---|
| [6417] | 33 | #include "core/GameMode.h" |
|---|
| 34 | #include "core/Resource.h" |
|---|
| [5896] | 35 | #include "core/XMLPort.h" |
|---|
| [6417] | 36 | #include "SoundManager.h" |
|---|
| [6506] | 37 | #include "SoundStreamer.h" |
|---|
| [3060] | 38 | |
|---|
| [6510] | 39 | #include <AL/alut.h> |
|---|
| 40 | |
|---|
| [5738] | 41 | namespace orxonox |
|---|
| [3060] | 42 | { |
|---|
| [5896] | 43 | CreateFactory(AmbientSound); |
|---|
| 44 | |
|---|
| 45 | AmbientSound::AmbientSound(BaseObject* creator) |
|---|
| 46 | : BaseObject(creator) |
|---|
| [6417] | 47 | , Synchronisable(creator) |
|---|
| 48 | , bPlayOnLoad_(false) |
|---|
| [3060] | 49 | { |
|---|
| [5896] | 50 | RegisterObject(AmbientSound); |
|---|
| [6417] | 51 | |
|---|
| 52 | // Ambient sounds always fade in |
|---|
| 53 | this->setVolume(0); |
|---|
| 54 | this->registerVariables(); |
|---|
| [3060] | 55 | } |
|---|
| 56 | |
|---|
| [6417] | 57 | void AmbientSound::preDestroy() |
|---|
| [3060] | 58 | { |
|---|
| [6417] | 59 | if (GameMode::playsSound()) |
|---|
| 60 | { |
|---|
| 61 | // Smoothly fade out by keeping a SmartPtr |
|---|
| 62 | SoundManager::getInstance().unregisterAmbientSound(this); |
|---|
| [6511] | 63 | this->soundstreamthread_.interrupt(); |
|---|
| [6417] | 64 | } |
|---|
| [3060] | 65 | } |
|---|
| 66 | |
|---|
| [6417] | 67 | void AmbientSound::registerVariables() |
|---|
| 68 | { |
|---|
| 69 | registerVariable(ambientSource_, ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::ambientSourceChanged)); |
|---|
| 70 | registerVariable(bLooping_, ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::loopingChanged)); |
|---|
| 71 | registerVariable(pitch_, ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::pitchChanged)); |
|---|
| 72 | registerVariable(bPlayOnLoad_, ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::playOnLoadChanged)); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| [5896] | 75 | void AmbientSound::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
|---|
| [3060] | 76 | { |
|---|
| [5896] | 77 | SUPER(AmbientSound, XMLPort, xmlelement, mode); |
|---|
| [6417] | 78 | BaseSound::XMLPortExtern(xmlelement, mode); |
|---|
| 79 | XMLPortParam(AmbientSound, "ambientSource", setAmbientSource, getAmbientSource, xmlelement, mode); |
|---|
| 80 | XMLPortParam(AmbientSound, "playOnLoad", setPlayOnLoad, getPlayOnLoad, xmlelement, mode); |
|---|
| [3060] | 81 | } |
|---|
| 82 | |
|---|
| [5896] | 83 | void AmbientSound::XMLEventPort(Element& xmlelement, XMLPort::Mode mode) |
|---|
| [3060] | 84 | { |
|---|
| [5896] | 85 | SUPER(AmbientSound, XMLEventPort, xmlelement, mode); |
|---|
| 86 | XMLPortEventState(AmbientSound, BaseObject, "play", play, xmlelement, mode); |
|---|
| [3060] | 87 | } |
|---|
| [6417] | 88 | |
|---|
| 89 | void AmbientSound::play() |
|---|
| 90 | { |
|---|
| 91 | if (GameMode::playsSound()) |
|---|
| 92 | SoundManager::getInstance().registerAmbientSound(this); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | void AmbientSound::stop() |
|---|
| 96 | { |
|---|
| 97 | if (GameMode::playsSound()) |
|---|
| 98 | SoundManager::getInstance().unregisterAmbientSound(this); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | void AmbientSound::pause() |
|---|
| 102 | { |
|---|
| 103 | if (GameMode::playsSound()) |
|---|
| 104 | SoundManager::getInstance().pauseAmbientSound(this); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | float AmbientSound::getRealVolume() |
|---|
| 108 | { |
|---|
| 109 | assert(GameMode::playsSound()); |
|---|
| 110 | return SoundManager::getInstance().getRealVolume(SoundType::Music); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | void AmbientSound::setAmbientSource(const std::string& source) |
|---|
| 114 | { |
|---|
| 115 | this->ambientSource_ = source; |
|---|
| 116 | this->moodChanged(this->getMood()); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | void AmbientSound::moodChanged(const std::string& mood) |
|---|
| 120 | { |
|---|
| 121 | if (GameMode::playsSound()) |
|---|
| 122 | { |
|---|
| 123 | const std::string& path = "ambient/" + MoodManager::getInstance().getMood() + '/' + this->ambientSource_; |
|---|
| 124 | shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(path); |
|---|
| 125 | if (fileInfo != NULL) |
|---|
| [6506] | 126 | this->setStreamSource(path); |
|---|
| [6417] | 127 | else |
|---|
| 128 | COUT(3) << "Sound: " << this->ambientSource_ << ": Not a valid name! Ambient sound will not change." << std::endl; |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | void AmbientSound::setPlayOnLoad(bool val) |
|---|
| 133 | { |
|---|
| 134 | this->bPlayOnLoad_ = val; |
|---|
| 135 | if (val) |
|---|
| 136 | this->play(); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | void AmbientSound::changedActivity() |
|---|
| 140 | { |
|---|
| 141 | SUPER(AmbientSound, changedActivity); |
|---|
| 142 | if (this->isActive()) |
|---|
| 143 | this->play(); |
|---|
| 144 | else |
|---|
| 145 | this->stop(); |
|---|
| 146 | } |
|---|
| [6506] | 147 | |
|---|
| 148 | // hacky solution for file streaming |
|---|
| 149 | void AmbientSound::setStreamSource(const std::string& source) |
|---|
| 150 | { |
|---|
| [6730] | 151 | if (!GameMode::playsSound()) |
|---|
| 152 | { |
|---|
| 153 | this->source_ = source; |
|---|
| 154 | return; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| [6769] | 157 | if(!alIsSource(this->audioSource_)) |
|---|
| 158 | this->audioSource_ = SoundManager::getInstance().getSoundSource(this); |
|---|
| 159 | |
|---|
| [6506] | 160 | if (this->source_ == source) |
|---|
| 161 | { |
|---|
| 162 | return; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | this->source_ = source; |
|---|
| 166 | // Don't load "" |
|---|
| 167 | if (source_.empty()) |
|---|
| 168 | return; |
|---|
| 169 | |
|---|
| 170 | if (this->soundstreamthread_.get_id() != boost::thread::id()) |
|---|
| 171 | { |
|---|
| 172 | this->soundstreamthread_.interrupt(); // unhandled interruptions lead to thread terminating ;-) |
|---|
| 173 | } |
|---|
| 174 | // Get resource info |
|---|
| 175 | shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(source); |
|---|
| 176 | if (fileInfo == NULL) |
|---|
| 177 | { |
|---|
| 178 | COUT(2) << "Sound: Warning: Sound file '" << source << "' not found" << std::endl; |
|---|
| 179 | return; |
|---|
| 180 | } |
|---|
| 181 | // Open data stream |
|---|
| 182 | DataStreamPtr dataStream = Resource::open(fileInfo); |
|---|
| 183 | |
|---|
| 184 | this->soundstreamthread_ = boost::thread(SoundStreamer(), this->audioSource_, dataStream); |
|---|
| [6562] | 185 | if(this->soundstreamthread_ == boost::thread()) |
|---|
| 186 | COUT(2) << "Sound: Failed to create thread." << std::endl; |
|---|
| [6769] | 187 | //SoundStreamer streamer; |
|---|
| 188 | //streamer(this->audioSource_, dataStream); |
|---|
| 189 | |
|---|
| [6509] | 190 | alSource3f(this->audioSource_, AL_POSITION, 0, 0, 0); |
|---|
| 191 | alSource3f(this->audioSource_, AL_VELOCITY, 0, 0, 0); |
|---|
| 192 | alSource3f(this->audioSource_, AL_DIRECTION, 0, 0, 0); |
|---|
| 193 | if (ALint error = alGetError()) |
|---|
| 194 | COUT(2) << "Sound: Warning: Setting source parameters to 0 failed: " << getALErrorString(error) << std::endl; |
|---|
| [6506] | 195 | } |
|---|
| 196 | |
|---|
| 197 | void AmbientSound::doStop() |
|---|
| 198 | { |
|---|
| [6507] | 199 | BaseSound::doStop(); |
|---|
| [6506] | 200 | this->soundstreamthread_.interrupt(); |
|---|
| 201 | } |
|---|
| [6674] | 202 | |
|---|
| 203 | void AmbientSound::doPlay() |
|---|
| 204 | { |
|---|
| 205 | BaseSound::doPlay(); |
|---|
| 206 | |
|---|
| 207 | if(GameMode::playsSound() && this->getSourceState() != AL_PLAYING) |
|---|
| 208 | { |
|---|
| 209 | if(!alIsSource(this->audioSource_)) |
|---|
| 210 | { |
|---|
| 211 | this->audioSource_ = SoundManager::getInstance().getSoundSource(this); |
|---|
| 212 | if(!alIsSource(this->audioSource_)) |
|---|
| 213 | return; |
|---|
| 214 | this->initialiseSource(); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | alSourcePlay(this->audioSource_); |
|---|
| 218 | if(int error = alGetError()) |
|---|
| 219 | COUT(2) << "Sound: Error playing sound: " << getALErrorString(error) << std::endl; |
|---|
| 220 | } |
|---|
| 221 | } |
|---|
| [5896] | 222 | } |
|---|