Homepage Demos Overview Downloads Tutorials Reference
Credits

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

Tekkotsu v2.2
Generated Tue Oct 19 14:19:16 2004 by Doxygen 1.3.9.1