Homepage Demos Overview Downloads Tutorials Reference
Credits

SoundManager Class Reference

#include <SoundManager.h>

List of all members.


Detailed Description

Provides sound effects and caching services, as well as mixing buffers for the SoundPlay process.

Provides easy methods for playing back sounds, either from files on the memory stick, or from dynamically generated buffers. You can chain playback commands so that when one sound finishes, another picks up automatically. This might be handy if, say, someone wants to write an MP3 player ;) The sounds would be too large to load into memory all at once, but you could load a block at a time and chain them so it seamlessly moves from one to the other.

You can also preload sounds (LoadFile()) before playing them so there's no delay while loading after you request a sound to be played. Just be sure to release the file (ReleaseFile()) again when you're done with it ;)

All functions will attempt to lock the SoundManager. Remember, this is running in a shared memory region, accessible by the SoundPlay process and both the Main and Motion processes (so MotionCommands can play sounds!)

One could be tempted to draw parallels to the MotionManager, and envision a system with SoundCommands that are handed over and can dynamically compute sound buffers as needed. If you have the time and inclination, the job's all yours... (Midi players, speech synthesizer, ...?)

Todo:
Volume control, variable playback speed, support more wav file formats (latter two are the same thing if you think about it -need to be able to resample on the fly)

Add functions to hand out regions to be filled out to avoid copying into the buffer.

Definition at line 51 of file SoundManager.h.

Public Types

typedef SoundManagerMsg::Snd_ID Snd_ID
 This is used for referring to sound data so you can start playing it or release it.
typedef unsigned short Play_ID
 This is for referring to instances of the play command so you can stop, pause, or monitor progress (later versions will send events upon completion).
enum  MixMode_t { Fast, Quality }
 Used to set the mode for mixing multiple sound channels. More...
enum  QueueMode_t { Enqueue, Pause, Stop, Override }

Public Member Functions

 SoundManager ()
 constructor
void InitAccess (OSubject *subj)
 Needed to send sounds to the SoundPlay process.
