Exercise: Stopping a Sound, and Mixing Sounds
We will modify the previous example to play a long, drawn-out howl
sound when the AIBO's back button is pressed. (For the ERS-7, which
has three back buttons, use MiddleBackButOffset instead of
BackButOffset.) If the back button is pressed again while the AIBO is
already howling, it should stop that howl and begin a new one. In
order to do this, we will retain the current howl's PlayID in a
protected data member called howl_id .
#include "Behaviors/BehaviorBase.h"
#include "Events/EventRouter.h"
#include "Sound/SoundManager.h"
class DstBehavior : public BehaviorBase {
protected:
SoundManager::Play_ID howl_id;
public:
DstBehavior() : BehaviorBase("DstBehavior"), howl_id(SoundManager::invalid_Play_ID) {}
virtual void doStart() {
BehaviorBase::doStart();
std::cout << getName() << " is starting up." << endl;
erouter->addListener(this,EventBase::buttonEGID);
sndman->loadFile("barkhigh.wav");
sndman->loadFile("barklow.wav");
sndman->loadFile("howl.wav");
}
virtual void doStop() {
sndman->releaseFile("barkhigh.wav");
sndman->releaseFile("barklow.wav");
sndman->releaseFile("howl.wav");
std::cout << getName() << " is shutting down." << endl;
BehaviorBase::doStop();
}
virtual void doEvent() {
switch ( event->getGeneratorID() ) {
case EventBase::buttonEGID:
std::cout << getName() << " got event: " << event->getDescription() << std::endl;
if (event->getSourceID()==RobotInfo::BackButOffset) {
if (event->getTypeID()==EventBase::activateETID) {
sndman->StopPlay(howl_id);
howl_id = sndman->playFile("howl.wav");
};
}
else
sndman->playFile((event->getMagnitude()==0) ? "barklow.wav" : "barkhigh.wav");
break;
default:
std::cout << "Unexpected event:" << event->getDescription() << std::endl;
};
}
};
REGISTER_BEHAVIOR(DstBehavior);
|
The sound manager will automatically mix the audio if we try to play
several sounds at once. Try pressing one of the paw buttons while the
AIBO is howling, and you'll hear a bark superimposed on the howl. If
you comment out the call to StopPlay() and press the back button while
the AIBO is howling, you'll hear a second howl superimposed on the
first.
|