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

#include <OpenALRenderer.h>

Inheritance diagram for Audio::OpenALRenderer:
Audio::Renderer Audio::BorrowedOpenALRenderer

Public Member Functions

 OpenALRenderer () throw (Exception)
 
virtual ~OpenALRenderer ()
 
virtual SharedPtr< SoundgetSound (const std::string &name, VSFileSystem::VSFileType type=VSFileSystem::UnknownFile, bool streaming=false) throw (Exception)
 
virtual bool owns (SharedPtr< Sound > sound)
 
virtual void attach (SharedPtr< Source > source) throw (Exception)
 
virtual void attach (SharedPtr< Listener > listener) throw (Exception)
 
virtual void detach (SharedPtr< Source > source) throw ()
 
virtual void detach (SharedPtr< Listener > listener) throw ()
 
virtual void setMeterDistance (Scalar distance) throw ()
 
virtual void setDopplerFactor (Scalar factor) throw ()
 
virtual void setOutputFormat (const Format &format) throw (Exception)
 
virtual void beginTransaction () throw (Exception)
 
virtual void commitTransaction () throw (Exception)
 
- Public Member Functions inherited from Audio::Renderer
 Renderer () throw (Exception)
 
virtual ~Renderer ()
 
virtual Scalar getMeterDistance () const throw ()
 
virtual Scalar getDopplerFactor () const throw ()
 
virtual const FormatgetOutputFormat () const throw ()
 

Protected Member Functions

virtual void checkContext () throw (Exception)
 
virtual void initContext () throw (Exception)
 
void setupDopplerEffect () throw (Exception)
 

Protected Attributes

AutoPtr
< __impl::OpenAL::RendererData
data
 

Detailed Description

OpenAL Renderer implementation

Remarks
Audio renderer implementation based on OpenAL.

Definition at line 29 of file OpenALRenderer.h.

Constructor & Destructor Documentation

Audio::OpenALRenderer::OpenALRenderer ( )
throw (Exception
)

Initialize the renderer with default or config-driven settings.

Definition at line 235 of file OpenALRenderer.cpp.

236  :
237  data(new RendererData)
238  {
239  }
Audio::OpenALRenderer::~OpenALRenderer ( )
virtual

Definition at line 241 of file OpenALRenderer.cpp.

242  {
243  }

Member Function Documentation

void Audio::OpenALRenderer::attach ( SharedPtr< Source source)
throw (Exception
)
virtual

Attach a source to this renderer

Remarks
A source may only be attached to one renderer. If the source was attached already, an exception will be thrown.
Attachment may mean resource allocation. Either immediate or deferred. So it may
fail if resources are scarce.

Implements Audio::Renderer.

Definition at line 279 of file OpenALRenderer.cpp.

281  {
282  checkContext();
283  source->setRenderable( SharedPtr<RenderableSource>(
284  source->getSound()->isStreaming()
285  ? (RenderableSource*)new OpenALRenderableStreamingSource(source.get())
286  : (RenderableSource*)new OpenALRenderableSource(source.get())
287  )
288  );
289  }
void Audio::OpenALRenderer::attach ( SharedPtr< Listener listener)
throw (Exception
)
virtual

Attach a listener to this renderer

Remarks
A listener may only be attached to one renderer. If the listener was attached already, an exception will be thrown.
Attachment may mean resource allocation. Either immediate or deferred. So it may
fail if resources are scarce.

Implements Audio::Renderer.

Definition at line 291 of file OpenALRenderer.cpp.

293  {
294  checkContext();
295  listener->setRenderable( SharedPtr<RenderableListener>(
296  new OpenALRenderableListener(listener.get()) ) );
297  }
void Audio::OpenALRenderer::beginTransaction ( )
throw (Exception
)
virtual

Begins a transaction

Remarks
state changes will be piled up and applied at commit. This is, though, an optional feature: renderers may choose not to implement it, and perform state changes immediately. In any case, the result ought to be the same.

Reimplemented from Audio::Renderer.

Definition at line 356 of file OpenALRenderer.cpp.

358  {
359  data->suspend();
360 
361  if (data->dirty.dopplerFactor)
363  }
void Audio::OpenALRenderer::checkContext ( )
throw (Exception
)
protectedvirtual

Makes sure the AL context is valid, creating one if necessary

Reimplemented in Audio::BorrowedOpenALRenderer.

Definition at line 345 of file OpenALRenderer.cpp.

347  {
348  if (!data->alDevice)
349  data->openDevice(NULL);
350  if (!data->alContext) {
351  data->openContext(getOutputFormat());
352  initContext();
353  }
354  }
void Audio::OpenALRenderer::commitTransaction ( )
throw (Exception
)
virtual

See Also
begin()

Reimplemented from Audio::Renderer.

Definition at line 365 of file OpenALRenderer.cpp.

367  {
368  data->commit();
369  }
void Audio::OpenALRenderer::detach ( SharedPtr< Source source)
throw (
)
virtual

Detach a source from this renderer.

Remarks
Immediately frees any allocated resources.

Implements Audio::Renderer.

Definition at line 299 of file OpenALRenderer.cpp.

301  {
302  // Just clear it... RenderableListener's destructor will handle everything fine.
303  source->setRenderable( SharedPtr<RenderableSource>() );
304  }
void Audio::OpenALRenderer::detach ( SharedPtr< Listener listener)
throw (
)
virtual

Detach a listener from this renderer.

Remarks
Immediately frees any allocated resources.

Implements Audio::Renderer.

Definition at line 306 of file OpenALRenderer.cpp.

