00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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 }