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("SoundTestBehavior"), 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   static std::string getClassDescription() { return "Plays different sounds when buttons are pressed.  Holding the chin button queues the sounds."; }
00083   virtual std::string getDescription() const { return getClassDescription(); }
00084 
00085 protected:
00086   //! called when a button is pressed - checks if it should enqueue or just play
00087   void play(const char* name) {
00088     if(!state->buttons[ChinButOffset]) {
00089 
00090       // Just play the sound
00091       // This is probably all you need how to do unless you want to get fancy
00092       sndman->PlayFile(name);
00093 
00094     } else {
00095 
00096       // Enqueue the sound - mainly useful if you have a set of sounds and want to play a song with them
00097       if(curplay==SoundManager::invalid_Play_ID || !pauseWhileChin && get_time()>=endtime) {
00098         //start a new chain, either this is the first or we already finished playing the chain
00099         curplay=sndman->PlayFile(name);
00100         if(pauseWhileChin)
00101           sndman->PausePlay(curplay);
00102       } else //add to existing chain
00103         sndman->ChainFile(curplay,name);
00104       endtime=sndman->GetRemainTime(curplay)+get_time()-SoundBufferTime;
00105       //-SoundBufferTime to guarrantee ID validity, see SoundManager::GetRemainTime() documentation
00106 
00107     }
00108   }
00109   static const bool pauseWhileChin=true; //!< if this is true, won't start playing chain until you release the chin button
00110   SoundManager::Play_ID curplay; //!< current chain (may not be valid if chin button not down or time is past #endtime)
00111   unsigned int endtime; //!< the expected end of play time for the current chain
00112   
00113   //!@name Event Templates
00114   //!Used to match against the different buttons that have sounds mapped to them
00115   EventBase LFr,RFr,LBk,RBk,Back;
00116   //@}
00117 };
00118 
00119   /*! @file
00120  * @brief Defines the SoundTestBehavior demo, which allows you to experiment with playing sounds different ways.
00121  * @author ejt (Creator)
00122  *
00123  * $Author: ejt $
00124  * $Name: tekkotsu-2_2_1 $
00125  * $Revision: 1.11 $
00126  * $State: Exp $
00127  * $Date: 2004/11/11 01:45:36 $
00128  */
00129 
00130 #endif

Tekkotsu v2.2.1
Generated Tue Nov 23 16:36:40 2004 by Doxygen 1.3.9.1