- Upgraded ambient sound source to rounded rectangule dimension
authorBastiaan Zapf <bzapf@example.org>
Tue, 10 May 2005 17:27:29 +0000 (17:27 +0000)
committerBastiaan Zapf <bzapf@example.org>
Tue, 10 May 2005 17:27:29 +0000 (17:27 +0000)
- A few general improvements

SVN-Revision: 2463

data/levels/test/ambient_sound.stl
src/object/ambient_sound.cpp
src/object/ambient_sound.h

index e4cd7ee..38a2846 100644 (file)
        (distance_bias 200.0) (sample "rain"))
        (ambient_sound (x 512) (y 900) (distance_factor 0.05) 
        (distance_bias 100.0) (sample "waterfall") (volume 0.2))
-       (ambient_sound (x 800) (y 900) (distance_factor 0.1) 
+       (ambient_sound (x 700) (y 900) (width 300) 
+(height 200) (distance_factor 0.1) 
        (distance_bias 50.0) (sample "lava"))
       (infoblock (x 128) (y 864)
         (message (_ "-Info
 #source playing the
 #rain effect should 
 #be audible here.
-#Find Waterfall and 
-#Lava sources with
-#smaller geometry 
-#further right.
 "))
       )
       (infoblock (x 512) (y 864)
         (message (_ "-Info
 #Waterfall
 #Volume 0.2
+#smaller geometry
+#than rain source
 "))
       )
-      (infoblock (x 800) (y 864)
+      (infoblock (x 700) (y 864)
         (message (_ "-Info
-#Lava
+#Lava (Area)
+#extending right
 "))
       )
    )
index ea7a9aa..bec0d9c 100644 (file)
@@ -30,6 +30,10 @@ AmbientSound::AmbientSound(const lisp::Lisp& lisp)
 
   position.x=0;
   position.y=0;
+
+  dimension.x=0;
+  dimension.y=0;
+
   distance_factor=0;
   distance_bias=0;
   maximumvolume=1;
@@ -38,14 +42,22 @@ AmbientSound::AmbientSound(const lisp::Lisp& lisp)
   if (!(lisp.get("x", position.x)&&lisp.get("y", position.y))) {
     std::cerr << "No Position in ambient_sound"  << std::endl;
   }
+
+  lisp.get("width" , dimension.x);
+  lisp.get("height", dimension.y);
+
   lisp.get("distance_factor",distance_factor);
   lisp.get("distance_bias"  ,distance_bias  );
   lisp.get("sample"         ,sample         );
   lisp.get("volume"         ,maximumvolume  );
 
+  // square all distances (saves us a sqrt later)
+
   distance_bias*=distance_bias;
   distance_factor*=distance_factor;
 
+  // set default silence_distance
+
   if (distance_factor == 0)
     silence_distance = 10e99;
   else
@@ -55,7 +67,6 @@ AmbientSound::AmbientSound(const lisp::Lisp& lisp)
 
   playing=-1; // not playing at the beginning
   latency=0;
-
 }
 
 AmbientSound::~AmbientSound() {
@@ -88,23 +99,49 @@ AmbientSound::update(float deltat)
 {
   if (latency--<=0) {
 
-    float dx=Sector::current()->player->get_pos().x-position.x;
-    float dy=Sector::current()->player->get_pos().y-position.y;
-    float sqrdistance=dx*dx+dy*dy;
+    float px,py;
+    float rx,ry;
+
+    // Player position
+
+    px=Sector::current()->player->get_pos().x;
+    py=Sector::current()->player->get_pos().y;
+
+    // Relate to which point in the area
+
+    rx=px<position.x?position.x:
+      (px<position.x+dimension.x?px:position.x+dimension.x);
+    ry=py<position.y?position.y:
+      (py<position.y+dimension.y?py:position.y+dimension.y);
+
+    // calculate square of distance
+
+    float sqrdistance=(px-rx)*(px-rx)+(py-ry)*(py-ry);
     
     sqrdistance-=distance_bias;
+
+    // inside the bias: full volume (distance 0)
     
     if (sqrdistance<0)
       sqrdistance=0;
     
+    // calculate target volume - will never become 0
+
     targetvolume=1/(1+sqrdistance*distance_factor);
+
     float rise=targetvolume/currentvolume;
+
+    // rise/fall half life?
+
     currentvolume*=pow(rise,deltat*10);
-    currentvolume += 1e-30;
+    currentvolume += 1e-6; // volume is at least 1e-6 (0 would never rise)
 
     if (playing>=0) {
+
+      // set the volume
       Mix_Volume(playing,(int)(currentvolume*maximumvolume*MIX_MAX_VOLUME));
-      if (sqrdistance>=silence_distance && currentvolume<0.05)
+
+      if (sqrdistance>=silence_distance && currentvolume<1e-3)
        stop_playing();
       latency=0;
     } else {
@@ -112,13 +149,16 @@ AmbientSound::update(float deltat)
        start_playing();
        latency=0;
       }
-      else 
-       latency=(int)(10*((sqrdistance-silence_distance)/silence_distance));
+      else // set a reasonable latency
+       latency=(int)(0.001/distance_factor);
+      //(int)(10*((sqrdistance-silence_distance)/silence_distance));
     }
+  } 
 
-  }
-  if (latency>0.001/distance_factor)
-    latency=(int)(0.001/distance_factor);
+  // heuristically measured "good" latency maximum
+
+  //  if (latency>0.001/distance_factor)
+  // latency=
 }
 
 void
index 00e43cf..864bf4a 100644 (file)
 //  02111-1307, USA.
 
 /**
- *  Ambient Sound Source, beta version. Features:
+ *  Ambient Sound Source, gamma version. Features:
  *
- *  - "disc" like structure. Full volume up to some distance
- *    (distance_bias) to the source, then fading proportional to
- *    inverse square distance
+ *  - "rounded rectancle" geometry with position, dimension and 
+ *    "rounding radius" (extending in all directions) of a 100% 
+ *    volume area, adjustable maximum volume, inverse square 
+ *    falloff outside area.
  *  
- *  - parameters for point source:
+ *  - degenerates gracefully to a disc for dimension=0
+ *  
+ *  - parameters:
+ *
  *    x, y               position
+ *    width, height      dimension
  *    distance_factor    high = steep fallofff
  *    distance_bias      high = big "100% disc"
  *    silence_distance   defaults reasonably.
+ *    sample             sample to be played back in loop mode
  * 
  *      basti_ 
  */
@@ -55,6 +61,7 @@ protected:
   virtual void stop_playing();
 private:
   Vector position;
+  Vector dimension;
 
   std::string sample;
   int playing;