Panda3D
 All Classes Functions Variables Enumerations
milesAudioSound.cxx
00001 // Filename: milesAudioSound.cxx
00002 // Created by:  drose (30Jul07)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "milesAudioSound.h"
00016 #ifdef HAVE_RAD_MSS //[
00017 
00018 #include "milesAudioManager.h"
00019 
00020 TypeHandle MilesAudioSound::_type_handle;
00021 
00022 #undef miles_audio_debug
00023 
00024 #ifndef NDEBUG //[
00025 #define miles_audio_debug(x) \
00026     audio_debug("MilesAudioSound \""<<get_name()<<"\" "<< x )
00027 #else //][
00028 #define miles_audio_debug(x) ((void)0)
00029 #endif //]
00030 
00031 ////////////////////////////////////////////////////////////////////
00032 //     Function: MilesAudioSound::Constructor
00033 //       Access: Private
00034 //  Description: 
00035 ////////////////////////////////////////////////////////////////////
00036 MilesAudioSound::
00037 MilesAudioSound(MilesAudioManager *manager,
00038                 const string &file_name) :
00039   _manager(manager),
00040   _file_name(file_name),
00041   _volume(1.0f), _balance(0), _play_rate(1.0f),
00042   _loop_count(1), 
00043   _active(true), 
00044   _paused(false),
00045   _start_time(0.0f),
00046   _got_start_time(false)
00047 {
00048   nassertv(!file_name.empty());
00049 }
00050 
00051 ////////////////////////////////////////////////////////////////////
00052 //     Function: MilesAudioSound::set_loop
00053 //       Access: Public, Virtual
00054 //  Description: 
00055 ////////////////////////////////////////////////////////////////////
00056 void MilesAudioSound::
00057 set_loop(bool loop) {
00058   // loop count of 0 means always loop
00059   set_loop_count((loop)?0:1);
00060 }
00061 
00062 ////////////////////////////////////////////////////////////////////
00063 //     Function: MilesAudioSound::get_loop
00064 //       Access: Public, Virtual
00065 //  Description: 
00066 ////////////////////////////////////////////////////////////////////
00067 bool MilesAudioSound::
00068 get_loop() const {
00069   return (_loop_count == 0);
00070 }
00071 
00072 ////////////////////////////////////////////////////////////////////
00073 //     Function: MilesAudioSound::
00074 //       Access: Public, Virtual
00075 //  Description: 
00076 ////////////////////////////////////////////////////////////////////
00077 void MilesAudioSound::
00078 set_loop_count(unsigned long loop_count) {
00079   if (_loop_count != loop_count) {
00080     _loop_count = loop_count;
00081     if (status() == PLAYING) {
00082       // hack:
00083       // For now, the loop count is picked up when the sound starts playing.
00084       // There may be a way to change the loop count of a playing sound, but
00085       // I'm going to focus on other things.  If you would like to change the
00086       // need to stop and start the sound, feel free.  Or, maybe I'll spend
00087       // time on it in the future.  Please set the loop option before starting
00088       // the sound.
00089       play();
00090     }
00091   }
00092 }
00093 
00094 ////////////////////////////////////////////////////////////////////
00095 //     Function: MilesAudioSound::
00096 //       Access: Public, Virtual
00097 //  Description: 
00098 ////////////////////////////////////////////////////////////////////
00099 unsigned long MilesAudioSound::
00100 get_loop_count() const {
00101   return _loop_count;
00102 }
00103 
00104 ////////////////////////////////////////////////////////////////////
00105 //     Function: MilesAudioSound::get_volume
00106 //       Access: Public, Virtual
00107 //  Description: 
00108 ////////////////////////////////////////////////////////////////////
00109 PN_stdfloat MilesAudioSound::
00110 get_volume() const {
00111   return _volume;
00112 }
00113 
00114 ////////////////////////////////////////////////////////////////////
00115 //     Function: MilesAudioSound::get_balance
00116 //       Access: Public, Virtual
00117 //  Description: 
00118 ////////////////////////////////////////////////////////////////////
00119 PN_stdfloat MilesAudioSound::
00120 get_balance() const {
00121   return _balance;
00122 }
00123 
00124 ////////////////////////////////////////////////////////////////////
00125 //     Function: MilesAudioSound::get_play_rate
00126 //       Access: Public, Virtual
00127 //  Description: 
00128 ////////////////////////////////////////////////////////////////////
00129 PN_stdfloat MilesAudioSound::
00130 get_play_rate() const {
00131   return _play_rate;
00132 }
00133 
00134 ////////////////////////////////////////////////////////////////////
00135 //     Function: MilesAudioSound::set_time
00136 //       Access: Public, Virtual
00137 //  Description: 
00138 ////////////////////////////////////////////////////////////////////
00139 void MilesAudioSound::
00140 set_time(PN_stdfloat time) {
00141   miles_audio_debug("set_time(time="<<time<<")");
00142 
00143   // Mark this position for the next play().
00144   _start_time = time;
00145   _got_start_time = true;
00146 }
00147 
00148 ////////////////////////////////////////////////////////////////////
00149 //     Function: MilesAudioSound::set_active
00150 //       Access: Public, Virtual
00151 //  Description: 
00152 ////////////////////////////////////////////////////////////////////
00153 void MilesAudioSound::
00154 set_active(bool active) {
00155   if (_manager == (MilesAudioManager *)NULL) {
00156     return;
00157   }
00158 
00159   miles_audio_debug("set_active(active="<<active<<")");
00160   if (_active != active) {
00161     _active = active;
00162     if (_active) {
00163       // ...activate the sound.
00164       if (_paused && _loop_count==0) {
00165         // ...this sound was looping when it was paused.
00166         _paused = false;
00167         play();
00168       }
00169 
00170     } else {
00171       // ...deactivate the sound.
00172       if (status() == PLAYING) {
00173         if (_loop_count == 0) {
00174           // ...we're pausing a looping sound.
00175           _paused = true;
00176         }
00177         _start_time = get_time();
00178         _got_start_time = true;
00179         stop();
00180       }
00181     }
00182   }
00183 }
00184 
00185 ////////////////////////////////////////////////////////////////////
00186 //     Function: MilesAudioSound::get_active
00187 //       Access: Public, Virtual
00188 //  Description: 
00189 ////////////////////////////////////////////////////////////////////
00190 bool MilesAudioSound::
00191 get_active() const {
00192   return _active;
00193 }
00194 
00195 ////////////////////////////////////////////////////////////////////
00196 //     Function: MilesAudioSound::set_finished_event
00197 //       Access: Public, Virtual
00198 //  Description: This is no longer implemented.
00199 ////////////////////////////////////////////////////////////////////
00200 void MilesAudioSound::
00201 set_finished_event(const string &event) {
00202   _finished_event = event;
00203 }
00204 
00205 ////////////////////////////////////////////////////////////////////
00206 //     Function: MilesAudioSound::get_finished_event
00207 //       Access: Public, Virtual
00208 //  Description: This is no longer implemented.
00209 ////////////////////////////////////////////////////////////////////
00210 const string &MilesAudioSound::
00211 get_finished_event() const {
00212   return _finished_event;
00213 }
00214 
00215 ////////////////////////////////////////////////////////////////////
00216 //     Function: MilesAudioSound::get_name
00217 //       Access: Public, Virtual
00218 //  Description: 
00219 ////////////////////////////////////////////////////////////////////
00220 const string &MilesAudioSound::
00221 get_name() const {
00222   return _file_name;
00223 }
00224 
00225 ////////////////////////////////////////////////////////////////////
00226 //     Function: MilesAudioSound::cleanup
00227 //       Access: Public, Virtual
00228 //  Description: Stops the sound from playing and releases any
00229 //               associated resources, in preparation for releasing
00230 //               the sound or shutting down the sound system.
00231 ////////////////////////////////////////////////////////////////////
00232 void MilesAudioSound::
00233 cleanup() {
00234 }
00235 
00236 #endif  //]
 All Classes Functions Variables Enumerations