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