pcce::SoundSubSystem Class Reference

#include <SoundSubSystem.h>

Inheritance diagram for pcce::SoundSubSystem:

pcce::IThreadedSubSystem pcce::ISubSystem

List of all members.

Public Member Functions

virtual const std::string vGetSubSystemName ()
void HandleBufferEvent (const tIEventPtr &e)
void HandleStreamEvent (const tIEventPtr &e)

Protected Member Functions

void vInitializeThread (tPropertyBagPtr configSection)
void vRunThread ()
void vShutdownThread ()

Private Types

typedef std::set
< tOpenALSourcePtr
tOpenALSourcePtrSet
typedef std::deque
< tOpenALSourcePtr
tOpenALSourcePtrDeque
typedef std::list< BufferHandletBufferHandleList
typedef std::map< tSoundID,
tOpenALSourcePtr
tSoundIDSourceMap
typedef std::map< tSoundID,
tOggStreamPtr
tStreamMap

Private Member Functions

tOpenALSourcePtr GetBufferSource (const tSoundID id, bool &newSource)
bool HasSource (const tSoundID id, tOpenALSourcePtr &source)
tOpenALSourcePtr AcquireBufferSource (const tSoundID id)
void ReleaseSource (const tSoundID id)
tOpenALSourcePtr DoAcquireSource ()
bool IsStreamActive (const tSoundID id, tOggStreamPtr &stream)
void ReleaseStream (const tSoundID id)
void UpdateStreams ()
void SanityCheck ()

Private Attributes

tOpenALSourcePtrSet mSources
tOpenALSourcePtrDeque mAvailSources
tBufferHandleList mPlayingBuffers
tSoundIDSourceMap mBufferSourceMap
tStreamMap mPlayingStreams

Classes

class  BufferHandle


Detailed Description

Definition at line 47 of file SoundSubSystem.h.


Member Typedef Documentation

Definition at line 74 of file SoundSubSystem.h.

Definition at line 75 of file SoundSubSystem.h.

typedef std::list< BufferHandle > pcce::SoundSubSystem::tBufferHandleList [private]

Definition at line 76 of file SoundSubSystem.h.

Definition at line 77 of file SoundSubSystem.h.

Definition at line 78 of file SoundSubSystem.h.


Member Function Documentation

virtual const std::string pcce::SoundSubSystem::vGetSubSystemName (  )  [inline, virtual]

Implements pcce::ISubSystem.

Definition at line 49 of file SoundSubSystem.h.

00049 { return "Sound"; };

void SoundSubSystem::HandleBufferEvent ( const tIEventPtr e  ) 

Definition at line 115 of file SoundSubSystem.cpp.

References DoGetBuffer(), GetBufferSource(), HasSource(), mPlayingBuffers, PCCE_AL_CHECK, PCCE_THROW, ReleaseSource(), SanityCheck(), pcce::saPause, pcce::saPlay, pcce::saReleaseSource, and pcce::saStop.

Referenced by vInitializeThread().

00115                                                           {
00116   boost::shared_ptr< SoundBufferEvent > event = boost::dynamic_pointer_cast< SoundBufferEvent >(e);
00117 
00118   SanityCheck();
00119   
00120   tOpenALSourcePtr source;
00121   tSoundID id = event->GetID();
00122   
00123   switch (event->GetAction()) {
00124     case saPlay: {
00125       bool newSource;
00126       source = GetBufferSource(id, newSource);
00127       BufferHandle newHandle(id, source);
00128       if (newSource) {
00129         alSourcei(source->GetSource(), AL_BUFFER, DoGetBuffer(event->GetResource()));
00130         PCCE_AL_CHECK();
00131       } else {
00132         mPlayingBuffers.remove(newHandle);
00133       }
00134       mPlayingBuffers.push_back(newHandle);
00135       alSourcePlay(source->GetSource());
00136       PCCE_AL_CHECK();
00137       break;
00138     }
00139     case saPause:
00140       if (HasSource(id, source)) {
00141         alSourcePause(source->GetSource());
00142         PCCE_AL_CHECK();
00143       }
00144       break;
00145     case saStop:
00146       if (HasSource(id, source)) {
00147         alSourceStop(source->GetSource());
00148         PCCE_AL_CHECK();
00149       }
00150       break;
00151     case saReleaseSource:
00152       ReleaseSource(id);
00153       break;
00154     default:
00155       PCCE_THROW("Unrecognized sound action");
00156   }
00157 
00158   SanityCheck();
00159 }

void SoundSubSystem::HandleStreamEvent ( const tIEventPtr e  ) 

Definition at line 161 of file SoundSubSystem.cpp.

References DoAcquireSource(), IsStreamActive(), mPlayingStreams, PCCE_THROW, ReleaseStream(), SanityCheck(), pcce::saPause, pcce::saPlay, pcce::saReleaseSource, and pcce::saStop.

Referenced by vInitializeThread().

