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::SoundBuffer Class Reference

#include <SoundBuffer.h>

Public Member Functions

 SoundBuffer () throw ()
 
 SoundBuffer (unsigned int capacity, const Format &format) throw (OutOfMemoryException)
 
 SoundBuffer (const SoundBuffer &other) throw (OutOfMemoryException)
 
void reserve (unsigned int capacity) throw (OutOfMemoryException)
 
void reserve (unsigned int capacity, const Format &format) throw (OutOfMemoryException)
 
unsigned int getByteCapacity () const throw ()
 
unsigned int getSampleCapacity () const throw ()
 
unsigned int getUsedBytes () const throw ()
 
void * getBuffer () throw ()
 
const void * getBuffer () const throw ()
 
const FormatgetFormat () const
 
void setFormat (const Format &newFormat) throw ()
 
void setUsedBytes (unsigned int used) throw ()
 
unsigned int getSampleCount () const throw ()
 
void reformat (const Format &newFormat) throw (Exception)
 
SoundBufferoperator= (const SoundBuffer &other) throw (OutOfMemoryException)
 
void swap (SoundBuffer &other) throw ()
 
void optimize () throw ()
 
void clear ()
 

Detailed Description

Sound Buffer class

Remarks
This class represents a buffer for sound data. Though codecs usually treat samples as raw bytes in some uninteresting (for the api) format, renderers don't have that luxury.
This class encapsulates buffers and associates a format to them,
allowing for the implementation of format conversion (should it be needed).
At this point no conversion is supported, and requiring such
a conversion would raise a NotImplementedException.

Definition at line 26 of file SoundBuffer.h.

Constructor & Destructor Documentation

Audio::SoundBuffer::SoundBuffer ( )
throw (
)

Create an empty buffer (zero capacity, default format)

Definition at line 14 of file SoundBuffer.cpp.

16  : buffer(0),
17  byteCapacity(0),
18  bytesUsed(0)
19  {
20  }
Audio::SoundBuffer::SoundBuffer ( unsigned int  capacity,
const Format format 
)
throw (OutOfMemoryException
)

Create a buffer of specified sample capacity and format

Definition at line 22 of file SoundBuffer.cpp.

24  : buffer(0),
25  byteCapacity(0),
26  bytesUsed(0)
27  {
28  reserve(capacity, format);
29  }
Audio::SoundBuffer::SoundBuffer ( const SoundBuffer other)
throw (OutOfMemoryException
)

Create a copy of the other buffer

Remarks
Only used bytes will be copied.

Definition at line 31 of file SoundBuffer.cpp.

References buffer, and VsnetOSS::memcpy().

33  {
34  bytesUsed = byteCapacity = other.bytesUsed;
35  buffer = malloc(byteCapacity);
36  if (buffer == 0)
37  throw OutOfMemoryException();
38  memcpy(buffer, other.buffer, bytesUsed);
39  format = other.format;
40  }

Member Function Documentation

void Audio::SoundBuffer::clear ( )
void* Audio::SoundBuffer::getBuffer ( )
throw (
)
inline

Get write access to the buffer

Definition at line 72 of file SoundBuffer.h.

Referenced by Audio::OpenALSimpleSound::loadImpl().

72 { return buffer; }
const void* Audio::SoundBuffer::getBuffer ( ) const
throw (
)
inline

Get read access to the buffer

Definition at line 75 of file SoundBuffer.h.

75 { return buffer; }
unsigned int Audio::SoundBuffer::getByteCapacity ( ) const
throw (
)
inline

Get a buffer's byte capacity

Definition at line 61 of file SoundBuffer.h.

61 { return byteCapacity; }
const Format& Audio::SoundBuffer::getFormat ( ) const
inline

Get the buffer's format

Definition at line 78 of file SoundBuffer.h.

78 { return format; }
unsigned int Audio::SoundBuffer::getSampleCapacity ( ) const
throw (
)
inline

Get a buffer's sample capacity

Remarks
Frame capacity actually, which is not the same for multichannel formats.

Definition at line 66 of file SoundBuffer.h.

References Audio::Format::frameSize().

66 { return byteCapacity / format.frameSize(); }
unsigned int Audio::SoundBuffer::getSampleCount ( ) const
throw (
)
inline

Get a buffer's sample capacity for a certain format

Definition at line 87 of file SoundBuffer.h.

