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(std::ios_base::ate),
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     if ( textstream.str().empty() )
00046       textstream << "Speech node " + getName() + " has no text to speak";
00047     curplay_id = sndman->speak(textstream.str(), showText);
00048     if ( curplay_id == SoundManager::invalid_Play_ID ) // something is broken, so punt
00049       postStateCompletion();
00050     else 
00051       erouter->addListener(this, EventBase::audioEGID, curplay_id, EventBase::deactivateETID);
00052   }
00053   
00054   //! deactivate the node, doesn't stop the sound playback unless the #autostop flag has been set
00055   virtual void stop() {
00056     if ( autostop )
00057       stopPlay();
00058     StateNode::stop();
00059   }
00060   
00061   //! receive audioEGID status event and post stateMachineEGID status event
00062   virtual void doEvent() {
00063     curplay_id = SoundManager::invalid_Play_ID;
00064     postStateCompletion();
00065   }
00066   
00067   //! interrupts playing of the current sound
00068   void stopPlay() {
00069     sndman->stopPlay(curplay_id);
00070     curplay_id = SoundManager::invalid_Play_ID;
00071   }
00072   
00073   //! returns the stored text associated with this node
00074   const std::string getText() const { return storedText; }
00075   
00076   //! sets the stored text associated with this node
00077   void setText(const std::string &text) { storedText = text; }
00078   
00079   //! controls whether the text will be printed as well as spoken
00080   void setShowText(bool s) { showText = s; }
00081   
00082   //! returns the current status of the #autostop flag
00083   bool getAutoStop() const { return autostop; }
00084   
00085   //! sets the #autostop flag
00086   void setAutoStop(bool astop) { autostop=astop; }
00087   
00088 protected:
00089   std::string storedText; //!< text to speak, accessed through setText() and getText()
00090   std::ostringstream textstream; //!< An ostringstream which the doStart can manipulate (normally by adding text)
00091   bool showText; //!< flag controlling whether spoken text should also be displayed on the console
00092   SoundManager::Play_ID curplay_id; //!< holds the playback identification so it can be halted any time
00093   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
00094   
00095 private:
00096   std::string savedText; //!< caches text between preStart() and postStart()
00097   std::streampos pos; //!< caches stream size between preStart() and postStart()
00098 };
00099 
00100 /*! @file
00101  * @brief Defines SpeechNode, a simple StateNode that speaks a string and posts a status event upon completion
00102  * @author dst (Creator)
00103  */
00104 
00105 #endif

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:51 2016 by Doxygen 1.6.3