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 lisp.get("distance_factor",distance_factor);
40 lisp.get("distance_bias" ,distance_bias );
41 lisp.get("sample" ,sample );
42 lisp.get("volume" ,maximumvolume );
44 // set dimension to zero if smaller than 64, which is default size in flexlay
46 if ((get_width() <= 64) || (get_height() <= 64)) {
50 // square all distances (saves us a sqrt later)
52 distance_bias*=distance_bias;
53 distance_factor*=distance_factor;
55 // set default silence_distance
57 if (distance_factor == 0)
58 silence_distance = 10e99;
60 silence_distance = 1/distance_factor;
62 lisp.get("silence_distance",silence_distance);
65 AmbientSound::AmbientSound(Vector pos, float factor, float bias, float vol, std::string file) :
66 sample(file), sound_source(0), latency(0), distance_factor(factor*factor),
67 distance_bias(bias*bias), maximumvolume(vol), currentvolume(0)
72 // set default silence_distance
74 if (distance_factor == 0)
75 silence_distance = 10e99;
77 silence_distance = 1/distance_factor;
80 AmbientSound::~AmbientSound() {
85 AmbientSound::hit(Player& )
90 AmbientSound::stop_playing() {
96 AmbientSound::start_playing()
99 sound_source = sound_manager->create_sound_source(sample);
101 throw std::runtime_error("file not found");
103 sound_source->set_gain(0);
104 sound_source->set_looping(true);
105 currentvolume=targetvolume=1e-20;
106 sound_source->play();
107 } catch(std::exception& e) {
108 log_warning << "Couldn't play '" << sample << "': " << e.what() << "" << std::endl;
116 AmbientSound::update(float deltat)
118 if (latency-- <= 0) {
123 px=Sector::current()->player->get_pos().x;
124 py=Sector::current()->player->get_pos().y;
126 // Relate to which point in the area
127 rx=px<bbox.p1.x?bbox.p1.x:
128 (px<bbox.p2.x?px:bbox.p2.x);
129 ry=py<bbox.p1.y?bbox.p1.y:
130 (py<bbox.p2.y?py:bbox.p2.y);
132 // calculate square of distance
133 float sqrdistance=(px-rx)*(px-rx)+(py-ry)*(py-ry);
134 sqrdistance-=distance_bias;
136 // inside the bias: full volume (distance 0)
140 // calculate target volume - will never become 0
141 targetvolume=1/(1+sqrdistance*distance_factor);
142 float rise=targetvolume/currentvolume;
144 // rise/fall half life?
145 currentvolume*=pow(rise,deltat*10);
146 currentvolume += 1e-6; // volume is at least 1e-6 (0 would never rise)
148 if (sound_source != 0) {
151 sound_source->set_gain(currentvolume*maximumvolume);
153 if (sqrdistance>=silence_distance && currentvolume<1e-3)
157 if (sqrdistance<silence_distance) {
161 else // set a reasonable latency
162 latency=(int)(0.001/distance_factor);
163 //(int)(10*((sqrdistance-silence_distance)/silence_distance));
167 // heuristically measured "good" latency maximum
169 // if (latency>0.001/distance_factor)
174 AmbientSound::draw(DrawingContext &)
179 AmbientSound::expose(HSQUIRRELVM vm, SQInteger table_idx)
181 if(name.empty()) return;
182 expose_object(vm, table_idx, dynamic_cast<Scripting::AmbientSound *>(this), name, false);
186 AmbientSound::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
188 if(name.empty()) return;
189 Scripting::unexpose_object(vm, table_idx, name);
192 IMPLEMENT_FACTORY(AmbientSound, "ambient_sound");