- Main Page
- Namespaces
- Classes
- Files

- File List

# Sound.cpp

    00001
    00002 //
    00003 // SFML - Simple and Fast Multimedia Library
    00004 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
    00005 //
    00006 // This software is provided 'as-is', without any express or implied warranty.
    00007 // In no event will the authors be held liable for any damages arising from the use of this software.
    00008 //
    00009 // Permission is granted to anyone to use this software for any purpose,
    00010 // including commercial applications, and to alter it and redistribute it freely,
    00011 // subject to the following restrictions:
    00012 //
    00013 // 1. The origin of this software must not be misrepresented;
    00014 //    you must not claim that you wrote the original software.
    00015 //    If you use this software in a product, an acknowledgment
    00016 //    in the product documentation would be appreciated but is not required.
    00017 //
    00018 // 2. Altered source versions must be plainly marked as such,
    00019 //    and must not be misrepresented as being the original software.
    00020 //
    00021 // 3. This notice may not be removed or altered from any source distribution.
    00022 //
    00024
    00026 // Headers
    00028 #include <SFML/Audio/Sound.hpp>
    00029 #include <SFML/Audio/SoundBuffer.hpp>
    00030 #include <SFML/Audio/OpenAL.hpp>
    00031
    00032
    00033 namespace sf
    00034 {
    00038 Sound::Sound()
    00039 {
    00040     ALCheck(alGenSources(1, &mySource));
    00041     ALCheck(alSourcei(mySource, AL_BUFFER, 0));
    00042 }
    00043
    00044
    00048 Sound::Sound(const SoundBuffer& Buffer, bool Loop, float Pitch, float Volume, const Vector3f& Position) :
    00049 myBuffer(&Buffer)
    00050 {
    00051     ALCheck(alGenSources(1, &mySource));
    00052
    00053     ALCheck(alSourcei (mySource, AL_BUFFER,   Buffer.myBuffer));
    00054     ALCheck(alSourcei (mySource, AL_LOOPING,  Loop));
    00055     ALCheck(alSourcef (mySource, AL_PITCH,    Pitch));
    00056     ALCheck(alSourcef (mySource, AL_GAIN,     Volume * 0.01f));
    00057     ALCheck(alSource3f(mySource, AL_POSITION, Position.x, Position.y, Position.z));
    00058 }
    00059
    00060
    00064 Sound::Sound(const Sound& Copy) :
    00065 AudioResource(Copy),
    00066 myBuffer     (Copy.myBuffer)
    00067 {
    00068     ALCheck(alGenSources(1, &mySource));
    00069
    00070     ALCheck(alSourcei (mySource, AL_BUFFER,   myBuffer ? myBuffer->myBuffer : 0));
    00071     ALCheck(alSourcei (mySource, AL_LOOPING,  Copy.GetLoop()));
    00072     ALCheck(alSourcef (mySource, AL_PITCH,    Copy.GetPitch()));
    00073     ALCheck(alSourcef (mySource, AL_GAIN,     Copy.GetVolume() * 0.01f));
    00074     ALCheck(alSource3f(mySource, AL_POSITION, Copy.GetPosition().x, Copy.GetPosition().y, Copy.GetPosition().z));
    00075 }
    00076
    00077
    00081 Sound::~Sound()
    00082 {
    00083     if (mySource)
    00084     {
    00085         if (myBuffer)
    00086         {
    00087             Stop();
    00088             ALCheck(alSourcei(mySource, AL_BUFFER, 0));
    00089         }
    00090         ALCheck(alDeleteSources(1, &mySource));
    00091     }
    00092 }
    00093
    00094
    00098 void Sound::Play()
    00099 {
    00100     ALCheck(alSourcePlay(mySource));
    00101 }
    00102
    00103
    00107 void Sound::Pause()
    00108 {
    00109     ALCheck(alSourcePause(mySource));
    00110 }
    00111
    00112
    00116 void Sound::Stop()
    00117 {
    00118     ALCheck(alSourceStop(mySource));
    00119 }
    00120
    00121
    00125 void Sound::SetBuffer(const SoundBuffer& Buffer)
    00126 {
    00127     myBuffer = &Buffer;
    00128     ALCheck(alSourcei(mySource, AL_BUFFER, myBuffer ? myBuffer->myBuffer : 0));
    00129 }
    00130
    00131
    00135 void Sound::SetLoop(bool Loop)
    00136 {
    00137     ALCheck(alSourcei(mySource, AL_LOOPING, Loop));
    00138 }
    00139
    00140
    00144 void Sound::SetPitch(float Pitch)
    00145 {
    00146     ALCheck(alSourcef(mySource, AL_PITCH, Pitch));
    00147 }
    00148
    00149
    00153 void Sound::SetVolume(float Volume)
    00154 {
    00155     ALCheck(alSourcef(mySource, AL_GAIN, Volume * 0.01f));
    00156 }
    00157
    00162 void Sound::SetPosition(float X, float Y, float Z)
    00163 {
    00164     ALCheck(alSource3f(mySource, AL_POSITION, X, Y, Z));
    00165 }
    00166
    00167
    00172 void Sound::SetPosition(const Vector3f& Position)
    00173 {
    00174     SetPosition(Position.x, Position.y, Position.z);
    00175 }
    00176
    00177
    00183 void Sound::SetRelativeToListener(bool Relative)
    00184 {
    00185     ALCheck(alSourcei(mySource, AL_SOURCE_RELATIVE, Relative));
    00186 }
    00187
    00188
    00194 void Sound::SetMinDistance(float MinDistance)
    00195 {
    00196     ALCheck(alSourcef(mySource, AL_REFERENCE_DISTANCE, MinDistance));
    00197 }
    00198
    00199
    00205 void Sound::SetAttenuation(float Attenuation)
    00206 {
    00207     ALCheck(alSourcef(mySource, AL_ROLLOFF_FACTOR, Attenuation));
    00208 }
    00209
    00210
    00214 void Sound::SetPlayingOffset(float TimeOffset)
    00215 {
    00216     ALCheck(alSourcef(mySource, AL_SEC_OFFSET, TimeOffset));
    00217 }
    00218
    00219
    00223 const SoundBuffer* Sound::GetBuffer() const
    00224 {
    00225     return myBuffer;
    00226 }
    00227
    00228
    00232 bool Sound::GetLoop() const
    00233 {
    00234     ALint Loop;
    00235     ALCheck(alGetSourcei(mySource, AL_LOOPING, &Loop));
    00236
    00237     return Loop != 0;
    00238 }
    00239
    00240
    00244 float Sound::GetPitch() const
    00245 {
    00246     ALfloat Pitch;
    00247     ALCheck(alGetSourcef(mySource, AL_PITCH, &Pitch));
    00248
    00249     return Pitch;
    00250 }
    00251
    00252
    00256 float Sound::GetVolume() const
    00257 {
    00258     ALfloat Gain;
    00259     ALCheck(alGetSourcef(mySource, AL_GAIN, &Gain));
    00260
    00261     return Gain * 100.f;
    00262 }
    00263
    00264
    00268 Vector3f Sound::GetPosition() const
    00269 {
    00270     Vector3f Position;
    00271     ALCheck(alGetSource3f(mySource, AL_POSITION, &Position.x, &Position.y, &Position.z));
    00272
    00273     return Position;
    00274 }
    00275
    00276
    00281 bool Sound::IsRelativeToListener() const
    00282 {
    00283     ALint Relative;
    00284     ALCheck(alGetSourcei(mySource, AL_SOURCE_RELATIVE, &Relative));
    00285
    00286     return Relative != 0;
    00287 }
    00288
    00289
    00293 float Sound::GetMinDistance() const
    00294 {
    00295     ALfloat MinDistance;
    00296     ALCheck(alGetSourcef(mySource, AL_REFERENCE_DISTANCE, &MinDistance));
    00297
    00298     return MinDistance;
    00299 }
    00300
    00301
    00305 float Sound::GetAttenuation() const
    00306 {
    00307     ALfloat Attenuation;
    00308     ALCheck(alGetSourcef(mySource, AL_ROLLOFF_FACTOR, &Attenuation));
    00309
    00310     return Attenuation;
    00311 }
    00312
    00313
    00317 float Sound::GetPlayingOffset() const
    00318 {
    00319     ALfloat Seconds = 0.f;
    00320     ALCheck(alGetSourcef(mySource, AL_SEC_OFFSET, &Seconds));
    00321
    00322     return Seconds;
    00323 }
    00324
    00325
    00329 Sound::Status Sound::GetStatus() const
    00330 {
    00331     ALint State;
    00332     ALCheck(alGetSourcei(mySource, AL_SOURCE_STATE, &State));
    00333
    00334     switch (State)
    00335     {
    00336         case AL_INITIAL :
    00337         case AL_STOPPED : return Stopped;
    00338         case AL_PAUSED :  return Paused;
    00339         case AL_PLAYING : return Playing;
    00340     }
    00341
    00342     return Stopped;
    00343 }
    00344
    00345
    00349 Sound& Sound::operator =(const Sound& Other)
    00350 {
    00351     Sound Temp(Other);
    00352
    00353     std::swap(mySource, Temp.mySource);
    00354     std::swap(myBuffer, Temp.myBuffer);
    00355
    00356     return *this;
    00357 }
    00358
    00359 } // namespace sf

:: Copyright © 2007-2008 Laurent Gomila, all rights reserved :: Documentation generated by doxygen 1.5.2 ::