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