[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" |
---|
[6931] | 38 | #include "util/Sleep.h" |
---|
[3060] | 39 | |
---|
[6510] | 40 | #include <AL/alut.h> |
---|
| 41 | |
---|
[5738] | 42 | namespace orxonox |
---|
[3060] | 43 | { |
---|
[5896] | 44 | CreateFactory(AmbientSound); |
---|
[6931] | 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 | |
---|
[5896] | 51 | AmbientSound::AmbientSound(BaseObject* creator) |
---|
| 52 | : BaseObject(creator) |
---|
[6417] | 53 | , Synchronisable(creator) |
---|
| 54 | , bPlayOnLoad_(false) |
---|
[3060] | 55 | { |
---|
[5896] | 56 | RegisterObject(AmbientSound); |
---|
[6417] | 57 | |
---|
| 58 | // Ambient sounds always fade in |
---|
[6931] | 59 | //this->setVolume(0); |
---|
[6417] | 60 | this->registerVariables(); |
---|
[3060] | 61 | } |
---|
| 62 | |
---|
[6417] | 63 | void AmbientSound::preDestroy() |
---|
[3060] | 64 | { |
---|
[6417] | 65 | if (GameMode::playsSound()) |
---|
| 66 | { |
---|
| 67 | // Smoothly fade out by keeping a SmartPtr |
---|
| 68 | SoundManager::getInstance().unregisterAmbientSound(this); |
---|
[6511] | 69 | this->soundstreamthread_.interrupt(); |
---|
[6417] | 70 | } |
---|
[3060] | 71 | } |
---|
| 72 | |
---|
[6417] | 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 | |
---|
[5896] | 81 | void AmbientSound::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[3060] | 82 | { |
---|
[5896] | 83 | SUPER(AmbientSound, XMLPort, xmlelement, mode); |
---|
[6417] | 84 | BaseSound::XMLPortExtern(xmlelement, mode); |
---|
| 85 | XMLPortParam(AmbientSound, "ambientSource", setAmbientSource, getAmbientSource, xmlelement, mode); |
---|
| 86 | XMLPortParam(AmbientSound, "playOnLoad", setPlayOnLoad, getPlayOnLoad, xmlelement, mode); |
---|
[3060] | 87 | } |
---|
| 88 | |
---|
[5896] | 89 | void AmbientSound::XMLEventPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[3060] | 90 | { |
---|
[5896] | 91 | SUPER(AmbientSound, XMLEventPort, xmlelement, mode); |
---|
| 92 | XMLPortEventState(AmbientSound, BaseObject, "play", play, xmlelement, mode); |
---|
[3060] | 93 | } |
---|
[6417] | 94 | |
---|
| 95 | void AmbientSound::play() |
---|
| 96 | { |
---|
| 97 | if (GameMode::playsSound()) |
---|
| 98 | SoundManager::getInstance().registerAmbientSound(this); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | void AmbientSound::stop() |
---|
| 102 | { |
---|
[6931] | 103 | if (GameMode::playsSound()) |
---|
[6417] | 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) |
---|
[6506] | 132 | this->setStreamSource(path); |
---|
[6417] | 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 | } |
---|
[6506] | 153 | |
---|
| 154 | // hacky solution for file streaming |
---|
| 155 | void AmbientSound::setStreamSource(const std::string& source) |
---|
| 156 | { |
---|
[6730] | 157 | if (!GameMode::playsSound()) |
---|
| 158 | { |
---|
| 159 | this->source_ = source; |
---|
| 160 | return; |
---|
| 161 | } |
---|
| 162 | |
---|
[6769] | 163 | if(!alIsSource(this->audioSource_)) |
---|
| 164 | this->audioSource_ = SoundManager::getInstance().getSoundSource(this); |
---|
| 165 | |
---|
[6506] | 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 | { |
---|
[6931] | 178 | this->soundstreamthread_.interrupt(); // terminate an old thread if necessary |
---|
[6506] | 179 | } |
---|
[6931] | 180 | |
---|
| 181 | // queue some init buffers |
---|
| 182 | COUT(4) << "Sound: Creating thread for " << source << std::endl; |
---|
[6506] | 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); |
---|
[6931] | 192 | |
---|
| 193 | alSourcei(this->audioSource_, AL_BUFFER, 0); |
---|
[6506] | 194 | |
---|
[6931] | 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); |
---|
[6562] | 253 | if(this->soundstreamthread_ == boost::thread()) |
---|
| 254 | COUT(2) << "Sound: Failed to create thread." << std::endl; |
---|
[6769] | 255 | //SoundStreamer streamer; |
---|
| 256 | //streamer(this->audioSource_, dataStream); |
---|
| 257 | |
---|
[6509] | 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; |
---|
[6506] | 263 | } |
---|
| 264 | |
---|
| 265 | void AmbientSound::doStop() |
---|
| 266 | { |
---|
[6507] | 267 | BaseSound::doStop(); |
---|
[6506] | 268 | this->soundstreamthread_.interrupt(); |
---|
| 269 | } |
---|
[6674] | 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 | } |
---|
[5896] | 290 | } |
---|