Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

ArSoundsQueue.h

00001 /*
00002 ActivMedia Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004,2005 ActivMedia Robotics, LLC
00004 
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 ActivMedia Robotics for information about a commercial version of ARIA at 
00022 robots@activmedia.com or 
00023 ActivMedia Robotics, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 
00025 */
00026 
00027 /*  $Id: ArSoundsQueue.h,v 1.23 2005/08/04 21:21:06 lafary Exp $
00028 
00029     This file is part of Aria, (C) Copyright 2004, ActivMedia Robotics
00030     <http://www.activmedia.com>
00031 
00032     Aria is free software; you can redistribute it and/or modify
00033     it under the terms of the GNU General Public License as published by
00034     the Free Software Foundation; either version 2 of the License, or
00035     (at your option) any later version.
00036 
00037     Foobar is distributed in the hope that it will be useful,
00038     but WITHOUT ANY WARRANTY; without even the implied warranty of
00039     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00040     GNU General Public License for more details.
00041 
00042     You should have received a copy of the GNU General Public License
00043     along with this software; if not, write to the Free Software
00044     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00045 */
00046 
00047 #ifndef ARSOUNDSQUEUE_H
00048 #define ARSOUNDSQUEUE_H
00049 
00050 
00051 #include "ariaTypedefs.h"
00052 #include "ArASyncTask.h"
00053 #include "ArCondition.h"
00054 #include "ArSpeech.h"
00055 #include <list>
00056 #include <string>
00057 #include <set>
00058 
00079 class ArSoundsQueue: public ArASyncTask
00080 {
00081 public:
00082 
00085   enum ItemType { SPEECH, SOUND_FILE, SOUND_DATA, OTHER };
00086 
00091   typedef ArRetFunctor2<bool, const char*, const char*> PlayItemFunctor;
00092   typedef ArFunctor InterruptItemFunctor;
00093 
00097   class Item {
00098   public:
00099     std::string data;
00100     ItemType type;
00101     std::string params;
00102     int priority;
00103     std::list<InterruptItemFunctor*> interruptCallbacks;
00104     std::list<PlayItemFunctor*> playCallbacks;
00105 
00106     Item();
00107     Item(std::string _data, ItemType _type, std::string _params, int priority);
00108     Item(std::string _data, ItemType _type, std::string _params, int priority, std::list<PlayItemFunctor*> callbacks); 
00109 
00110     Item(const Item& toCopy);
00111 
00113     bool operator==(const Item& other) const
00114     {
00115       return (other.type == type && other.params == params && other.data == data);
00116     }
00117 
00119     void play();
00120 
00122     void interrupt();
00123   };
00124 
00125 
00126   ArSoundsQueue();
00127 
00135   ArSoundsQueue(ArRetFunctor<bool> *speakInitCB, 
00136                     PlayItemFunctor *speakCB = 0, 
00137         InterruptItemFunctor *interruptSpeechCB = 0,
00138                     ArRetFunctor<bool> *playInitCB = 0, 
00139                     PlayItemFunctor *playFileCB = 0,
00140         InterruptItemFunctor *interruptFileCB = 0);
00141 
00147   ArSoundsQueue(ArSpeechSynth* speechSynthesizer, 
00148                     ArRetFunctor<bool> *playInitCB = 0, 
00149                     PlayItemFunctor *playFileCB = 0,
00150         InterruptItemFunctor *interruptFileCB = 0);
00151 
00152   virtual ~ArSoundsQueue();
00153 
00158   void addInitCallback(ArRetFunctor<bool> *cb) {
00159     myInitCallbacks.push_back(cb);
00160   }
00161 
00163   void setSpeakInitCallback(ArRetFunctor<bool> *cb) {
00164     addInitCallback(cb);
00165   }
00166 
00168   void addItem(ArSoundsQueue::Item item);
00169 
00171   void addItem(ItemType type, const char* data, std::list<PlayItemFunctor*> callbacks, int priority = 0, const char* params = 0);
00172 
00176   bool isInitialized()
00177   {
00178     return myInitialized;
00179   }
00180 
00184   bool isSpeakingOrPlaying(void) { return (myPlayingSomething); }
00185 
00187   bool isPlaying() { return myPlayingSomething; }
00188 
00189 
00194   bool isSpeaking() { return myPlayingSomething; }
00195 
00196 
00198   void run(void) { runInThisThread(); }
00199 
00201   void runAsync(void) { create(false); }
00202 
00206   void pause();
00207 
00209   void resume() ;
00210 
00212   bool isPaused();
00213 
00215   void interrupt();
00216 
00221   void clearQueue();
00222 
00225   void stop() ;
00226 
00229   ArFunctor* getPauseCallback() 
00230   {
00231     return new ArFunctorC<ArSoundsQueue>(this, &ArSoundsQueue::pause);
00232   }
00233 
00236   ArFunctor* getResumeCallback() 
00237   {
00238     return new ArFunctorC<ArSoundsQueue>(this, &ArSoundsQueue::resume);
00239   }
00240 
00241 
00243   size_t getCurrentQueueSize()
00244   {
00245     size_t size;
00246     lock();
00247     size = myQueue.size();
00248     unlock();
00249     return size;
00250   }
00251 
00253   void addSoundStartedCallback(ArFunctor* f)
00254   {
00255     myStartPlaybackCBList.push_back(f);
00256   }
00257 
00259   void remSoundStartedCallback(ArFunctor* f)
00260   {
00261     myStartPlaybackCBList.remove(f);
00262   }
00263 
00266   void addSoundFinishedCallback(ArFunctor* f)
00267   {
00268     myEndPlaybackCBList.push_back(f);
00269   }
00270 
00273   void remSoundFinishedCallback(ArFunctor* f)
00274   {
00275     myEndPlaybackCBList.remove(f);
00276   }
00277 
00281   void addQueueNonemptyCallback(ArFunctor* f)
00282   {
00283     myQueueNonemptyCallbacks.push_back(f);
00284   }
00285 
00287   void remQueueNonemptyCallback(ArFunctor* f)
00288   {
00289     myQueueNonemptyCallbacks.remove(f);
00290   }
00291 
00297   void addQueueEmptyCallback(ArFunctor* f)
00298   {
00299     myQueueEmptyCallbacks.push_back(f);
00300   }
00301 
00303   void remQueueEmptyCallback(ArFunctor* f)
00304   {
00305     myQueueEmptyCallbacks.remove(f);
00306   }
00307 
00308   
00318   std::set<int> findPendingItems(const char* item);
00319 
00321   void removePendingItems(const char* item, ItemType type);
00322 
00324   void removePendingItems(const char* data);
00325 
00327   void removePendingItems(int priority);
00328 
00330   void removePendingItems(int priority, ItemType type);
00331 
00333   void removePendingItems(ItemType type);
00334 
00335   std::string nextItem(ItemType type);
00336   std::string nextItem(int priority);
00337   std::string nextItem(ItemType type, int priority);
00338 
00340 
00341   
00347   void setSpeakCallback(PlayItemFunctor *cb) {
00348     myDefaultSpeakCB = cb;
00349   }
00350 
00354   void setInterruptSpeechCallback(InterruptItemFunctor *cb) {
00355     myDefaultInterruptSpeechCB = cb;
00356   }
00357 
00362   void setPlayFileCallback(PlayItemFunctor *cb) {
00363     myDefaultPlayFileCB = cb;
00364   }
00365 
00367   void setPlayWavFileCallback(PlayItemFunctor* cb) {
00368     setPlayFileCallback(cb);
00369   }
00370 
00373   void setInterruptFileCallback(InterruptItemFunctor *cb) {
00374     myDefaultInterruptFileCB = cb;
00375   }
00376 
00378   void setInterruptWavFileCallback(InterruptItemFunctor* cb) {
00379     setInterruptFileCallback(cb);
00380   }
00381 
00390   void speak(const char *fmt, ...);
00391 
00393   void speakWithVoice(const char* voice, const char* fmt, ...);
00394 
00396   void speakWithPriority(int priority, const char* fmt, ...);
00397 
00404   void play(const char *filename_fmt, ...);
00405 
00406 #ifndef SWIG
00407 
00413   ArSoundsQueue::Item createDefaultSpeechItem(const char* speech = 0);
00414 
00421   ArSoundsQueue::Item createDefaultFileItem(const char* filename = 0);
00423 #endif // SWIG
00424   
00426   virtual void *runThread(void *arg);
00427 
00428 protected:
00429   bool myInitialized;
00430   std::list<Item> myQueue;
00431   ArMutex myQueueMutex;
00432   void lock() {
00433     myQueueMutex.lock();
00434   }
00435   void unlock() {
00436     myQueueMutex.unlock();
00437   }
00438   bool tryLock() {
00439     return myQueueMutex.tryLock();
00440   }
00441 
00443   std::list< ArRetFunctor<bool>* > myInitCallbacks;
00444 
00445   bool myPlayingSomething;
00446   Item myLastItem; 
00447 
00449 
00450   PlayItemFunctor *myDefaultSpeakCB;
00451   InterruptItemFunctor *myDefaultInterruptSpeechCB;
00452   PlayItemFunctor *myDefaultPlayFileCB;
00453   InterruptItemFunctor *myDefaultInterruptFileCB;
00455  
00456   int myPauseRequestCount;  
00457   ArCondition myPausedCondition;    
00458 
00460 
00461   std::list<ArFunctor*> myStartPlaybackCBList;
00462   std::list<ArFunctor*> myEndPlaybackCBList;
00463   std::list<ArFunctor*> myQueueNonemptyCallbacks;
00464   std::list<ArFunctor*> myQueueEmptyCallbacks;
00466 
00468   void invokeCallbacks(const std::list<ArFunctor*>& lst);
00469 
00471   void invokeCallbacks(const std::list<ArRetFunctor<bool>*>& lst);
00472 
00477   void pushQueueItem(Item item);
00478   void pushQueueItem_NoLock(Item item);
00480 
00483   Item popQueueItem();
00484   Item popQueueItem_NoLock();
00486 
00487 };
00488 
00489 #endif
00490 

Generated on Wed Oct 19 12:56:37 2005 for Aria by  doxygen 1.4.0