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

#include <OpenALRenderableSource.h>

Inheritance diagram for Audio::OpenALRenderableSource:
Audio::RenderableSource Audio::UserData

Public Member Functions

 OpenALRenderableSource (Source *source)
 
virtual ~OpenALRenderableSource ()
 
- Public Member Functions inherited from Audio::RenderableSource
virtual ~RenderableSource ()
 
void startPlaying (Timestamp start=0) throw (Exception)
 
void stopPlaying () throw ()
 
bool isPlaying () const throw ()
 
Timestamp getPlayingTime () const throw (Exception)
 
SourcegetSource () const throw ()
 
void seek (Timestamp time) throw (Exception)
 
void update (int flags, const Listener &sceneListener) throw ()
 
- Public Member Functions inherited from Audio::UserData
virtual ~UserData ()
 

Protected Member Functions

virtual void startPlayingImpl (Timestamp start) throw (Exception)
 
virtual void stopPlayingImpl () throw (Exception)
 
virtual bool isPlayingImpl () const throw (Exception)
 
virtual Timestamp getPlayingTimeImpl () const throw (Exception)
 
virtual void updateImpl (int flags, const Listener &sceneListener) throw (Exception)
 
virtual void seekImpl (Timestamp time) throw (Exception)
 
ALuint getALSource () const
 
void attachALBuffers () throw (Exception)
 
- Protected Member Functions inherited from Audio::RenderableSource
 RenderableSource (Source *source) throw ()
 

Additional Inherited Members

- Public Types inherited from Audio::RenderableSource
enum  UpdateFlags {
  UPDATE_ALL = 0x0F, UPDATE_LOCATION = 0x01, UPDATE_ATTRIBUTES = 0x02, UPDATE_EFFECTS = 0x04,
  UPDATE_GAIN = 0x08
}
 

Detailed Description

OpenAL Renderable Source class

Remarks
This class implements the RenderableSource interface for the OpenAL renderer.

Definition at line 23 of file OpenALRenderableSource.h.

Constructor & Destructor Documentation

Audio::OpenALRenderableSource::OpenALRenderableSource ( Source source)

Definition at line 28 of file OpenALRenderableSource.cpp.

29  : RenderableSource(source),
30  alSource(0),
31  alBuffersAttached(false)
32  {
33  alGenSources(1,&alSource);
34  }
Audio::OpenALRenderableSource::~OpenALRenderableSource ( )
virtual

Definition at line 36 of file OpenALRenderableSource.cpp.

37  {
38  alDeleteSources(1,&alSource);
39  }

Member Function Documentation

void Audio::OpenALRenderableSource::attachALBuffers ( )
throw (Exception
)
protected

Attach AL buffers from the source's AL sound, if not attached already.

Note
It will fail with an assertion if the attached sound isn't an OpenAL sound.

Definition at line 139 of file OpenALRenderableSource.cpp.

References checkAlError, getALSource(), Audio::Source::getSound(), and Audio::RenderableSource::getSource().

141  {
142  if (!alBuffersAttached) {
143  SharedPtr<Sound> sound = getSource()->getSound();
144 
145  if (!sound->isLoaded())
146  sound->load();
147 
148  assert(!sound->isStreaming() && "OpenALRenderableSource can only handle streaming sounds");
149 
150  // Attachment to a simple sound, just assign the AL buffer to this AL source
151  ALBufferHandle alBuffer = dynamic_cast<OpenALSimpleSound*>(sound.get())->getAlBuffer();
152  ALSourceHandle alSource = getALSource();
153  alSourcei(alSource, AL_BUFFER, alBuffer);
154  alBuffersAttached = true;
155 
156  checkAlError();
157  }
158  }
ALuint Audio::OpenALRenderableSource::getALSource ( ) const
inlineprotected

Derived classes may use the underlying AL source handle to set additional attributes

Definition at line 53 of file OpenALRenderableSource.h.

Referenced by attachALBuffers(), getPlayingTimeImpl(), and isPlayingImpl().

53 { return alSource; }
Timestamp Audio::OpenALRenderableSource::getPlayingTimeImpl ( ) const
throw (Exception
)
protectedvirtual
See Also
RenderableSource::getPlayingTimeImpl.

Implements Audio::RenderableSource.

Definition at line 73 of file OpenALRenderableSource.cpp.

References AL_SEC_OFFSET, f, and getALSource().

75  {
76  ALfloat offs = -1.f;
77  alGetSourcef(getALSource(), AL_SEC_OFFSET, &offs);
78 
79  if (offs < 0.f)
80  throw NotImplementedException("getPlayingTimeImpl");
81 
82  return Timestamp(offs);
83  }
bool Audio::OpenALRenderableSource::isPlayingImpl ( ) const
throw (Exception
)
protectedvirtual
See Also
RenderableSource::isPlayingImpl.

Implements Audio::RenderableSource.

Definition at line 65 of file OpenALRenderableSource.cpp.

References getALSource().

