1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
4 // This file is part of Scorched3D.
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>
24 #include <OpenAL/al.h>
29 SoundSource::SoundSource() :
30 source_(0), buffer_(0)
34 SoundSource::~SoundSource()
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
)
52 alSourcei(source_
, AL_SOURCE_RELATIVE
, (relative
?AL_TRUE
:AL_FALSE
));
55 void SoundSource::setPosition(Vector
&position
)
58 alSourcefv(source_
, AL_POSITION
, position
);
61 void SoundSource::setVelocity(Vector
&velocity
)
64 alSourcefv(source_
, AL_VELOCITY
, velocity
);
67 void SoundSource::setGain(float gain
)
70 alSourcef(source_
, AL_GAIN
, gain
);
73 void SoundSource::setReferenceDistance(float refDist
)
76 alSourcef(source_
, AL_REFERENCE_DISTANCE
, refDist
);
79 void SoundSource::setRolloff(float rolloff
)
82 alSourcef(source_
, AL_ROLLOFF_FACTOR
, rolloff
);
85 void SoundSource::play(SoundBuffer
*buffer
, bool repeat
)
87 if (!buffer
|| !source_
) return;
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;
115 void SoundSource::destroy()
117 if (!source_
) return;
120 alDeleteSources(1, &source_
);
123 bool SoundSource::create()
129 alGenSources(1, &source_
);
130 if ((error
= alGetError()) != AL_NO_ERROR
)
135 alSourcef(source_
, AL_REFERENCE_DISTANCE
, 75.0f
);