pcce::OggStream Class Reference

#include <OggStream.h>

List of all members.

Public Member Functions

 OggStream (const std::string fileName, tOpenALSourcePtr source)
 ~OggStream ()
bool IsPlaying ()
void Play ()
void Pause ()
void Stop ()
void Update ()
tOpenALSourcePtr GetSource ()
void SetSource (tOpenALSourcePtr source)

Private Types

enum  tStreamState { ssPlaying, ssPaused, ssStopped }

Private Member Functions

unsigned int QueuedBufferCount ()
void EmptyQueuedBuffers ()
unsigned int ProcessedBuffers ()
bool FillBuffer (ALuint &buffer)
void QueueBuffer (const ALuint &buffer)

Private Attributes

tStreamState mState
tOpenALSourcePtr mSource
OggFile mFile
ALenum mFormat
ALsizei mFreq
ALuint mBuffers [sBufferCount]
unsigned int mQueuedBuffers
unsigned int mProcessedBuffers
bool mStreamActive

Static Private Attributes

static const size_t sBufferCount = 2


Detailed Description

Definition at line 43 of file OggStream.h.


Member Enumeration Documentation

Enumerator:
ssPlaying 
ssPaused 
ssStopped 

Definition at line 61 of file OggStream.h.


Constructor & Destructor Documentation

OggStream::OggStream ( const std::string  fileName,
tOpenALSourcePtr  source 
)

Definition at line 37 of file OggStream.cpp.

References pcce::ClearALError(), pcce::OggFile::GetChannelCount(), pcce::OggFile::GetFrequency(), mBuffers, mFile, mFormat, mFreq, pcce::OggFile::Open(), PCCE_AL_CHECK, PCCE_THROW, and sBufferCount.

00037                                                                      :
00038   mState(ssStopped),
00039   mSource(source),
00040   mFile(fileName),
00041   mQueuedBuffers(0),
00042   mProcessedBuffers(0),
00043   mStreamActive(false)
00044 {
00045   mFile.Open();
00046   switch (mFile.GetChannelCount()) {
00047     case 1:
00048       mFormat = AL_FORMAT_MONO16;
00049       break;
00050     case 2:
00051       mFormat = AL_FORMAT_STEREO16;
00052       break;
00053     default:
00054       PCCE_THROW("Ogg file must have 1 or 2 channels");
00055   }
00056   mFreq = mFile.GetFrequency();
00057 
00058   ClearALError();
00059   alGenBuffers(sBufferCount, mBuffers);
00060   PCCE_AL_CHECK();
00061 }

OggStream::~OggStream (  ) 

Definition at line 63 of file OggStream.cpp.

References pcce::ClearALError(), mBuffers, mSource, sBufferCount, and Stop().

00063                       {
00064   try {
00065     if (mSource) {
00066       Stop();
00067     }
00068   } catch (Exception&) {
00069   }
00070   
00071   ClearALError();
00072   alDeleteBuffers(sBufferCount, mBuffers);
00073   ClearALError();
00074 }


Member Function Documentation

bool OggStream::IsPlaying (  ) 

Definition at line 76 of file OggStream.cpp.

References mProcessedBuffers, and mQueuedBuffers.

00076                           {
00077   return mProcessedBuffers == mQueuedBuffers;
00078 }

void OggStream::Play (  ) 

Definition at line 80 of file OggStream.cpp.

References pcce::ClearALError(), FillBuffer(), mBuffers, mSource, mState, mStreamActive, PCCE_AL_CHECK, PCCE_CHECK, QueueBuffer(), sBufferCount, ssPlaying, and Update().

00080                      {
00081   PCCE_CHECK(mSource, "Cannot play OggStream without source");
00082   
00083   if (mState != ssPlaying) {
00084     if (!mStreamActive) {
00085       for (size_t i = 0; i < sBufferCount; ++i) {
00086         mStreamActive = FillBuffer(mBuffers[i]);
00087         if (!mStreamActive) {
00088           break;
00089         }
00090         QueueBuffer(mBuffers[i]);
00091       }
00092     }
00093 
00094     mState = ssPlaying;
00095   }
00096   
00097   Update();
00098   
00099   ClearALError();
00100   alSourcePlay(mSource->GetSource());
00101   PCCE_AL_CHECK();
00102 }

void OggStream::Pause (  ) 

Definition at line 104 of file OggStream.cpp.

