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