308  {
309  // Just clear it... RenderableListener's destructor will handle everything fine.
310  listener->setRenderable( SharedPtr<RenderableListener>() );
311  }
SharedPtr< Sound > Audio::OpenALRenderer::getSound ( const std::string &  name,
VSFileSystem::VSFileType  type = VSFileSystem::UnknownFile,
bool  streaming = false 
)
throw (Exception
)
virtual

Create a sound, from the stream of the specified name.

Parameters
nameThe path of the soundfile.
typeThe file type (needed by the filesystem).
straemingIf true, requests for a streaming sound - one that is not loaded to memory for playback, but rather read on-demand during playback.
Remarks
The name is the path of a soundstream as it would be passed to the CodecRegistry.
Streaming sounds can also be created, which depending on the renderer implementation
may or may not result in a different kind of sound resource. Most notably, the difference may depend on runtime-dependant state. For instance, it is quite reasonable that a streaming sound won't be created if the same sound file exists in a non-streaming form.
See Also
CodecRegistry

Implements Audio::Renderer.

Definition at line 245 of file OpenALRenderer.cpp.

250  {
251  checkContext();
252  SharedPtr<Sound> sound = data->lookupSound(type,name);
253  if (!sound.get() || streaming) {
254  if (streaming) {
255  // Streaming sounds cannot be cached, so if a streaming sound
256  // is in the cache, it must be evicted and re-created
257  sound.reset();
258  data->addSound(
259  type,
260  name,
261  sound = SharedPtr<Sound>(new OpenALStreamingSound(name,type))
262  );
263  } else {
264  data->addSound(
265  type,
266  name,
267  sound = SharedPtr<Sound>(new OpenALSimpleSound(name,type))
268  );
269  }
270  }
271  return sound;
272  }
void Audio::OpenALRenderer::initContext ( )
throw (Exception
)
protectedvirtual

Sets expected defaults into the context

Definition at line 371 of file OpenALRenderer.cpp.

373  {
374  // Set the distance model
375  alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
376 
377  // Flag everything as dirty
378  data->dirty.meterDistance = 1;
379  data->dirty.dopplerFactor = 1;
380  }
bool Audio::OpenALRenderer::owns ( SharedPtr< Sound sound)
virtual

Return whether the specified sound has been created using this renderer or not

Implements Audio::Renderer.

Definition at line 274 of file OpenALRenderer.cpp.

275  {
276  return !data->lookupSound(sound).isNull();
277  }
void Audio::OpenALRenderer::setDopplerFactor ( Scalar  factor)
throw (
)
virtual

Sets how much the doppler effect will be accounted for.

Remarks
This sets a semi-opaque value which controls how much of the doppler effect will be simulated. All that is required is that 0 maps to fully disabled doppler effect, 1 maps to a realistic effect, in between a disimulated effect, and above 1 an exaggerated effect. The spec is purposefully vague on the specifics.

Reimplemented from Audio::Renderer.

Definition at line 326 of file OpenALRenderer.cpp.

References Audio::Renderer::setDopplerFactor().

328  {
330 
331  // Just flag it as dirty so that the next commit reconfigures the doppler effect.
332  data->dirty.dopplerFactor = 1;
333  }
void Audio::OpenALRenderer::setMeterDistance ( Scalar  distance)
throw (
)
virtual

Sets the distance in world units that represents one meter.

Remarks
This reference distance is required by environmental effect processing to accurately account for distance factors beyond simple gain falloff.

Reimplemented from Audio::Renderer.

Definition at line 313 of file OpenALRenderer.cpp.

References Audio::Renderer::setMeterDistance().

315  {
316  // ToDo
317  // Nothing yet - this is an extension to OpenAL 1.1's specs and in this phase
318  // we'll implement only basic functionality.
319  Renderer::setMeterDistance(distance);
320 
321  // meterDistance affects doppler settings (since it affects the speed of sound)
322  data->dirty.dopplerFactor = 1;
323  data->dirty.meterDistance = 1;
324  }
void Audio::OpenALRenderer::setOutputFormat ( const Format format)
throw (Exception
)
virtual

Sets the (preferred) output format.

Remarks
Renderers are encouraged to set their effective output format to the closest "better" format, where better is defined as either having heigher sampling frequency, bit depth, or number of channels. The effective output format, if known, must be reflected in subsequent calls to getOutputFormat.

Reimplemented from Audio::Renderer.

Reimplemented in Audio::BorrowedOpenALRenderer.

Definition at line 335 of file OpenALRenderer.cpp.

References Audio::Renderer::setOutputFormat().

337  {
338  if (!data->alDevice)
339  data->openDevice(NULL);
340  data->closeContext();
341  data->openContext(format);
343  }
void Audio::OpenALRenderer::setupDopplerEffect ( )
throw (Exception
)
protected

Sets doppler effect globals into the context

Definition at line 382 of file OpenALRenderer.cpp.

References checkAlError, and clearAlError.

384  {
385  clearAlError();
386 
387  // First of all, compute the speed of sound (in world units)
388  Scalar speedOfSound = 343.3f * getMeterDistance();
389 
390  // Set doppler factor and speed of sound
391  alDopplerFactor(getDopplerFactor());
392 #ifdef _WIN32
393  alDopplerVelocity(speedOfSound);
394 #else
395  alSpeedOfSound(speedOfSound);
396 #endif
397 
398  data->dirty.dopplerFactor = 0;
399 
400  checkAlError();
401  }

Member Data Documentation

AutoPtr<__impl::OpenAL::RendererData> Audio::OpenALRenderer::data
protected

Definition at line 32 of file OpenALRenderer.h.


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