- ambient_sound improved (now does silence, volume,
authorBastiaan Zapf <bzapf@example.org>
Sun, 8 May 2005 13:30:41 +0000 (13:30 +0000)
committerBastiaan Zapf <bzapf@example.org>
Sun, 8 May 2005 13:30:41 +0000 (13:30 +0000)
  reasonable latencies when not playing)
- new sound effect (lava.wav) put into sounds

SVN-Revision: 2441

basest/levels/test/ambient_sound.stl
basest/levels/test/messages.pot
basest/sounds/lava.wav [new file with mode: 0644]
src/object/ambient_sound.cpp
src/object/ambient_sound.h
src/scripting/wrapper.cpp
src/scripting/wrapper.h

index 3356744..e4cd7ee 100644 (file)
          (speed 0.500000)
        )
        (spawnpoint (name "main") (x 96) (y 160))
-       (ambient_sound (x 128) (y 800) (distance_factor "0.1") (distance_bias "200.0") (sample "rain"))
-      (infoblock (x 128) (y 800)
+       (ambient_sound (x 128) (y 900) (distance_factor 0.01) 
+       (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) 
+       (distance_bias 50.0) (sample "lava"))
+      (infoblock (x 128) (y 864)
         (message (_ "-Info
-#An ambient sound source
-#should be audible here"))
+#An ambient sound 
+#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
+"))
+      )
+      (infoblock (x 800) (y 864)
+        (message (_ "-Info
+#Lava
+"))
       )
    )
  )
index b4156b1..afd8d33 100644 (file)
@@ -8,7 +8,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: PACKAGE VERSION\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2005-05-07 18:58+0200\n"
+"POT-Creation-Date: 2005-05-08 15:26+0200\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -20,21 +20,6 @@ msgstr ""
 msgid "Sector Test"
 msgstr ""
 
-#: basest/levels/test/verticalforest.stl:5
-msgid "Inside The Tree"
-msgstr ""
-
-#: basest/levels/test/ambient_sound.stl:4
-msgid "Ambient Sound Test"
-msgstr ""
-
-#: basest/levels/test/ambient_sound.stl:138
-msgid ""
-"-Info\n"
-"#An ambient sound source\n"
-"#should be audible here"
-msgstr ""
-
 #: basest/levels/test/bonusblock.stl:4
 msgid "Bonus Block Test"
 msgstr ""
@@ -47,10 +32,44 @@ msgid ""
 "#This rocks"
 msgstr ""
 
+#: basest/levels/test/yeti.stl:4
+msgid "Yeti Test"
+msgstr ""
+
 #: basest/levels/test/script.stl:4
 msgid "Scripting Test"
 msgstr ""
 
-#: basest/levels/test/yeti.stl:4
-msgid "Yeti Test"
+#: basest/levels/test/verticalforest.stl:5
+msgid "Inside The Tree"
+msgstr ""
+
+#: basest/levels/test/ambient_sound.stl:4
+msgid "Ambient Sound Test"
+msgstr ""
+
+#: basest/levels/test/ambient_sound.stl:143
+msgid ""
+"-Info\n"
+"#An ambient sound \n"
+"#source playing the\n"
+"#rain effect should \n"
+"#be audible here.\n"
+"#Find Waterfall and \n"
+"#Lava sources with\n"
+"#smaller geometry \n"
+"#further right.\n"
+msgstr ""
+
+#: basest/levels/test/ambient_sound.stl:155
+msgid ""
+"-Info\n"
+"#Waterfall\n"
+"#Volume 0.2\n"
+msgstr ""
+
+#: basest/levels/test/ambient_sound.stl:161
+msgid ""
+"-Info\n"
+"#Lava\n"
 msgstr ""
diff --git a/basest/sounds/lava.wav b/basest/sounds/lava.wav
new file mode 100644 (file)
index 0000000..40a24ea
Binary files /dev/null and b/basest/sounds/lava.wav differ
index e50e946..0307813 100644 (file)
@@ -32,6 +32,7 @@ AmbientSound::AmbientSound(const lisp::Lisp& lisp)
   position.y=0;
   distance_factor=0;
   distance_bias=0;
+  maximumvolume=1;
   sample="";
 
   if (!(lisp.get("x", position.x)&&lisp.get("y", position.y))) {
@@ -40,16 +41,26 @@ AmbientSound::AmbientSound(const lisp::Lisp& lisp)
   lisp.get("distance_factor",distance_factor);
   lisp.get("distance_bias"  ,distance_bias  );
   lisp.get("sample"         ,sample         );
+  lisp.get("volume"         ,maximumvolume  );
+
+  distance_bias*=distance_bias;
+  distance_factor*=distance_factor;
 
   if (distance_factor == 0)
     silence_distance = 10e99;
   else
-    silence_distance = distance_factor*10e2;
+    silence_distance = 1/distance_factor;
+  
+  lisp.get("silence_distance",silence_distance);
+
+  playing=-1; // not playing at the beginning
+  latency=0;
 
-  playing=0;
-  startPlaying();
 }
 
+AmbientSound::~AmbientSound() {
+  stop_playing();
+}
 
 void
 AmbientSound::hit(Player& )
@@ -57,7 +68,15 @@ AmbientSound::hit(Player& )
 }
 
 void
-AmbientSound::startPlaying()
+AmbientSound::stop_playing() {
+  if (playing>=0) {
+    Mix_HaltChannel(playing);
+    playing=-1;
+  }
+}
+
+void
+AmbientSound::start_playing()
 {
   playing=sound_manager->play_sound(sample,-1);
   Mix_Volume(playing,0);
@@ -65,24 +84,41 @@ AmbientSound::startPlaying()
 }
 
 void
-AmbientSound::action(float) 
+AmbientSound::action(float deltat
 {
-  float dx=Sector::current()->player->get_pos().x-position.x;
-  float dy=Sector::current()->player->get_pos().y-position.y;
-  float distance=sqrt(dx*dx+dy*dy);
-
-  distance-=distance_bias;
-
-  if (distance<0)
-    distance=0;
-
-  targetvolume=1/(1+distance*distance_factor);
-  float rise=targetvolume/currentvolume;
-  currentvolume*=pow(rise,.05);
-  currentvolume += 1e-30;
-  //  std::cout << currentvolume << " " << targetvolume << std::endl;
-  Mix_Volume(playing,(int)(currentvolume*MIX_MAX_VOLUME));
+  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;
+    
+    sqrdistance-=distance_bias;
+    
+    if (sqrdistance<0)
+      sqrdistance=0;
+    
+    targetvolume=1/(1+sqrdistance*distance_factor);
+    float rise=targetvolume/currentvolume;
+    currentvolume*=pow(rise,deltat*10);
+    currentvolume += 1e-30;
+
+    if (playing>=0) {
+      Mix_Volume(playing,(int)(currentvolume*maximumvolume*MIX_MAX_VOLUME));
+      if (sqrdistance>=silence_distance && currentvolume<0.05)
+       stop_playing();
+      latency=0;
+    } else {
+      if (sqrdistance<silence_distance) {
+       start_playing();
+       latency=0;
+      }
+      else 
+       latency=(int)(10*((sqrdistance-silence_distance)/silence_distance));
+    }
 
+  }
+  if (latency>0.001/distance_factor)
+    latency=(int)(0.001/distance_factor);
 }
 
 void
index f6fd285..433e60e 100644 (file)
  *    (distance_bias) to the source, then fading proportional to
  *    inverse square distance
  *  
- *  This is experimental and clicks a little. It is not possible to
- *  get the clicks out without sample-granular volume adjustment.
- *
+ *  - parameters for point source:
+ *    x, y               position
+ *    distance_factor    high = steep fallofff
+ *    distance_bias      high = big "100% disc"
+ *    silence_distance   defaults reasonably.
+ * 
  *      basti_ 
  */
 
@@ -43,22 +46,25 @@ class AmbientSound : public GameObject
 {
 public:
   AmbientSound(const lisp::Lisp& lisp);
-
+  ~AmbientSound();
 protected:
   virtual void hit(Player& player);
   virtual void action(float time);
   virtual void draw(DrawingContext&);
-  virtual void startPlaying();
+  virtual void start_playing();
+  virtual void stop_playing();
 private:
   Vector position;
 
   std::string sample;
   int playing;
+  int latency;
 
   float distance_factor;  /// distance scaling
   float distance_bias;    /// 100% volume disc radius
   float silence_distance; /// not implemented yet 
 
+  float maximumvolume; /// maximum volume
   float targetvolume;  /// how loud we want to be
   float currentvolume; /// how loud we are
 
index aae11d8..8b82637 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * WARNING: This file is automatically generated from './build/i686-pc-linux-gnu/optimize/miniswig.tmp' - do not change
+ * WARNING: This file is automatically generated from './build/i686-pc-linux-gnu/debug/miniswig.tmp' - do not change
  */
 
 #include <config.h>
index 35f7184..f25aea1 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * WARNING: This file is automatically generated from './build/i686-pc-linux-gnu/optimize/miniswig.tmp' - do not change
+ * WARNING: This file is automatically generated from './build/i686-pc-linux-gnu/debug/miniswig.tmp' - do not change
  */
 #ifndef __supertux_WRAPPER_H__
 #define __supertux_WRAPPER_H__