Sound.cpp
1
2//
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
33namespace sf
34{
39{
40 ALCheck(alGenSources(1, &mySource));
41 ALCheck(alSourcei(mySource, AL_BUFFER, 0));
42}
43
44
48Sound::Sound(const SoundBuffer& Buffer, bool Loop, float Pitch, float Volume, const Vector3f& Position) :
49myBuffer(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
64Sound::Sound(const Sound& Copy) :
65AudioResource(Copy),
66myBuffer(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());
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
101/// Play the sound
102////////////////////////////////////////////////////////////
104{
105 ALCheck(alSourcePlay(mySource));
106}
107
108
113{
114 ALCheck(alSourcePause(mySource));
115}
116
117
122{
123 ALCheck(alSourceStop(mySource));
124}
125
126
130void 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
149void Sound::SetLoop(bool Loop)
150{
151 ALCheck(alSourcei(mySource, AL_LOOPING, Loop));
152}
153
154
158void Sound::SetPitch(float Pitch)
159{
160 ALCheck(alSourcef(mySource, AL_PITCH, Pitch));
161}
162
163
167void Sound::SetVolume(float Volume)
168{
169 ALCheck(alSourcef(mySource, AL_GAIN, Volume * 0.01f));
171
176void Sound::SetPosition(float X, float Y, float Z)
177{
178 ALCheck(alSource3f(mySource, AL_POSITION, X, Y, Z));
179}
181
186void Sound::SetPosition(const Vector3f& Position)
187{
188 SetPosition(Position.x, Position.y, Position.z);
189}
191
198{
199 ALCheck(alSourcei(mySource, AL_SOURCE_RELATIVE, Relative));
200}
201
202
208void Sound::SetMinDistance(float MinDistance)
209{
210 ALCheck(alSourcef(mySource, AL_REFERENCE_DISTANCE, MinDistance));
211}
212
213
219void Sound::SetAttenuation(float Attenuation)
220{
221 ALCheck(alSourcef(mySource, AL_ROLLOFF_FACTOR, Attenuation));
223
224
228void Sound::SetPlayingOffset(float TimeOffset)
229{
230 ALCheck(alSourcef(mySource, AL_SEC_OFFSET, TimeOffset));
231}
232
233
239 return myBuffer;
240}
241
242
246bool Sound::GetLoop() const
248 ALint Loop;
249 ALCheck(alGetSourcei(mySource, AL_LOOPING, &Loop));
250
251 return Loop != 0;
252}
253
254
255////////////////////////////////////////////////////////////
256/// Get the pitch
257////////////////////////////////////////////////////////////
258float Sound::GetPitch() const
259{
260 ALfloat Pitch;
261 ALCheck(alGetSourcef(mySource, AL_PITCH, &Pitch));
262
263 return Pitch;
264}
265
266
270float Sound::GetVolume() const
271{
272 ALfloat Gain;
273 ALCheck(alGetSourcef(mySource, AL_GAIN, &Gain));
274
275 return Gain * 100.f;
276}
277
278
282Vector3f Sound::GetPosition() const
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
AudioResource()
Default constructor.
SoundBuffer is the low-level for loading and manipulating sound buffers.
float GetPlayingOffset() const
Get the current playing position of the sound.
Definition Sound.cpp:331
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
void ResetBuffer()
Reset the internal buffer.
Definition Sound.cpp:394
void SetVolume(float Volume)
Set the sound volume.
Definition Sound.cpp:167
const SoundBuffer * GetBuffer() const
Get the source buffer.
Definition Sound.cpp:237
bool GetLoop() const
Tell whether or not the sound is looping.
Definition Sound.cpp:246
Status
Enumeration of the sound states.
Definition Sound.hpp:53
@ Playing
Sound is playing.
Definition Sound.hpp:56
@ Stopped
Sound is not playing.
Definition Sound.hpp:54
@ Paused
Sound is paused.
Definition Sound.hpp:55
bool IsRelativeToListener() const
Tell if the sound's position is relative to the listener's position, or if it's absolute.
Definition Sound.cpp:295
Sound()
Default constructor.
Definition Sound.cpp:38
void SetLoop(bool Loop)
Set the sound loop state.
Definition Sound.cpp:149
float GetVolume() const
Get the volume.
Definition Sound.cpp:270
void Pause()
Pause the sound.
Definition Sound.cpp:112
float GetAttenuation() const
Get the attenuation factor.
Definition Sound.cpp:319
void SetRelativeToListener(bool Relative)
Make the sound's position relative to the listener's position, or absolute.
Definition Sound.cpp:197
Vector3f GetPosition() const
Get the sound position.
Definition Sound.cpp:282
void SetBuffer(const SoundBuffer &Buffer)
Set the source buffer.
Definition Sound.cpp:130
void Stop()
Stop the sound.
Definition Sound.cpp:121
void SetPosition(float X, float Y, float Z)
Set the sound position (take 3 values).
Definition Sound.cpp:176
float GetPitch() const
Get the pitch.
Definition Sound.cpp:258
Sound & operator=(const Sound &Other)
Assignment operator.
Definition Sound.cpp:363
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()
Destructor.
Definition Sound.cpp:85
Status GetStatus() const
Get the status of the sound (stopped, paused, playing).
Definition Sound.cpp:343
float GetMinDistance() const
Get the minimum distance.
Definition Sound.cpp:307
void SetPlayingOffset(float TimeOffset)
Set the current playing position of the sound.
Definition Sound.cpp:228
void SetPitch(float Pitch)
Set the sound pitch.
Definition Sound.cpp:158
void Play()
Play the sound.
Definition Sound.cpp:103
T z
Z coordinate of the vector.
Definition Vector3.hpp:62
T x
X coordinate of the vector.
Definition Vector3.hpp:60
T y
Y coordinate of the vector.
Definition Vector3.hpp:61