Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Audio::Stream Class Referenceabstract

#include <Stream.h>

Classes

class  NoBufferException
 

Public Member Functions

virtual ~Stream ()
 
const std::string & getPath () const throw ()
 
const FormatgetFormat () const throw ()
 
double getLength () throw (Exception)
 
unsigned int read (void *buffer, unsigned int bufferSize) throw (Exception)
 
double getPosition () const throw ()
 
void seek (double position) throw (Exception)
 

Protected Member Functions

 Stream (const std::string &path) throw (Exception)
 
FormatgetFormatInternal () throw ()
 
virtual double getLengthImpl () const =0 throw (Exception)
 
virtual double getPositionImpl () const =0 throw ()
 
virtual void seekImpl (double position)=0 throw (Exception)
 
virtual void getBufferImpl (void *&buffer, unsigned int &bufferSize)=0 throw (Exception)
 
virtual void nextBufferImpl ()=0 throw (Exception)
 

Detailed Description

Stream abstract class

Remarks
This is the interface to all kinds of streams.
This abstract class has but a few
See Also
Codec to create Stream instances.

Definition at line 21 of file Stream.h.

Constructor & Destructor Documentation

Audio::Stream::Stream ( const std::string &  path)
throw (Exception
)
protected

Internal constructor used by derived classes

Definition at line 17 of file Stream.cpp.

18  {
19  }
Audio::Stream::~Stream ( )
virtual

Definition at line 21 of file Stream.cpp.

22  {
23  }

Member Function Documentation

virtual void Audio::Stream::getBufferImpl ( void *&  buffer,
unsigned int bufferSize 
)
throw (Exception
)
protectedpure virtual

Get the stream's current reading buffer.

Remarks
This buffer is to remain valid until any stream advancement is made through seekImpl or nextBufferImpl, or the object gets destroyed. Repeated getBufferImpl calls will not modify the stream, though internal state may be modified in transparent ways (allowing for lazy decoding). In short, getBufferImpl is an idempotent member function.
Asking the buffer where nextBufferImpl() has never been called would raise
a NoBuffer exception.
const Format& Audio::Stream::getFormat ( ) const
throw (
)
inline

Return the format of the stream.

Definition at line 59 of file Stream.h.

59 { return streamFormat; }
Format& Audio::Stream::getFormatInternal ( )
throw (
)
inlineprotected

Internal write access to stream format, for derived classes

Definition at line 50 of file Stream.h.

50 { return streamFormat; }
double Audio::Stream::getLength ( )
throw (Exception
)

Return the length of the stream.

Remarks
This may not be an inocuous function. Some codecs can't return the length of a file without parsing it entirely, and as such it cannot be const. So use sparingly.

Definition at line 25 of file Stream.cpp.

References getLengthImpl().

26  {
27  return getLengthImpl();
28  }
virtual double Audio::Stream::getLengthImpl ( ) const
throw (Exception
)
protectedpure virtual
See Also
getLength

Referenced by getLength().

const std::string& Audio::Stream::getPath ( ) const
throw (
)
inline

Return the path of the associated file.

Definition at line 56 of file Stream.h.

56 { return filePath; };
double Audio::Stream::getPosition ( ) const
throw (
)

Get the stream's current position, in seconds

Definition at line 30 of file Stream.cpp.

References getPositionImpl().

31  {
32  return getPositionImpl();
33  }
virtual double Audio::Stream::getPositionImpl ( ) const
throw (
)
protectedpure virtual
See Also
getPosition

Referenced by getPosition().

virtual void Audio::Stream::nextBufferImpl ( )
throw (Exception
)
protectedpure virtual

Advance the stream by reading a new buffer.

Remarks
The data read by this member is accessible through getBufferImpl().
After a successful call to this member, getBufferImpl will never raise
a NoBuffer exception.
See Also
getBufferImpl
unsigned int Audio::Stream::read ( void *  buffer,
unsigned int  bufferSize 
)
throw (Exception
)

Fill the specified buffer with data from the stream, and advance.

Remarks
If not enough data is available, but some is, instead of throwing an EndOfStream exception the function will return the amount of data actually fetched, in bytes. If no bytes at all are available, though, and EndOfStream exception will be raised.
Parameters
buffera pointer to a memory buffer where to extract data.
bufferSizethe size of the memory area pointed to by buffer, and the maximum amount of data to be extracted from the stream, if available.

Definition at line 40 of file Stream.cpp.

References buffer, VsnetOSS::memcpy(), and min().

41  {
42  void *rbuffer;
43  void *rbufferEnd;
44  unsigned int rbufferSize;
45  unsigned int rode = 0;
46 
47  try {
48  getBufferImpl(rbuffer, rbufferSize);
49  } catch (NoBufferException) {
51  getBufferImpl(rbuffer, rbufferSize);
52  curBufferPos = rbuffer;
53  }
54  rbufferEnd = ((char*)rbuffer) + rbufferSize;
55 
56  while (bufferSize > 0) {
57  if (!((curBufferPos >= rbuffer) && (curBufferPos < rbufferEnd))) {
59  getBufferImpl(rbuffer, rbufferSize);
60  curBufferPos = rbuffer;
61  rbufferEnd = ((char*)rbuffer) + rbufferSize;
62  }
63 
64  size_t remaining = min( bufferSize, (unsigned int)((char*)rbufferEnd - (char*)curBufferPos) ); //is there no std::ptrdiff?
65  memcpy(buffer, curBufferPos, remaining);
66  buffer = (void*)((char*)buffer + remaining);
67  curBufferPos = (void*)((char*)curBufferPos + remaining);
68  bufferSize -= remaining;
69  rode += remaining;
70  }
71 
72  return rode;
73  }
void Audio::Stream::seek ( double  position)
throw (Exception
)

Set the stream's position, in seconds

Remarks
The final position in the stream after this operation is not guaranteed to be as requested, but an approximation given codec limitations. The only exception being seek(0), which is guaranteed to seek to the beginning of the stream.

Definition at line 35 of file Stream.cpp.

36  {
37  seekImpl(position);
38  }
virtual void Audio::Stream::seekImpl ( double  position)
throw (Exception
)
protectedpure virtual
See Also
seek

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