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.
26 #include "ambient_sound.hpp"
27 #include "object_factory.hpp"
28 #include "lisp/lisp.hpp"
30 #include "audio/sound_manager.hpp"
31 #include "audio/sound_source.hpp"
33 #include "scripting/squirrel_util.hpp"
35 AmbientSound::AmbientSound(const lisp::Lisp& lisp) :
36 MovingObject(lisp), sample(""), sound_source(0), latency(0),
37 distance_factor(0), distance_bias(0), maximumvolume(1), currentvolume(0)
39 set_group( COLGROUP_DISABLED );
42 lisp.get("width" , dimensionX);
43 lisp.get("height", dimensionY);
44 set_size( dimensionX, dimensionY );
46 lisp.get("distance_factor",distance_factor);
47 lisp.get("distance_bias" ,distance_bias );
48 lisp.get("sample" ,sample );
49 lisp.get("volume" ,maximumvolume );
51 // set dimension to zero if smaller than 64, which is default size in flexlay
53 if ((get_width() <= 64) && (get_height() <= 64)) {
57 // square all distances (saves us a sqrt later)
59 distance_bias*=distance_bias;
60 distance_factor*=distance_factor;
62 // set default silence_distance
64 if (distance_factor == 0)
65 silence_distance = 10e99;
67 silence_distance = 1/distance_factor;
69 lisp.get("silence_distance",silence_distance);
72 AmbientSound::AmbientSound(Vector pos, float factor, float bias, float vol, std::string file) :
73 sample(file), sound_source(0), latency(0), distance_factor(factor*factor),
74 distance_bias(bias*bias), maximumvolume(vol), currentvolume(0)
79 // set default silence_distance
81 if (distance_factor == 0)
82 silence_distance = 10e99;
84 silence_distance = 1/distance_factor;
87 AmbientSound::~AmbientSound() {
92 AmbientSound::hit(Player& )
97 AmbientSound::stop_playing() {
103 AmbientSound::start_playing()
106 sound_source = sound_manager->create_sound_source(sample);
108 throw std::runtime_error("file not found");
110 sound_source->set_gain(0);
111 sound_source->set_looping(true);
112 currentvolume=targetvolume=1e-20;
113 sound_source->play();
114 } catch(std::exception& e) {
115 log_warning << "Couldn't play '" << sample << "': " << e.what() << "" << std::endl;
123 AmbientSound::update(float deltat)
125 if (latency-- <= 0) {
130 px=Sector::current()->player->get_pos().x;
131 py=Sector::current()->player->get_pos().y;
133 // Relate to which point in the area
134 rx=px<bbox.p1.x?bbox.p1.x:
135 (px<bbox.p2.x?px:bbox.p2.x);
136 ry=py<bbox.p1.y?bbox.p1.y:
137 (py<bbox.p2.y?py:bbox.p2.y);
139 // calculate square of distance
140 float sqrdistance=(px-rx)*(px-rx)+(py-ry)*(py-ry);
141 sqrdistance-=distance_bias;
143 // inside the bias: full volume (distance 0)
147 // calculate target volume - will never become 0
148 targetvolume=1/(1+sqrdistance*distance_factor);
149 float rise=targetvolume/currentvolume;
151 // rise/fall half life?
152 currentvolume*=pow(rise,deltat*10);
153 currentvolume += 1e-6; // volume is at least 1e-6 (0 would never rise)
155 if (sound_source != 0) {
158 sound_source->set_gain(currentvolume*maximumvolume);
160 if (sqrdistance>=silence_distance && currentvolume<1e-3)
164 if (sqrdistance<silence_distance) {
168 else // set a reasonable latency
169 latency=(int)(0.001/distance_factor);
170 //(int)(10*((sqrdistance-silence_distance)/silence_distance));
174 // heuristically measured "good" latency maximum
176 // if (latency>0.001/distance_factor)
181 AmbientSound::draw(DrawingContext &)
186 AmbientSound::expose(HSQUIRRELVM vm, SQInteger table_idx)
188 if(name.empty()) return;
189 expose_object(vm, table_idx, dynamic_cast<Scripting::AmbientSound *>(this), name, false);
193 AmbientSound::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
195 if(name.empty()) return;
196 Scripting::unexpose_object(vm, table_idx, name);
199 IMPLEMENT_FACTORY(AmbientSound, "ambient_sound");