1 // ambient_sound.cpp basti_
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 #include "ambient_sound.hpp"
26 #include "object_factory.hpp"
27 #include "lisp/lisp.hpp"
29 #include "audio/sound_manager.hpp"
30 #include "audio/sound_source.hpp"
33 AmbientSound::AmbientSound(const lisp::Lisp& lisp)
46 if (!(lisp.get("x", position.x)&&lisp.get("y", position.y))) {
47 msg_warning("No Position in ambient_sound");
50 lisp.get("width" , dimension.x);
51 lisp.get("height", dimension.y);
53 lisp.get("distance_factor",distance_factor);
54 lisp.get("distance_bias" ,distance_bias );
55 lisp.get("sample" ,sample );
56 lisp.get("volume" ,maximumvolume );
58 // set dimension to zero if smaller than 64, which is default size in flexlay
60 if ((dimension.x <= 64) || (dimension.y <= 64)) {
65 // square all distances (saves us a sqrt later)
67 distance_bias*=distance_bias;
68 distance_factor*=distance_factor;
70 // set default silence_distance
72 if (distance_factor == 0)
73 silence_distance = 10e99;
75 silence_distance = 1/distance_factor;
77 lisp.get("silence_distance",silence_distance);
79 sound_source = 0; // not playing at the beginning
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 = 10e99;
101 silence_distance = 1/distance_factor;
103 sound_source = 0; // not playing at the beginning
107 AmbientSound::~AmbientSound() {
112 AmbientSound::hit(Player& )
117 AmbientSound::stop_playing() {
123 AmbientSound::start_playing()
126 sound_source = sound_manager->create_sound_source(sample);
128 throw std::runtime_error("file not found");
130 sound_source->set_gain(0);
131 sound_source->set_looping(true);
132 currentvolume=targetvolume=1e-20;
133 sound_source->play();
134 } catch(std::exception& e) {
135 msg_warning("Couldn't play '" << sample << "': " << e.what() << "");
142 AmbientSound::update(float deltat)
151 px=Sector::current()->player->get_pos().x;
152 py=Sector::current()->player->get_pos().y;
154 // 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
163 float sqrdistance=(px-rx)*(px-rx)+(py-ry)*(py-ry);
165 sqrdistance-=distance_bias;
167 // inside the bias: full volume (distance 0)
172 // calculate target volume - will never become 0
174 targetvolume=1/(1+sqrdistance*distance_factor);
176 float rise=targetvolume/currentvolume;
178 // rise/fall half life?
180 currentvolume*=pow(rise,deltat*10);
181 currentvolume += 1e-6; // volume is at least 1e-6 (0 would never rise)
183 if (sound_source != 0) {
186 sound_source->set_gain(currentvolume*maximumvolume);
188 if (sqrdistance>=silence_distance && currentvolume<1e-3)
192 if (sqrdistance<silence_distance) {
196 else // set a reasonable latency
197 latency=(int)(0.001/distance_factor);
198 //(int)(10*((sqrdistance-silence_distance)/silence_distance));
202 // heuristically measured "good" latency maximum
204 // if (latency>0.001/distance_factor)
209 AmbientSound::draw(DrawingContext &)
213 IMPLEMENT_FACTORY(AmbientSound, "ambient_sound");