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