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 | * ... |
---|
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 | |
---|
39 | namespace orxonox |
---|
40 | { |
---|
41 | CreateFactory(AmbientSound); |
---|
42 | |
---|
43 | AmbientSound::AmbientSound(BaseObject* creator) |
---|
44 | : BaseObject(creator) |
---|
45 | , Synchronisable(creator) |
---|
46 | , bPlayOnLoad_(false) |
---|
47 | { |
---|
48 | RegisterObject(AmbientSound); |
---|
49 | |
---|
50 | // Ambient sounds always fade in |
---|
51 | this->setVolume(0); |
---|
52 | this->registerVariables(); |
---|
53 | } |
---|
54 | |
---|
55 | void AmbientSound::preDestroy() |
---|
56 | { |
---|
57 | if (GameMode::playsSound()) |
---|
58 | { |
---|
59 | // Smoothly fade out by keeping a SmartPtr |
---|
60 | SoundManager::getInstance().unregisterAmbientSound(this); |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | void AmbientSound::registerVariables() |
---|
65 | { |
---|
66 | registerVariable(ambientSource_, ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::ambientSourceChanged)); |
---|
67 | registerVariable(bLooping_, ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::loopingChanged)); |
---|
68 | registerVariable(pitch_, ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::pitchChanged)); |
---|
69 | registerVariable(bPlayOnLoad_, ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::playOnLoadChanged)); |
---|
70 | } |
---|
71 | |
---|
72 | void AmbientSound::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
73 | { |
---|
74 | SUPER(AmbientSound, XMLPort, xmlelement, mode); |
---|
75 | BaseSound::XMLPortExtern(xmlelement, mode); |
---|
76 | XMLPortParam(AmbientSound, "ambientSource", setAmbientSource, getAmbientSource, xmlelement, mode); |
---|
77 | XMLPortParam(AmbientSound, "playOnLoad", setPlayOnLoad, getPlayOnLoad, xmlelement, mode); |
---|
78 | } |
---|
79 | |
---|
80 | void AmbientSound::XMLEventPort(Element& xmlelement, XMLPort::Mode mode) |
---|
81 | { |
---|
82 | SUPER(AmbientSound, XMLEventPort, xmlelement, mode); |
---|
83 | XMLPortEventState(AmbientSound, BaseObject, "play", play, xmlelement, mode); |
---|
84 | } |
---|
85 | |
---|
86 | void AmbientSound::play() |
---|
87 | { |
---|
88 | if (GameMode::playsSound()) |
---|
89 | SoundManager::getInstance().registerAmbientSound(this); |
---|
90 | } |
---|
91 | |
---|
92 | void AmbientSound::stop() |
---|
93 | { |
---|
94 | if (GameMode::playsSound()) |
---|
95 | SoundManager::getInstance().unregisterAmbientSound(this); |
---|
96 | } |
---|
97 | |
---|
98 | void AmbientSound::pause() |
---|
99 | { |
---|
100 | if (GameMode::playsSound()) |
---|
101 | SoundManager::getInstance().pauseAmbientSound(this); |
---|
102 | } |
---|
103 | |
---|
104 | float AmbientSound::getRealVolume() |
---|
105 | { |
---|
106 | assert(GameMode::playsSound()); |
---|
107 | return SoundManager::getInstance().getRealVolume(SoundType::Music); |
---|
108 | } |
---|
109 | |
---|
110 | void AmbientSound::setAmbientSource(const std::string& source) |
---|
111 | { |
---|
112 | this->ambientSource_ = source; |
---|
113 | this->moodChanged(this->getMood()); |
---|
114 | } |
---|
115 | |
---|
116 | void AmbientSound::moodChanged(const std::string& mood) |
---|
117 | { |
---|
118 | if (GameMode::playsSound()) |
---|
119 | { |
---|
120 | const std::string& path = "ambient/" + MoodManager::getInstance().getMood() + '/' + this->ambientSource_; |
---|
121 | shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(path); |
---|
122 | if (fileInfo != NULL) |
---|
123 | this->setStreamSource(path); |
---|
124 | else |
---|
125 | COUT(3) << "Sound: " << this->ambientSource_ << ": Not a valid name! Ambient sound will not change." << std::endl; |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | void AmbientSound::setPlayOnLoad(bool val) |
---|
130 | { |
---|
131 | this->bPlayOnLoad_ = val; |
---|
132 | if (val) |
---|
133 | this->play(); |
---|
134 | } |
---|
135 | |
---|
136 | void AmbientSound::changedActivity() |
---|
137 | { |
---|
138 | SUPER(AmbientSound, changedActivity); |
---|
139 | if (this->isActive()) |
---|
140 | this->play(); |
---|
141 | else |
---|
142 | this->stop(); |
---|
143 | } |
---|
144 | |
---|
145 | // hacky solution for file streaming |
---|
146 | void AmbientSound::setStreamSource(const std::string& source) |
---|
147 | { |
---|
148 | this->audioSource_ = SoundManager::getInstance().getSoundSource(this); |
---|
149 | if (this->source_ == source) |
---|
150 | { |
---|
151 | return; |
---|
152 | } |
---|
153 | |
---|
154 | this->source_ = source; |
---|
155 | // Don't load "" |
---|
156 | if (source_.empty()) |
---|
157 | return; |
---|
158 | |
---|
159 | if (this->soundstreamthread_.get_id() != boost::thread::id()) |
---|
160 | { |
---|
161 | this->soundstreamthread_.interrupt(); // unhandled interruptions lead to thread terminating ;-) |
---|
162 | } |
---|
163 | // Get resource info |
---|
164 | shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(source); |
---|
165 | if (fileInfo == NULL) |
---|
166 | { |
---|
167 | COUT(2) << "Sound: Warning: Sound file '" << source << "' not found" << std::endl; |
---|
168 | return; |
---|
169 | } |
---|
170 | // Open data stream |
---|
171 | DataStreamPtr dataStream = Resource::open(fileInfo); |
---|
172 | |
---|
173 | this->soundstreamthread_ = boost::thread(SoundStreamer(), this->audioSource_, dataStream); |
---|
174 | this->initialiseSource(); |
---|
175 | } |
---|
176 | |
---|
177 | void AmbientSound::doStop() |
---|
178 | { |
---|
179 | SUPER(AmbientSound, doStop); |
---|
180 | this->soundstreamthread_.interrupt(); |
---|
181 | } |
---|
182 | } |
---|