00161                                                           {
00162   boost::shared_ptr< SoundStreamEvent > event = boost::dynamic_pointer_cast< SoundStreamEvent >(e);
00163   
00164   SanityCheck();
00165   
00166   tOggStreamPtr stream;
00167   tSoundID id = event->GetID();
00168   
00169   switch (event->GetAction()) {
00170     case saPlay:
00171       if (!IsStreamActive(id, stream)) {
00172         tOpenALSourcePtr source = DoAcquireSource();
00173         stream.reset(new OggStream(event->GetFileName(), source));
00174         mPlayingStreams[id] = stream;
00175       }
00176       stream->Play();
00177       break;
00178     case saPause:
00179       if (IsStreamActive(id, stream)) {
00180         stream->Pause();
00181       }
00182       break;
00183     case saStop:
00184       if (IsStreamActive(id, stream)) {
00185         stream->Stop();
00186       }
00187       break;
00188     case saReleaseSource:
00189       ReleaseStream(id);
00190       break;
00191     default:
00192       PCCE_THROW("Unrecognized sound action");
00193   }
00194   
00195   SanityCheck();
00196 }

void SoundSubSystem::vInitializeThread ( tPropertyBagPtr  configSection  )  [protected, virtual]

Implements pcce::IThreadedSubSystem.

Definition at line 54 of file SoundSubSystem.cpp.

References pcce::IThreadedSubSystem::AddThreadHandler(), pcce::ClearALError(), pcce::Format(), HandleBufferEvent(), HandleStreamEvent(), pcce::InitializeAL(), mAvailSources, mSources, PCCE_AL_CHECK, PCCE_THROW, SanityCheck(), and pcce::EventType< T >::sGetEventType().

00054                                                                     {
00055   // Config setting name
00056   const string SoundSourceCount = "sources";
00057   // This seems like an ok default...
00058   unsigned int sourceCount = 16;
00059   // Just a sanity check, so we don't end up trying to create a billion sources or something
00060   const unsigned int MaxSourceCount = 256;
00061 
00062   ClearALError();
00063   InitializeAL();
00064   PCCE_AL_CHECK();
00065 
00066   if (configSection) {
00067     sourceCount = configSection->GetValueDef(SoundSourceCount, sourceCount).AsUnsignedInt();
00068     if ((sourceCount == 0) || (sourceCount > MaxSourceCount)) {
00069       tVariantList v;
00070       v.push_back(SoundSourceCount);
00071       v.push_back(MaxSourceCount);
00072       PCCE_THROW(Format("\"%\" config setting must be between 1 and %", v));
00073     }
00074   }
00075 
00076   for (unsigned int i = 0; i < sourceCount; ++i) {
00077     tOpenALSourcePtr s(new OpenALSource());
00078     mSources.insert(s);
00079     mAvailSources.push_back(s);
00080   }
00081 
00082   AddThreadHandler(SoundBufferEvent::sGetEventType(), bind(&SoundSubSystem::HandleBufferEvent, this, _1));
00083   AddThreadHandler(SoundStreamEvent::sGetEventType(), bind(&SoundSubSystem::HandleStreamEvent, this, _1));
00084   
00085   SanityCheck();
00086 }

void SoundSubSystem::vRunThread (  )  [protected, virtual]

void SoundSubSystem::vShutdownThread (  )  [protected, virtual]

tOpenALSourcePtr SoundSubSystem::GetBufferSource ( const tSoundID  id,
bool &  newSource 
) [private]

Definition at line 198 of file SoundSubSystem.cpp.

References AcquireBufferSource(), and HasSource().

Referenced by HandleBufferEvent().

00198                                                                                    {
00199   tOpenALSourcePtr result;
00200   
00201   newSource = !HasSource(id, result);
00202   if (newSource) {
00203     result = AcquireBufferSource(id);
00204   }
00205   
00206   return result;
00207 }

bool SoundSubSystem::HasSource ( const tSoundID  id,
tOpenALSourcePtr source 
) [private]

Definition at line 209 of file SoundSubSystem.cpp.

References mBufferSourceMap.

Referenced by GetBufferSource(), HandleBufferEvent(), and ReleaseSource().

00209                                                                           {
00210   bool result;
00211   tSoundIDSourceMap::const_iterator i = mBufferSourceMap.find(id);
00212   
00213   result = (i != mBufferSourceMap.end()); 
00214   if (result) {
00215     source = i->second;
00216   }
00217   
00218   return result;
00219 }

tOpenALSourcePtr SoundSubSystem::AcquireBufferSource ( const tSoundID  id  )  [private]

Definition at line 221 of file SoundSubSystem.cpp.

References DoAcquireSource(), and mBufferSourceMap.

Referenced by GetBufferSource().

00221                                                                       {
00222   tOpenALSourcePtr result = DoAcquireSource();
00223 
00224   mBufferSourceMap[id] = result;
00225   
00226   return result;
00227 }

void SoundSubSystem::ReleaseSource ( const tSoundID  id  )  [private]

Definition at line 254 of file SoundSubSystem.cpp.

References HasSource(), mAvailSources, mBufferSourceMap, mPlayingBuffers, and PCCE_AL_CHECK.

Referenced by HandleBufferEvent().

