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