git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / client / sound / SoundSource.cpp
blobefb33fad69e79e478ef3f2e5b6f3213bfb83d933
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
3 //
4 // This file is part of Scorched3D.
5 //
6 // Scorched3D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // Scorched3D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with Scorched3D; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ////////////////////////////////////////////////////////////////////////////////
21 #include <sound/SoundSource.h>
22 #include <sound/SoundBuffer.h>
23 #ifdef __DARWIN__
24 #include <OpenAL/al.h>
25 #else
26 #include <AL/al.h>
27 #endif
29 SoundSource::SoundSource() :
30 source_(0), buffer_(0)
34 SoundSource::~SoundSource()
36 delete buffer_;
37 buffer_ = 0;
38 destroy();
41 bool SoundSource::getPlaying()
43 if (!source_) return false;
44 ALint state = AL_STOPPED;
45 alGetSourcei(source_, AL_SOURCE_STATE, &state);
46 return (state == AL_PLAYING);
49 void SoundSource::setRelative(bool relative)
51 if (!source_) return;
52 alSourcei(source_, AL_SOURCE_RELATIVE, (relative?AL_TRUE:AL_FALSE));
55 void SoundSource::setPosition(Vector &position)
57 if (!source_) return;
58 alSourcefv(source_, AL_POSITION, position);
61 void SoundSource::setVelocity(Vector &velocity)
63 if (!source_) return;
64 alSourcefv(source_, AL_VELOCITY, velocity);
67 void SoundSource::setGain(float gain)
69 if (!source_) return;
70 alSourcef(source_, AL_GAIN, gain);
73 void SoundSource::setReferenceDistance(float refDist)
75 if (!source_) return;
76 alSourcef(source_, AL_REFERENCE_DISTANCE, refDist);
79 void SoundSource::setRolloff(float rolloff)
81 if (!source_) return;
82 alSourcef(source_, AL_ROLLOFF_FACTOR, rolloff);
85 void SoundSource::play(SoundBuffer *buffer, bool repeat)
87 if (!buffer || !source_) return;
89 stop();
90 if (buffer_) delete buffer_;
91 buffer_ = buffer->createSourceInstance(source_);
93 buffer_->play(repeat);
96 void SoundSource::simulate(bool repeat)
98 if (!source_ || !buffer_) return;
100 buffer_->simulate(repeat);
103 void SoundSource::stop()
105 if (!source_ || !buffer_) return;
106 if (getPlaying())
108 buffer_->stop();
111 delete buffer_;
112 buffer_ = 0;
115 void SoundSource::destroy()
117 if (!source_) return;
119 stop();
120 alDeleteSources(1, &source_);
123 bool SoundSource::create()
125 unsigned int error;
127 // Generate Sources
128 alGetError();
129 alGenSources(1, &source_);
130 if ((error = alGetError()) != AL_NO_ERROR)
132 return false;
135 alSourcef(source_, AL_REFERENCE_DISTANCE, 75.0f);
136 return true;