00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <sound/VirtualSoundSource.h>
00022 #include <sound/PlayingSoundSource.h>
00023 #include <sound/SoundSource.h>
00024 #include <sound/Sound.h>
00025
00026 VirtualSoundSource::VirtualSoundSource(
00027 unsigned int priority, bool looping, bool managed) :
00028 playingSource_(0),
00029 priority_(priority),
00030 gain_(1.0f), refDist_(75.0f), rolloff_(1.0f),
00031 buffer_(0),
00032 relative_(false),
00033 managed_(managed),
00034 looping_(looping)
00035 {
00036 if (managed_) Sound::instance()->addManaged(this);
00037 }
00038
00039 VirtualSoundSource::~VirtualSoundSource()
00040 {
00041 stop();
00042 playingSource_ = 0;
00043 }
00044
00045 void VirtualSoundSource::play(SoundBuffer *buffer)
00046 {
00047 stop();
00048
00049 buffer_ = buffer;
00050 Sound::instance()->addPlaying(this);
00051 }
00052
00053 void VirtualSoundSource::actualPlay()
00054 {
00055 playingSource_->getActualSource()->setGain(gain_);
00056 playingSource_->getActualSource()->setRolloff(rolloff_);
00057 playingSource_->getActualSource()->setReferenceDistance(refDist_);
00058 playingSource_->getActualSource()->setRelative(relative_);
00059 playingSource_->getActualSource()->setPosition(position_);
00060 playingSource_->getActualSource()->setVelocity(velocity_);
00061 playingSource_->getActualSource()->play(buffer_, looping_);
00062 }
00063
00064 void VirtualSoundSource::stop()
00065 {
00066 if (playingSource_)
00067 {
00068 Sound::instance()->removePlaying(this);
00069 }
00070 }
00071
00072 void VirtualSoundSource::setPlayingSource(PlayingSoundSource *s)
00073 {
00074 playingSource_ = s;
00075 }
00076
00077 bool VirtualSoundSource::getPlaying()
00078 {
00079 if (playingSource_ && playingSource_->getActualSource())
00080 {
00081 return playingSource_->getActualSource()->getPlaying();
00082 }
00083 return false;
00084 }
00085
00086 void VirtualSoundSource::setRelative()
00087 {
00088 relative_ = true;
00089 if (playingSource_ && playingSource_->getActualSource())
00090 {
00091 playingSource_->getActualSource()->setRelative(true);
00092 }
00093 }
00094
00095 void VirtualSoundSource::setPosition(Vector &position)
00096 {
00097 position_ = position;
00098 if (playingSource_ && playingSource_->getActualSource())
00099 {
00100 playingSource_->getActualSource()->setPosition(position);
00101 }
00102 }
00103
00104 void VirtualSoundSource::setVelocity(Vector &velocity)
00105 {
00106 velocity_ = velocity;
00107 if (playingSource_ && playingSource_->getActualSource())
00108 {
00109 playingSource_->getActualSource()->setVelocity(velocity);
00110 }
00111 }
00112
00113 void VirtualSoundSource::setGain(float gain)
00114 {
00115 gain_ = gain;
00116 if (playingSource_ && playingSource_->getActualSource())
00117 {
00118 playingSource_->getActualSource()->setGain(gain);
00119 }
00120 }
00121
00122 void VirtualSoundSource::setReferenceDistance(float refDist)
00123 {
00124 refDist_ = refDist;
00125 if (playingSource_ && playingSource_->getActualSource())
00126 {
00127 playingSource_->getActualSource()->setReferenceDistance(refDist);
00128 }
00129 }
00130
00131 void VirtualSoundSource::setRolloff(float rolloff)
00132 {
00133 rolloff_ = rolloff;
00134 if (playingSource_ && playingSource_->getActualSource())
00135 {
00136 playingSource_->getActualSource()->setRolloff(rolloff);
00137 }
00138 }
00139
00140 void VirtualSoundSource::updateDistance(Vector &listener)
00141 {
00142 Vector pos = getPosition();
00143 if (!getRelative())
00144 {
00145 pos -= listener;
00146 }
00147 distance_ = pos[0] * pos[0] + pos[1] * pos[1] + pos[2] * pos[2];
00148 }