SFML logo
  • 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/ALCheck.hpp>
00031 
00032 
00033 namespace sf
00034 {
00036 Sound::Sound()
00037 {
00038 }
00039 
00040 
00042 Sound::Sound(const SoundBuffer& buffer, bool loop, float pitch, float volume, const Vector3f& position) :
00043 myBuffer(NULL)
00044 {
00045     SetBuffer(buffer);
00046     SetLoop(loop);
00047     SetPitch(pitch);
00048     SetVolume(volume);
00049     SetPosition(position);
00050 }
00051 
00052 
00054 Sound::Sound(const Sound& copy) :
00055 SoundSource(copy),
00056 myBuffer   (NULL)
00057 {
00058     if (copy.myBuffer)
00059         SetBuffer(*copy.myBuffer);
00060     SetLoop(copy.GetLoop());
00061 }
00062 
00063 
00065 Sound::~Sound()
00066 {
00067     Stop();
00068     if (myBuffer)
00069         myBuffer->DetachSound(this);
00070 }
00071 
00072 
00074 void Sound::Play()
00075 {
00076     ALCheck(alSourcePlay(mySource));
00077 }
00078 
00079 
00081 void Sound::Pause()
00082 {
00083     ALCheck(alSourcePause(mySource));
00084 }
00085 
00086 
00088 void Sound::Stop()
00089 {
00090     ALCheck(alSourceStop(mySource));
00091 }
00092 
00093 
00095 void Sound::SetBuffer(const SoundBuffer& buffer)
00096 {
00097     // First detach from the previous buffer
00098     if (myBuffer)
00099     {
00100         Stop();
00101         myBuffer->DetachSound(this);
00102     }
00103 
00104     // Assign and use the new buffer
00105     myBuffer = &buffer;
00106     myBuffer->AttachSound(this);
00107     ALCheck(alSourcei(mySource, AL_BUFFER, myBuffer->myBuffer));
00108 }
00109 
00110 
00112 void Sound::SetLoop(bool Loop)
00113 {
00114     ALCheck(alSourcei(mySource, AL_LOOPING, Loop));
00115 }
00116 
00117 
00119 void Sound::SetPlayingOffset(float timeOffset)
00120 {
00121     ALCheck(alSourcef(mySource, AL_SEC_OFFSET, timeOffset));
00122 }
00123 
00124 
00126 const SoundBuffer* Sound::GetBuffer() const
00127 {
00128     return myBuffer;
00129 }
00130 
00131 
00133 bool Sound::GetLoop() const
00134 {
00135     ALint loop;
00136     ALCheck(alGetSourcei(mySource, AL_LOOPING, &loop));
00137 
00138     return loop != 0;
00139 }
00140 
00141 
00143 float Sound::GetPlayingOffset() const
00144 {
00145     ALfloat seconds = 0.f;
00146     ALCheck(alGetSourcef(mySource, AL_SEC_OFFSET, &seconds));
00147 
00148     return seconds;
00149 }
00150 
00151 
00153 Sound::Status Sound::GetStatus() const
00154 {
00155     return SoundSource::GetStatus();
00156 }
00157 
00158 
00160 Sound& Sound::operator =(const Sound& right)
00161 {
00162     // Here we don't use the copy-and-swap idiom, because it would mess up
00163     // the list of sound instances contained in the buffers
00164 
00165     // Detach the sound instance from the previous buffer (if any)
00166     if (myBuffer)
00167     {
00168         Stop();
00169         myBuffer->DetachSound(this);
00170         myBuffer = NULL;
00171     }
00172 
00173     // Copy the sound attributes
00174     if (right.myBuffer)
00175         SetBuffer(*right.myBuffer);
00176     SetLoop(right.GetLoop());
00177     SetPitch(right.GetPitch());
00178     SetVolume(right.GetVolume());
00179     SetPosition(right.GetPosition());
00180     SetRelativeToListener(right.IsRelativeToListener());
00181     SetMinDistance(right.GetMinDistance());
00182     SetAttenuation(right.GetAttenuation());
00183 
00184     return *this;
00185 }
00186 
00187 
00189 void Sound::ResetBuffer()
00190 {
00191     // First stop the sound in case it is playing
00192     Stop();
00193 
00194     // Detach the buffer
00195     ALCheck(alSourcei(mySource, AL_BUFFER, 0));
00196     myBuffer = NULL;
00197 }
00198 
00199 } // namespace sf

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