Snd_ID LoadFile (std::string const &name)
 loads a wav file (if it matches Config::sound_config settings - can't do resampling yet)
Snd_ID LoadBuffer (const char buf[], unsigned int len)
 loads raw samples from a buffer (assumes matches Config::sound_config settings)
void ReleaseFile (std::string const &name)
 Marks the sound buffer to be released after the last play command completes (or right now if not being played).
void Release (Snd_ID id)
 Marks the sound buffer to be released after the last play command completes (or right now if not being played).
Play_ID PlayFile (std::string const &name)
 play a wav file (if it matches Config::sound_config settings - can't do resampling yet)
Play_ID PlayBuffer (const char buf[], unsigned int len)
 loads raw samples from a buffer (assumes buffer matches Config::sound_config settings)
Play_ID Play (Snd_ID id)
 plays a previously loaded buffer or file
Play_ID ChainFile (Play_ID base, std::string const &next)
 allows automatic queuing of sounds - good for dynamic sound sources!
Play_ID ChainBuffer (Play_ID base, const char buf[], unsigned int len)
 allows automatic queuing of sounds - good for dynamic sound sources!
Play_ID Chain (Play_ID base, Snd_ID next)
 allows automatic queuing of sounds - good for dynamic sound sources!
void StopPlay ()
 Lets you stop playback of all sounds.
void StopPlay (Play_ID id)
 Lets you stop playback of a sound.
void PausePlay (Play_ID id)
 Lets you pause playback.
void ResumePlay (Play_ID id)
 Lets you resume playback.
void SetMode (unsigned int max_channels, MixMode_t mixer_mode, QueueMode_t queuing_mode)
 Lets you control the maximum number of channels (currently playing sounds), method for mixing (applies when max_chan>1), and queuing method (for when overflow channels).
unsigned int GetRemainTime (Play_ID id) const
 Gives the time until the sound finishes, in milliseconds. Subtract 32 to get guarranteed valid time for this ID.
unsigned int CopyTo (OSoundVectorData *data)
 Copies the sound data to the OPENR buffer, ready to be passed to the system, only called by SoundPlay.
void ReceivedMsg (const ONotifyEvent &event)
 updates internal data structures on the SoundPlay side - you shouldn't be calling this
unsigned int GetNumPlaying ()
 returns number of sounds currently playing

Static Public Attributes

const Snd_ID invalid_Snd_ID = (Snd_ID)-1
 for reporting errors
const Snd_ID MAX_SND = 50
 the number of sounds that can be loaded at any given time
const Play_ID invalid_Play_ID = (Play_ID)-1
 for reporting errors
const Play_ID MAX_PLAY = 256
 the number of sounds that can be enqueued at the same time (see MixMode_t)
const unsigned int MAX_NAME_LEN = 64
 maximum length of a path

Protected Types

typedef ListMemBuf< SoundData,
MAX_SND, Snd_ID
sndlist_t
 For convenience.
typedef ListMemBuf< PlayState,
MAX_PLAY, Play_ID
playlist_t
 For convenience.
typedef ListMemBuf< Play_ID,
MAX_PLAY, Play_ID
chanlist_t
 For convenience.

Protected Member Functions

Snd_ID lookupPath (std::string const &name) const
 Looks to see if name matches any of the sounds in sndlist (converts to absolute path if not already).
void selectChannels (std::vector< Play_ID > &mix)
 selects which of the channels are actually to be mixed together, depending on queue_mode
void updateChannels (const std::vector< Play_ID > &mixs, size_t used)
 update the offsets of sounds which weren't mixed (when needed depending on queue_mode)
bool endPlay (Play_ID id)
 called when a buffer end is reached, may reset buffer to next in chain, or just StopPlay()
 SoundManager (const SoundManager &)
 don't call
SoundManager operator= (const SoundManager &)
 don't call

Static Protected Member Functions

RCRegion * initRegion (unsigned int size)
 Sets up a shared region to hold a sound - rounds to nearest page size.

Protected Attributes

sndlist_t sndlist
 Holds a list of all currently loaded sounds.
playlist_t playlist
 Holds a list of all sounds currently enqueued.
chanlist_t chanlist
 Holds a list of all currently playing sounds, ordered newest (front) to oldest(back).
MixMode_t mix_mode
 Current mixing mode, set by SetMode();.
QueueMode_t queue_mode
 Current queuing mode, set by SetMode();.
unsigned int max_chan
 Current maximum number of sounds to mix together.
MutexLock< ProcessID::NumProcesses > lock
 Prevents multiple processes from accessing at the same time.
OSubject * subjs [ProcessID::NumProcesses]
 For automatic transmission of shared regions to SoundPlay.


Member Typedef Documentation

typedef ListMemBuf<Play_ID,MAX_PLAY,Play_ID> SoundManager::chanlist_t [protected]
 

For convenience.

Definition at line 218 of file SoundManager.h.

typedef unsigned short SoundManager::Play_ID
 

This is for referring to instances of the play command so you can stop, pause, or monitor progress (later versions will send events upon completion).

Definition at line 62 of file SoundManager.h.

Referenced by Chain(), ChainBuffer(), ChainFile(), endPlay(), and Play().

typedef ListMemBuf<PlayState,MAX_PLAY,Play_ID> SoundManager::playlist_t [protected]
 

For convenience.

Definition at line 214 of file SoundManager.h.

typedef SoundManagerMsg::Snd_ID SoundManager::Snd_ID
 

This is used for referring to sound data so you can start playing it or release it.

Definition at line 57 of file SoundManager.h.

Referenced by ChainBuffer(), ChainFile(), CopyTo(), LoadFile(), PlayBuffer(), PlayFile(), and updateChannels().

typedef ListMemBuf<SoundData,MAX_SND,Snd_ID> SoundManager::sndlist_t [protected]
 

For convenience.

Definition at line 201 of file SoundManager.h.


Member Enumeration Documentation

enum SoundManager::MixMode_t
 

Used to set the mode for mixing multiple sound channels.

Feel free to add a higher quality mixer if you're an audiophile - I'm pretty new to sound processing

Enumeration values:
Fast  uses bit shifting trick, but can result in reduced volume with more active channels, best if you set max_channels to a power of 2
Quality  uses real division to maintain volume level, although still a rather naive (but relatively fast) algorithm

Definition at line 70 of file SoundManager.h.

enum SoundManager::QueueMode_t
 

Enumeration values:
Enqueue  newer sounds are played when a channel opens up (when old sound finishes)
Pause  newer sounds pause oldest sound, which continues when a channel opens
Stop  newer sounds stop oldest sound
Override  older sounds have play heads advanced, but don't get mixed until a channel opens

Definition at line 75 of file SoundManager.h.


Constructor & Destructor Documentation

SoundManager::SoundManager  ) 
 

constructor

Definition at line 19 of file SoundManager.cc.

SoundManager::SoundManager const SoundManager  )  [protected]
 

