SoundSource.cpp
1 /******************************************************************************
2 
3  nomlib - C++11 cross-platform game engine
4 
5 Copyright (c) 2013, Jeffrey Carpenter <jeffrey.carp@gmail.com>
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 ******************************************************************************/
29 #include "nomlib/audio/AL/SoundSource.hpp"
30 
31 namespace nom {
32  namespace OpenAL {
33 
35 {
36 NOM_LOG_TRACE ( NOM );
37 
38 AL_CHECK_ERR ( alGenSources ( 1, &source_id ) );
39 AL_CHECK_ERR ( alSourcei ( source_id, AL_BUFFER, 0 ) );
40 }
41 
42 SoundSource::~SoundSource ( void )
43 {
44 NOM_LOG_TRACE ( NOM );
45 
46 AL_CHECK_ERR ( alSourcei ( source_id, AL_BUFFER, 0 ) );
47 AL_CHECK_ERR ( alDeleteSources ( 1, &source_id ) );
48 }
49 
50 float SoundSource::getVolume ( void ) const
51 {
52  ALfloat volume;
53 
54 AL_CHECK_ERR ( alGetSourcef ( source_id, AL_GAIN, &volume ) );
55 
56  return volume * 100.f;
57 }
58 
59 float SoundSource::getMinVolume ( void ) const
60 {
61  ALfloat min_volume;
62 
63 AL_CHECK_ERR ( alGetSourcef ( source_id, AL_MIN_GAIN, &min_volume ) );
64 
65  return min_volume;
66 }
67 
68 float SoundSource::getMaxVolume ( void ) const
69 {
70  ALfloat max_volume;
71 
72 AL_CHECK_ERR ( alGetSourcef ( source_id, AL_MAX_GAIN, &max_volume ) );
73 
74  return max_volume;
75 }
76 
77 float SoundSource::getPitch ( void ) const
78 {
79  ALfloat pitch;
80 
81 AL_CHECK_ERR ( alGetSourcef ( source_id, AL_PITCH, &pitch ) );
82 
83  return pitch;
84 }
85 
86 bool SoundSource::getLooping ( void ) const
87 {
88  ALint looping;
89 
90 AL_CHECK_ERR ( alGetSourcei ( source_id, AL_LOOPING, &looping ) );
91 
92  switch ( looping )
93  {
94  case AL_TRUE: return true; break;
95  case AL_FALSE: return false; break;
96  }
97 
98  return false;
99 }
100 
101 Point3f SoundSource::getPosition ( void ) const
102 {
103  Point3f position;
104 
105 AL_CHECK_ERR ( alGetSource3f ( source_id, AL_POSITION, &position.x, &position.y,
106  &position.z )
107  );
108 
109  return position;
110 }
111 
112 Point3f SoundSource::getVelocity ( void ) const
113 {
114  Point3f velocity;
115 
116 AL_CHECK_ERR ( alGetSource3f ( source_id, AL_VELOCITY, &velocity.x, &velocity.y,
117  &velocity.z )
118  );
119 
120  return velocity;
121 }
122 
123 bool SoundSource::getPositionRelativeToListener ( void ) const
124 {
125  ALint relative;
126 
127 AL_CHECK_ERR ( alGetSourcei ( source_id, AL_SOURCE_RELATIVE, &relative ) );
128 
129  return (relative != 0);
130 }
131 
132 float SoundSource::getMinDistance ( void ) const
133 {
134  ALfloat distance;
135 
136 AL_CHECK_ERR ( alGetSourcef ( source_id, AL_REFERENCE_DISTANCE, &distance ) );
137 
138  return distance;
139 }
140 
141 float SoundSource::getAttenuation ( void ) const
142 {
143  ALfloat attenuation;
144 
145 AL_CHECK_ERR ( alGetSourcef ( source_id, AL_ROLLOFF_FACTOR, &attenuation ) );
146 
147  return attenuation;
148 }
149 
150 int32 SoundSource::getBufferID ( void ) const
151 {
152  ALint buffer_id;
153 
154 AL_CHECK_ERR ( alGetSourcei ( source_id, AL_BUFFER, &buffer_id ) );
155 
156  return buffer_id;
157 }
158 
159 float SoundSource::getPlayPosition ( void ) const
160 {
161  ALfloat playback_position;
162 
163 AL_CHECK_ERR ( alGetSourcef ( source_id, AL_SEC_OFFSET, &playback_position ) );
164 
165  return playback_position;
166 }
167 
168 SoundStatus SoundSource::getStatus ( void ) const
169 {
170  ALint state;
171 
172 AL_CHECK_ERR ( alGetSourcei ( source_id, AL_SOURCE_STATE, &state ) );
173 
174  switch ( state )
175  {
176  case AL_INITIAL:
177  case AL_STOPPED: return Stopped;
178  case AL_PAUSED: return Paused;
179  case AL_PLAYING: return Playing;
180  }
181 
182  return Stopped;
183 }
184 
185 void SoundSource::setVolume ( float gain )
186 {
187 AL_CHECK_ERR ( alSourcef ( source_id, AL_GAIN, gain * 0.01f ) );
188 }
189 
190 void SoundSource::setMinVolume ( float gain )
191 {
192 AL_CHECK_ERR ( alSourcef ( source_id, AL_MIN_GAIN, gain ) );
193 }
194 
195 void SoundSource::setMaxVolume ( float gain )
196 {
197 AL_CHECK_ERR ( alSourcef ( source_id, AL_MAX_GAIN, gain ) );
198 }
199 
200 void SoundSource::setPitch ( float pitch )
201 {
202 AL_CHECK_ERR ( alSourcef ( source_id, AL_PITCH, pitch ) );
203 }
204 
205 void SoundSource::setLooping ( bool loops )
206 {
207 AL_CHECK_ERR ( alSourcei ( source_id, AL_LOOPING, loops ) );
208 }
209 
210 void SoundSource::setPosition ( float x, float y, float z )
211 {
212 AL_CHECK_ERR ( alSource3f ( source_id, AL_POSITION, x, y, z ) );
213 }
214 
215 void SoundSource::setPosition ( const Point3f& position )
216 {
217  this->setPosition ( position.x, position.y, position.z );
218 }
219 
220 void SoundSource::setVelocity ( float x, float y, float z )
221 {
222 AL_CHECK_ERR ( alSource3f ( source_id, AL_VELOCITY, x, y, z ) );
223 }
224 
225 void SoundSource::setVelocity ( const Point3f& velocity )
226 {
227  this->setVelocity ( velocity.x, velocity.y, velocity.z );
228 }
229 
230 void SoundSource::setPositionRelativeToListener ( bool relative )
231 {
232 AL_CHECK_ERR ( alSourcei ( source_id, AL_SOURCE_RELATIVE, relative ) );
233 }
234 
235 void SoundSource::setMinDistance ( float distance )
236 {
237 AL_CHECK_ERR ( alSourcef ( source_id, AL_REFERENCE_DISTANCE, distance ) );
238 }
239 
240 void SoundSource::setAttenuation ( float attenuation )
241 {
242 AL_CHECK_ERR ( alSourcef ( source_id, AL_ROLLOFF_FACTOR, attenuation ) );
243 }
244 
245 void SoundSource::setPlayPosition ( float seconds )
246 {
247 AL_CHECK_ERR ( alSourcef ( source_id, AL_SEC_OFFSET, seconds ) );
248 }
249 
250 
251  } // namespace OpenAL
252 } // namespace nom
void setPlayPosition(float seconds)
Set playback position of source in seconds.
void setVolume(float gain)
int32 getBufferID(void) const
Obtain buffer identifier of source.
SoundStatus getStatus(void) const
Obtain current state of sound.
float getVolume(void) const
Definition: SoundSource.cpp:50
uint32 source_id
Source identification; used by OpenAL.
float getPlayPosition(void) const
Obtain the current playback position of source in seconds.
SoundSource(void)
Constructor can only be called from deriving classes.
Definition: SoundSource.cpp:34