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
24 #include "ambient_sound.h"
25 #include "object_factory.h"
26 #include "lisp/lisp.h"
28 #include "audio/sound_manager.h"
29 #include "audio/sound_source.h"
31 AmbientSound::AmbientSound(const lisp::Lisp& lisp)
44 if (!(lisp.get("x", position.x)&&lisp.get("y", position.y))) {
45 std::cerr << "No Position in ambient_sound" << std::endl;
48 lisp.get("width" , dimension.x);
49 lisp.get("height", dimension.y);
51 lisp.get("distance_factor",distance_factor);
52 lisp.get("distance_bias" ,distance_bias );
53 lisp.get("sample" ,sample );
54 lisp.get("volume" ,maximumvolume );
56 // set dimension to zero if smaller than 64, which is default size in flexlay
58 if ((dimension.x <= 64) || (dimension.y <= 64)) {
63 // square all distances (saves us a sqrt later)
65 distance_bias*=distance_bias;
66 distance_factor*=distance_factor;
68 // set default silence_distance
70 if (distance_factor == 0)
71 silence_distance = 10e99;
73 silence_distance = 1/distance_factor;
75 lisp.get("silence_distance",silence_distance);
77 sound_source = 0; // not playing at the beginning
81 AmbientSound::AmbientSound(Vector pos, float factor, float bias, float vol, std::string file)
89 distance_factor=factor*factor;
90 distance_bias=bias*bias;
94 // set default silence_distance
96 if (distance_factor == 0)
97 silence_distance = 10e99;
99 silence_distance = 1/distance_factor;
101 sound_source = 0; // not playing at the beginning
105 AmbientSound::~AmbientSound() {
110 AmbientSound::hit(Player& )
115 AmbientSound::stop_playing() {
121 AmbientSound::start_playing()
124 std::string filename = "sounds/";
127 sound_source = sound_manager->create_sound_source(filename);
129 throw std::runtime_error("file not found");
131 sound_source->set_gain(0);
132 sound_source->set_looping(true);
133 currentvolume=targetvolume=1e-20;
134 sound_source->play();
135 } catch(std::exception& e) {
136 std::cerr << "Couldn't play '" << sample << "': " << e.what() << "\n";
143 AmbientSound::update(float deltat)
152 px=Sector::current()->player->get_pos().x;
153 py=Sector::current()->player->get_pos().y;
155 // Relate to which point in the area
157 rx=px<position.x?position.x:
158 (px<position.x+dimension.x?px:position.x+dimension.x);
159 ry=py<position.y?position.y:
160 (py<position.y+dimension.y?py:position.y+dimension.y);
162 // calculate square of distance
164 float sqrdistance=(px-rx)*(px-rx)+(py-ry)*(py-ry);
166 sqrdistance-=distance_bias;
168 // inside the bias: full volume (distance 0)
173 // calculate target volume - will never become 0
175 targetvolume=1/(1+sqrdistance*distance_factor);
177 float rise=targetvolume/currentvolume;
179 // rise/fall half life?
181 currentvolume*=pow(rise,deltat*10);
182 currentvolume += 1e-6; // volume is at least 1e-6 (0 would never rise)
184 if (sound_source != 0) {
187 sound_source->set_gain(currentvolume*maximumvolume);
189 if (sqrdistance>=silence_distance && currentvolume<1e-3)
193 if (sqrdistance<silence_distance) {
197 else // set a reasonable latency
198 latency=(int)(0.001/distance_factor);
199 //(int)(10*((sqrdistance-silence_distance)/silence_distance));
203 // heuristically measured "good" latency maximum
205 // if (latency>0.001/distance_factor)
210 AmbientSound::draw(DrawingContext &)
214 IMPLEMENT_FACTORY(AmbientSound, "ambient_sound");