2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "audio/sound_manager.hpp"
21 #include "audio/sound_source.hpp"
22 #include "object/ambient_sound.hpp"
23 #include "object/camera.hpp"
24 #include "scripting/squirrel_util.hpp"
25 #include "supertux/object_factory.hpp"
26 #include "supertux/sector.hpp"
27 #include "util/reader.hpp"
29 AmbientSound::AmbientSound(const Reader& lisp) :
55 if (!(lisp.get("x", position.x)&&lisp.get("y", position.y))) {
56 log_warning << "No Position in ambient_sound" << std::endl;
59 lisp.get("name" , name);
60 lisp.get("width" , dimension.x);
61 lisp.get("height", dimension.y);
63 lisp.get("distance_factor",distance_factor);
64 lisp.get("distance_bias" ,distance_bias );
65 lisp.get("sample" ,sample );
66 lisp.get("volume" ,maximumvolume );
68 // set dimension to zero if smaller than 64, which is default size in flexlay
70 if ((dimension.x <= 64) || (dimension.y <= 64)) {
75 // square all distances (saves us a sqrt later)
77 distance_bias*=distance_bias;
78 distance_factor*=distance_factor;
80 // set default silence_distance
82 if (distance_factor == 0)
83 silence_distance = std::numeric_limits<float>::max();
85 silence_distance = 1/distance_factor;
87 lisp.get("silence_distance",silence_distance);
89 sound_source.reset(); // not playing at the beginning
90 SoundManager::current()->preload(sample);
94 AmbientSound::AmbientSound(Vector pos, float factor, float bias, float vol, std::string file) :
115 distance_factor=factor*factor;
116 distance_bias=bias*bias;
119 // set default silence_distance
121 if (distance_factor == 0)
122 silence_distance = std::numeric_limits<float>::max();
124 silence_distance = 1/distance_factor;
126 sound_source = 0; // not playing at the beginning
127 SoundManager::current()->preload(sample);
131 AmbientSound::~AmbientSound()
137 AmbientSound::hit(Player& )
142 AmbientSound::stop_playing()
144 sound_source.reset();
148 AmbientSound::start_playing()
151 sound_source = SoundManager::current()->create_sound_source(sample);
153 throw std::runtime_error("file not found");
155 sound_source->set_gain(0);
156 sound_source->set_looping(true);
157 currentvolume=targetvolume=1e-20f;
158 sound_source->play();
159 } catch(std::exception& e) {
160 log_warning << "Couldn't play '" << sample << "': " << e.what() << "" << std::endl;
161 sound_source.reset();
167 AmbientSound::update(float deltat)
169 if (latency-- <= 0) {
173 if (!Sector::current() || !Sector::current()->camera) return;
175 px=Sector::current()->camera->get_center().x;
176 py=Sector::current()->camera->get_center().y;
178 // Relate to which point in the area
179 rx=px<position.x?position.x:
180 (px<position.x+dimension.x?px:position.x+dimension.x);
181 ry=py<position.y?position.y:
182 (py<position.y+dimension.y?py:position.y+dimension.y);
184 // calculate square of distance
185 float sqrdistance=(px-rx)*(px-rx)+(py-ry)*(py-ry);
186 sqrdistance-=distance_bias;
188 // inside the bias: full volume (distance 0)
192 // calculate target volume - will never become 0
193 targetvolume=1/(1+sqrdistance*distance_factor);
194 float rise=targetvolume/currentvolume;
196 // rise/fall half life?
197 currentvolume*=pow(rise,deltat*10);
198 currentvolume += 1e-6f; // volume is at least 1e-6 (0 would never rise)
200 if (sound_source != 0) {
203 sound_source->set_gain(currentvolume*maximumvolume);
205 if (sqrdistance>=silence_distance && currentvolume<1e-3)
209 if (sqrdistance<silence_distance) {
213 else // set a reasonable latency
214 latency=(int)(0.001/distance_factor);
215 //(int)(10*((sqrdistance-silence_distance)/silence_distance));
219 // heuristically measured "good" latency maximum
221 // if (latency>0.001/distance_factor)
226 AmbientSound::draw(DrawingContext &)
231 AmbientSound::expose(HSQUIRRELVM vm, SQInteger table_idx)
233 scripting::AmbientSound* _this = static_cast<scripting::AmbientSound*> (this);
234 expose_object(vm, table_idx, _this, name, false);
238 AmbientSound::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
240 scripting::unexpose_object(vm, table_idx, name);
244 AmbientSound::set_pos(float x, float y)
251 AmbientSound::get_pos_x() const
257 AmbientSound::get_pos_y() const