Massive copyright update. I'm sorry if I'm crediting Matze for something he didn...
[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
34 AmbientSound::AmbientSound(const lisp::Lisp& lisp)
35 {
36   position.x=0;
37   position.y=0;
38
39   dimension.x=0;
40   dimension.y=0;
41
42   distance_factor=0;
43   distance_bias=0;
44   maximumvolume=1;
45   sample="";
46
47   if (!(lisp.get("x", position.x)&&lisp.get("y", position.y))) {
48     log_warning << "No Position in ambient_sound" << std::endl;
49   }
50
51   lisp.get("width" , dimension.x);
52   lisp.get("height", dimension.y);
53
54   lisp.get("distance_factor",distance_factor);
55   lisp.get("distance_bias"  ,distance_bias  );
56   lisp.get("sample"         ,sample         );
57   lisp.get("volume"         ,maximumvolume  );
58
59   // set dimension to zero if smaller than 64, which is default size in flexlay
60   
61   if ((dimension.x <= 64) || (dimension.y <= 64)) {
62     dimension.x = 0;
63     dimension.y = 0;
64   }
65   
66   // square all distances (saves us a sqrt later)
67
68   distance_bias*=distance_bias;
69   distance_factor*=distance_factor;
70
71   // set default silence_distance
72
73   if (distance_factor == 0)
74     silence_distance = 10e99;
75   else
76     silence_distance = 1/distance_factor;
77   
78   lisp.get("silence_distance",silence_distance);
79
80   sound_source = 0; // not playing at the beginning
81   latency=0;
82 }
83
84 AmbientSound::AmbientSound(Vector pos, float factor, float bias, float vol, std::string file)
85 {
86   position.x=pos.x;
87   position.y=pos.y;
88
89   dimension.x=0;
90   dimension.y=0;
91
92   distance_factor=factor*factor;
93   distance_bias=bias*bias;
94   maximumvolume=vol;
95   sample=file;
96   
97   // set default silence_distance
98
99   if (distance_factor == 0)
100     silence_distance = 10e99;
101   else
102     silence_distance = 1/distance_factor;
103
104   sound_source = 0; // not playing at the beginning
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-20;
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   }
140 }
141
142 void
143 AmbientSound::update(float deltat) 
144 {
145   if (latency--<=0) {
146
147     float px,py;
148     float rx,ry;
149
150     // Player position
151
152     px=Sector::current()->player->get_pos().x;
153     py=Sector::current()->player->get_pos().y;
154
155     // Relate to which point in the area
156
157     rx=px<position.x?position.x:
158       (px<position.x+dimension.x?px:position.x+dimension.x);
159     ry=py<position.y?position.y:
160       (py<position.y+dimension.y?py:position.y+dimension.y);
161
162     // calculate square of distance
163
164     float sqrdistance=(px-rx)*(px-rx)+(py-ry)*(py-ry);
165     
166     sqrdistance-=distance_bias;
167
168     // inside the bias: full volume (distance 0)
169     
170     if (sqrdistance<0)
171       sqrdistance=0;
172     
173     // calculate target volume - will never become 0
174
175     targetvolume=1/(1+sqrdistance*distance_factor);
176
177     float rise=targetvolume/currentvolume;
178
179     // rise/fall half life?
180
181     currentvolume*=pow(rise,deltat*10);
182     currentvolume += 1e-6; // volume is at least 1e-6 (0 would never rise)
183
184     if (sound_source != 0) {
185
186       // set the volume
187       sound_source->set_gain(currentvolume*maximumvolume);
188
189       if (sqrdistance>=silence_distance && currentvolume<1e-3)
190         stop_playing();
191       latency=0;
192     } else {
193       if (sqrdistance<silence_distance) {
194         start_playing();
195         latency=0;
196       }
197       else // set a reasonable latency
198         latency=(int)(0.001/distance_factor);
199       //(int)(10*((sqrdistance-silence_distance)/silence_distance));
200     }
201   } 
202
203   // heuristically measured "good" latency maximum
204
205   //  if (latency>0.001/distance_factor)
206   // latency=
207 }
208
209 void
210 AmbientSound::draw(DrawingContext &) 
211 {
212 }
213
214 IMPLEMENT_FACTORY(AmbientSound, "ambient_sound");