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