Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

SpeechNode.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_SpeechNode_h_
00003 #define INCLUDED_SpeechNode_h_
00004 
00005 #include <sstream>   // for ostringstream
00006 
00007 #include "Behaviors/StateNode.h"
00008 #include "Sound/SoundManager.h"
00009 
00010 //! A StateNode that speaks text upon startup and posts a status event on completion.
00011 /*! Speech text comes from three places: the @a text argument, a DataEvent<string>,
00012  *  and anything the user writes to #textstream, in that order.
00013  *  By default, sound playback will continue even after this node has been deactivated.
00014  *  If this is not the desired behavior, set the #autostop flag (through setAutoStop())
00015  */
00016 class SpeechNode : public StateNode {
00017 public:
00018   //! constructor, specify instance name and string to speak
00019   SpeechNode(const std::string& nodename, const std::string& text="")
00020     : StateNode(nodename), storedText(text), textstream(),
00021       showText(true), curplay_id(SoundManager::invalid_Play_ID), autostop(false),
00022       savedText(), pos() {}
00023   
00024   //! activate the node, starts playing the text
00025   virtual void preStart() {
00026     StateNode::preStart();
00027     
00028     // Speech text comes from three places: the storedText variable, a DataEvent<string>,
00029     // and anything the user writes to textstream.  We concatenate them in that order.
00030     textstream.str(storedText);
00031     if ( event!=NULL ) {
00032       const DataEvent<std::string> *datev = dynamic_cast<const DataEvent<std::string>*>(event);
00033       if ( datev != NULL )
00034         textstream << (!storedText.empty() ? " " : "") << datev->getData();
00035     }
00036     savedText = textstream.str();
00037     textstream << (textstream.str().empty() ? "" : " ");  // add a trailing space in case user adds text
00038     pos = textstream.tellp();
00039   }
00040   
00041   virtual void postStart() {
00042     StateNode::postStart();
00043     if ( textstream.tellp() == pos )
00044       textstream.str(savedText);  // get rid of the trailing space if user didn't add text
00045     curplay_id = sndman->speak(textstream.str(), showText);
00046     if ( curplay_id == SoundManager::invalid_Play_ID ) // something is broken, so punt
00047       postStateCompletion();
00048     else 
00049       erouter->addListener(this, EventBase::audioEGID, curplay_id);
00050   }
00051   
00052   //! deactivate the node, doesn't stop the sound playback unless the #autostop flag has been set
00053   virtual void stop() {
00054     if(autostop)
00055       stopPlay();
00056     StateNode::stop();
00057   }
00058   
00059   //! receive audioEGID status event and post stateMachineEGID status event
00060   virtual void doEvent() {
00061     curplay_id = SoundManager::invalid_Play_ID;
00062     postStateCompletion();
00063   }
00064   
00065   //! interrupts playing of the current sound
00066   void stopPlay() {
00067     sndman->stopPlay(curplay_id);
00068     curplay_id = SoundManager::invalid_Play_ID;
00069   }
00070   
00071   //! returns the stored text associated with this node
00072   const std::string getText() const { return storedText; }
00073   
00074   //! sets the stored text associated with this node
00075   void setText(const std::string &text) { storedText = text; }
00076   
00077   //! controls whether the text will be printed as well as spoken
00078   void setShowText(bool s) { showText = s; }
00079   
00080   //! returns the current status of the #autostop flag
00081   bool getAutoStop() { return autostop; }
00082   
00083   //! sets the #autostop flag
00084   void setAutoStop(bool astop) { autostop=astop; }
00085   
00086 protected:
00087   std::string storedText; //!< text to speak, accessed through setText() and getText()
00088   std::ostringstream textstream; //!< An ostringstream which the doStart can manipulate (normally by adding text)
00089   bool showText; //!< flag controlling whether spoken text should also be displayed on the console
00090   SoundManager::Play_ID curplay_id; //!< holds the playback identification so it can be halted any time
00091   bool autostop; //!< if set to true by setAutoStop(), when this node is deactivated, playback will be halted.  Otherwise, playback will continue even after the node is deactivated
00092   
00093 private:
00094   std::string savedText; //!< caches text between preStart() and postStart()
00095   std::streampos pos; //!< caches stream size between preStart() and postStart()
00096 };
00097 
00098 /*! @file
00099  * @brief Defines SpeechNode, a simple StateNode that speaks a string and posts a status event upon completion
00100  * @author dst (Creator)
00101  */
00102 
00103 #endif

Tekkotsu v5.1CVS
Generated Fri Mar 16 05:26:52 2012 by Doxygen 1.6.3