don't call


Member Function Documentation

SoundManager::Play_ID SoundManager::Chain Play_ID  base,
Snd_ID  next
 

allows automatic queuing of sounds - good for dynamic sound sources!

if you chain more than once to the same base, the new buffers are appended to the end of the chain - the new buffer doesn't replace the current chain

Returns:
base - just for convenience of multiple calls

Definition at line 236 of file SoundManager.cc.

SoundManager::Play_ID SoundManager::ChainBuffer Play_ID  base,
const char  buf[],
unsigned int  len
 

allows automatic queuing of sounds - good for dynamic sound sources!

if you chain more than once to the same base, the new buffers are appended to the end of the chain - the new buffer doesn't replace the current chain

Returns:
base - just for convenience of multiple calls

Definition at line 216 of file SoundManager.cc.

SoundManager::Play_ID SoundManager::ChainFile Play_ID  base,
std::string const &  next
 

allows automatic queuing of sounds - good for dynamic sound sources!

if you chain more than once to the same base, the new buffers are appended to the end of the chain - the new buffer doesn't replace the current chain

Returns:
base - just for convenience of multiple calls

Definition at line 196 of file SoundManager.cc.

Referenced by SoundTestBehavior::play().

unsigned int SoundManager::CopyTo OSoundVectorData *  data  ) 
 

Copies the sound data to the OPENR buffer, ready to be passed to the system, only called by SoundPlay.

Returns:
the number of active sounds

Definition at line 320 of file SoundManager.cc.

Referenced by SoundPlay::doSendSound().

bool SoundManager::endPlay Play_ID  id  )  [protected]
 

called when a buffer end is reached, may reset buffer to next in chain, or just StopPlay()

Definition at line 633 of file SoundManager.cc.

Referenced by CopyTo(), selectChannels(), and updateChannels().

unsigned int SoundManager::GetNumPlaying  )  [inline]
 

returns number of sounds currently playing

Definition at line 170 of file SoundManager.h.

Referenced by SoundPlay::doSendSound(), and SoundPlay::GotSoundMsg().

unsigned int SoundManager::GetRemainTime Play_ID  id  )  const
 

Gives the time until the sound finishes, in milliseconds. Subtract 32 to get guarranteed valid time for this ID.

You should be passing the beginning of a chain to get proper results...
May be slightly conservative (will report too small a time) because this does not account for delay until SoundPlay picks up the message that a sound has been added.
However, it is slighly optimistic (will report too large a time) because it processes a buffer all at one go, so it could mark the sound as finished (and cause the ID to go invalid) up to RobotInfo::SoundBufferTime (32 ms) before the sound finishes. So subtract SoundBufferTime if you want to be absolutely sure the ID will still valid.

Definition at line 308 of file SoundManager.cc.

Referenced by SoundTestBehavior::play().

void SoundManager::InitAccess OSubject *  subj  ) 
 

Needed to send sounds to the SoundPlay process.

Definition at line 24 of file SoundManager.cc.

Referenced by SoundPlay::DoInit(), and MMCombo::GotSoundManager().

RCRegion * SoundManager::initRegion unsigned int  size  )  [static, protected]
 

Sets up a shared region to hold a sound - rounds to nearest page size.

Definition at line 534 of file SoundManager.cc.

Referenced by LoadBuffer(), Play(), and Release().

SoundManager::Snd_ID SoundManager::LoadBuffer const char  buf[],
unsigned int  len
 

loads raw samples from a buffer (assumes matches Config::sound_config settings)

The sound data will be cached until Release() is called a matching number of times.
This function is useful for dynamic sound sources. A copy will be made.

Definition at line 65 of file SoundManager.cc.

Referenced by ChainBuffer(), LoadFile(), and PlayBuffer().

SoundManager::Snd_ID SoundManager::LoadFile std::string const &  name  ) 
 

