Small style fix
[supertux.git] / src / object / ambient_sound.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include <limits>
18 #include <math.h>
19
20 #include "audio/sound_manager.hpp"
21 #include "audio/sound_source.hpp"
22 #include "object/ambient_sound.hpp"
23 #include "object/camera.hpp"
24 #include "scripting/squirrel_util.hpp"
25 #include "supertux/object_factory.hpp"
26 #include "supertux/sector.hpp"
27 #include "util/reader.hpp"
28
29 AmbientSound::AmbientSound(const Reader& lisp) :
30   name(),
31   position(),
32   dimension(),
33   sample(),
34   sound_source(),
35   latency(),
36   distance_factor(),
37   distance_bias(),
38   silence_distance(),
39   maximumvolume(),
40   targetvolume(),
41   currentvolume(),
42   volume_ptr()
43 {
44   position.x = 0;
45   position.y = 0;
46
47   dimension.x = 0;
48   dimension.y = 0;
49
50   distance_factor = 0;
51   distance_bias = 0;
52   maximumvolume = 1;
53   currentvolume = 0;
54
55   if (!(lisp.get("x", position.x)&&lisp.get("y", position.y))) {
56     log_warning << "No Position in ambient_sound" << std::endl;
57   }
58
59   lisp.get("name" , name);
60   lisp.get("width" , dimension.x);
61   lisp.get("height", dimension.y);
62
63   lisp.get("distance_factor",distance_factor);
64   lisp.get("distance_bias"  ,distance_bias  );
65   lisp.get("sample"         ,sample         );
66   lisp.get("volume"         ,maximumvolume  );
67
68   // set dimension to zero if smaller than 64, which is default size in flexlay
69
70   if ((dimension.x <= 64) || (dimension.y <= 64)) {
71     dimension.x = 0;
72     dimension.y = 0;
73   }
74
75   // square all distances (saves us a sqrt later)
76
77   distance_bias*=distance_bias;
78   distance_factor*=distance_factor;
79
80   // set default silence_distance
81
82   if (distance_factor == 0)
83     silence_distance = std::numeric_limits<float>::max();
84   else
85     silence_distance = 1/distance_factor;
86
87   lisp.get("silence_distance",silence_distance);
88
89   sound_source.reset(); // not playing at the beginning
90   SoundManager::current()->preload(sample);
91   latency=0;
92 }
93
94 AmbientSound::AmbientSound(Vector pos, float factor, float bias, float vol, std::string file) :
95   name(),
96   position(),
97   dimension(),
98   sample(file),
99   sound_source(),
100   latency(),
101   distance_factor(),
102   distance_bias(),
103   silence_distance(),
104   maximumvolume(),
105   targetvolume(),
106   currentvolume(),
107   volume_ptr()
108 {
109   position.x=pos.x;
110   position.y=pos.y;
111
112   dimension.x=0;
113   dimension.y=0;
114
115   distance_factor=factor*factor;
116   distance_bias=bias*bias;
117   maximumvolume=vol;
118
119   // set default silence_distance
120
121   if (distance_factor == 0)
122     silence_distance = std::numeric_limits<float>::max();
123   else
124     silence_distance = 1/distance_factor;
125
126   sound_source = 0; // not playing at the beginning
127   SoundManager::current()->preload(sample);
128   latency=0;
129 }
130
131 AmbientSound::~AmbientSound()
132 {
133   stop_playing();
134 }
135
136 void
137 AmbientSound::hit(Player& )
138 {
139 }
140
141 void
142 AmbientSound::stop_playing()
143 {
144   sound_source.reset();
145 }
146
147 void
148 AmbientSound::start_playing()
149 {
150   try {
151     sound_source = SoundManager::current()->create_sound_source(sample);
152     if(!sound_source)
153       throw std::runtime_error("file not found");
154
155     sound_source->set_gain(0);
156     sound_source->set_looping(true);
157     currentvolume=targetvolume=1e-20f;
158     sound_source->play();
159   } catch(std::exception& e) {
160     log_warning << "Couldn't play '" << sample << "': " << e.what() << "" << std::endl;
161     sound_source.reset();
162     remove_me();
163   }
164 }
165
166 void
167 AmbientSound::update(float deltat)
168 {
169   if (latency-- <= 0) {
170     float px,py;
171     float rx,ry;
172
173     if (!Sector::current() || !Sector::current()->camera) return;
174     // Camera position
175     px=Sector::current()->camera->get_center().x;
176     py=Sector::current()->camera->get_center().y;
177
178     // Relate to which point in the area
179     rx=px<position.x?position.x:
180       (px<position.x+dimension.x?px:position.x+dimension.x);
181     ry=py<position.y?position.y:
182       (py<position.y+dimension.y?py:position.y+dimension.y);
183
184     // calculate square of distance
185     float sqrdistance=(px-rx)*(px-rx)+(py-ry)*(py-ry);
186     sqrdistance-=distance_bias;
187
188     // inside the bias: full volume (distance 0)
189     if (sqrdistance<0)
190       sqrdistance=0;
191
192     // calculate target volume - will never become 0
193     targetvolume=1/(1+sqrdistance*distance_factor);
194     float rise=targetvolume/currentvolume;
195
196     // rise/fall half life?
197     currentvolume*=pow(rise,deltat*10);
198     currentvolume += 1e-6f; // volume is at least 1e-6 (0 would never rise)
199
200     if (sound_source != 0) {
201
202       // set the volume
203       sound_source->set_gain(currentvolume*maximumvolume);
204
205       if (sqrdistance>=silence_distance && currentvolume<1e-3)
206         stop_playing();
207       latency=0;
208     } else {
209       if (sqrdistance<silence_distance) {
210         start_playing();
211         latency=0;
212       }
213       else // set a reasonable latency
214         latency=(int)(0.001/distance_factor);
215       //(int)(10*((sqrdistance-silence_distance)/silence_distance));
216     }
217   }
218
219   // heuristically measured "good" latency maximum
220
221   //  if (latency>0.001/distance_factor)
222   // latency=
223 }
224
225 void
226 AmbientSound::draw(DrawingContext &)
227 {
228 }
229
230 void
231 AmbientSound::expose(HSQUIRRELVM vm, SQInteger table_idx)
232 {
233   scripting::AmbientSound* _this = static_cast<scripting::AmbientSound*> (this);
234   expose_object(vm, table_idx, _this, name, false);
235 }
236
237 void
238 AmbientSound::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
239 {
240   scripting::unexpose_object(vm, table_idx, name);
241 }
242
243 void
244 AmbientSound::set_pos(float x, float y)
245 {
246   position.x = x;
247   position.y = y;
248 }
249
250 float
251 AmbientSound::get_pos_x() const
252 {
253   return position.x;
254 }
255
256 float
257 AmbientSound::get_pos_y() const
258 {
259   return position.y;
260 }
261
262 /* EOF */