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)
44 if (!(lisp.get("x", position.x)&&lisp.get("y", position.y))) {
45 log_warning << "No Position in ambient_sound" << std::endl;
48 lisp.get("name" , name);
49 lisp.get("width" , dimension.x);
50 lisp.get("height", dimension.y);
52 lisp.get("distance_factor",distance_factor);
53 lisp.get("distance_bias" ,distance_bias );
54 lisp.get("sample" ,sample );
55 lisp.get("volume" ,maximumvolume );
57 // set dimension to zero if smaller than 64, which is default size in flexlay
59 if ((dimension.x <= 64) || (dimension.y <= 64)) {
64 // square all distances (saves us a sqrt later)
66 distance_bias*=distance_bias;
67 distance_factor*=distance_factor;
69 // set default silence_distance
71 if (distance_factor == 0)
72 silence_distance = std::numeric_limits<float>::max();
74 silence_distance = 1/distance_factor;
76 lisp.get("silence_distance",silence_distance);
78 sound_source = 0; // not playing at the beginning
79 sound_manager->preload(sample);
83 AmbientSound::AmbientSound(Vector pos, float factor, float bias, float vol, std::string file)
91 distance_factor=factor*factor;
92 distance_bias=bias*bias;
96 // set default silence_distance
98 if (distance_factor == 0)
99 silence_distance = std::numeric_limits<float>::max();
101 silence_distance = 1/distance_factor;
103 sound_source = 0; // not playing at the beginning
104 sound_manager->preload(sample);
108 AmbientSound::~AmbientSound() {
113 AmbientSound::hit(Player& )
118 AmbientSound::stop_playing() {
124 AmbientSound::start_playing()
127 sound_source = sound_manager->create_sound_source(sample);
129 throw std::runtime_error("file not found");
131 sound_source->set_gain(0);
132 sound_source->set_looping(true);
133 currentvolume=targetvolume=1e-20f;
134 sound_source->play();
135 } catch(std::exception& e) {
136 log_warning << "Couldn't play '" << sample << "': " << e.what() << "" << std::endl;
144 AmbientSound::update(float deltat)
146 if (latency-- <= 0) {
150 if (!Sector::current() || !Sector::current()->camera) return;
152 px=Sector::current()->camera->get_center().x;
153 py=Sector::current()->camera->get_center().y;
155 // Relate to which point in the area
156 rx=px<position.x?position.x:
157 (px<position.x+dimension.x?px:position.x+dimension.x);
158 ry=py<position.y?position.y:
159 (py<position.y+dimension.y?py:position.y+dimension.y);
161 // calculate square of distance
162 float sqrdistance=(px-rx)*(px-rx)+(py-ry)*(py-ry);
163 sqrdistance-=distance_bias;
165 // inside the bias: full volume (distance 0)
169 // calculate target volume - will never become 0
170 targetvolume=1/(1+sqrdistance*distance_factor);
171 float rise=targetvolume/currentvolume;
173 // rise/fall half life?
174 currentvolume*=pow(rise,deltat*10);
175 currentvolume += 1e-6f; // volume is at least 1e-6 (0 would never rise)
177 if (sound_source != 0) {
180 sound_source->set_gain(currentvolume*maximumvolume);
182 if (sqrdistance>=silence_distance && currentvolume<1e-3)
186 if (sqrdistance<silence_distance) {
190 else // set a reasonable latency
191 latency=(int)(0.001/distance_factor);
192 //(int)(10*((sqrdistance-silence_distance)/silence_distance));
196 // heuristically measured "good" latency maximum
198 // if (latency>0.001/distance_factor)
203 AmbientSound::draw(DrawingContext &)
208 AmbientSound::expose(HSQUIRRELVM vm, SQInteger table_idx)
210 Scripting::AmbientSound* interface = static_cast<Scripting::AmbientSound*> (this);
211 expose_object(vm, table_idx, interface, name, false);
215 AmbientSound::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
217 Scripting::unexpose_object(vm, table_idx, name);
221 AmbientSound::set_pos(float x, float y)
228 AmbientSound::get_pos_x() const
234 AmbientSound::get_pos_y() const
239 IMPLEMENT_FACTORY(AmbientSound, "ambient_sound");