[cppcheck] Part 2: Some further style fixes etc.
[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   position(),
31   dimension(),
32   sample(),
33   sound_source(),
34   latency(),
35   distance_factor(),
36   distance_bias(),
37   silence_distance(),
38   maximumvolume(),
39   targetvolume(),
40   currentvolume(),
41   volume_ptr()
42 {
43   position.x = 0;
44   position.y = 0;
45
46   dimension.x = 0;
47   dimension.y = 0;
48
49   distance_factor = 0;
50   distance_bias = 0;
51   maximumvolume = 1;
52   currentvolume = 0;
53
54   if (!(lisp.get("x", position.x)&&lisp.get("y", position.y))) {
55     log_warning << "No Position in ambient_sound" << std::endl;
56   }
57
58   lisp.get("name" , name);
59   lisp.get("width" , dimension.x);
60   lisp.get("height", dimension.y);
61
62   lisp.get("distance_factor",distance_factor);
63   lisp.get("distance_bias"  ,distance_bias  );
64   lisp.get("sample"         ,sample         );
65   lisp.get("volume"         ,maximumvolume  );
66
67   // set dimension to zero if smaller than 64, which is default size in flexlay
68
69   if ((dimension.x <= 64) || (dimension.y <= 64)) {
70     dimension.x = 0;
71     dimension.y = 0;
72   }
73
74   // square all distances (saves us a sqrt later)
75
76   distance_bias*=distance_bias;
77   distance_factor*=distance_factor;
78
79   // set default silence_distance
80
81   if (distance_factor == 0)
82     silence_distance = std::numeric_limits<float>::max();
83   else
84     silence_distance = 1/distance_factor;
85
86   lisp.get("silence_distance",silence_distance);
87
88   sound_source.reset(); // not playing at the beginning
89   SoundManager::current()->preload(sample);
90   latency=0;
91 }
92
93 AmbientSound::AmbientSound(Vector pos, float factor, float bias, float vol, std::string file) :
94   position(),
95   dimension(),
96   sample(file),
97   sound_source(),
98   latency(),
99   distance_factor(),
100   distance_bias(),
101   silence_distance(),
102   maximumvolume(),
103   targetvolume(),
104   currentvolume(),
105   volume_ptr()
106 {
107   position.x=pos.x;
108   position.y=pos.y;
109
110   dimension.x=0;
111   dimension.y=0;
112
113   distance_factor=factor*factor;
114   distance_bias=bias*bias;
115   maximumvolume=vol;
116
117   // set default silence_distance
118
119   if (distance_factor == 0)
120     silence_distance = std::numeric_limits<float>::max();
121   else
122     silence_distance = 1/distance_factor;
123
124   sound_source = 0; // not playing at the beginning
125   SoundManager::current()->preload(sample);
126   latency=0;
127 }
128
129 AmbientSound::~AmbientSound()
130 {
131   stop_playing();
132 }
133
134 void
135 AmbientSound::hit(Player& )
136 {
137 }
138
139 void
140 AmbientSound::stop_playing()
141 {
142   sound_source.reset();
143 }
144
145 void
146 AmbientSound::start_playing()
147 {
148   try {
149     sound_source = SoundManager::current()->create_sound_source(sample);
150     if(!sound_source)
151       throw std::runtime_error("file not found");
152
153     sound_source->set_gain(0);
154     sound_source->set_looping(true);
155     currentvolume=targetvolume=1e-20f;
156     sound_source->play();
157   } catch(std::exception& e) {
158     log_warning << "Couldn't play '" << sample << "': " << e.what() << "" << std::endl;
159     sound_source.reset();
160     remove_me();
161   }
162 }
163
164 void
165 AmbientSound::update(float deltat)
166 {
167   if (latency-- <= 0) {
168     float px,py;
169     float rx,ry;
170
171     if (!Sector::current() || !Sector::current()->camera) return;
172     // Camera position
173     px=Sector::current()->camera->get_center().x;
174     py=Sector::current()->camera->get_center().y;
175
176     // Relate to which point in the area
177     rx=px<position.x?position.x:
178       (px<position.x+dimension.x?px:position.x+dimension.x);
179     ry=py<position.y?position.y:
180       (py<position.y+dimension.y?py:position.y+dimension.y);
181
182     // calculate square of distance
183     float sqrdistance=(px-rx)*(px-rx)+(py-ry)*(py-ry);
184     sqrdistance-=distance_bias;
185
186     // inside the bias: full volume (distance 0)
187     if (sqrdistance<0)
188       sqrdistance=0;
189
190     // calculate target volume - will never become 0
191     targetvolume=1/(1+sqrdistance*distance_factor);
192     float rise=targetvolume/currentvolume;
193
194     // rise/fall half life?
195     currentvolume*=pow(rise,deltat*10);
196     currentvolume += 1e-6f; // volume is at least 1e-6 (0 would never rise)
197
198     if (sound_source != 0) {
199
200       // set the volume
201       sound_source->set_gain(currentvolume*maximumvolume);
202
203       if (sqrdistance>=silence_distance && currentvolume<1e-3)
204         stop_playing();
205       latency=0;
206     } else {
207       if (sqrdistance<silence_distance) {
208         start_playing();
209         latency=0;
210       }
211       else // set a reasonable latency
212         latency=(int)(0.001/distance_factor);
213       //(int)(10*((sqrdistance-silence_distance)/silence_distance));
214     }
215   }
216
217   // heuristically measured "good" latency maximum
218
219   //  if (latency>0.001/distance_factor)
220   // latency=
221 }
222
223 void
224 AmbientSound::draw(DrawingContext &)
225 {
226 }
227
228 void
229 AmbientSound::expose(HSQUIRRELVM vm, SQInteger table_idx)
230 {
231   scripting::AmbientSound* _this = static_cast<scripting::AmbientSound*> (this);
232   expose_object(vm, table_idx, _this, name, false);
233 }
234
235 void
236 AmbientSound::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
237 {
238   scripting::unexpose_object(vm, table_idx, name);
239 }
240
241 void
242 AmbientSound::set_pos(float x, float y)
243 {
244   position.x = x;
245   position.y = y;
246 }
247
248 float
249 AmbientSound::get_pos_x() const
250 {
251   return position.x;
252 }
253
254 float
255 AmbientSound::get_pos_y() const
256 {
257   return position.y;
258 }
259
260 /* EOF */