Aria  2.8.0
soundsQueueExample.cpp

This program demonstrates the sound output queue ArSoundsQueueShows how to operate the sounds queue and add WAV files to play.

You may specify up to 10 file names on the command line. This example only demonstrates using the sounds queue for WAV file playback. For demonstration of using the sounds queue for speech synthesis, see the examples provided with either the ArSpeechSynth_Cepstral or ArSpeechSynth_Festival libraries.

Usage: soundQueue <wav file names>

/*
Adept MobileRobots Robotics Interface for Applications (ARIA)
Copyright (C) 2004, 2005 ActivMedia Robotics LLC
Copyright (C) 2006, 2007, 2008, 2009, 2010 MobileRobots Inc.
Copyright (C) 2011, 2012, 2013 Adept Technology
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
If you wish to redistribute ARIA under different terms, contact
Adept MobileRobots for information about a commercial version of ARIA at
robots@mobilerobots.com or
Adept MobileRobots, 10 Columbia Drive, Amherst, NH 03031; +1-603-881-7960
*/
#include "Aria.h"
#include "ariaTypedefs.h"
#include "ariaUtil.h"
#include "ArSoundsQueue.h"
#include "ArSoundPlayer.h"
#include <iostream>
#include <vector>
using namespace std;
void queueNowEmpty() {
printf("The sound queue is now empty.\n");
}
void queueNowNonempty() {
printf("The sound queue is now non-empty.\n");
}
bool no() {
// just a false tautology
return false;
}
int main(int argc, char** argv) {
//ArLog::init(ArLog::StdErr, ArLog::Verbose);
// Create the sound queue.
ArSoundsQueue soundQueue;
// Set WAV file callbacks
// Notifications when the queue goes empty or non-empty.
soundQueue.addQueueEmptyCallback(new ArGlobalFunctor(&queueNowEmpty));
soundQueue.addQueueNonemptyCallback(new ArGlobalFunctor(&queueNowNonempty));
// Run the sound queue in a new thread
soundQueue.runAsync();
// Get WAV file names from command line
if(argc < 2)
{
cerr << "Usage: " << argv[0] << " <up to ten WAV sound file names...>\n";
}
std::vector<const char*> filenames;
for(int i = 1; i < min(argc, 11); i++)
{
filenames.push_back(argv[i]);
}
// This functor can be used to cancel all sound playback until removed
ArGlobalRetFunctor<bool> dontPlayItem(&no);
{
cout << "Queue is " <<
string(soundQueue.isPaused()?"paused":(soundQueue.isPlaying()?"playing":"ready"))
<< ", with " << soundQueue.getCurrentQueueSize() << " pending sounds." << endl
<< "Enter a command followed by the enter key:\n"
<< "\tp\trequest pause state (cumulative)\n"
<< "\tr\trequest resume state (cumulative)\n"
<< "\ti\tinterrupt current sound\n"
<< "\tc\tclear the queue\n"
<< "\tC\tclear priority < 4 from the queue.\n"
<< "\tn\tAdd " << filenames[0] << " to the queue, but with a condition callback to prevent it playing.\n"
<< "\tv\tAdjust volume -50%\n"
<< "\tV\tAdjust volume +50%\n"
<< "\to\tAdjust volume -100%\n"
<< "\tO\tAdjust volume +100%\n"
<< "\tl\tAdjust volume -200%\n"
<< "\tL\tAdjust volume +200%\n"
<< "\t-\tSet volume adjustment to normal level\n"
;
for(size_t i = 0; i < filenames.size(); i++)
cout << "\t" << i << "\tadd " << filenames[i] << " to the queue\n";
cout << "\tq\tquit\n\n";
int c = getchar();
if(c == '\n')
continue;
switch(c)
{
case 'p': soundQueue.pause(); break;
case 'r': soundQueue.resume(); break;
case 'i': soundQueue.interrupt(); break;
case 'q': soundQueue.stop(); ArUtil::sleep(100); Aria::exit(0);
case 'c': soundQueue.clearQueue(); break;
case 'C': soundQueue.removePendingItems(4); break;
case 'n':
{
cout << "Adding \"" << filenames[0] << "\" but with a condition callback that will prevent it from playing...\n";
ArSoundsQueue::Item item = soundQueue.createDefaultFileItem(filenames[0]);
item.playbackConditionCallbacks.push_back(&dontPlayItem);
soundQueue.addItem(item);
break;
}
case 'v': ArSoundPlayer::setVolumePercent(-50.0); break;
case 'V': ArSoundPlayer::setVolumePercent(50.0); break;
case 'o': ArSoundPlayer::setVolumePercent(-100.0); break;
case 'O': ArSoundPlayer::setVolumePercent(100.0); break;
case 'l': ArSoundPlayer::setVolumePercent(-200.0); break;
case 'L': ArSoundPlayer::setVolumePercent(200.0); break;
case '-': ArSoundPlayer::setVolumePercent(0.0); break;
default:
if(filenames.size() > 0 && c >= '0' && c <= '9')
{
size_t i = c - '0';
if(i < filenames.size())
{
cout << "Adding \"" << filenames[i] << "\" to the queue...\n";
soundQueue.play(filenames[i]);
}
}
}
}
cout << "ended.\n";
return 0;
}
ArSoundPlayer::getPlayWavFileCallback
static ArRetFunctor2< bool, const char *, const char * > * getPlayWavFileCallback()
Return the static functor for playWavFile.
Definition: ArSoundPlayer.cpp:39
ArGlobalRetFunctor< bool >
ArSoundsQueue::clearQueue
void clearQueue()
Empty the queue.
Definition: ArSoundsQueue.cpp:628
ArSoundsQueue::Item
A sound item in the queue, with callbacks for dealing with the item and the data to pass to those cal...
Definition: ArSoundsQueue.h:85
ArSoundsQueue::getCurrentQueueSize
size_t getCurrentQueueSize()
Get the current size of the speech/sound playback queue.
Definition: ArSoundsQueue.h:243
ArSoundsQueue::resume
void resume()
Resume processing the sounds queue.
Definition: ArSoundsQueue.cpp:591
ArSoundsQueue::stop
void stop()
End the processing thread.
Definition: ArSoundsQueue.cpp:606
ArSoundsQueue::play
void play(const char *filename)
Add a sound file to the queue for default sound file playback.
Definition: ArSoundsQueue.cpp:349
ArSoundsQueue::interrupt
void interrupt()
If sound is currently being played or speech is being spoken, interrupt it. (but continue processing ...
Definition: ArSoundsQueue.cpp:616
ArSoundsQueue::runAsync
void runAsync(void)
Begin processing the sounds queue in a background thread.
Definition: ArSoundsQueue.h:196
ArSoundsQueue::setPlayWavFileCallback
void setPlayWavFileCallback(PlayItemFunctor *cb)
Definition: ArSoundsQueue.h:407
ArSoundPlayer::getStopPlayingCallback
static ArFunctor * getStopPlayingCallback()
Return the static functor for stopPlaying().
Definition: ArSoundPlayer.cpp:45
ArSoundPlayer::setVolumePercent
static void setVolumePercent(double pct)
Set volume as a "percent" of normal, where 100% is normal or natural volume, 50% is increased by 50%,...
Definition: ArSoundPlayer.cpp:301
ArSoundsQueue
This class manages a queue of items to play as WAV files or as text to speak using a speech synthesiz...
Definition: ArSoundsQueue.h:62
Aria::exit
static void exit(int exitCode=0)
Shutdown all Aria processes/threads, call exit callbacks, and exit the program.
Definition: Aria.cpp:367
ArSoundsQueue::isPlaying
bool isPlaying()
Returns true if an item is currently being played.
Definition: ArSoundsQueue.h:182
Aria::init
static void init(SigHandleMethod method=SIGHANDLE_THREAD, bool initSockets=true, bool sigHandleExitNotShutdown=true)
Initialize Aria global data struture and perform OS-specific initialization, including adding OS sign...
Definition: Aria.cpp:128
ArSoundsQueue::addItem
void addItem(ArSoundsQueue::Item item)
Add (a copy of) the given item to the queue.
Definition: ArSoundsQueue.cpp:288
ArSoundsQueue::setInterruptWavFileCallback
void setInterruptWavFileCallback(InterruptItemFunctor *cb)
Definition: ArSoundsQueue.h:420
ArSoundsQueue::isPaused
bool isPaused()
Definition: ArSoundsQueue.cpp:611
ArSoundsQueue::addQueueEmptyCallback
void addQueueEmptyCallback(ArFunctor *f)
Add a callback functor to be invoked when the sound queue becomes empty and the last sound has finish...
Definition: ArSoundsQueue.h:327
ArUtil::sleep
static void sleep(unsigned int ms)
Sleep for the given number of milliseconds.
Definition: ariaUtil.cpp:151
ArSoundsQueue::addQueueNonemptyCallback
void addQueueNonemptyCallback(ArFunctor *f)
Add a callback functor to be invoked when a the sound queue becomes non-empty, that is,...
Definition: ArSoundsQueue.h:311
ArSoundsQueue::createDefaultFileItem
ArSoundsQueue::Item createDefaultFileItem(const char *filename=0)
Return an item set up for sound file playback with previously set default play callbacks.
Definition: ArSoundsQueue.cpp:453
ArGlobalFunctor
Functor for a global function with no parameters.
Definition: ArFunctor.h:648
Aria::getRunning
static bool getRunning(void)
Sees if Aria is still running (mostly for the thread in main)
Definition: Aria.cpp:753
ArSoundsQueue::removePendingItems
void removePendingItems(const char *item, ItemType type)
Remove pending items with the given data and type.
Definition: ArSoundsQueue.cpp:651
ArSoundsQueue::pause
void pause()
Temporarily stop processing the sounds queue.
Definition: ArSoundsQueue.cpp:584