SoundSource.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <sound/SoundSource.h>
00022 #include <sound/SoundBuffer.h>
00023 #ifdef __DARWIN__
00024 #include <OpenAL/al.h>
00025 #else
00026 #include <AL/al.h>
00027 #endif
00028 
00029 SoundSource::SoundSource() : 
00030         source_(0), buffer_(0)
00031 {
00032 }
00033 
00034 SoundSource::~SoundSource()
00035 {
00036         delete buffer_;
00037         buffer_ = 0;
00038         destroy();
00039 }
00040 
00041 bool SoundSource::getPlaying()
00042 {
00043         if (!source_) return false;
00044         ALint state = AL_STOPPED;
00045         alGetSourcei(source_, AL_SOURCE_STATE, &state);
00046         return (state == AL_PLAYING);
00047 }
00048 
00049 void SoundSource::setRelative(bool relative)
00050 {
00051         if (!source_) return;
00052         alSourcei(source_, AL_SOURCE_RELATIVE, (relative?AL_TRUE:AL_FALSE));
00053 }
00054 
00055 void SoundSource::setPosition(Vector &position)
00056 {
00057         if (!source_) return;
00058         alSourcefv(source_, AL_POSITION, position);
00059 }
00060 
00061 void SoundSource::setVelocity(Vector &velocity)
00062 {
00063         if (!source_) return;
00064         alSourcefv(source_, AL_VELOCITY, velocity);
00065 }
00066 
00067 void SoundSource::setGain(float gain)
00068 {
00069         if (!source_) return;
00070         alSourcef(source_, AL_GAIN, gain);
00071 }
00072 
00073 void SoundSource::setReferenceDistance(float refDist)
00074 {
00075         if (!source_) return;
00076         alSourcef(source_, AL_REFERENCE_DISTANCE, refDist);
00077 }
00078 
00079 void SoundSource::setRolloff(float rolloff)
00080 {
00081         if (!source_) return;
00082         alSourcef(source_, AL_ROLLOFF_FACTOR, rolloff);
00083 }
00084 
00085 void SoundSource::play(SoundBuffer *buffer, bool repeat)
00086 {
00087         if (!buffer || !source_) return;
00088 
00089         stop();
00090         if (buffer_) delete buffer_;
00091         buffer_ = buffer->createSourceInstance(source_);
00092 
00093         buffer_->play(repeat);
00094 }
00095 
00096 void SoundSource::simulate(bool repeat)
00097 {
00098         if (!source_ || !buffer_) return;
00099 
00100         buffer_->simulate(repeat);
00101 }
00102 
00103 void SoundSource::stop()
00104 {
00105         if (!source_ || !buffer_) return;
00106         if (getPlaying()) 
00107         {
00108                 buffer_->stop();
00109         }
00110 
00111         delete buffer_;
00112         buffer_ = 0;
00113 }
00114 
00115 void SoundSource::destroy()
00116 {
00117         if (!source_) return;
00118 
00119         stop();
00120         alDeleteSources(1, &source_);
00121 }
00122 
00123 bool SoundSource::create()
00124 {
00125         unsigned int error;
00126 
00127         // Generate Sources
00128         alGetError();
00129         alGenSources(1, &source_);
00130         if ((error = alGetError()) != AL_NO_ERROR)
00131         {
00132                 return false;
00133         }
00134 
00135         alSourcef(source_, AL_REFERENCE_DISTANCE, 75.0f);
00136         return true;
00137 }

Generated on Mon Feb 16 15:14:47 2009 for Scorched3D by  doxygen 1.5.3