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

#include <OpenALSimpleSound.h>

Inheritance diagram for Audio::OpenALSimpleSound:
Audio::SimpleSound Audio::Sound

Public Member Functions

 OpenALSimpleSound (const std::string &name, VSFileSystem::VSFileType type=VSFileSystem::UnknownFile) throw ()
 
ALBufferHandle getAlBuffer () const
 
virtual ~OpenALSimpleSound ()
 
- Public Member Functions inherited from Audio::SimpleSound
virtual ~SimpleSound ()
 
VSFileSystem::VSFileType getType () const
 
- Public Member Functions inherited from Audio::Sound
virtual ~Sound ()
 
const std::string & getName () const throw ()
 
const FormatgetFormat () const throw ()
 
bool isLoaded () const throw ()
 
bool isLoading () const throw ()
 
bool isStreaming () const throw ()
 
void load (bool wait=true) throw (Exception)
 
void unload () throw ()
 

Protected Member Functions

virtual void loadImpl (bool wait) throw (Exception)
 
virtual void unloadImpl () throw ()
 
- Protected Member Functions inherited from Audio::SimpleSound
 SimpleSound (const std::string &name, VSFileSystem::VSFileType type=VSFileSystem::UnknownFile, bool streaming=false) throw ()
 
bool isStreamLoaded () const
 
void loadStream () throw (Exception)
 
void closeStream () throw (ResourceNotLoadedException)
 
SharedPtr< StreamgetStream () const throw (ResourceNotLoadedException)
 
void readBuffer (SoundBuffer &buffer) throw (Exception)
 
virtual void abortLoad () throw ()
 
- Protected Member Functions inherited from Audio::Sound
 Sound (const std::string &name, bool streaming) throw ()
 
FormatgetFormat () throw ()
 
virtual void onLoaded (bool success) throw ()
 
virtual void waitLoad () throw (Exception)
 

Additional Inherited Members

- Protected Attributes inherited from Audio::Sound
struct Audio::Sound::Flags flags
 

Detailed Description

OpenAL Simple Sound implementation class

Remarks
This class implements simple (non-streaming) OpenAL sounds. This will load the whole sound into a single OpenAL buffer.
See Also
Sound, SimpleSound

Definition at line 25 of file OpenALSimpleSound.h.

Constructor & Destructor Documentation

Audio::OpenALSimpleSound::OpenALSimpleSound ( const std::string &  name,
VSFileSystem::VSFileType  type = VSFileSystem::UnknownFile 
)
throw (
)

Internal constructor used by derived classes

Definition at line 25 of file OpenALSimpleSound.cpp.

26  :
27  SimpleSound(name, type, false),
28  bufferHandle(AL_NULL_BUFFER)
29  {
30  }
Audio::OpenALSimpleSound::~OpenALSimpleSound ( )
virtual

Definition at line 32 of file OpenALSimpleSound.cpp.

33  {
34  }

Member Function Documentation

ALBufferHandle Audio::OpenALSimpleSound::getAlBuffer ( ) const
inline

Package-private: the OpenAL renderer package uses this, YOU DON'T

Definition at line 34 of file OpenALSimpleSound.h.

34 { return bufferHandle; }
void Audio::OpenALSimpleSound::loadImpl ( bool  wait)
throw (Exception
)
protectedvirtual

Load the resource

Note
Assume it is unloaded and not loading

Implements Audio::Sound.

Definition at line 36 of file OpenALSimpleSound.cpp.

References Audio::__impl::OpenAL::asALFormat(), Audio::Format::bitsPerSample, buffer, checkAlError, clearAlError, e, Audio::SoundBuffer::getBuffer(), Audio::SoundBuffer::getUsedBytes(), max(), VsnetOSS::memcpy(), Audio::Format::nativeOrder, Audio::SoundBuffer::optimize(), Audio::SoundBuffer::reserve(), Audio::Format::sampleFrequency, Audio::SoundBuffer::setUsedBytes(), Audio::Format::signedSamples, and Audio::SoundBuffer::swap().