67  {
68  ALint state = 0;
69  alGetSourcei(getALSource(), AL_SOURCE_STATE, &state);
70  return (state == AL_PLAYING);
71  }
void Audio::OpenALRenderableSource::seekImpl ( Timestamp  time)
throw (Exception
)
protectedvirtual
See Also
RenderableSource::seekImpl.

Implements Audio::RenderableSource.

Definition at line 160 of file OpenALRenderableSource.cpp.

References AL_SEC_OFFSET, checkAlErrorCode, clearAlError, and error.

162  {
163  // Tell the AL to jump to the specified position
164  // NOTE: lots of implementations don't support it
165  // but according to OpenAL 1.1 specs they should
166  clearAlError();
167  ALuint als = getALSource();
168  alSourcef(als, AL_SEC_OFFSET, time);
169 
170  ALenum error = alGetError();
171  if (error == ALC_INVALID_ENUM) {
172  // This version of the AL does not support seeking
173  // fail silently
174  // TODO: must log the fact to console as a warning
175  } else {
176  checkAlErrorCode(error);
177  }
178  }
void Audio::OpenALRenderableSource::startPlayingImpl ( Timestamp  start)
throw (Exception
)
protectedvirtual
See Also
RenderableSource::startPlayingImpl.

Implements Audio::RenderableSource.

Definition at line 41 of file OpenALRenderableSource.cpp.

References checkAlError, clearAlError, and start.

43  {
44  if (!isPlayingImpl()) {
45  // Make sure we have an attached sound
47 
48  // Tell the AL to start playing (from the specified position)
49  clearAlError();
50  ALuint als = getALSource();
51  alSourcePlay(als);
52  checkAlError();
53 
54  if (start != 0)
55  seekImpl(start);
56  }
57  }
void Audio::OpenALRenderableSource::stopPlayingImpl ( )
throw (Exception
)
protectedvirtual
See Also
RenderableSource::stopPlayingImpl.

Implements Audio::RenderableSource.

Definition at line 59 of file OpenALRenderableSource.cpp.

61  {
62  alSourceStop(alSource);
63  }
void Audio::OpenALRenderableSource::updateImpl ( int  flags,
const Listener sceneListener 
)
throw (Exception
)
protectedvirtual
See Also
RenderableSource::updateImpl.

Implements Audio::RenderableSource.

Definition at line 85 of file OpenALRenderableSource.cpp.

References Audio::alSource3f(), checkAlError, clearAlError, f, Audio::Source::getAngleRange(), Audio::Source::getDirection(), Audio::Source::getGain(), Audio::Source::getPosition(), Audio::Source::getRadius(), Audio::Source::getVelocity(), Audio::Source::isAttenuated(), Audio::Source::isLooping(), Audio::Source::isRelative(), M_1_PI, Audio::Range< T >::max, and Audio::Range< T >::min.

87  {
88  Source *source = getSource();
90 
91  clearAlError();
92 
93  if (flags & UPDATE_ATTRIBUTES) {
94  // Distance attenuation
95  if (source->isAttenuated()) {
96  alSourcef(als, AL_REFERENCE_DISTANCE, source->getRadius());
97  alSourcef(als, AL_ROLLOFF_FACTOR, 1.f);
98  } else {
99  alSourcef(als, AL_ROLLOFF_FACTOR, 0.f);
100  }
101  // Cone
102  {
103  Range<Scalar> angleRange = source->getAngleRange();
104  alSourcef(als, AL_CONE_INNER_ANGLE, float(angleRange.min) * M_1_PI * 360.f);
105  alSourcef(als, AL_CONE_OUTER_ANGLE, float(angleRange.max) * M_1_PI * 360.f);
106  alSourcef(als, AL_CONE_OUTER_GAIN , 0.f);
107  }
108  // Relativity
109  alSourcei(als, AL_SOURCE_RELATIVE, source->isRelative() ? AL_TRUE : AL_FALSE);
110  // Looping
111  alSourcei(als, AL_LOOPING, source->isLooping() ? AL_TRUE : AL_FALSE);
112  }
113  if (flags & UPDATE_GAIN) {
114  // Gain
115  alSourcef(als, AL_GAIN, source->getGain());
116  }
117  if (flags & UPDATE_LOCATION) {
118  if (source->isRelative()) {
119  alSource3f(als, AL_POSITION, source->getPosition());
120  alSource3f(als, AL_VELOCITY, source->getVelocity());
121  alSource3f(als, AL_DIRECTION, source->getDirection());
122  } else {
123  alSource3f(als, AL_POSITION,
124  source->getPosition() - sceneListener.getPosition() );
125  alSource3f(als, AL_VELOCITY,
126  sceneListener.toLocalDirection(
127  source->getVelocity() - sceneListener.getVelocity()
128  ) );
129  alSource3f(als, AL_DIRECTION,
130  sceneListener.toLocalDirection(
131  source->getDirection()
132  ) );
133  }
134  }
135 
136  checkAlError();
137  }

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