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