References Audio::Format::frameSize().

87 { return bytesUsed / format.frameSize(); }
unsigned int Audio::SoundBuffer::getUsedBytes ( ) const
throw (
)
inline

Get the portion of the buffer actually used for holding useful data

Definition at line 69 of file SoundBuffer.h.

Referenced by Audio::OpenALSimpleSound::loadImpl().

69 { return bytesUsed; }
SoundBuffer & Audio::SoundBuffer::operator= ( const SoundBuffer other)
throw (OutOfMemoryException
)

Copy the given buffer as if SoundBuffer(buffer) was called

Definition at line 42 of file SoundBuffer.cpp.

References buffer, and VsnetOSS::memcpy().

44  {
45  bytesUsed = byteCapacity = other.bytesUsed;
46  buffer = realloc(buffer, byteCapacity);
47  if (buffer == 0)
48  throw OutOfMemoryException();
49  memcpy(buffer, other.buffer, bytesUsed);
50  format = other.format;
51 
52  return *this;
53  }
void Audio::SoundBuffer::optimize ( )
throw (
)

Free extra memory allocated

Definition at line 89 of file SoundBuffer.cpp.

Referenced by Audio::OpenALSimpleSound::loadImpl().

91  {
92  if (bytesUsed == 0) {
93  if (buffer) {
94  free(buffer);
95  buffer = 0;
96  }
97  bytesUsed = byteCapacity = 0;
98  } else {
99  if (bytesUsed != byteCapacity)
100  buffer = realloc(buffer, byteCapacity = bytesUsed);
101  }
102  }
void Audio::SoundBuffer::reformat ( const Format newFormat)
throw (Exception
)

Reformat the samples in the buffer without reallocating if possible (inplace)

Remarks
If the new format requires more bytes than the buffer's byte capacity, reallocation will be unavoidable. However, if the same buffer is used for conversion of several packets, subsequent operations on same-sized packets will not require such a reallocation, since if the new format requires less bytes only the used bytes count will be modified leaving the same byte capacity.

Definition at line 73 of file SoundBuffer.cpp.

75  {
76  if (newFormat != format)
77  throw(NotImplementedException("Format conversion"));
78  }
void Audio::SoundBuffer::reserve ( unsigned int  capacity)
throw (OutOfMemoryException
)

Set a buffer's capacity.

Parameters
capacityThe buffer's capacity in bytes
Remarks
Destroys the current data in the buffer.

Definition at line 55 of file SoundBuffer.cpp.

References buffer.

Referenced by Audio::OpenALSimpleSound::loadImpl().

57  {
58  byteCapacity = capacity;
59  bytesUsed = 0;
60 
61  buffer = realloc(buffer, byteCapacity);
62  if (buffer == 0)
63  throw OutOfMemoryException();
64  }
void Audio::SoundBuffer::reserve ( unsigned int  capacity,
const Format format 
)
throw (OutOfMemoryException
)

Set a buffer's capacity and format.

Parameters
capacityThe buffer's capacity in samples (or frames) for 'format'
formatThe new format associated to the buffer
Remarks
Destroys the current data in the buffer.

Definition at line 66 of file SoundBuffer.cpp.

68  {
69  format = _format;
70  reserve(capacity * _format.frameSize());
71  }
void Audio::SoundBuffer::setFormat ( const Format newFormat)
throw (
)
inline

Set the format of the stream mantaining the capacity yet destroying all current data

Definition at line 81 of file SoundBuffer.h.

81 { format = newFormat; bytesUsed = 0; }
void Audio::SoundBuffer::setUsedBytes ( unsigned int  used)
throw (
)
inline

Set the portion of the buffer actually used for holding useful data

Definition at line 84 of file SoundBuffer.h.

Referenced by Audio::OpenALSimpleSound::loadImpl().

84 { bytesUsed = used; }
void Audio::SoundBuffer::swap ( SoundBuffer other)
throw (
)

Swap buffer contents and format It's an inherently quick operation, since it only swaps pointers and descriptors.

Definition at line 80 of file SoundBuffer.cpp.

References buffer.

Referenced by Audio::OpenALSimpleSound::loadImpl().

82  {
83  std::swap(buffer, other.buffer);
84  std::swap(byteCapacity, other.byteCapacity);
85  std::swap(bytesUsed, other.bytesUsed);
86  std::swap(format, other.format);
87  }

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