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

soundsQueueExample.cpp

00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 MobileRobots Inc.
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 MobileRobots for information about a commercial version of ARIA at 
00022 robots@mobilerobots.com or 
00023 MobileRobots Inc, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 */
00025 
00026 
00042 #include "Aria.h"
00043 #include "ariaTypedefs.h"
00044 #include "ariaUtil.h"
00045 #include "ArSoundsQueue.h"
00046 #include "ArSoundPlayer.h"
00047 #include <iostream>
00048 #include <vector>
00049 
00050 using namespace std;
00051 
00052 void queueNowEmpty() {
00053   printf("The sound queue is now empty.\n");
00054 }
00055 
00056 void queueNowNonempty() {
00057   printf("The sound queue is now non-empty.\n");
00058 }
00059 
00060 bool no() {
00061   // just a false tautology
00062   return false;
00063 }
00064 
00065 int main(int argc, char** argv) {
00066   Aria::init();
00067   //ArLog::init(ArLog::StdErr, ArLog::Verbose);
00068 
00069   // Create the sound queue.
00070   ArSoundsQueue soundQueue;
00071 
00072   // Set WAV file callbacks 
00073   soundQueue.setPlayWavFileCallback(ArSoundPlayer::getPlayWavFileCallback());
00074   soundQueue.setInterruptWavFileCallback(ArSoundPlayer::getStopPlayingCallback());
00075 
00076   // Notifications when the queue goes empty or non-empty.
00077   soundQueue.addQueueEmptyCallback(new ArGlobalFunctor(&queueNowEmpty));
00078   soundQueue.addQueueNonemptyCallback(new ArGlobalFunctor(&queueNowNonempty));
00079 
00080   // Run the sound queue in a new thread
00081   soundQueue.runAsync();
00082 
00083   // Get WAV file names from command line
00084   if(argc < 2) 
00085   {
00086     cerr << "Usage: " << argv[0] << " <up to ten WAV sound file names...>\n";
00087     exit(-1);
00088   }
00089   std::vector<const char*> filenames;
00090   for(int i = 1; i < min(argc, 11); i++) 
00091   {
00092     filenames.push_back(argv[i]);
00093   }
00094 
00095   // This functor can be used to cancel all sound playback until removed
00096   ArGlobalRetFunctor<bool> dontPlayItem(&no);
00097   
00098   while(Aria::getRunning())
00099   {
00100     cout << "Queue is " << 
00101       string(soundQueue.isPaused()?"paused":(soundQueue.isPlaying()?"playing":"ready")) 
00102       << ", with " << soundQueue.getCurrentQueueSize() << " pending sounds." << endl
00103       << "Enter a command followed by the enter key:\n"
00104       << "\tp\trequest pause state (cumulative)\n"
00105       << "\tr\trequest resume state (cumulative)\n" 
00106       << "\ti\tinterrupt current sound\n"
00107       << "\tc\tclear the queue\n"
00108       << "\tC\tclear priority < 4 from the queue.\n"
00109       << "\tn\tAdd " << filenames[0] << " to the queue, but with a condition callback to prevent it playing.\n"
00110       ;
00111     for(size_t i = 0; i < filenames.size(); i++)
00112       cout << "\t" << i << "\tadd " << filenames[i] << " to the queue\n";
00113     cout << "\tq\tquit\n\n";
00114 
00115     int c = getchar();
00116     if(c == '\n')
00117       continue;
00118     switch(c)
00119     {
00120       case 'p': soundQueue.pause(); break;
00121       case 'r': soundQueue.resume(); break;
00122       case 'i': soundQueue.interrupt(); break;
00123       case 'q': soundQueue.stop(); ArUtil::sleep(100); exit(0);
00124       case 'c': soundQueue.clearQueue(); break;
00125       case 'C': soundQueue.removePendingItems(4); break;
00126       case 'n': 
00127       {
00128         cout << "Adding \"" << filenames[0] << "\" but with a condition callback that will prevent it from playing...\n";
00129         ArSoundsQueue::Item item = soundQueue.createDefaultFileItem(filenames[0]);
00130         item.playbackConditionCallbacks.push_back(&dontPlayItem);
00131         soundQueue.addItem(item);
00132         break;
00133       }
00134       default:
00135         if(filenames.size() > 0 && c >= '0' && c <= '9')
00136         {
00137           size_t i = c - '0';
00138           if(i < filenames.size()) 
00139           {
00140             cout << "Adding \"" << filenames[i] << "\" to the queue...\n"; 
00141             soundQueue.play(filenames[i]);
00142           } 
00143         }
00144     }
00145   }
00146   cout << "Aria ended.\n";
00147   Aria::shutdown();
00148   return 0;
00149 }
00150 
00151 

Generated on Tue Feb 20 10:51:42 2007 for Aria by  doxygen 1.4.0