Homepage Demos Overview Downloads Tutorials Reference
Credits
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members | Related Pages | Search

SoundTestBehavior.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_SoundTestBehavior_h_
00003 #define INCLUDED_SoundTestBehavior_h_
00004 
00005 #include "Behaviors/BehaviorBase.h"
00006 #include "SoundPlay/SoundManager.h"
00007 
00008 //! allows you to experiment with playing sounds different ways.
00009 /*! A different sound will be played for each of the buttons, except the head buttons.
00010  *  When the chin button is held down, any sounds (from this behavior) will be queued
00011  *  up and then played successively once the chin button is released.
00012  *
00013  *  Notice that this doesn't preload all needed sounds:\n
00014  *  - @c barkmed.wav is listed in /ms/config/tekkotsu.cfg as a preloaded system sound
00015  *  - @c growl.wav will be loaded before being played automatically - notice the
00016  *    hiccup this can cause.
00017  */
00018 class SoundTestBehavior : public BehaviorBase {
00019 public:
00020   //! Constructor
00021   SoundTestBehavior()
00022     : BehaviorBase(), curplay(SoundManager::invalid_Play_ID), endtime(0),
00023       LFr(EventBase::buttonEGID,ButtonSourceID::LFrPawSID,EventBase::activateETID),
00024       RFr(EventBase::buttonEGID,ButtonSourceID::RFrPawSID,EventBase::activateETID),
00025       LBk(EventBase::buttonEGID,ButtonSourceID::LBkPawSID,EventBase::activateETID),
00026       RBk(EventBase::buttonEGID,ButtonSourceID::RBkPawSID,EventBase::activateETID),
00027       Back(EventBase::buttonEGID,ButtonSourceID::BackButSID,EventBase::activateETID)
00028   {}
00029   
00030   
00031   //! Load some sounds, listen for button events
00032   virtual void DoStart() {
00033     BehaviorBase::DoStart();
00034     erouter->addListener(this,EventBase::buttonEGID);
00035     sndman->LoadFile("yap.wav");
00036     sndman->LoadFile("howl.wav");
00037     sndman->LoadFile("whimper.wav");
00038   }
00039 
00040   //! Release sounds we loaded in DoStart()
00041   virtual void DoStop() {
00042     BehaviorBase::DoStop();
00043     erouter->removeListener(this);
00044     sndman->ReleaseFile("howl.wav");
00045     sndman->ReleaseFile("yap.wav");
00046     sndman->ReleaseFile("whimper.wav");
00047   }
00048 
00049   //! Play the sound corresponding to the button
00050   virtual void processEvent(const EventBase& event) {
00051     if(event==LFr)
00052       play("howl.wav");
00053     else if(event==RFr)
00054       play("yap.wav");
00055     else if(event==LBk)
00056       play("whimper.wav");
00057     else if(event==RBk)
00058       play("growl.wav");
00059     else if(event==Back)
00060       play("barkmed.wav");
00061     else if(event.getSourceID()==ButtonSourceID::ChinButSID)
00062       if(event.getTypeID()==EventBase::activateETID) {
00063         //start a new chain
00064         curplay=SoundManager::invalid_Play_ID;
00065         endtime=0;
00066       } else if(pauseWhileChin)
00067         sndman->ResumePlay(curplay);
00068   }
00069 
00070   //! returns name to system
00071   virtual std::string getName() const { return "SoundTestBehavior"; }
00072   static std::string getClassDescription() { return "Plays different sounds when buttons are pressed.  Holding the chin button queues the sounds."; }
00073 protected:
00074   //! called when a button is pressed - checks if it should enqueue or just play
00075   void play(const char* name) {
00076     if(!state->buttons[ChinButOffset]) {
00077 
00078       // Just play the sound
00079       // This is probably all you need how to do unless you want to get fancy
00080       sndman->PlayFile(name);
00081 
00082     } else {
00083 
00084       // Enqueue the sound - mainly useful if you have a set of sounds and want to play a song with them
00085       if(curplay==SoundManager::invalid_Play_ID || !pauseWhileChin && get_time()>=endtime) {
00086         //start a new chain, either this is the first or we already finished playing the chain
00087         curplay=sndman->PlayFile(name);
00088         if(pauseWhileChin)
00089           sndman->PausePlay(curplay);
00090       } else //add to existing chain
00091         sndman->ChainFile(curplay,name);
00092       endtime=sndman->GetRemainTime(curplay)+get_time()-SoundBufferTime;
00093       //-SoundBufferTime to guarrantee ID validity, see SoundManager::GetRemainTime() documentation
00094 
00095     }
00096   }
00097   static const bool pauseWhileChin=true; //!< if this is true, won't start playing chain until you release the chin button
00098   SoundManager::Play_ID curplay; //!< current chain (may not be valid if chin button not down or time is past #endtime)
00099   unsigned int endtime; //!< the expected end of play time for the current chain
00100   
00101   //!@name Event Templates
00102   //!Used to match against the different buttons that have sounds mapped to them
00103   EventBase LFr,RFr,LBk,RBk,Back;
00104   //@}
00105 };
00106 
00107   /*! @file
00108  * @brief Defines the SoundTestBehavior demo, which allows you to experiment with playing sounds different ways.
00109  * @author ejt (Creator)
00110  *
00111  * $Author: ejt $
00112  * $Name: tekkotsu-1_4_1 $
00113  * $Revision: 1.8 $
00114  * $State: Exp $
00115  * $Date: 2003/06/05 17:03:15 $
00116  */
00117 
00118 #endif

Tekkotsu v1.4
Generated Sat Jul 19 00:06:31 2003 by Doxygen 1.3.2