Sound.cpp
1 //
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
26 // Headers
28 #include <SFML/Audio/Sound.hpp>
29 #include <SFML/Audio/SoundBuffer.hpp>
30 #include <SFML/Audio/OpenAL.hpp>
31 
32 
33 namespace sf
34 {
39 {
40  ALCheck(alGenSources(1, &mySource));
41  ALCheck(alSourcei(mySource, AL_BUFFER, 0));
42 }
43 
44 
48 Sound::Sound(const SoundBuffer& Buffer, bool Loop, float Pitch, float Volume, const Vector3f& Position) :
49 myBuffer(NULL)
50 {
51  ALCheck(alGenSources(1, &mySource));
52 
53  SetBuffer(Buffer);
54  SetLoop(Loop);
55  SetPitch(Pitch);
56  SetVolume(Volume);
57  SetPosition(Position);
58 }
59 
60 
64 Sound::Sound(const Sound& Copy) :
65 AudioResource(Copy),
66 myBuffer(NULL)
67 {
68  ALCheck(alGenSources(1, &mySource));
69 
70  if (Copy.myBuffer)
71  SetBuffer(*Copy.myBuffer);
72  SetLoop(Copy.GetLoop());
73  SetPitch(Copy.GetPitch());
74  SetVolume(Copy.GetVolume());
75  SetPosition(Copy.GetPosition());
79 }
80 
81 
86 {
87  if (mySource)
88  {
89  if (myBuffer)
90  {
91  Stop();
92  ALCheck(alSourcei(mySource, AL_BUFFER, 0));
93  myBuffer->DetachSound(this);
94  }
95  ALCheck(alDeleteSources(1, &mySource));
96  }
97 }
98 
99 
104 {
105  ALCheck(alSourcePlay(mySource));
106 }
107 
108 
113 {
114  ALCheck(alSourcePause(mySource));
115 }
116 
117 
122 {
123  ALCheck(alSourceStop(mySource));
124 }
125 
126 
130 void Sound::SetBuffer(const SoundBuffer& Buffer)
131 {
132  // First detach from the previous buffer
133  if (myBuffer)
134  {
135  Stop();
136  myBuffer->DetachSound(this);
137  }
138 
139  // Assign and use the new buffer
140  myBuffer = &Buffer;
141  myBuffer->AttachSound(this);
142  ALCheck(alSourcei(mySource, AL_BUFFER, myBuffer->myBuffer));
143 }
144 
145 
149 void Sound::SetLoop(bool Loop)
150 {
151  ALCheck(alSourcei(mySource, AL_LOOPING, Loop));
152 }
153 
154 
158 void Sound::SetPitch(float Pitch)
159 {
160  ALCheck(alSourcef(mySource, AL_PITCH, Pitch));
161 }
162 
163 
167 void Sound::SetVolume(float Volume)
168 {
169  ALCheck(alSourcef(mySource, AL_GAIN, Volume * 0.01f));
170 }
171 
176 void Sound::SetPosition(float X, float Y, float Z)
177 {
178  ALCheck(alSource3f(mySource, AL_POSITION, X, Y, Z));
179 }
180 
181 
186 void Sound::SetPosition(const Vector3f& Position)
187 {
188  SetPosition(Position.x, Position.y, Position.z);
189 }
190 
191 
197 void Sound::SetRelativeToListener(bool Relative)
198 {
199  ALCheck(alSourcei(mySource, AL_SOURCE_RELATIVE, Relative));
200 }
201 
202 
208 void Sound::SetMinDistance(float MinDistance)
209 {
210  ALCheck(alSourcef(mySource, AL_REFERENCE_DISTANCE, MinDistance));
211 }
212 
213 
219 void Sound::SetAttenuation(float Attenuation)
220 {
221  ALCheck(alSourcef(mySource, AL_ROLLOFF_FACTOR, Attenuation));
222 }
223 
224 
228 void Sound::SetPlayingOffset(float TimeOffset)
229 {
230  ALCheck(alSourcef(mySource, AL_SEC_OFFSET, TimeOffset));
231 }
232 
233 
238 {
239  return myBuffer;
240 }
241 
242 
246 bool Sound::GetLoop() const
247 {
248  ALint Loop;
249  ALCheck(alGetSourcei(mySource, AL_LOOPING, &Loop));
250 
251  return Loop != 0;
252 }
253 
254 
258 float Sound::GetPitch() const
259 {
260  ALfloat Pitch;
261  ALCheck(alGetSourcef(mySource, AL_PITCH, &Pitch));
262 
263  return Pitch;
264 }
265 
266 
270 float Sound::GetVolume() const
271 {
272  ALfloat Gain;
273  ALCheck(alGetSourcef(mySource, AL_GAIN, &Gain));
274 
275  return Gain * 100.f;
276 }
277 
278 
283 {
284  Vector3f Position;
285  ALCheck(alGetSource3f(mySource, AL_POSITION, &Position.x, &Position.y, &Position.z));
286 
287  return Position;
288 }
289 
290 
296 {
297  ALint Relative;
298  ALCheck(alGetSourcei(mySource, AL_SOURCE_RELATIVE, &Relative));
299 
300  return Relative != 0;
301 }
302 
303 
308 {
309  ALfloat MinDistance;
310  ALCheck(alGetSourcef(mySource, AL_REFERENCE_DISTANCE, &MinDistance));
311 
312  return MinDistance;
313 }
314 
315 
320 {
321  ALfloat Attenuation;
322  ALCheck(alGetSourcef(mySource, AL_ROLLOFF_FACTOR, &Attenuation));
323 
324  return Attenuation;
325 }
326 
327 
332 {
333  ALfloat Seconds = 0.f;
334  ALCheck(alGetSourcef(mySource, AL_SEC_OFFSET, &Seconds));
335 
336  return Seconds;
337 }
338 
339 
344 {
345  ALint State;
346  ALCheck(alGetSourcei(mySource, AL_SOURCE_STATE, &State));
347 
348  switch (State)
349  {
350  case AL_INITIAL :
351  case AL_STOPPED : return Stopped;
352  case AL_PAUSED : return Paused;
353  case AL_PLAYING : return Playing;
354  }
355 
356  return Stopped;
357 }
358 
359 
364 {
365  // Here we don't use the copy-and-swap idiom, because it would mess up
366  // the list of sound instances contained in the buffers
367 
368  // Detach the sound instance from the previous buffer (if any)
369  if (myBuffer)
370  {
371  Stop();
372  myBuffer->DetachSound(this);
373  myBuffer = NULL;
374  }
375 
376  // Copy the sound attributes
377  if (Other.myBuffer)
378  SetBuffer(*Other.myBuffer);
379  SetLoop(Other.GetLoop());
380  SetPitch(Other.GetPitch());
381  SetVolume(Other.GetVolume());
382  SetPosition(Other.GetPosition());
386 
387  return *this;
388 }
389 
390 
395 {
396  // First stop the sound in case it is playing
397  Stop();
398 
399  // Detach the buffer
400  ALCheck(alSourcei(mySource, AL_BUFFER, 0));
401  myBuffer = NULL;
402 }
403 
404 } // namespace sf
void SetPitch(float Pitch)
Set the sound pitch.
Definition: Sound.cpp:158
void SetRelativeToListener(bool Relative)
Make the sound&#39;s position relative to the listener&#39;s position, or absolute.
Definition: Sound.cpp:197
void Stop()
Stop the sound.
Definition: Sound.cpp:121
T z
Z coordinate of the vector.
Definition: Vector3.hpp:62
void Play()
Play the sound.
Definition: Sound.cpp:103
void SetPosition(float X, float Y, float Z)
Set the sound position (take 3 values).
Definition: Sound.cpp:176
Sound is not playing.
Definition: Sound.hpp:54
Sound defines the properties of a sound such as position, volume, pitch, etc.
Definition: Sound.hpp:45
Status GetStatus() const
Get the status of the sound (stopped, paused, playing)
Definition: Sound.cpp:343
void SetAttenuation(float Attenuation)
Set the attenuation factor - the higher the attenuation, the more the sound will be attenuated with d...
Definition: Sound.cpp:219
Sound()
Default constructor.
Definition: Sound.cpp:38
bool IsRelativeToListener() const
Tell if the sound&#39;s position is relative to the listener&#39;s position, or if it&#39;s absolute.
Definition: Sound.cpp:295
Vector3f GetPosition() const
Get the sound position.
Definition: Sound.cpp:282
Vector3 is an utility class for manipulating 3 dimensional vectors.
Definition: Vector3.hpp:37
bool GetLoop() const
Tell whether or not the sound is looping.
Definition: Sound.cpp:246
void SetPlayingOffset(float TimeOffset)
Set the current playing position of the sound.
Definition: Sound.cpp:228
float GetPlayingOffset() const
Get the current playing position of the sound.
Definition: Sound.cpp:331
Sound & operator=(const Sound &Other)
Assignment operator.
Definition: Sound.cpp:363
Sound is paused.
Definition: Sound.hpp:55
void Pause()
Pause the sound.
Definition: Sound.cpp:112
T y
Y coordinate of the vector.
Definition: Vector3.hpp:61
float GetMinDistance() const
Get the minimum distance.
Definition: Sound.cpp:307
Sound is playing.
Definition: Sound.hpp:56
void SetBuffer(const SoundBuffer &Buffer)
Set the source buffer.
Definition: Sound.cpp:130
const SoundBuffer * GetBuffer() const
Get the source buffer.
Definition: Sound.cpp:237
void ResetBuffer()
Reset the internal buffer.
Definition: Sound.cpp:394
void SetVolume(float Volume)
Set the sound volume.
Definition: Sound.cpp:167
float GetAttenuation() const
Get the attenuation factor.
Definition: Sound.cpp:319
~Sound()
Destructor.
Definition: Sound.cpp:85
void SetLoop(bool Loop)
Set the sound loop state.
Definition: Sound.cpp:149
T x
X coordinate of the vector.
Definition: Vector3.hpp:60
Status
Enumeration of the sound states.
Definition: Sound.hpp:52
float GetVolume() const
Get the volume.
Definition: Sound.cpp:270
void SetMinDistance(float MinDistance)
Set the minimum distance - closer than this distance, the listener will hear the sound at its maximum...
Definition: Sound.cpp:208
float GetPitch() const
Get the pitch.
Definition: Sound.cpp:258
SoundBuffer is the low-level for loading and manipulating sound buffers.
Definition: SoundBuffer.hpp:46
Abstract base class for every class that owns a device-dependant resource – allow them to initialize ...