D:/Programmation/Cpp/SFML/src/SFML/Audio/Sound.cpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007 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 
00036 sfSound::sfSound(const sfSoundBuffer* Buffer, bool Loop, float Pitch, float Volume, float X, float Y, float Z) :
00037 myBuffer(Buffer)
00038 {
00039     ALfloat Position[3];
00040     Position[0] = X;
00041     Position[1] = Y;
00042     Position[2] = Z;
00043 
00044     myImpl = new sf_private::sfSoundImpl(Loop, Pitch, Volume, Position);
00045 
00046     SetBuffer(Buffer);
00047 }
00048 
00049 
00053 void sfSound::Play()
00054 {
00055     CloneIfNotUnique();
00056 
00057     sfALCheck(alSourcePlay(myImpl->Source));
00058 }
00059 
00060 
00064 void sfSound::Pause()
00065 {
00066     CloneIfNotUnique();
00067 
00068     sfALCheck(alSourcePause(myImpl->Source));
00069 }
00070 
00071 
00075 void sfSound::Stop()
00076 {
00077     CloneIfNotUnique();
00078 
00079     sfALCheck(alSourceStop(myImpl->Source));
00080 }
00081 
00082 
00086 void sfSound::SetBuffer(const sfSoundBuffer* Buffer)
00087 {
00088     CloneIfNotUnique();
00089 
00090     myBuffer = Buffer;
00091     if (myBuffer)
00092         sfALCheck(alSourcei(myImpl->Source, AL_BUFFER, myBuffer->GetId()));
00093 }
00094 
00095 
00099 void sfSound::SetLoop(bool Loop)
00100 {
00101     CloneIfNotUnique();
00102 
00103     sfALCheck(alSourcei(myImpl->Source, AL_LOOPING, Loop));
00104 }
00105 
00106 
00110 void sfSound::SetPitch(float Pitch)
00111 {
00112     CloneIfNotUnique();
00113 
00114     sfALCheck(alSourcef(myImpl->Source, AL_PITCH, Pitch));
00115 }
00116 
00117 
00121 void sfSound::SetVolume(float Volume)
00122 {
00123     CloneIfNotUnique();
00124 
00125     sfALCheck(alSourcef(myImpl->Source, AL_GAIN, Volume * 0.01f));
00126 }
00127 
00128 
00132 void sfSound::SetPosition(float X, float Y, float Z)
00133 {
00134     CloneIfNotUnique();
00135 
00136     sfALCheck(alSource3f(myImpl->Source, AL_POSITION, X, Y, Z));
00137 }
00138 
00139 
00143 const sfSoundBuffer* sfSound::GetBuffer() const
00144 {
00145     return myBuffer;
00146 }
00147 
00148 
00152 bool sfSound::GetLoop() const
00153 {
00154     ALint Loop;
00155     sfALCheck(alGetSourcei(myImpl->Source, AL_LOOPING, &Loop));
00156 
00157     return Loop != 0;
00158 }
00159 
00160 
00164 float sfSound::GetPitch() const
00165 {
00166     ALfloat Pitch;
00167     sfALCheck(alGetSourcef(myImpl->Source, AL_PITCH, &Pitch));
00168 
00169     return Pitch;
00170 }
00171 
00172 
00176 float sfSound::GetVolume() const
00177 {
00178     ALfloat Gain;
00179     sfALCheck(alGetSourcef(myImpl->Source, AL_GAIN, &Gain));
00180 
00181     return Gain * 100.f;
00182 }
00183 
00184 
00188 void sfSound::GetPosition(float& X, float& Y, float& Z) const
00189 {
00190     sfALCheck(alGetSource3f(myImpl->Source, AL_POSITION, &X, &Y, &Z));
00191 }
00192 
00193 
00197 sfSoundStatus sfSound::GetStatus() const
00198 {
00199     ALint State;
00200     sfALCheck(alGetSourcei(myImpl->Source, AL_SOURCE_STATE, &State));
00201 
00202     switch (State)
00203     {
00204         case AL_INITIAL :
00205         case AL_STOPPED : return Stopped;
00206         case AL_PAUSED :  return Paused;
00207         case AL_PLAYING : return Playing;
00208     }
00209 
00210     return Stopped;
00211 }
00212 
00213 
00217 float sfSound::GetPlayingOffset() const
00218 {
00219     ALfloat Seconds = 0.f;
00220     sfALCheck(alGetSourcef(myImpl->Source, AL_SEC_OFFSET, &Seconds));
00221 
00222     return Seconds;
00223 }
00224 
00225 
00230 void sfSound::CloneIfNotUnique()
00231 {
00232     if (myImpl.GetRefCount() > 1)
00233     {
00234         // Get the current position
00235         float Position[3];
00236         GetPosition(Position[0], Position[1], Position[2]);
00237 
00238         // Duplicate the implementation
00239         myImpl = new sf_private::sfSoundImpl(GetLoop(), GetPitch(), GetVolume(), Position);
00240 
00241         // Rebind the buffer
00242         SetBuffer(myBuffer);
00243     }
00244 }

Generated for SFML by  doxygen 1.5.2