re3/src/audio/oal/channel.cpp

215 lines
3.9 KiB
C++
Raw Normal View History

2020-05-04 17:33:48 +00:00
#include "channel.h"
#ifdef AUDIO_OAL
2020-05-07 06:26:16 +00:00
#include "common.h"
2020-05-04 17:33:48 +00:00
#include "sampman.h"
2020-05-11 02:55:57 +00:00
#ifndef _WIN32
#include <float.h>
#endif
2020-05-04 17:33:48 +00:00
extern bool IsFXSupported();
CChannel::CChannel()
{
2020-05-07 06:26:16 +00:00
alSource = AL_NONE;
2020-05-04 17:33:48 +00:00
alFilter = AL_FILTER_NULL;
SetDefault();
}
void CChannel::SetDefault()
{
2020-05-07 06:26:16 +00:00
alBuffer = AL_NONE;
2020-05-04 17:33:48 +00:00
Pitch = 1.0f;
Gain = 1.0f;
Mix = 0.0f;
Position[0] = 0.0f; Position[1] = 0.0f; Position[2] = 0.0f;
Distances[0] = 0.0f; Distances[1] = FLT_MAX;
LoopCount = 1;
LoopPoints[0] = 0; LoopPoints[1] = -1;
Frequency = MAX_FREQ;
}
void CChannel::Reset()
{
ClearBuffer();
SetDefault();
}
void CChannel::Init(bool Is2D)
{
ASSERT(!HasSource());
2020-05-07 06:26:16 +00:00
alGenSources(1, &alSource);
2020-05-04 17:33:48 +00:00
if ( HasSource() )
{
2020-05-07 06:26:16 +00:00
alSourcei(alSource, AL_SOURCE_RELATIVE, AL_TRUE);
2020-05-04 17:33:48 +00:00
if ( IsFXSupported() )
2020-05-07 06:26:16 +00:00
alSource3i(alSource, AL_AUXILIARY_SEND_FILTER, AL_EFFECTSLOT_NULL, 0, AL_FILTER_NULL);
2020-05-04 17:33:48 +00:00
if ( Is2D )
{
2020-05-07 06:26:16 +00:00
alSource3f(alSource, AL_POSITION, 0.0f, 0.0f, 0.0f);
alSourcef (alSource, AL_GAIN, 1.0f);
2020-05-04 17:33:48 +00:00
}
else
{
if ( IsFXSupported() )
alGenFilters(1,&alFilter);
}
}
}
void CChannel::Term()
{
Stop();
if ( HasSource() )
{
if ( IsFXSupported() )
{
2020-05-07 06:26:16 +00:00
alSource3i(alSource, AL_AUXILIARY_SEND_FILTER, AL_EFFECTSLOT_NULL, 0, AL_FILTER_NULL);
2020-05-04 17:33:48 +00:00
if(alFilter != AL_FILTER_NULL)
alDeleteFilters(1,&alFilter);
}
2020-05-07 06:26:16 +00:00
alDeleteSources(1, &alSource);
2020-05-04 17:33:48 +00:00
}
2020-05-07 06:26:16 +00:00
alSource = AL_NONE;
2020-05-04 17:33:48 +00:00
alFilter = AL_FILTER_NULL;
}
void CChannel::Start()
{
if ( !HasSource() ) return;
if ( LoopPoints[0] != 0 && LoopPoints[0] != -1 )
2020-05-07 06:26:16 +00:00
alBufferiv(alBuffer, AL_LOOP_POINTS_SOFT, LoopPoints);
alSourcei (alSource, AL_BUFFER, alBuffer);
alSourcePlay(alSource);
2020-05-04 17:33:48 +00:00
}
void CChannel::Stop()
{
if ( HasSource() )
2020-05-07 06:26:16 +00:00
alSourceStop(alSource);
2020-05-04 17:33:48 +00:00
Reset();
}
bool CChannel::HasSource()
{
2020-05-07 06:26:16 +00:00
return alSource != AL_NONE;
2020-05-04 17:33:48 +00:00
}
bool CChannel::IsUsed()
{
if ( HasSource() )
{
ALint sourceState;
2020-05-07 06:26:16 +00:00
alGetSourcei(alSource, AL_SOURCE_STATE, &sourceState);
2020-05-04 17:33:48 +00:00
return sourceState == AL_PLAYING;
}
return false;
}
void CChannel::SetPitch(float pitch)
{
if ( !HasSource() ) return;
2020-05-07 06:26:16 +00:00
alSourcef(alSource, AL_PITCH, pitch);
2020-05-04 17:33:48 +00:00
}
void CChannel::SetGain(float gain)
{
if ( !HasSource() ) return;
2020-05-07 06:26:16 +00:00
alSourcef(alSource, AL_GAIN, gain);
2020-05-04 17:33:48 +00:00
}
void CChannel::SetVolume(int32 vol)
{
SetGain(ALfloat(vol) / MAX_VOLUME);
}
void CChannel::SetSampleID(uint32 nSfx)
{
Sample = nSfx;
}
void CChannel::SetFreq(int32 freq)
{
Frequency = freq;
}
void CChannel::SetCurrentFreq(uint32 freq)
{
SetPitch(ALfloat(freq) / Frequency);
}
void CChannel::SetLoopCount(int32 loopCount) // fake. TODO:
{
if ( !HasSource() ) return;
2020-05-07 06:26:16 +00:00
alSourcei(alSource, AL_LOOPING, loopCount == 1 ? AL_FALSE : AL_TRUE);
2020-05-04 17:33:48 +00:00
}
void CChannel::SetLoopPoints(ALint start, ALint end)
{
LoopPoints[0] = start;
LoopPoints[1] = end;
}
void CChannel::SetPosition(float x, float y, float z)
{
if ( !HasSource() ) return;
2020-05-07 06:26:16 +00:00
alSource3f(alSource, AL_POSITION, x, y, z);
2020-05-04 17:33:48 +00:00
}
void CChannel::SetDistances(float max, float min)
{
if ( !HasSource() ) return;
2020-05-07 06:26:16 +00:00
alSourcef (alSource, AL_MAX_DISTANCE, max);
alSourcef (alSource, AL_REFERENCE_DISTANCE, min);
alSourcef (alSource, AL_MAX_GAIN, 1.0f);
alSourcef (alSource, AL_ROLLOFF_FACTOR, 1.0f);
2020-05-04 17:33:48 +00:00
}
void CChannel::SetPan(uint32 pan)
{
2020-05-07 06:26:16 +00:00
SetPosition((pan-63)/64.0f, 0.0f, Sqrt(1.0f-SQR((pan-63)/64.0f)));
2020-05-04 17:33:48 +00:00
}
void CChannel::SetBuffer(ALuint buffer)
{
2020-05-07 06:26:16 +00:00
alBuffer = buffer;
2020-05-04 17:33:48 +00:00
}
void CChannel::ClearBuffer()
{
if ( !HasSource() ) return;
SetBuffer(AL_NONE);
2020-05-07 06:26:16 +00:00
alSourcei(alSource, AL_BUFFER, AL_NONE);
2020-05-04 17:33:48 +00:00
}
void CChannel::SetReverbMix(ALuint slot, float mix)
{
if ( !IsFXSupported() ) return;
if ( !HasSource() ) return;
if ( alFilter == AL_FILTER_NULL ) return;
Mix = mix;
EAX3_SetReverbMix(alFilter, mix);
2020-05-07 06:26:16 +00:00
alSource3i(alSource, AL_AUXILIARY_SEND_FILTER, slot, 0, alFilter);
2020-05-04 17:33:48 +00:00
}
void CChannel::UpdateReverb(ALuint slot)
{
if ( !IsFXSupported() ) return;
if ( !HasSource() ) return;
if ( alFilter == AL_FILTER_NULL ) return;
EAX3_SetReverbMix(alFilter, Mix);
2020-05-07 06:26:16 +00:00
alSource3i(alSource, AL_AUXILIARY_SEND_FILTER, slot, 0, alFilter);
2020-05-04 17:33:48 +00:00
}
2020-05-11 02:55:57 +00:00
#endif