summaryrefslogtreecommitdiffstats
path: root/src/sound/oalsound/channel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/sound/oalsound/channel.cpp')
-rw-r--r--src/sound/oalsound/channel.cpp97
1 files changed, 72 insertions, 25 deletions
diff --git a/src/sound/oalsound/channel.cpp b/src/sound/oalsound/channel.cpp
index 7d8244b..19394c6 100644
--- a/src/sound/oalsound/channel.cpp
+++ b/src/sound/oalsound/channel.cpp
@@ -14,10 +14,10 @@
// * You should have received a copy of the GNU General Public License
// * along with this program. If not, see http://www.gnu.org/licenses/.
-// channel.cpp
#include "channel.h"
+#define MIN(a, b) (a > b ? b : a)
Channel::Channel() {
alGenSources(1, &mSource);
@@ -31,11 +31,17 @@ Channel::Channel() {
mPriority = 0;
mBuffer = nullptr;
+ mLoop = false;
+ mInitFrequency = 0.0f;
+ mStartAmplitude = 0.0f;
+ mStartFrequency = 0.0f;
+ mChangeFrequency = 0.0f;
}
Channel::~Channel() {
if (mReady) {
+ alSourceStop(mSource);
alSourcei(mSource, AL_BUFFER, 0);
alDeleteSources(1, &mSource);
if (alCheck())
@@ -45,9 +51,10 @@ Channel::~Channel() {
bool Channel::Play() {
- if (!mReady)
+ if (!mReady || mBuffer == nullptr)
return false;
-
+
+ alSourcei(mSource, AL_LOOPING, static_cast<ALint>(mLoop));
alSourcePlay(mSource);
if (alCheck())
GetLogger()->Warn("Could not play audio sound source. Code: %d\n", alGetCode());
@@ -56,7 +63,7 @@ bool Channel::Play() {
bool Channel::SetPosition(Math::Vector pos) {
- if (!mReady)
+ if (!mReady || mBuffer == nullptr)
return false;
alSource3f(mSource, AL_POSITION, pos.x, pos.y, pos.z);
@@ -70,7 +77,7 @@ bool Channel::SetPosition(Math::Vector pos) {
bool Channel::SetFrequency(float freq)
{
- if (!mReady)
+ if (!mReady || mBuffer == nullptr)
return false;
alSourcef(mSource, AL_PITCH, freq);
@@ -82,10 +89,19 @@ bool Channel::SetFrequency(float freq)
}
+bool Channel::AdjustFrequency(float freq)
+{
+ if (!mReady || mBuffer == nullptr)
+ return false;
+
+ return SetFrequency(mInitFrequency + fabs(freq));
+}
+
+
float Channel::GetFrequency()
{
ALfloat freq;
- if (!mReady)
+ if (!mReady || mBuffer == nullptr)
return 0;
alGetSourcef(mSource, AL_PITCH, &freq);
@@ -100,10 +116,10 @@ float Channel::GetFrequency()
bool Channel::SetVolume(float vol)
{
- if (!mReady || vol < 0)
+ if (!mReady || vol < 0 || mBuffer == nullptr)
return false;
- alSourcef(mSource, AL_GAIN, vol / MAXVOLUME);
+ alSourcef(mSource, AL_GAIN, MIN(powf(vol, 0.2f), 1.0f));
if (alCheck()) {
GetLogger()->Warn("Could not set sound volume to '%f'. Code: %d\n", vol, alGetCode());
return false;
@@ -115,7 +131,7 @@ bool Channel::SetVolume(float vol)
float Channel::GetVolume()
{
ALfloat vol;
- if (!mReady)
+ if (!mReady || mBuffer == nullptr)
return 0;
alGetSourcef(mSource, AL_GAIN, &vol);
@@ -124,7 +140,7 @@ float Channel::GetVolume()
return 0;
}
- return vol * MAXVOLUME;
+ return vol;
}
@@ -143,6 +159,7 @@ void Channel::SetPriority(int pri)
void Channel::SetStartAmplitude(float gain)
{
mStartAmplitude = gain;
+ SetVolume(mStartAmplitude);
}
@@ -158,12 +175,6 @@ void Channel::SetChangeFrequency(float freq)
}
-void Channel::SetInitFrequency(float freq)
-{
- mInitFrequency = freq;
-}
-
-
float Channel::GetStartAmplitude()
{
return mStartAmplitude;
@@ -201,6 +212,9 @@ void Channel::ResetOper()
Sound Channel::GetSoundType() {
+ if (!mReady || mBuffer == nullptr)
+ return SOUND_NONE;
+
return mBuffer->GetSoundType();
}
@@ -208,9 +222,14 @@ Sound Channel::GetSoundType() {
bool Channel::SetBuffer(Buffer *buffer) {
if (!mReady)
return false;
-
- assert(buffer);
+
+ Stop();
mBuffer = buffer;
+ if (buffer == nullptr) {
+ alSourcei(mSource, AL_BUFFER, 0);
+ return true;
+ }
+
alSourcei(mSource, AL_BUFFER, buffer->GetBuffer());
if (alCheck()) {
GetLogger()->Warn("Could not set sound buffer. Code: %d\n", alGetCode());
@@ -221,19 +240,26 @@ bool Channel::SetBuffer(Buffer *buffer) {
}
-void Channel::AdjustFrequency(float freq) {
- SetFrequency(freq * mInitFrequency);
-}
-
+bool Channel::FreeBuffer() {
+ if (!mReady)
+ return false;
+
+ if (!mBuffer) {
+ return false;
+ }
-void Channel::AdjustVolume(float volume) {
- SetVolume(mStartAmplitude * volume);
+ alSourceStop(mSource);
+ alSourcei(mSource, AL_BUFFER, 0);
+ delete mBuffer;
+ mBuffer = nullptr;
+ return true;
}
bool Channel::IsPlaying() {
ALint status;
- if (!mReady) return false;
+ if (!mReady || mBuffer == nullptr)
+ return false;
alGetSourcei(mSource, AL_SOURCE_STATE, &status);
if (alCheck()) {
@@ -249,8 +275,15 @@ bool Channel::IsReady() {
return mReady;
}
+bool Channel::IsLoaded() {
+ return mBuffer != nullptr;
+}
+
bool Channel::Stop() {
+ if (!mReady || mBuffer == nullptr)
+ return false;
+
alSourceStop(mSource);
if (alCheck()) {
GetLogger()->Warn("Could not stop sound. Code: %d\n", alGetCode());
@@ -262,6 +295,9 @@ bool Channel::Stop() {
float Channel::GetCurrentTime()
{
+ if (!mReady || mBuffer == nullptr)
+ return 0.0f;
+
ALfloat current;
alGetSourcef(mSource, AL_SEC_OFFSET, &current);
if (alCheck()) {
@@ -274,6 +310,9 @@ float Channel::GetCurrentTime()
void Channel::SetCurrentTime(float current)
{
+ if (!mReady || mBuffer == nullptr)
+ return;
+
alSourcef(mSource, AL_SEC_OFFSET, current);
if (alCheck())
GetLogger()->Warn("Could not get source current play time. Code: %d\n", alGetCode());
@@ -282,6 +321,9 @@ void Channel::SetCurrentTime(float current)
float Channel::GetDuration()
{
+ if (!mReady || mBuffer == nullptr)
+ return 0.0f;
+
return mBuffer->GetDuration();
}
@@ -302,3 +344,8 @@ void Channel::PopEnvelope()
{
mOper.pop_front();
}
+
+
+void Channel::SetLoop(bool loop) {
+ mLoop = loop;
+}