Browse Source

fix set_time()--it should set the time for the next play() call, not start the sound playing at that time

David Rose 16 years ago
parent
commit
7cac7363dd
2 changed files with 71 additions and 48 deletions
  1. 69 48
      panda/src/audiotraits/fmodAudioSound.cxx
  2. 2 0
      panda/src/audiotraits/fmodAudioSound.h

+ 69 - 48
panda/src/audiotraits/fmodAudioSound.cxx

@@ -41,6 +41,8 @@ FmodAudioSound(AudioManager *manager, Filename file_name, bool positional) {
   audio_debug("FmodAudioSound::FmodAudioSound() Creating new sound, filename: " << file_name  );
   audio_debug("FmodAudioSound::FmodAudioSound() Creating new sound, filename: " << file_name  );
 
 
   _active = manager->get_active();
   _active = manager->get_active();
+  _paused = false;
+  _start_time = 0.0;
 
 
   //Local Variables that are needed.
   //Local Variables that are needed.
   FMOD_RESULT result;
   FMOD_RESULT result;
@@ -152,7 +154,7 @@ FmodAudioSound::
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 void FmodAudioSound::
 void FmodAudioSound::
 play() {
 play() {
-  set_time(0.0);
+  start_playing();
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -192,6 +194,7 @@ stop() {
     }
     }
     fmod_audio_errcheck("_channel->stop()", result);
     fmod_audio_errcheck("_channel->stop()", result);
   }
   }
+  _start_time = 0.0;
 }
 }
 
 
 
 
@@ -283,10 +286,72 @@ get_loop_count() const {
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: FmodAudioSound::set_time
 //     Function: FmodAudioSound::set_time
 //       Access: public
 //       Access: public
-//  Description: Starts playing from the specified location.
+//  Description: Sets the time at which the next play() operation will
+//               begin.  If we are already playing, skips to that time
+//               immediatey.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 void FmodAudioSound::
 void FmodAudioSound::
 set_time(float start_time) {
 set_time(float start_time) {
+  _start_time = start_time;
+
+  if (status() == PLAYING) {
+    // Already playing; skip to the indicated time.
+    start_playing();
+  }
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: FmodAudioSound::get_time
+//       Access: public
+//  Description: Gets the play position within the sound
+////////////////////////////////////////////////////////////////////
+float FmodAudioSound::
+get_time() const {
+  FMOD_RESULT result;
+  unsigned int current_time;
+
+  if (_channel == 0) {
+    return 0.0f;
+  }
+
+  result = _channel->getPosition( &current_time , FMOD_TIMEUNIT_MS );
+  if (result == FMOD_ERR_INVALID_HANDLE) {
+    return 0.0f;
+  }
+  fmod_audio_errcheck("_channel->getPosition()", result);
+  
+  return ((double)current_time) / 1000.0;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: FmodAudioSound::set_volume(float vol)
+//       Access: public
+//  Description: 0.0 to 1.0 scale of volume converted to Fmod's
+//               internal 0.0 to 255.0 scale.
+////////////////////////////////////////////////////////////////////
+void FmodAudioSound::
+set_volume(float vol) {
+  _volume = vol;
+  set_volume_on_channel();
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: FmodAudioSound::get_volume
+//       Access: public
+//  Description: Gets the current volume of a sound.  1 is Max. O is Min.
+////////////////////////////////////////////////////////////////////
+float FmodAudioSound::
+get_volume() const {
+  return _volume;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: FmodAudioSound::start_playing
+//       Access: Private
+//  Description: Starts the sound playing at _start_time.
+////////////////////////////////////////////////////////////////////
+void FmodAudioSound::
+start_playing() {
   FMOD_RESULT result;
   FMOD_RESULT result;
 
 
   if (!_active) {
   if (!_active) {
@@ -294,7 +359,7 @@ set_time(float start_time) {
     return;
     return;
   }
   }
   
   
-  int startTime = (int)(start_time * 1000);
+  int startTime = (int)(_start_time * 1000);
   
   
   if (_channel != 0) {
   if (_channel != 0) {
     // try backing up current sound.
     // try backing up current sound.
@@ -338,51 +403,6 @@ set_time(float start_time) {
   }
   }
 }
 }
 
 
-////////////////////////////////////////////////////////////////////
-//     Function: FmodAudioSound::get_time
-//       Access: public
-//  Description: Gets the play position within the sound
-////////////////////////////////////////////////////////////////////
-float FmodAudioSound::
-get_time() const {
-  FMOD_RESULT result;
-  unsigned int current_time;
-
-  if (_channel == 0) {
-    return 0.0f;
-  }
-
-  result = _channel->getPosition( &current_time , FMOD_TIMEUNIT_MS );
-  if (result == FMOD_ERR_INVALID_HANDLE) {
-    return 0.0f;
-  }
-  fmod_audio_errcheck("_channel->getPosition()", result);
-  
-  return ((double)current_time) / 1000.0;
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: FmodAudioSound::set_volume(float vol)
-//       Access: public
-//  Description: 0.0 to 1.0 scale of volume converted to Fmod's
-//               internal 0.0 to 255.0 scale.
-////////////////////////////////////////////////////////////////////
-void FmodAudioSound::
-set_volume(float vol) {
-  _volume = vol;
-  set_volume_on_channel();
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: FmodAudioSound::get_volume
-//       Access: public
-//  Description: Gets the current volume of a sound.  1 is Max. O is Min.
-////////////////////////////////////////////////////////////////////
-float FmodAudioSound::
-get_volume() const {
-  return _volume;
-}
-
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: FmodAudioSound::set_volume_on_channel()
 //     Function: FmodAudioSound::set_volume_on_channel()
 //       Access: Private
 //       Access: Private
@@ -806,6 +826,7 @@ set_active(bool active) {
         if (get_loop_count() == 0) {
         if (get_loop_count() == 0) {
           // ...we're pausing a looping sound.
           // ...we're pausing a looping sound.
           _paused = true;
           _paused = true;
+          _start_time = get_time();
         }
         }
         stop();
         stop();
       }
       }

+ 2 - 0
panda/src/audiotraits/fmodAudioSound.h

@@ -178,6 +178,7 @@ class EXPCL_FMOD_AUDIO FmodAudioSound : public AudioSound {
   float _min_dist;
   float _min_dist;
   float _max_dist;
   float _max_dist;
 
 
+  void start_playing();
   void set_volume_on_channel();
   void set_volume_on_channel();
   void set_balance_on_channel();
   void set_balance_on_channel();
   void set_play_rate_on_channel();
   void set_play_rate_on_channel();
@@ -191,6 +192,7 @@ class EXPCL_FMOD_AUDIO FmodAudioSound : public AudioSound {
   
   
   bool _active;
   bool _active;
   bool _paused;
   bool _paused;
+  float _start_time;
   
   
   string _finished_event;
   string _finished_event;