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"
32 AmbientSound::AmbientSound(const lisp::Lisp& lisp)
45 if (!(lisp.get("x", position.x)&&lisp.get("y", position.y))) {
46 std::cerr << "No Position in ambient_sound" << std::endl;
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 = 10e99;
74 silence_distance = 1/distance_factor;
76 lisp.get("silence_distance",silence_distance);
78 sound_source = 0; // not playing at the beginning
82 AmbientSound::AmbientSound(Vector pos, float factor, float bias, float vol, std::string file)
90 distance_factor=factor*factor;
91 distance_bias=bias*bias;
95 // set default silence_distance
97 if (distance_factor == 0)
98 silence_distance = 10e99;
100 silence_distance = 1/distance_factor;
102 sound_source = 0; // not playing at the beginning
106 AmbientSound::~AmbientSound() {
111 AmbientSound::hit(Player& )
116 AmbientSound::stop_playing() {
122 AmbientSound::start_playing()
125 sound_source = sound_manager->create_sound_source(sample);
127 throw std::runtime_error("file not found");
129 sound_source->set_gain(0);
130 sound_source->set_looping(true);
131 currentvolume=targetvolume=1e-20;
132 sound_source->play();
133 } catch(std::exception& e) {
134 std::cerr << "Couldn't play '" << sample << "': " << e.what() << "\n";
141 AmbientSound::update(float deltat)
150 px=Sector::current()->player->get_pos().x;
151 py=Sector::current()->player->get_pos().y;
153 // Relate to which point in the area
155 rx=px<position.x?position.x:
156 (px<position.x+dimension.x?px:position.x+dimension.x);
157 ry=py<position.y?position.y:
158 (py<position.y+dimension.y?py:position.y+dimension.y);
160 // calculate square of distance
162 float sqrdistance=(px-rx)*(px-rx)+(py-ry)*(py-ry);
164 sqrdistance-=distance_bias;
166 // inside the bias: full volume (distance 0)
171 // calculate target volume - will never become 0
173 targetvolume=1/(1+sqrdistance*distance_factor);
175 float rise=targetvolume/currentvolume;
177 // rise/fall half life?
179 currentvolume*=pow(rise,deltat*10);
180 currentvolume += 1e-6; // volume is at least 1e-6 (0 would never rise)
182 if (sound_source != 0) {
185 sound_source->set_gain(currentvolume*maximumvolume);
187 if (sqrdistance>=silence_distance && currentvolume<1e-3)
191 if (sqrdistance<silence_distance) {
195 else // set a reasonable latency
196 latency=(int)(0.001/distance_factor);
197 //(int)(10*((sqrdistance-silence_distance)/silence_distance));
201 // heuristically measured "good" latency maximum
203 // if (latency>0.001/distance_factor)
208 AmbientSound::draw(DrawingContext &)
212 IMPLEMENT_FACTORY(AmbientSound, "ambient_sound");