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