loads a wav file (if it matches Config::sound_config settings - can't do resampling yet)

Since the SoundManager does the loading, if the same file is being played more than once, only once copy is stored in memory

Parameters:
name can be either a full path, or a partial path relative to Config::sound_config::root
Returns:
ID number for future references (can also use name) The sound data will be cached until ReleaseFile() or Release() is called a matching number of times

Definition at line 30 of file SoundManager.cc.

Referenced by ChainFile(), SoundPlay::DoInit(), SoundTestBehavior::DoStart(), Controller::DoStart(), CameraBehavior::DoStart(), PlayFile(), and PaceTargetsMachine::setup().

SoundManager::Snd_ID SoundManager::lookupPath std::string const &  name  )  const [protected]
 

Looks to see if name matches any of the sounds in sndlist (converts to absolute path if not already).

Definition at line 543 of file SoundManager.cc.

Referenced by LoadFile(), and ReleaseFile().

SoundManager SoundManager::operator= const SoundManager  )  [protected]
 

don't call

void SoundManager::PausePlay Play_ID  id  ) 
 

Lets you pause playback.

Definition at line 277 of file SoundManager.cc.

Referenced by SoundTestBehavior::play().

SoundManager::Play_ID SoundManager::Play Snd_ID  id  ) 
 

plays a previously loaded buffer or file

Definition at line 162 of file SoundManager.cc.

Referenced by PlayBuffer(), and PlayFile().

SoundManager::Play_ID SoundManager::PlayBuffer const char  buf[],
unsigned int  len
 

loads raw samples from a buffer (assumes buffer matches Config::sound_config settings)

The sound data will be released after done playing

Definition at line 150 of file SoundManager.cc.

SoundManager::Play_ID SoundManager::PlayFile std::string const &  name  ) 
 

play a wav file (if it matches Config::sound_config settings - can't do resampling yet)

Will do a call to LoadFile() first, and then automatically release the sound again when complete.

Parameters:
name can be either a full path, or a partial path relative to Config::sound_config::root
Returns:
ID number for future references The sound data will not be cached after done playing unless a call to LoadFile is made

Definition at line 138 of file SoundManager.cc.

Referenced by ControlBase::doCancel(), ControlBase::doNextItem(), ControlBase::doPrevItem(), ControlBase::doReadStdIn(), WaypointWalkControl::WaypointEditControl::doSelect(), WaypointWalkControl::doSelect(), WalkCalibration::doSelect(), SensorObserverControl::doSelect(), EventLogger::doSelect(), ControlBase::doSelect(), SoundNode::DoStart(), WalkCalibration::err(), Transition::fire(), CameraBehavior::openNextFile(), SoundTestBehavior::play(), WalkCalibration::processEvent(), CameraBehavior::processEvent(), BanditMachine::WaitNode::processEvent(), AutoGetupBehavior::processEvent(), WalkControllerBehavior::runCommand(), RunSequenceControl< SequenceSize >::selectedFile(), PlaySoundControl::selectedFile(), ControlBase::setHilights(), EmergencyStopMC::setStopped(), and WalkCalibration::takeInput().

void SoundManager::ReceivedMsg const ONotifyEvent &  event  ) 
 

updates internal data structures on the SoundPlay side - you shouldn't be calling this

Definition at line 504 of file SoundManager.cc.

Referenced by SoundPlay::GotSoundMsg().

void SoundManager::Release Snd_ID  id  ) 
 

Marks the sound buffer to be released after the last play command completes (or right now if not being played).

Definition at line 109 of file SoundManager.cc.

Referenced by endPlay(), ReleaseFile(), and StopPlay().

void SoundManager::ReleaseFile std::string const &  name  ) 
 

Marks the sound buffer to be released after the last play command completes (or right now if not being played).

Definition at line 103 of file SoundManager.cc.

Referenced by SoundPlay::DoDestroy(), SoundTestBehavior::DoStop(), Controller::DoStop(), CameraBehavior::DoStop(), and PaceTargetsMachine::teardown().

void SoundManager::ResumePlay Play_ID  id  ) 
 

Lets you resume playback.

Definition at line 289 of file SoundManager.cc.

Referenced by SoundTestBehavior::processEvent().

void SoundManager::selectChannels std::vector< Play_ID > &  mix  )  [protected]
 

selects which of the channels are actually to be mixed together, depending on queue_mode

Definition at line 552 of file SoundManager.cc.

Referenced by CopyTo().

void SoundManager::SetMode unsigned int  max_channels,
MixMode_t  mixer_mode,
QueueMode_t  queuing_mode
 