References pcce::ClearALError(), mSource, mState, PCCE_AL_CHECK, PCCE_CHECK, ssPaused, and ssStopped.

00104                       {
00105   PCCE_CHECK(mSource, "Cannot pause OggStream without source");
00106   
00107   PCCE_CHECK(mState != ssStopped, "Cannot pause stopped OggStream");
00108   
00109   mState = ssPaused;
00110 
00111   ClearALError();
00112   alSourcePause(mSource->GetSource());
00113   PCCE_AL_CHECK();
00114 }

void OggStream::Stop (  ) 

Definition at line 116 of file OggStream.cpp.

References pcce::ClearALError(), EmptyQueuedBuffers(), mFile, mProcessedBuffers, mQueuedBuffers, mSource, mState, mStreamActive, PCCE_AL_CHECK, PCCE_CHECK, pcce::OggFile::Rewind(), and ssStopped.

Referenced by SetSource(), and ~OggStream().

00116                      {
00117   PCCE_CHECK(mSource, "Cannot stop OggStream without source");
00118   
00119   if (mState != ssStopped) {
00120     mState = ssStopped;
00121 
00122     ClearALError();
00123     alSourceStop(mSource->GetSource());
00124     PCCE_AL_CHECK();
00125 
00126     EmptyQueuedBuffers();
00127 
00128     mFile.Rewind();
00129     mQueuedBuffers = 0;
00130     mProcessedBuffers = 0;
00131 
00132     mStreamActive = false;
00133   }
00134 }

void OggStream::Update (  ) 

Definition at line 136 of file OggStream.cpp.

References pcce::ClearALError(), FillBuffer(), mProcessedBuffers, mSource, mStreamActive, PCCE_AL_CHECK, PCCE_CHECK, ProcessedBuffers(), QueueBuffer(), and sBufferCount.

Referenced by Play().

00136                        {
00137   PCCE_CHECK(mSource, "Cannot update OggStream without source");
00138   
00139   unsigned int processed;
00140   ALuint buffers[sBufferCount];
00141 
00142   processed = ProcessedBuffers();
00143   if (processed == 0) {
00144     return;
00145   }
00146   
00147   PCCE_CHECK(processed <= sBufferCount, "OggStream::Update: More buffers processed than allocated");
00148   
00149   mProcessedBuffers += processed;
00150   
00151   ClearALError();
00152   alSourceUnqueueBuffers(mSource->GetSource(), processed, buffers);
00153   PCCE_AL_CHECK();
00154   
00155   if (!mStreamActive) {
00156     return;
00157   }
00158   
00159   for (size_t i = 0; i < processed; ++i) {
00160     mStreamActive = FillBuffer(buffers[i]);
00161     if (!mStreamActive) {
00162       break;
00163     }
00164     QueueBuffer(buffers[i]);
00165   }
00166 }

tOpenALSourcePtr OggStream::GetSource (  ) 

Definition at line 168 of file OggStream.cpp.

References mSource.

00168                                       {
00169   return mSource;
00170 }

void OggStream::SetSource ( tOpenALSourcePtr  source  ) 

Definition at line 172 of file OggStream.cpp.

References mSource, and Stop().

00172                                                  {
00173   if (mSource) {
00174     Stop();
00175   }
00176   mSource = source;
00177   // Assign it to the right buffers!
00178 }

unsigned int OggStream::QueuedBufferCount (  )  [private]

Definition at line 180 of file OggStream.cpp.

References pcce::ClearALError(), mSource, PCCE_AL_CHECK, and PCCE_CHECK.

Referenced by EmptyQueuedBuffers().

00180                                           {
00181   PCCE_CHECK(mSource, "OggStream::QueuedBufferCount: mSource is not set");
00182   
00183   ALint queued;
00184   ClearALError();
00185   alGetSourcei(mSource->GetSource(), AL_BUFFERS_QUEUED, &queued);
00186   PCCE_AL_CHECK();
00187   
00188   if (queued < 0) {
00189     queued = 0;
00190   }
00191   return queued;
00192 }

void OggStream::EmptyQueuedBuffers (  )  [private]

Definition at line 194 of file OggStream.cpp.

References pcce::ClearALError(), pcce::GetALErrorStr(), mSource, PCCE_CHECK, PCCE_THROW, QueuedBufferCount(), and sBufferCount.

Referenced by Stop().

