1 ////////////////////////////////////////////////////////////
2 //
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2018 Laurent Gomila (laurent@sfml-dev.org)
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 //
23 ////////////////////////////////////////////////////////////
24 
25 ////////////////////////////////////////////////////////////
26 // Headers
27 ////////////////////////////////////////////////////////////
28 #include <SFML/Audio/SoundSource.hpp>
29 #include <SFML/Audio/ALCheck.hpp>
30 
31 
32 namespace sf
33 {
34 ////////////////////////////////////////////////////////////
SoundSource()35 SoundSource::SoundSource()
36 {
37     alCheck(alGenSources(1, &m_source));
38     alCheck(alSourcei(m_source, AL_BUFFER, 0));
39 }
40 
41 
42 ////////////////////////////////////////////////////////////
SoundSource(const SoundSource & copy)43 SoundSource::SoundSource(const SoundSource& copy)
44 {
45     alCheck(alGenSources(1, &m_source));
46     alCheck(alSourcei(m_source, AL_BUFFER, 0));
47 
48     setPitch(copy.getPitch());
49     setVolume(copy.getVolume());
50     setPosition(copy.getPosition());
51     setRelativeToListener(copy.isRelativeToListener());
52     setMinDistance(copy.getMinDistance());
53     setAttenuation(copy.getAttenuation());
54 }
55 
56 
57 ////////////////////////////////////////////////////////////
~SoundSource()58 SoundSource::~SoundSource()
59 {
60     alCheck(alSourcei(m_source, AL_BUFFER, 0));
61     alCheck(alDeleteSources(1, &m_source));
62 }
63 
64 
65 ////////////////////////////////////////////////////////////
setPitch(float pitch)66 void SoundSource::setPitch(float pitch)
67 {
68     alCheck(alSourcef(m_source, AL_PITCH, pitch));
69 }
70 
71 
72 ////////////////////////////////////////////////////////////
setVolume(float volume)73 void SoundSource::setVolume(float volume)
74 {
75     alCheck(alSourcef(m_source, AL_GAIN, volume * 0.01f));
76 }
77 
78 
79 ////////////////////////////////////////////////////////////
setPosition(float x,float y,float z)80 void SoundSource::setPosition(float x, float y, float z)
81 {
82     alCheck(alSource3f(m_source, AL_POSITION, x, y, z));
83 }
84 
85 
86 ////////////////////////////////////////////////////////////
setPosition(const Vector3f & position)87 void SoundSource::setPosition(const Vector3f& position)
88 {
89     setPosition(position.x, position.y, position.z);
90 }
91 
92 
93 ////////////////////////////////////////////////////////////
setRelativeToListener(bool relative)94 void SoundSource::setRelativeToListener(bool relative)
95 {
96     alCheck(alSourcei(m_source, AL_SOURCE_RELATIVE, relative));
97 }
98 
99 
100 ////////////////////////////////////////////////////////////
setMinDistance(float distance)101 void SoundSource::setMinDistance(float distance)
102 {
103     alCheck(alSourcef(m_source, AL_REFERENCE_DISTANCE, distance));
104 }
105 
106 
107 ////////////////////////////////////////////////////////////
setAttenuation(float attenuation)108 void SoundSource::setAttenuation(float attenuation)
109 {
110     alCheck(alSourcef(m_source, AL_ROLLOFF_FACTOR, attenuation));
111 }
112 
113 
114 ////////////////////////////////////////////////////////////
getPitch() const115 float SoundSource::getPitch() const
116 {
117     ALfloat pitch;
118     alCheck(alGetSourcef(m_source, AL_PITCH, &pitch));
119 
120     return pitch;
121 }
122 
123 
124 ////////////////////////////////////////////////////////////
getVolume() const125 float SoundSource::getVolume() const
126 {
127     ALfloat gain;
128     alCheck(alGetSourcef(m_source, AL_GAIN, &gain));
129 
130     return gain * 100.f;
131 }
132 
133 
134 ////////////////////////////////////////////////////////////
getPosition() const135 Vector3f SoundSource::getPosition() const
136 {
137     Vector3f position;
138     alCheck(alGetSource3f(m_source, AL_POSITION, &position.x, &position.y, &position.z));
139 
140     return position;
141 }
142 
143 
144 ////////////////////////////////////////////////////////////
isRelativeToListener() const145 bool SoundSource::isRelativeToListener() const
146 {
147     ALint relative;
148     alCheck(alGetSourcei(m_source, AL_SOURCE_RELATIVE, &relative));
149 
150     return relative != 0;
151 }
152 
153 
154 ////////////////////////////////////////////////////////////
getMinDistance() const155 float SoundSource::getMinDistance() const
156 {
157     ALfloat distance;
158     alCheck(alGetSourcef(m_source, AL_REFERENCE_DISTANCE, &distance));
159 
160     return distance;
161 }
162 
163 
164 ////////////////////////////////////////////////////////////
getAttenuation() const165 float SoundSource::getAttenuation() const
166 {
167     ALfloat attenuation;
168     alCheck(alGetSourcef(m_source, AL_ROLLOFF_FACTOR, &attenuation));
169 
170     return attenuation;
171 }
172 
173 
174 ////////////////////////////////////////////////////////////
operator =(const SoundSource & right)175 SoundSource& SoundSource::operator =(const SoundSource& right)
176 {
177     // Leave m_source untouched -- it's not necessary to destroy and
178     // recreate the OpenAL sound source, hence no copy-and-swap idiom
179 
180     // Assign the sound attributes
181     setPitch(right.getPitch());
182     setVolume(right.getVolume());
183     setPosition(right.getPosition());
184     setRelativeToListener(right.isRelativeToListener());
185     setMinDistance(right.getMinDistance());
186     setAttenuation(right.getAttenuation());
187 
188     return *this;
189 }
190 
191 
192 ////////////////////////////////////////////////////////////
getStatus() const193 SoundSource::Status SoundSource::getStatus() const
194 {
195     ALint status;
196     alCheck(alGetSourcei(m_source, AL_SOURCE_STATE, &status));
197 
198     switch (status)
199     {
200         case AL_INITIAL:
201         case AL_STOPPED: return Stopped;
202         case AL_PAUSED:  return Paused;
203         case AL_PLAYING: return Playing;
204     }
205 
206     return Stopped;
207 }
208 
209 } // namespace sf
210