38  {
39  // just in case
40  unloadImpl();
41 
42  try {
43 
44  flags.loading = 1;
45 
46  // load the stream
47  try {
48  loadStream();
49  } catch(ResourceAlreadyLoadedException e) {
50  // Weird...
51  getStream()->seek(0);
52  }
53  SharedPtr<Stream> stream = getStream();
54 
55  // setup formatted buffer
56  // if the format does not match an OpenAL built-in format, we must convert it.
57  Format targetFormat = stream->getFormat();
58  targetFormat.signedSamples = (targetFormat.bitsPerSample > 8);
59  targetFormat.nativeOrder = 1;
60  if (targetFormat.bitsPerSample > 8)
61  targetFormat.bitsPerSample = 16;
62  else
63  targetFormat.bitsPerSample = 8;
64 
65  // Set capacity to half a second or 16k samples, whatever's bigger
66  size_t bufferCapacity =
67  std::max( 16384U, targetFormat.sampleFrequency/2 );
68 
69  // Prepare a list of buffers, we'll stack them here and later append them
70  std::list<SoundBuffer> buffers;
71 
72  try {
73  while (true) {
74  // Prepare a new buffer
75  buffers.push_back(SoundBuffer());
76  SoundBuffer &buffer = buffers.back();
77  buffer.reserve(bufferCapacity, targetFormat);
78 
79  // Fill it in
80  readBuffer(buffer);
81 
82  // Make sure we're not wasting memory
83  buffer.optimize();
84 
85  // Break if there's no more data
86  if (buffer.getUsedBytes() == 0) {
87  buffers.pop_back();
88  break;
89  }
90  }
91  closeStream();
92  } catch(EndOfStreamException e) {
93  closeStream();
94  } catch(Exception e) {
95  closeStream();
96  throw e;
97  }
98 
99  // Free the stream, asap
100  stream.reset();
101 
102  // Collapse the chunks into a single buffer
103  SoundBuffer buffer;
104 
105  if (buffers.size() > 1) {
106  // Create a compound buffer with all buffers concatenated
107  {
108  unsigned int finalBytes = 0;
109  for (std::list<SoundBuffer>::const_iterator it = buffers.begin(); it != buffers.end(); ++it)
110  finalBytes += it->getUsedBytes();
111  buffer.reserve(finalBytes);
112  }
113 
114  {
115  char* buf = (char*)buffer.getBuffer();
116  for (std::list<SoundBuffer>::const_iterator it = buffers.begin(); it != buffers.end(); ++it) {
117  memcpy(buf, it->getBuffer(), it->getUsedBytes());
118  buf += it->getUsedBytes();
119  buffer.setUsedBytes( buffer.getUsedBytes() + it->getUsedBytes() );
120  }
121  }
122  } else if (buffers.size() > 0) {
123  buffer.swap(buffers.back());
124  } else {
125  throw CorruptStreamException(true);
126  }
127 
128  // Free the buffers, asap
129  // The AL will copy to their own buffers, freeing now kind of makes certain
130  // the AL will have enough memory to do so
131  // (kind of since if memory is allocated off the DSP card, it could still fail)
132  buffers.clear();
133 
134  // Send the data to the AL
135  clearAlError();
136 
137  alGenBuffers(1,&bufferHandle);
138  checkAlError();
139 
140  alBufferData(bufferHandle,
141  asALFormat(targetFormat),
142  buffer.getBuffer(), buffer.getUsedBytes(),
143  targetFormat.sampleFrequency);
144  checkAlError();
145 
146  onLoaded(true);
147  } catch(Exception e) {
148  onLoaded(false);
149  throw e;
150  }
151  }
void Audio::OpenALSimpleSound::unloadImpl ( )
throw (
)
protectedvirtual

Unload the resource.

Note
Assume it is loaded

Implements Audio::Sound.

Definition at line 153 of file OpenALSimpleSound.cpp.

References AL_NULL_BUFFER.

155  {
156  if (bufferHandle == AL_NULL_BUFFER)
157  return;
158 
159  alDeleteBuffers(1, &bufferHandle);
160  bufferHandle = AL_NULL_BUFFER;
161  }

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