#include <ambient_sound.hpp>
Inherits GameObject, ScriptInterface, and scripting::AmbientSound.
Public Member Functions | |
AmbientSound (const Reader &lisp) | |
AmbientSound (Vector pos, float factor, float bias, float vol, std::string file) | |
~AmbientSound () | |
void | set_pos (Vector newpos) |
const Vector | get_pos () const |
Scriptable Methods | |
void | set_pos (float x, float y) |
float | get_pos_x () const |
float | get_pos_y () const |
Protected Member Functions | |
virtual void | hit (Player &player) |
virtual void | update (float time) |
This function is called once per frame and allows the object to update it's state. | |
virtual void | draw (DrawingContext &) |
The GameObject should draw itself onto the provided DrawingContext if this function is called. | |
virtual void | start_playing () |
virtual void | stop_playing () |
virtual void | expose (HSQUIRRELVM vm, SQInteger table_idx) |
virtual void | unexpose (HSQUIRRELVM vm, SQInteger table_idx) |
Private Member Functions | |
AmbientSound (const AmbientSound &) | |
this will be used by the volume adjustment effect. | |
AmbientSound & | operator= (const AmbientSound &) |
Private Attributes | |
std::string | name |
user-defined name for use in scripts or empty string if not scriptable | |
Vector | position |
Vector | dimension |
std::string | sample |
SoundSource * | sound_source |
int | latency |
float | distance_factor |
float | distance_bias |
distance scaling | |
float | silence_distance |
100% volume disc radius | |
float | maximumvolume |
not implemented yet | |
float | targetvolume |
maximum volume | |
float | currentvolume |
how loud we want to be | |
float * | volume_ptr |
how loud we are |
Definition at line 50 of file ambient_sound.hpp.
AmbientSound::AmbientSound | ( | const Reader & | lisp | ) |
Definition at line 29 of file ambient_sound.cpp.
References currentvolume, dimension, distance_bias, distance_factor, lisp::Lisp::get(), latency, log_warning, maximumvolume, name, position, SoundManager::preload(), sample, silence_distance, sound_manager, sound_source, Vector::x, and Vector::y.
00029 : 00030 name(), 00031 position(), 00032 dimension(), 00033 sample(), 00034 sound_source(), 00035 latency(), 00036 distance_factor(), 00037 distance_bias(), 00038 silence_distance(), 00039 maximumvolume(), 00040 targetvolume(), 00041 currentvolume(), 00042 volume_ptr() 00043 { 00044 name=""; 00045 position.x = 0; 00046 position.y = 0; 00047 00048 dimension.x = 0; 00049 dimension.y = 0; 00050 00051 distance_factor = 0; 00052 distance_bias = 0; 00053 maximumvolume = 1; 00054 sample = ""; 00055 currentvolume = 0; 00056 00057 if (!(lisp.get("x", position.x)&&lisp.get("y", position.y))) { 00058 log_warning << "No Position in ambient_sound" << std::endl; 00059 } 00060 00061 lisp.get("name" , name); 00062 lisp.get("width" , dimension.x); 00063 lisp.get("height", dimension.y); 00064 00065 lisp.get("distance_factor",distance_factor); 00066 lisp.get("distance_bias" ,distance_bias ); 00067 lisp.get("sample" ,sample ); 00068 lisp.get("volume" ,maximumvolume ); 00069 00070 // set dimension to zero if smaller than 64, which is default size in flexlay 00071 00072 if ((dimension.x <= 64) || (dimension.y <= 64)) { 00073 dimension.x = 0; 00074 dimension.y = 0; 00075 } 00076 00077 // square all distances (saves us a sqrt later) 00078 00079 distance_bias*=distance_bias; 00080 distance_factor*=distance_factor; 00081 00082 // set default silence_distance 00083 00084 if (distance_factor == 0) 00085 silence_distance = std::numeric_limits<float>::max(); 00086 else 00087 silence_distance = 1/distance_factor; 00088 00089 lisp.get("silence_distance",silence_distance); 00090 00091 sound_source = 0; // not playing at the beginning 00092 sound_manager->preload(sample); 00093 latency=0; 00094 }
AmbientSound::AmbientSound | ( | Vector | pos, | |
float | factor, | |||
float | bias, | |||
float | vol, | |||
std::string | file | |||
) |
Definition at line 96 of file ambient_sound.cpp.
References dimension, distance_bias, distance_factor, latency, maximumvolume, position, SoundManager::preload(), sample, silence_distance, sound_manager, sound_source, Vector::x, and Vector::y.
00096 : 00097 name(), 00098 position(), 00099 dimension(), 00100 sample(), 00101 sound_source(), 00102 latency(), 00103 distance_factor(), 00104 distance_bias(), 00105 silence_distance(), 00106 maximumvolume(), 00107 targetvolume(), 00108 currentvolume(), 00109 volume_ptr() 00110 { 00111 position.x=pos.x; 00112 position.y=pos.y; 00113 00114 dimension.x=0; 00115 dimension.y=0; 00116 00117 distance_factor=factor*factor; 00118 distance_bias=bias*bias; 00119 maximumvolume=vol; 00120 sample=file; 00121 00122 // set default silence_distance 00123 00124 if (distance_factor == 0) 00125 silence_distance = std::numeric_limits<float>::max(); 00126 else 00127 silence_distance = 1/distance_factor; 00128 00129 sound_source = 0; // not playing at the beginning 00130 sound_manager->preload(sample); 00131 latency=0; 00132 }
AmbientSound::~AmbientSound | ( | ) | [virtual] |
Reimplemented from scripting::AmbientSound.
Definition at line 134 of file ambient_sound.cpp.
References stop_playing().
00135 { 00136 stop_playing(); 00137 }
AmbientSound::AmbientSound | ( | const AmbientSound & | ) | [private] |
this will be used by the volume adjustment effect.
void AmbientSound::set_pos | ( | Vector | newpos | ) | [inline] |
Definition at line 59 of file ambient_sound.hpp.
References position.
00060 { 00061 position=newpos; 00062 }
const Vector AmbientSound::get_pos | ( | ) | const [inline] |
Definition at line 63 of file ambient_sound.hpp.
References position.
00064 { 00065 return position; 00066 }
void AmbientSound::set_pos | ( | float | x, | |
float | y | |||
) | [virtual] |
Implements scripting::AmbientSound.
Definition at line 249 of file ambient_sound.cpp.
References position, Vector::x, and Vector::y.
float AmbientSound::get_pos_x | ( | ) | const [virtual] |
Implements scripting::AmbientSound.
Definition at line 256 of file ambient_sound.cpp.
References position, and Vector::x.
float AmbientSound::get_pos_y | ( | ) | const [virtual] |
Implements scripting::AmbientSound.
Definition at line 262 of file ambient_sound.cpp.
References position, and Vector::y.
void AmbientSound::hit | ( | Player & | player | ) | [protected, virtual] |
void AmbientSound::update | ( | float | time | ) | [protected, virtual] |
This function is called once per frame and allows the object to update it's state.
The elapsed_time is the time since the last frame in seconds and should be the base for all timed calculations (don't use SDL_GetTicks directly as this will fail in pause mode)
Implements GameObject.
Definition at line 172 of file ambient_sound.cpp.
References Sector::camera, scripting::camera(), Sector::current(), currentvolume, dimension, distance_bias, distance_factor, Camera::get_center(), latency, maximumvolume, position, SoundSource::set_gain(), silence_distance, sound_source, start_playing(), stop_playing(), targetvolume, Vector::x, and Vector::y.
00173 { 00174 if (latency-- <= 0) { 00175 float px,py; 00176 float rx,ry; 00177 00178 if (!Sector::current() || !Sector::current()->camera) return; 00179 // Camera position 00180 px=Sector::current()->camera->get_center().x; 00181 py=Sector::current()->camera->get_center().y; 00182 00183 // Relate to which point in the area 00184 rx=px<position.x?position.x: 00185 (px<position.x+dimension.x?px:position.x+dimension.x); 00186 ry=py<position.y?position.y: 00187 (py<position.y+dimension.y?py:position.y+dimension.y); 00188 00189 // calculate square of distance 00190 float sqrdistance=(px-rx)*(px-rx)+(py-ry)*(py-ry); 00191 sqrdistance-=distance_bias; 00192 00193 // inside the bias: full volume (distance 0) 00194 if (sqrdistance<0) 00195 sqrdistance=0; 00196 00197 // calculate target volume - will never become 0 00198 targetvolume=1/(1+sqrdistance*distance_factor); 00199 float rise=targetvolume/currentvolume; 00200 00201 // rise/fall half life? 00202 currentvolume*=pow(rise,deltat*10); 00203 currentvolume += 1e-6f; // volume is at least 1e-6 (0 would never rise) 00204 00205 if (sound_source != 0) { 00206 00207 // set the volume 00208 sound_source->set_gain(currentvolume*maximumvolume); 00209 00210 if (sqrdistance>=silence_distance && currentvolume<1e-3) 00211 stop_playing(); 00212 latency=0; 00213 } else { 00214 if (sqrdistance<silence_distance) { 00215 start_playing(); 00216 latency=0; 00217 } 00218 else // set a reasonable latency 00219 latency=(int)(0.001/distance_factor); 00220 //(int)(10*((sqrdistance-silence_distance)/silence_distance)); 00221 } 00222 } 00223 00224 // heuristically measured "good" latency maximum 00225 00226 // if (latency>0.001/distance_factor) 00227 // latency= 00228 }
void AmbientSound::draw | ( | DrawingContext & | ) | [protected, virtual] |
The GameObject should draw itself onto the provided DrawingContext if this function is called.
Implements GameObject.
Definition at line 231 of file ambient_sound.cpp.
void AmbientSound::start_playing | ( | ) | [protected, virtual] |
Definition at line 152 of file ambient_sound.cpp.
References SoundManager::create_sound_source(), currentvolume, log_warning, SoundSource::play(), GameObject::remove_me(), sample, SoundSource::set_gain(), SoundSource::set_looping(), sound_manager, sound_source, and targetvolume.
Referenced by update().
00153 { 00154 try { 00155 sound_source = sound_manager->create_sound_source(sample); 00156 if(!sound_source) 00157 throw std::runtime_error("file not found"); 00158 00159 sound_source->set_gain(0); 00160 sound_source->set_looping(true); 00161 currentvolume=targetvolume=1e-20f; 00162 sound_source->play(); 00163 } catch(std::exception& e) { 00164 log_warning << "Couldn't play '" << sample << "': " << e.what() << "" << std::endl; 00165 delete sound_source; 00166 sound_source = 0; 00167 remove_me(); 00168 } 00169 }
void AmbientSound::stop_playing | ( | ) | [protected, virtual] |
Definition at line 145 of file ambient_sound.cpp.
References sound_source.
Referenced by update(), and ~AmbientSound().
00146 { 00147 delete sound_source; 00148 sound_source = 0; 00149 }
void AmbientSound::expose | ( | HSQUIRRELVM | vm, | |
SQInteger | table_idx | |||
) | [protected, virtual] |
Implements ScriptInterface.
Definition at line 236 of file ambient_sound.cpp.
References scripting::expose_object(), and name.
00237 { 00238 scripting::AmbientSound* _this = static_cast<scripting::AmbientSound*> (this); 00239 expose_object(vm, table_idx, _this, name, false); 00240 }
void AmbientSound::unexpose | ( | HSQUIRRELVM | vm, | |
SQInteger | table_idx | |||
) | [protected, virtual] |
Implements ScriptInterface.
Definition at line 243 of file ambient_sound.cpp.
References name, and scripting::unexpose_object().
00244 { 00245 scripting::unexpose_object(vm, table_idx, name); 00246 }
AmbientSound& AmbientSound::operator= | ( | const AmbientSound & | ) | [private] |
std::string AmbientSound::name [private] |
user-defined name for use in scripts or empty string if not scriptable
Reimplemented from GameObject.
Definition at line 89 of file ambient_sound.hpp.
Referenced by AmbientSound(), expose(), and unexpose().
Vector AmbientSound::position [private] |
Definition at line 90 of file ambient_sound.hpp.
Referenced by AmbientSound(), get_pos(), get_pos_x(), get_pos_y(), set_pos(), and update().
Vector AmbientSound::dimension [private] |
std::string AmbientSound::sample [private] |
SoundSource* AmbientSound::sound_source [private] |
Definition at line 94 of file ambient_sound.hpp.
Referenced by AmbientSound(), start_playing(), stop_playing(), and update().
int AmbientSound::latency [private] |
float AmbientSound::distance_factor [private] |
float AmbientSound::distance_bias [private] |
distance scaling
Definition at line 98 of file ambient_sound.hpp.
Referenced by AmbientSound(), and update().
float AmbientSound::silence_distance [private] |
100% volume disc radius
Definition at line 99 of file ambient_sound.hpp.
Referenced by AmbientSound(), and update().
float AmbientSound::maximumvolume [private] |
not implemented yet
Definition at line 101 of file ambient_sound.hpp.
Referenced by AmbientSound(), and update().
float AmbientSound::targetvolume [private] |
maximum volume
Definition at line 102 of file ambient_sound.hpp.
Referenced by start_playing(), and update().
float AmbientSound::currentvolume [private] |
how loud we want to be
Definition at line 103 of file ambient_sound.hpp.
Referenced by AmbientSound(), start_playing(), and update().
float* AmbientSound::volume_ptr [private] |