00194                                    {
00195   unsigned int queued;
00196   ALuint buffers[sBufferCount];
00197   
00198   PCCE_CHECK(mSource, "OggStream: mSource is not valid");
00199 
00200   queued = QueuedBufferCount();
00201   if (queued == 0) {
00202     return;
00203   }
00204   
00205   PCCE_CHECK(queued <= sBufferCount, "OggStream::EmptyQueuedBuffers: More buffers queued than allocated");
00206   
00207   if (!alIsSource(mSource->GetSource())) {
00208     PCCE_THROW("Not source");
00209   }
00210   ClearALError();
00211   alSourceUnqueueBuffers(mSource->GetSource(), queued, buffers);
00212   //PCCE_AL_CHECK();
00213   ALenum error = alGetError();
00214   if (AL_NO_ERROR != error) {
00215     PCCE_THROW(GetALErrorStr(error));
00216   }
00217   
00218   PCCE_CHECK(QueuedBufferCount() == 0, "didnt unqueue");
00219 }

unsigned int OggStream::ProcessedBuffers (  )  [private]

Definition at line 221 of file OggStream.cpp.

References pcce::ClearALError(), mSource, PCCE_AL_CHECK, and PCCE_CHECK.

Referenced by Update().

00221                                          {
00222   PCCE_CHECK(mSource, "OggStream::ProcessedBuffers: mSource is not set");
00223   
00224   ALint processed;
00225   
00226   ClearALError();
00227   alGetSourcei(mSource->GetSource(), AL_BUFFERS_PROCESSED, &processed);
00228   PCCE_AL_CHECK();
00229   
00230   if (processed < 0) {
00231     processed = 0;
00232   }
00233   return processed;
00234 }

bool OggStream::FillBuffer ( ALuint &  buffer  )  [private]

Definition at line 236 of file OggStream.cpp.

References BUFFER_SIZE, pcce::ClearALError(), mFile, mFormat, mFreq, PCCE_AL_CHECK, and pcce::OggFile::Read().

Referenced by Play(), and Update().

00236                                          {
00237   const size_t BUFFER_SIZE = 65536;
00238   size_t readCount;
00239   tCharVector data;
00240   
00241   readCount = mFile.Read(BUFFER_SIZE, data);
00242   // If readCount is 0, we hit the end of the file
00243   if (readCount == 0) {
00244     return false;
00245   }
00246   
00247   ClearALError();
00248   alBufferData(buffer, mFormat, &data[0], data.size(), mFreq);
00249   PCCE_AL_CHECK();
00250   
00251   return true;
00252 }

void OggStream::QueueBuffer ( const ALuint &  buffer  )  [private]

Definition at line 254 of file OggStream.cpp.

References pcce::ClearALError(), mQueuedBuffers, mSource, PCCE_AL_CHECK, and PCCE_CHECK.

Referenced by Play(), and Update().

00254                                                 {
00255   PCCE_CHECK(mSource, "Cannot queue buffer without a source");
00256   
00257   ClearALError();
00258   alSourceQueueBuffers(mSource->GetSource(), 1, &buffer);
00259   PCCE_AL_CHECK();
00260   
00261   ++mQueuedBuffers;
00262 }


Member Data Documentation

const size_t pcce::OggStream::sBufferCount = 2 [static, private]

Definition at line 60 of file OggStream.h.

Referenced by EmptyQueuedBuffers(), OggStream(), Play(), Update(), and ~OggStream().

Definition at line 63 of file OggStream.h.

Referenced by Pause(), Play(), and Stop().

Definition at line 65 of file OggStream.h.

Referenced by FillBuffer(), OggStream(), and Stop().

ALenum pcce::OggStream::mFormat [private]

Definition at line 66 of file OggStream.h.

Referenced by FillBuffer(), and OggStream().

ALsizei pcce::OggStream::mFreq [private]

Definition at line 67 of file OggStream.h.

Referenced by FillBuffer(), and OggStream().

Definition at line 68 of file OggStream.h.

Referenced by OggStream(), Play(), and ~OggStream().

unsigned int pcce::OggStream::mQueuedBuffers [private]

Definition at line 69 of file OggStream.h.

Referenced by IsPlaying(), QueueBuffer(), and Stop().

unsigned int pcce::OggStream::mProcessedBuffers [private]

Definition at line 70 of file OggStream.h.

Referenced by IsPlaying(), Stop(), and Update().

Definition at line 71 of file OggStream.h.

Referenced by Play(), Stop(), and Update().


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