00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef ARSOUNDSQUEUE_H
00028 #define ARSOUNDSQUEUE_H
00029
00030
00031 #include "ariaTypedefs.h"
00032 #include "ArASyncTask.h"
00033 #include "ArCondition.h"
00034 #include "ArSpeech.h"
00035 #include <list>
00036 #include <string>
00037 #include <set>
00038
00060 class ArSoundsQueue: public ArASyncTask
00061 {
00062 public:
00063
00066 enum ItemType { SPEECH, SOUND_FILE, SOUND_DATA, OTHER };
00067
00072 typedef ArRetFunctor2<bool, const char*, const char*> PlayItemFunctor;
00073 typedef ArFunctor InterruptItemFunctor;
00074
00078 typedef ArRetFunctor<bool> PlaybackConditionFunctor;
00079
00083 class Item {
00084 public:
00085 std::string data;
00086 ItemType type;
00087 std::string params;
00088 int priority;
00089 std::list<InterruptItemFunctor*> interruptCallbacks;
00090 std::list<PlayItemFunctor*> playCallbacks;
00091 std::list<ArFunctor*> doneCallbacks;
00092 std::list<PlaybackConditionFunctor*> playbackConditionCallbacks;
00093
00094 AREXPORT Item();
00095 AREXPORT Item(std::string _data, ItemType _type, std::string _params = "", int priority = 0);
00096 AREXPORT Item(std::string _data, ItemType _type, std::string _params, int priority, std::list<PlayItemFunctor*> callbacks);
00097
00098 AREXPORT Item(const ArSoundsQueue::Item& toCopy);
00099
00101 bool operator==(const Item& other) const
00102 {
00103 return (other.type == type && other.params == params && other.data == data);
00104 }
00105
00107 void play();
00108
00111 void interrupt();
00112
00115 void done();
00116 };
00117
00118
00119 AREXPORT ArSoundsQueue();
00120
00128 AREXPORT ArSoundsQueue(ArRetFunctor<bool> *speakInitCB,
00129 PlayItemFunctor *speakCB = 0,
00130 InterruptItemFunctor *interruptSpeechCB = 0,
00131 ArRetFunctor<bool> *playInitCB = 0,
00132 PlayItemFunctor *playFileCB = 0,
00133 InterruptItemFunctor *interruptFileCB = 0);
00134
00140 AREXPORT ArSoundsQueue(ArSpeechSynth* speechSynthesizer,
00141 ArRetFunctor<bool> *playInitCB = 0,
00142 PlayItemFunctor *playFileCB = 0,
00143 InterruptItemFunctor *interruptFileCB = 0);
00144
00145 AREXPORT virtual ~ArSoundsQueue();
00146
00151 AREXPORT void addInitCallback(ArRetFunctor<bool> *cb) {
00152 myInitCallbacks.push_back(cb);
00153 }
00154
00156 AREXPORT void setSpeakInitCallback(ArRetFunctor<bool> *cb) {
00157 addInitCallback(cb);
00158 }
00159
00161 AREXPORT void addItem(ArSoundsQueue::Item item);
00162
00164 AREXPORT void addItem(ItemType type, const char* data, std::list<PlayItemFunctor*> callbacks, int priority = 0, const char* params = 0);
00165
00169 AREXPORT bool isInitialized()
00170 {
00171 return myInitialized;
00172 }
00173
00177 AREXPORT bool isSpeakingOrPlaying(void) { return (myPlayingSomething); }
00178
00180 AREXPORT bool isPlaying() { return myPlayingSomething; }
00181
00182
00187 AREXPORT bool isSpeaking() { return myPlayingSomething; }
00188
00189
00191 AREXPORT void run(void) { runInThisThread(); }
00192
00194 AREXPORT void runAsync(void) { create(false); }
00195
00199 AREXPORT void pause();
00200
00202 AREXPORT void resume() ;
00203
00205 AREXPORT bool isPaused();
00206
00208 AREXPORT void interrupt();
00209
00214 AREXPORT void clearQueue();
00215
00223 AREXPORT void stop() ;
00224
00227 AREXPORT ArFunctor* getPauseCallback()
00228 {
00229 return new ArFunctorC<ArSoundsQueue>(this, &ArSoundsQueue::pause);
00230 }
00231
00234 AREXPORT ArFunctor* getResumeCallback()
00235 {
00236 return new ArFunctorC<ArSoundsQueue>(this, &ArSoundsQueue::resume);
00237 }
00238
00239
00241 AREXPORT size_t getCurrentQueueSize()
00242 {
00243 size_t size;
00244 lock();
00245 size = myQueue.size();
00246 unlock();
00247 return size;
00248 }
00249
00251 AREXPORT void addSoundStartedCallback(ArFunctor* f)
00252 {
00253 myStartPlaybackCBList.push_back(f);
00254 }
00255
00257 AREXPORT void remSoundStartedCallback(ArFunctor* f)
00258 {
00259 myStartPlaybackCBList.remove(f);
00260 }
00261
00264 AREXPORT void addSoundFinishedCallback(ArFunctor* f)
00265 {
00266 myEndPlaybackCBList.push_back(f);
00267 }
00268
00271 AREXPORT void remSoundFinishedCallback(ArFunctor* f)
00272 {
00273 myEndPlaybackCBList.remove(f);
00274 }
00275
00279 AREXPORT void addQueueNonemptyCallback(ArFunctor* f)
00280 {
00281 myQueueNonemptyCallbacks.push_back(f);
00282 }
00283
00285 AREXPORT void remQueueNonemptyCallback(ArFunctor* f)
00286 {
00287 myQueueNonemptyCallbacks.remove(f);
00288 }
00289
00295 AREXPORT void addQueueEmptyCallback(ArFunctor* f)
00296 {
00297 myQueueEmptyCallbacks.push_back(f);
00298 }
00299
00301 AREXPORT void remQueueEmptyCallback(ArFunctor* f)
00302 {
00303 myQueueEmptyCallbacks.remove(f);
00304 }
00305
00306
00307
00317 AREXPORT std::set<int> findPendingItems(const char* item);
00318
00320 AREXPORT void removePendingItems(const char* item, ItemType type);
00321
00323 AREXPORT void removePendingItems(const char* data);
00324
00326 AREXPORT void removePendingItems(int priority);
00327
00329 AREXPORT void removePendingItems(int priority, ItemType type);
00330
00332 AREXPORT void removePendingItems(ItemType type);
00333
00334 AREXPORT std::string nextItem(ItemType type);
00335 AREXPORT std::string nextItem(int priority);
00336 AREXPORT std::string nextItem(ItemType type, int priority);
00337
00339
00340
00347 AREXPORT void setSpeakCallback(PlayItemFunctor *cb) {
00348 myDefaultSpeakCB = cb;
00349 }
00350
00355 AREXPORT void setInterruptSpeechCallback(InterruptItemFunctor *cb) {
00356 myDefaultInterruptSpeechCB = cb;
00357 }
00358
00364 AREXPORT void setPlayFileCallback(PlayItemFunctor *cb) {
00365 myDefaultPlayFileCB = cb;
00366 }
00367
00369 AREXPORT void setPlayWavFileCallback(PlayItemFunctor* cb) {
00370 setPlayFileCallback(cb);
00371 }
00372
00377 AREXPORT void setInterruptFileCallback(InterruptItemFunctor *cb) {
00378 myDefaultInterruptFileCB = cb;
00379 }
00380
00382 AREXPORT void setInterruptWavFileCallback(InterruptItemFunctor* cb) {
00383 setInterruptFileCallback(cb);
00384 }
00385
00386
00388
00389
00390 #if !(defined(WIN32) && defined(_MANAGED))
00391
00400 AREXPORT void speak(const char *fmt, ...);
00401
00403 AREXPORT void speakWithVoice(const char* voice, const char* fmt, ...);
00404
00406 AREXPORT void speakWithPriority(int priority, const char* fmt, ...);
00407
00414 AREXPORT void play(const char *filename_fmt, ...);
00415
00416 #endif // MS Managed C++
00417
00424 AREXPORT ArSoundsQueue::Item createDefaultSpeechItem(const char* speech = 0);
00425
00432 AREXPORT ArSoundsQueue::Item createDefaultFileItem(const char* filename = 0);
00433
00435
00438 AREXPORT void setDefaultPlayConditionCB(PlaybackConditionFunctor* f) {
00439 myDefaultPlayConditionCB = f;
00440 }
00441
00443
00444 AREXPORT virtual void *runThread(void *arg);
00445
00446 protected:
00447 bool myInitialized;
00448 std::list<Item> myQueue;
00449 ArMutex myQueueMutex;
00450 void lock() {
00451 myQueueMutex.lock();
00452 }
00453 void unlock() {
00454 myQueueMutex.unlock();
00455 }
00456 bool tryLock() {
00457 return myQueueMutex.tryLock();
00458 }
00459
00461 std::list< ArRetFunctor<bool>* > myInitCallbacks;
00462
00463 bool myPlayingSomething;
00464 Item myLastItem;
00465
00467
00468 PlayItemFunctor *myDefaultSpeakCB;
00469 InterruptItemFunctor *myDefaultInterruptSpeechCB;
00470 PlayItemFunctor *myDefaultPlayFileCB;
00471 InterruptItemFunctor *myDefaultInterruptFileCB;
00473
00474 int myPauseRequestCount;
00475 ArCondition myPausedCondition;
00476
00478
00479 std::list<ArFunctor*> myStartPlaybackCBList;
00480 std::list<ArFunctor*> myEndPlaybackCBList;
00481 std::list<ArFunctor*> myQueueNonemptyCallbacks;
00482 std::list<ArFunctor*> myQueueEmptyCallbacks;
00484
00485 PlaybackConditionFunctor* myDefaultPlayConditionCB;
00486
00488 void invokeCallbacks(const std::list<ArFunctor*>& lst);
00489
00491 void invokeCallbacks(const std::list<ArRetFunctor<bool>*>& lst);
00492
00497 void pushQueueItem(Item item);
00498 void pushQueueItem_NoLock(Item item);
00500
00503 Item popQueueItem();
00504 Item popQueueItem_NoLock();
00506
00507 };
00508
00509 #endif
00510