Lets you control the maximum number of channels (currently playing sounds), method for mixing (applies when max_chan>1), and queuing method (for when overflow channels).

Definition at line 300 of file SoundManager.cc.

void SoundManager::StopPlay Play_ID  id  ) 
 

Lets you stop playback of a sound.

Definition at line 257 of file SoundManager.cc.

void SoundManager::StopPlay  ) 
 

Lets you stop playback of all sounds.

Definition at line 251 of file SoundManager.cc.

Referenced by SoundPlay::DoStop(), endPlay(), PlaySoundControl::selectedFile(), and SoundNode::StopPlay().

void SoundManager::updateChannels const std::vector< Play_ID > &  mixs,
size_t  used
[protected]
 

update the offsets of sounds which weren't mixed (when needed depending on queue_mode)

Definition at line 597 of file SoundManager.cc.

Referenced by CopyTo().


Member Data Documentation

chanlist_t SoundManager::chanlist [protected]
 

Holds a list of all currently playing sounds, ordered newest (front) to oldest(back).

Definition at line 220 of file SoundManager.h.

Referenced by CopyTo(), PausePlay(), Play(), ResumePlay(), selectChannels(), StopPlay(), and updateChannels().

const Play_ID SoundManager::invalid_Play_ID = (Play_ID)-1 [static]
 

for reporting errors

Definition at line 63 of file SoundManager.h.

Referenced by Chain(), ChainBuffer(), and SoundManager::PlayState::PlayState().

const Snd_ID SoundManager::invalid_Snd_ID = (Snd_ID)-1 [static]
 

for reporting errors

Definition at line 58 of file SoundManager.h.

Referenced by SoundManager::PlayState::PlayState().

MutexLock<ProcessID::NumProcesses> SoundManager::lock [mutable, protected]
 

Prevents multiple processes from accessing at the same time.

Definition at line 232 of file SoundManager.h.

Referenced by CopyTo(), GetRemainTime(), LoadBuffer(), LoadFile(), PausePlay(), Play(), PlayBuffer(), PlayFile(), Release(), ReleaseFile(), ResumePlay(), SetMode(), and StopPlay().

unsigned int SoundManager::max_chan [protected]
 

Current maximum number of sounds to mix together.

Definition at line 229 of file SoundManager.h.

Referenced by SetMode().

const unsigned int SoundManager::MAX_NAME_LEN = 64 [static]
 

maximum length of a path

Definition at line 66 of file SoundManager.h.

const Play_ID SoundManager::MAX_PLAY = 256 [static]
 

the number of sounds that can be enqueued at the same time (see MixMode_t)

Definition at line 64 of file SoundManager.h.

const Snd_ID SoundManager::MAX_SND = 50 [static]
 

the number of sounds that can be loaded at any given time

Definition at line 59 of file SoundManager.h.

MixMode_t SoundManager::mix_mode [protected]
 

Current mixing mode, set by SetMode();.

Definition at line 223 of file SoundManager.h.

Referenced by CopyTo(), and SetMode().

playlist_t SoundManager::playlist [protected]
 

Holds a list of all sounds currently enqueued.

Definition at line 216 of file SoundManager.h.

Referenced by Chain(), ChainBuffer(), ChainFile(), CopyTo(), endPlay(), GetRemainTime(), Play(), PlayBuffer(), PlayFile(), ReceivedMsg(), selectChannels(), StopPlay(), and updateChannels().

QueueMode_t SoundManager::queue_mode [protected]
 

Current queuing mode, set by SetMode();.

Definition at line 226 of file SoundManager.h.

Referenced by SetMode().

sndlist_t SoundManager::sndlist [protected]
 

Holds a list of all currently loaded sounds.

Definition at line 203 of file SoundManager.h.

Referenced by CopyTo(), GetRemainTime(), LoadBuffer(), LoadFile(), lookupPath(), Play(), PlayBuffer(), PlayFile(), ReceivedMsg(), Release(), selectChannels(), and updateChannels().

OSubject* SoundManager::subjs[ProcessID::NumProcesses] [protected]
 

For automatic transmission of shared regions to SoundPlay.

Definition at line 235 of file SoundManager.h.

Referenced by InitAccess(), LoadBuffer(), Play(), and Release().


The documentation for this class was generated from the following files:

Tekkotsu v2.2
Generated Tue Oct 19 14:20:23 2004 by Doxygen 1.3.9.1