* Added a boolean "disable-physics" property to powerups. We can't afford losing...
[supertux.git] / src / object / ambient_sound.cpp
1 // ambient_sound.cpp   basti_
2 //
3 //  SuperTux
4 //
5 //  This program is free software; you can redistribute it and/or
6 //  modify it under the terms of the GNU General Public License
7 //  as published by the Free Software Foundation; either version 2
8 //  of the License, or (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 // 
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program; if not, write to the Free Software
17 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18 //  02111-1307, USA.
19 #include <config.h>
20
21 #include <math.h>
22 #include <stdexcept>
23 #include <iostream>
24
25 #include "ambient_sound.hpp"
26 #include "object_factory.hpp"
27 #include "lisp/lisp.hpp"
28 #include "sector.hpp"
29 #include "audio/sound_manager.hpp"
30 #include "audio/sound_source.hpp"
31
32 AmbientSound::AmbientSound(const lisp::Lisp& lisp)
33 {
34   position.x=0;
35   position.y=0;
36
37   dimension.x=0;
38   dimension.y=0;
39
40   distance_factor=0;
41   distance_bias=0;
42   maximumvolume=1;
43   sample="";
44
45   if (!(lisp.get("x", position.x)&&lisp.get("y", position.y))) {
46     std::cerr << "No Position in ambient_sound"  << std::endl;
47   }
48
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 = 10e99;
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   latency=0;
80 }
81
82 AmbientSound::AmbientSound(Vector pos, float factor, float bias, float vol, std::string file)
83 {
84   position.x=pos.x;
85   position.y=pos.y;
86
87   dimension.x=0;
88   dimension.y=0;
89
90   distance_factor=factor*factor;
91   distance_bias=bias*bias;
92   maximumvolume=vol;
93   sample=file;
94   
95   // set default silence_distance
96
97   if (distance_factor == 0)
98     silence_distance = 10e99;
99   else
100     silence_distance = 1/distance_factor;
101
102   sound_source = 0; // not playing at the beginning
103   latency=0;
104 }
105
106 AmbientSound::~AmbientSound() {
107   stop_playing();
108 }
109
110 void
111 AmbientSound::hit(Player& )
112 {
113 }
114
115 void
116 AmbientSound::stop_playing() {
117   delete sound_source;
118   sound_source = 0;
119 }
120
121 void
122 AmbientSound::start_playing()
123 {
124   try {
125     std::string filename = "sounds/";
126     filename += sample;
127     filename += ".wav";
128     sound_source = sound_manager->create_sound_source(filename);
129     if(!sound_source)
130       throw std::runtime_error("file not found");
131    
132     sound_source->set_gain(0);
133     sound_source->set_looping(true);
134     currentvolume=targetvolume=1e-20;
135     sound_source->play();
136   } catch(std::exception& e) {
137     std::cerr << "Couldn't play '" << sample << "': " << e.what() << "\n";
138     delete sound_source;
139     sound_source = 0;
140   }
141 }
142
143 void
144 AmbientSound::update(float deltat) 
145 {
146   if (latency--<=0) {
147
148     float px,py;
149     float rx,ry;
150
151     // Player position
152
153     px=Sector::current()->player->get_pos().x;
154     py=Sector::current()->player->get_pos().y;
155
156     // Relate to which point in the area
157
158     rx=px<position.x?position.x:
159       (px<position.x+dimension.x?px:position.x+dimension.x);
160     ry=py<position.y?position.y:
161       (py<position.y+dimension.y?py:position.y+dimension.y);
162
163     // calculate square of distance
164
165     float sqrdistance=(px-rx)*(px-rx)+(py-ry)*(py-ry);
166     
167     sqrdistance-=distance_bias;
168
169     // inside the bias: full volume (distance 0)
170     
171     if (sqrdistance<0)
172       sqrdistance=0;
173     
174     // calculate target volume - will never become 0
175
176     targetvolume=1/(1+sqrdistance*distance_factor);
177
178     float rise=targetvolume/currentvolume;
179
180     // rise/fall half life?
181
182     currentvolume*=pow(rise,deltat*10);
183     currentvolume += 1e-6; // 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 IMPLEMENT_FACTORY(AmbientSound, "ambient_sound");