4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include "ambient_sound.hpp"
28 #include "object_factory.hpp"
29 #include "lisp/lisp.hpp"
31 #include "audio/sound_manager.hpp"
32 #include "audio/sound_source.hpp"
34 #include "scripting/squirrel_util.hpp"
36 AmbientSound::AmbientSound(const lisp::Lisp& lisp)
51 if (!(lisp.get("x", position.x)&&lisp.get("y", position.y))) {
52 log_warning << "No Position in ambient_sound" << std::endl;
55 lisp.get("name" , name);
56 lisp.get("width" , dimension.x);
57 lisp.get("height", dimension.y);
59 lisp.get("distance_factor",distance_factor);
60 lisp.get("distance_bias" ,distance_bias );
61 lisp.get("sample" ,sample );
62 lisp.get("volume" ,maximumvolume );
64 // set dimension to zero if smaller than 64, which is default size in flexlay
66 if ((dimension.x <= 64) || (dimension.y <= 64)) {
71 // square all distances (saves us a sqrt later)
73 distance_bias*=distance_bias;
74 distance_factor*=distance_factor;
76 // set default silence_distance
78 if (distance_factor == 0)
79 silence_distance = std::numeric_limits<float>::max();
81 silence_distance = 1/distance_factor;
83 lisp.get("silence_distance",silence_distance);
85 sound_source = 0; // not playing at the beginning
89 AmbientSound::AmbientSound(Vector pos, float factor, float bias, float vol, std::string file)
97 distance_factor=factor*factor;
98 distance_bias=bias*bias;
102 // set default silence_distance
104 if (distance_factor == 0)
105 silence_distance = std::numeric_limits<float>::max();
107 silence_distance = 1/distance_factor;
109 sound_source = 0; // not playing at the beginning
113 AmbientSound::~AmbientSound() {
118 AmbientSound::hit(Player& )
123 AmbientSound::stop_playing() {
129 AmbientSound::start_playing()
132 sound_source = sound_manager->create_sound_source(sample);
134 throw std::runtime_error("file not found");
136 sound_source->set_gain(0);
137 sound_source->set_looping(true);
138 currentvolume=targetvolume=1e-20f;
139 sound_source->play();
140 } catch(std::exception& e) {
141 log_warning << "Couldn't play '" << sample << "': " << e.what() << "" << std::endl;
149 AmbientSound::update(float deltat)
151 if (latency-- <= 0) {
156 px=Sector::current()->player->get_pos().x;
157 py=Sector::current()->player->get_pos().y;
159 // Relate to which point in the area
160 rx=px<position.x?position.x:
161 (px<position.x+dimension.x?px:position.x+dimension.x);
162 ry=py<position.y?position.y:
163 (py<position.y+dimension.y?py:position.y+dimension.y);
165 // calculate square of distance
166 float sqrdistance=(px-rx)*(px-rx)+(py-ry)*(py-ry);
167 sqrdistance-=distance_bias;
169 // inside the bias: full volume (distance 0)
173 // calculate target volume - will never become 0
174 targetvolume=1/(1+sqrdistance*distance_factor);
175 float rise=targetvolume/currentvolume;
177 // rise/fall half life?
178 currentvolume*=pow(rise,deltat*10);
179 currentvolume += 1e-6f; // volume is at least 1e-6 (0 would never rise)
181 if (sound_source != 0) {
184 sound_source->set_gain(currentvolume*maximumvolume);
186 if (sqrdistance>=silence_distance && currentvolume<1e-3)
190 if (sqrdistance<silence_distance) {
194 else // set a reasonable latency
195 latency=(int)(0.001/distance_factor);
196 //(int)(10*((sqrdistance-silence_distance)/silence_distance));
200 // heuristically measured "good" latency maximum
202 // if (latency>0.001/distance_factor)
207 AmbientSound::draw(DrawingContext &)
212 AmbientSound::expose(HSQUIRRELVM vm, SQInteger table_idx)
214 Scripting::AmbientSound* interface = static_cast<Scripting::AmbientSound*> (this);
215 expose_object(vm, table_idx, interface, name, false);
219 AmbientSound::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
221 Scripting::unexpose_object(vm, table_idx, name);
225 AmbientSound::set_pos(float x, float y)
232 AmbientSound::get_pos_x() const
238 AmbientSound::get_pos_y() const
243 IMPLEMENT_FACTORY(AmbientSound, "ambient_sound");