00254                                                     {
00255   tOpenALSourcePtr source;
00256   if (HasSource(id, source)) {
00257     BufferHandle h(id, source);
00258     mPlayingBuffers.remove(h);
00259     mBufferSourceMap.erase(id);
00260 
00261     alSourceStop(source->GetSource());
00262     PCCE_AL_CHECK();
00263     alSourcei(source->GetSource(), AL_BUFFER, AL_NONE);
00264     PCCE_AL_CHECK();
00265     
00266     mAvailSources.push_back(source);
00267   }
00268 }

tOpenALSourcePtr SoundSubSystem::DoAcquireSource (  )  [private]

Definition at line 229 of file SoundSubSystem.cpp.

References mAvailSources, mBufferSourceMap, mPlayingBuffers, PCCE_AL_CHECK, and PCCE_CHECK.

Referenced by AcquireBufferSource(), and HandleStreamEvent().

00229                                                  {
00230   tOpenALSourcePtr result;
00231   
00232   if (!mAvailSources.empty()) {
00233     result = mAvailSources.front();
00234     mAvailSources.pop_front();
00235   } else {
00236     PCCE_CHECK(!mPlayingBuffers.empty(), "Out of sources (too many streams active?)");
00237     
00238     BufferHandle handle = mPlayingBuffers.front();
00239     mPlayingBuffers.pop_front();
00240     mBufferSourceMap.erase(handle.mID);
00241 
00242     result = handle.mSource;
00243     alSourceStop(result->GetSource());
00244     PCCE_AL_CHECK();
00245     // This probably isn't necessary since we are about to assign this source
00246     //   to another buffer, but I'll do it to be safe
00247     alSourcei(result->GetSource(), AL_BUFFER, AL_NONE);
00248     PCCE_AL_CHECK();
00249   }
00250   
00251   return result;
00252 }

bool SoundSubSystem::IsStreamActive ( const tSoundID  id,
tOggStreamPtr stream 
) [private]

Definition at line 270 of file SoundSubSystem.cpp.

References mPlayingStreams.

Referenced by HandleStreamEvent().

00270                                                                             {
00271   tStreamMap::const_iterator i = mPlayingStreams.find(id);
00272   bool result = (i != mPlayingStreams.end());
00273   if (result) {
00274     stream = i->second;
00275   }
00276   return result;
00277 }

void SoundSubSystem::ReleaseStream ( const tSoundID  id  )  [private]

Definition at line 279 of file SoundSubSystem.cpp.

References mAvailSources, mPlayingStreams, and PCCE_AL_CHECK.

Referenced by HandleStreamEvent().

00279                                                     {
00280   tStreamMap::iterator i = mPlayingStreams.find(id);
00281   if (i != mPlayingStreams.end()) {
00282     tOggStreamPtr stream = i->second;
00283     tOpenALSourcePtr source = stream->GetSource();
00284     
00285     mPlayingStreams.erase(i);
00286     
00287     stream->Stop();
00288     stream.reset();
00289 
00290     alSourceStop(source->GetSource());
00291     PCCE_AL_CHECK();
00292     alSourcei(source->GetSource(), AL_BUFFER, AL_NONE);
00293     PCCE_AL_CHECK();
00294     
00295     mAvailSources.push_back(source);
00296   }
00297 }

void SoundSubSystem::UpdateStreams (  )  [private]

Definition at line 299 of file SoundSubSystem.cpp.

References mPlayingStreams.

Referenced by vRunThread().

00299                                    {
00300   for (tStreamMap::const_iterator i = mPlayingStreams.begin(); i != mPlayingStreams.end(); ++i) {
00301     (i->second)->Update();
00302   }
00303 }

void SoundSubSystem::SanityCheck (  )  [private]

Definition at line 305 of file SoundSubSystem.cpp.

References pcce::Format(), mAvailSources, mBufferSourceMap, mPlayingBuffers, mPlayingStreams, mSources, and PCCE_THROW.

Referenced by HandleBufferEvent(), HandleStreamEvent(), vInitializeThread(), and vShutdownThread().

00305                                  {
00306   if (mAvailSources.size() + mPlayingBuffers.size() + mPlayingStreams.size() != mSources.size()) {
00307     tVariantList list;
00308     list.push_back(mAvailSources.size());
00309     list.push_back(mPlayingBuffers.size());
00310     list.push_back(mPlayingStreams.size());
00311     list.push_back(mSources.size());
00312     PCCE_THROW(Format("Avail (%) + Playing (%) + Streaming (%) != Total (%)", list));
00313   }
00314   if (mPlayingBuffers.size() != mBufferSourceMap.size()) {
00315     tVariantList list;
00316     list.push_back(mPlayingBuffers.size());
00317     list.push_back(mBufferSourceMap.size());
00318     PCCE_THROW(Format("Playing count (%) != map count (%)", list));
00319   }
00320 }


Member Data Documentation

Definition at line 80 of file SoundSubSystem.h.

Referenced by SanityCheck(), vInitializeThread(), and vShutdownThread().

Definition at line 82 of file SoundSubSystem.h.

Referenced by DoAcquireSource(), HandleBufferEvent(), ReleaseSource(), and SanityCheck().


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

Generated on Thu Mar 6 11:39:28 2008 for Protocce by  doxygen 1.5.5