summaryrefslogtreecommitdiffstats
path: root/src/audio/oal
diff options
context:
space:
mode:
Diffstat (limited to 'src/audio/oal')
-rw-r--r--src/audio/oal/aldlist.cpp26
-rw-r--r--src/audio/oal/aldlist.h15
-rw-r--r--src/audio/oal/channel.cpp93
-rw-r--r--src/audio/oal/channel.h7
-rw-r--r--src/audio/oal/stream.cpp96
-rw-r--r--src/audio/oal/stream.h5
6 files changed, 172 insertions, 70 deletions
diff --git a/src/audio/oal/aldlist.cpp b/src/audio/oal/aldlist.cpp
index 881418c1..6024adf2 100644
--- a/src/audio/oal/aldlist.cpp
+++ b/src/audio/oal/aldlist.cpp
@@ -24,12 +24,6 @@
#include "aldlist.h"
-#ifndef _WIN32
-#define _stricmp strcasecmp
-#define _strnicmp strncasecmp
-#define _strdup strdup
-#endif
-
#ifdef AUDIO_OAL
/*
* Init call
@@ -47,8 +41,8 @@ ALDeviceList::ALDeviceList()
defaultDeviceIndex = 0;
if (alcIsExtensionPresent(NULL, "ALC_ENUMERATION_EXT")) {
- devices = (char *)alcGetString(NULL, ALC_DEVICE_SPECIFIER);
- defaultDeviceName = (char *)alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
+ devices = (char *)alcGetString(NULL, ALC_ALL_DEVICES_SPECIFIER);
+ defaultDeviceName = (char *)alcGetString(NULL, ALC_DEFAULT_ALL_DEVICES_SPECIFIER);
index = 0;
// go through device list (each device terminated with a single NULL, list terminated with double NULL)
@@ -62,17 +56,11 @@ ALDeviceList::ALDeviceList()
if (context) {
alcMakeContextCurrent(context);
// if new actual device name isn't already in the list, then add it...
- actualDeviceName = alcGetString(device, ALC_DEVICE_SPECIFIER);
- bool bNewName = true;
- for (unsigned int i = 0; i < GetNumDevices(); i++) {
- if (strcmp(GetDeviceName(i), actualDeviceName) == 0) {
- bNewName = false;
- }
- }
- if ((bNewName) && (actualDeviceName != NULL) && (strlen(actualDeviceName) > 0)) {
- ALDEVICEINFO ALDeviceInfo;
+ actualDeviceName = alcGetString(device, ALC_ALL_DEVICES_SPECIFIER);
+ if ((actualDeviceName != NULL) && (strlen(actualDeviceName) > 0)) {
+ ALDEVICEINFO &ALDeviceInfo = aDeviceInfo[nNumOfDevices++];
ALDeviceInfo.bSelected = true;
- ALDeviceInfo.strDeviceName = _strdup(actualDeviceName);
+ ALDeviceInfo.SetName(actualDeviceName);
alcGetIntegerv(device, ALC_MAJOR_VERSION, sizeof(int), &ALDeviceInfo.iMajorVersion);
alcGetIntegerv(device, ALC_MINOR_VERSION, sizeof(int), &ALDeviceInfo.iMinorVersion);
@@ -105,8 +93,6 @@ ALDeviceList::ALDeviceList()
// Get Source Count
ALDeviceInfo.uiSourceCount = GetMaxNumSources();
-
- aDeviceInfo[nNumOfDevices++] = ALDeviceInfo;
}
alcMakeContextCurrent(NULL);
alcDestroyContext(context);
diff --git a/src/audio/oal/aldlist.h b/src/audio/oal/aldlist.h
index 417bd314..bebb6791 100644
--- a/src/audio/oal/aldlist.h
+++ b/src/audio/oal/aldlist.h
@@ -21,7 +21,7 @@ enum
};
struct ALDEVICEINFO {
- const char *strDeviceName;
+ char *strDeviceName;
int iMajorVersion;
int iMinorVersion;
unsigned int uiSourceCount;
@@ -33,6 +33,19 @@ struct ALDEVICEINFO {
strDeviceName = NULL;
Extensions = 0;
}
+
+ ~ALDEVICEINFO()
+ {
+ delete[] strDeviceName;
+ strDeviceName = NULL;
+ }
+
+ void SetName(const char *name)
+ {
+ if(strDeviceName) delete[] strDeviceName;
+ strDeviceName = new char[strlen(name) + 1];
+ strcpy(strDeviceName, name);
+ }
};
typedef ALDEVICEINFO *LPALDEVICEINFO;
diff --git a/src/audio/oal/channel.cpp b/src/audio/oal/channel.cpp
index 673a4aed..04e7e529 100644
--- a/src/audio/oal/channel.cpp
+++ b/src/audio/oal/channel.cpp
@@ -10,18 +10,22 @@
extern bool IsFXSupported();
-ALuint alSources[MAXCHANNELS+MAX2DCHANNELS];
-ALuint alFilters[MAXCHANNELS+MAX2DCHANNELS];
-ALuint alBuffers[MAXCHANNELS+MAX2DCHANNELS];
+ALuint alSources[NUM_CHANNELS];
+ALuint alFilters[NUM_CHANNELS];
+ALuint alBuffers[NUM_CHANNELS];
bool bChannelsCreated = false;
+int32 CChannel::channelsThatNeedService = 0;
+
+uint8 tempStereoBuffer[PED_BLOCKSIZE * 2];
+
void
CChannel::InitChannels()
{
- alGenSources(MAXCHANNELS+MAX2DCHANNELS, alSources);
- alGenBuffers(MAXCHANNELS+MAX2DCHANNELS, alBuffers);
+ alGenSources(NUM_CHANNELS, alSources);
+ alGenBuffers(NUM_CHANNELS, alBuffers);
if (IsFXSupported())
- alGenFilters(MAXCHANNELS + MAX2DCHANNELS, alFilters);
+ alGenFilters(NUM_CHANNELS, alFilters);
bChannelsCreated = true;
}
@@ -30,13 +34,13 @@ CChannel::DestroyChannels()
{
if (bChannelsCreated)
{
- alDeleteSources(MAXCHANNELS + MAX2DCHANNELS, alSources);
+ alDeleteSources(NUM_CHANNELS, alSources);
memset(alSources, 0, sizeof(alSources));
- alDeleteBuffers(MAXCHANNELS + MAX2DCHANNELS, alBuffers);
+ alDeleteBuffers(NUM_CHANNELS, alBuffers);
memset(alBuffers, 0, sizeof(alBuffers));
if (IsFXSupported())
{
- alDeleteFilters(MAXCHANNELS + MAX2DCHANNELS, alFilters);
+ alDeleteFilters(NUM_CHANNELS, alFilters);
memset(alFilters, 0, sizeof(alFilters));
}
bChannelsCreated = false;
@@ -48,6 +52,7 @@ CChannel::CChannel()
{
Data = nil;
DataSize = 0;
+ bIs2D = false;
SetDefault();
}
@@ -59,7 +64,9 @@ void CChannel::SetDefault()
Position[0] = 0.0f; Position[1] = 0.0f; Position[2] = 0.0f;
Distances[0] = 0.0f; Distances[1] = FLT_MAX;
- LoopCount = 1;
+
+ LoopCount = 1;
+ LastProcessedOffset = UINT32_MAX;
LoopPoints[0] = 0; LoopPoints[1] = -1;
Frequency = MAX_FREQ;
@@ -67,6 +74,10 @@ void CChannel::SetDefault()
void CChannel::Reset()
{
+ // Here is safe because ctor don't call this
+ if (LoopCount > 1)
+ channelsThatNeedService--;
+
ClearBuffer();
SetDefault();
}
@@ -82,6 +93,7 @@ void CChannel::Init(uint32 _id, bool Is2D)
if ( Is2D )
{
+ bIs2D = true;
alSource3f(alSources[id], AL_POSITION, 0.0f, 0.0f, 0.0f);
alSourcef(alSources[id], AL_GAIN, 1.0f);
}
@@ -105,7 +117,20 @@ void CChannel::Start()
if ( !HasSource() ) return;
if ( !Data ) return;
- alBufferData(alBuffers[id], AL_FORMAT_MONO16, Data, DataSize, Frequency);
+ if ( bIs2D )
+ {
+ // convert mono data to stereo
+ int16 *monoData = (int16*)Data;
+ int16 *stereoData = (int16*)tempStereoBuffer;
+ for (size_t i = 0; i < DataSize / 2; i++)
+ {
+ *(stereoData++) = *monoData;
+ *(stereoData++) = *(monoData++);
+ }
+ alBufferData(alBuffers[id], AL_FORMAT_STEREO16, tempStereoBuffer, DataSize * 2, Frequency);
+ }
+ else
+ alBufferData(alBuffers[id], AL_FORMAT_MONO16, Data, DataSize, Frequency);
if ( LoopPoints[0] != 0 && LoopPoints[0] != -1 )
alBufferiv(alBuffers[id], AL_LOOP_POINTS_SOFT, LoopPoints);
alSourcei(alSources[id], AL_BUFFER, alBuffers[id]);
@@ -165,10 +190,51 @@ void CChannel::SetCurrentFreq(uint32 freq)
SetPitch(ALfloat(freq) / Frequency);
}
-void CChannel::SetLoopCount(int32 loopCount) // fake. TODO:
+void CChannel::SetLoopCount(int32 count)
{
if ( !HasSource() ) return;
- alSourcei(alSources[id], AL_LOOPING, loopCount == 1 ? AL_FALSE : AL_TRUE);
+
+ // 0: loop indefinitely, 1: play one time, 2: play two times etc...
+ // only > 1 needs manual processing
+
+ if (LoopCount > 1 && count < 2)
+ channelsThatNeedService--;
+ else if (LoopCount < 2 && count > 1)
+ channelsThatNeedService++;
+
+ alSourcei(alSources[id], AL_LOOPING, count == 1 ? AL_FALSE : AL_TRUE);
+ LoopCount = count;
+}
+
+bool CChannel::Update()
+{
+ if (!HasSource()) return false;
+ if (LoopCount < 2) return false;
+
+ ALint state;
+ alGetSourcei(alSources[id], AL_SOURCE_STATE, &state);
+ if (state == AL_STOPPED) {
+ debug("Looping channels(%d in this case) shouldn't report AL_STOPPED, but nvm\n", id);
+ SetLoopCount(1);
+ return true;
+ }
+
+ assert(channelsThatNeedService > 0 && "Ref counting is broken");
+
+ ALint offset;
+ alGetSourcei(alSources[id], AL_SAMPLE_OFFSET, &offset);
+
+ // Rewound
+ if (offset < LastProcessedOffset) {
+ LoopCount--;
+ if (LoopCount == 1) {
+ // Playing last tune...
+ channelsThatNeedService--;
+ alSourcei(alSources[id], AL_LOOPING, AL_FALSE);
+ }
+ }
+ LastProcessedOffset = offset;
+ return true;
}
void CChannel::SetLoopPoints(ALint start, ALint end)
@@ -200,6 +266,7 @@ void CChannel::SetPan(int32 pan)
void CChannel::ClearBuffer()
{
if ( !HasSource() ) return;
+ alSourcei(alSources[id], AL_LOOPING, AL_FALSE);
alSourcei(alSources[id], AL_BUFFER, AL_NONE);
Data = nil;
DataSize = 0;
diff --git a/src/audio/oal/channel.h b/src/audio/oal/channel.h
index 81817a32..872646c8 100644
--- a/src/audio/oal/channel.h
+++ b/src/audio/oal/channel.h
@@ -19,7 +19,11 @@ class CChannel
float Distances[2];
int32 LoopCount;
ALint LoopPoints[2];
+ ALint LastProcessedOffset;
+ bool bIs2D;
public:
+ static int32 channelsThatNeedService;
+
static void InitChannels();
static void DestroyChannels();
@@ -37,7 +41,7 @@ public:
void SetVolume(int32 vol);
void SetSampleData(void *_data, size_t _DataSize, int32 freq);
void SetCurrentFreq(uint32 freq);
- void SetLoopCount(int32 loopCount); // fake
+ void SetLoopCount(int32 count);
void SetLoopPoints(ALint start, ALint end);
void SetPosition(float x, float y, float z);
void SetDistances(float max, float min);
@@ -45,6 +49,7 @@ public:
void ClearBuffer();
void SetReverbMix(ALuint slot, float mix);
void UpdateReverb(ALuint slot);
+ bool Update();
};
#endif \ No newline at end of file
diff --git a/src/audio/oal/stream.cpp b/src/audio/oal/stream.cpp
index 19fa4ec4..ed73e940 100644
--- a/src/audio/oal/stream.cpp
+++ b/src/audio/oal/stream.cpp
@@ -464,8 +464,8 @@ public:
#endif
#ifdef AUDIO_OAL_USE_MPG123
-// fuzzy seek eliminates stutter when playing ADF but spams errors a lot (nothing breaks though)
-#define MP3_USE_FUZZY_SEEK
+// fuzzy seek eliminates stutter when playing ADF but spams errors a lot (and breaks radio sometimes)
+//#define MP3_USE_FUZZY_SEEK
class CMP3File : public IDecoder
{
@@ -492,6 +492,8 @@ public:
{
#ifdef MP3_USE_FUZZY_SEEK
mpg123_param(m_pMH, MPG123_FLAGS, MPG123_FUZZY | MPG123_SEEKBUFFER | MPG123_GAPLESS | MPG123_QUIET, 0.0);
+#else
+ mpg123_param(m_pMH, MPG123_FLAGS, MPG123_SEEKBUFFER | MPG123_GAPLESS, 0.0);
#endif
long rate = 0;
int channels = 0;
@@ -499,6 +501,7 @@ public:
m_bOpened = mpg123_open(m_pMH, path) == MPG123_OK
&& mpg123_getformat(m_pMH, &rate, &channels, &encoding) == MPG123_OK;
+
m_nRate = rate;
m_nChannels = channels;
@@ -601,6 +604,8 @@ public:
{
#ifdef MP3_USE_FUZZY_SEEK
mpg123_param(m_pMH, MPG123_FLAGS, MPG123_FUZZY | MPG123_SEEKBUFFER | MPG123_GAPLESS | MPG123_QUIET, 0.0);
+#else
+ mpg123_param(m_pMH, MPG123_FLAGS, MPG123_SEEKBUFFER | MPG123_GAPLESS, 0.0);
#endif
long rate = 0;
int channels = 0;
@@ -980,7 +985,8 @@ CStream::CStream(char *filename, ALuint *sources, ALuint (&buffers)[NUM_STREAMBU
m_bReset(false),
m_nVolume(0),
m_nPan(0),
- m_nPosBeforeReset(0)
+ m_nPosBeforeReset(0),
+ m_nLoopCount(1)
{
// Be case-insensitive on linux (from https://github.com/OneSadCookie/fcaseopen/)
@@ -1078,7 +1084,7 @@ bool CStream::IsPlaying()
ALint sourceState[2];
alGetSourcei(m_pAlSources[0], AL_SOURCE_STATE, &sourceState[0]);
alGetSourcei(m_pAlSources[1], AL_SOURCE_STATE, &sourceState[1]);
- if ( m_bActive || sourceState[0] == AL_PLAYING || sourceState[1] == AL_PLAYING)
+ if (sourceState[0] == AL_PLAYING || sourceState[1] == AL_PLAYING)
return true;
}
@@ -1150,6 +1156,7 @@ void CStream::SetPan(uint8 nPan)
m_nPan = nPan;
}
+// Should only be called if source is stopped
void CStream::SetPosMS(uint32 nPos)
{
if ( !IsOpened() ) return;
@@ -1232,10 +1239,16 @@ void CStream::ClearBuffers()
alSourceUnqueueBuffers(m_pAlSources[1], 1, &value);
}
-bool CStream::Setup()
+bool CStream::Setup(bool imSureQueueIsEmpty)
{
if ( IsOpened() )
{
+ alSourcei(m_pAlSources[0], AL_LOOPING, AL_FALSE);
+ alSourcei(m_pAlSources[1], AL_LOOPING, AL_FALSE);
+ if (!imSureQueueIsEmpty) {
+ SetPlay(false);
+ ClearBuffers();
+ }
m_pSoundFile->Seek(0);
//SetPosition(0.0f, 0.0f, 0.0f);
SetPitch(1.0f);
@@ -1246,6 +1259,13 @@ bool CStream::Setup()
return IsOpened();
}
+void CStream::SetLoopCount(int32 count)
+{
+ if ( !HasSource() ) return;
+
+ m_nLoopCount = count;
+}
+
void CStream::SetPlay(bool state)
{
if ( !HasSource() ) return;
@@ -1305,7 +1325,7 @@ void CStream::Update()
if ( !m_bPaused )
{
- ALint sourceState[2];
+ ALint totalBuffers[2] = { 0, 0 };
ALint buffersProcessed[2] = { 0, 0 };
// Relying a lot on left buffer states in here
@@ -1313,44 +1333,51 @@ void CStream::Update()
do
{
//alSourcef(m_pAlSources[0], AL_ROLLOFF_FACTOR, 0.0f);
- alGetSourcei(m_pAlSources[0], AL_SOURCE_STATE, &sourceState[0]);
+ alGetSourcei(m_pAlSources[0], AL_BUFFERS_QUEUED, &totalBuffers[0]);
alGetSourcei(m_pAlSources[0], AL_BUFFERS_PROCESSED, &buffersProcessed[0]);
//alSourcef(m_pAlSources[1], AL_ROLLOFF_FACTOR, 0.0f);
- alGetSourcei(m_pAlSources[1], AL_SOURCE_STATE, &sourceState[1]);
+ alGetSourcei(m_pAlSources[1], AL_BUFFERS_QUEUED, &totalBuffers[1]);
alGetSourcei(m_pAlSources[1], AL_BUFFERS_PROCESSED, &buffersProcessed[1]);
} while (buffersProcessed[0] != buffersProcessed[1]);
- ALint looping = AL_FALSE;
- alGetSourcei(m_pAlSources[0], AL_LOOPING, &looping);
-
- if ( looping == AL_TRUE )
- {
- TRACE("stream set looping");
- alSourcei(m_pAlSources[0], AL_LOOPING, AL_TRUE);
- alSourcei(m_pAlSources[1], AL_LOOPING, AL_TRUE);
- }
-
assert(buffersProcessed[0] == buffersProcessed[1]);
- while( buffersProcessed[0]-- )
+ // Correcting OpenAL concepts here:
+ // AL_BUFFERS_QUEUED = Number of *all* buffers in queue, including processed, processing and pending
+ // AL_BUFFERS_PROCESSED = Index of the buffer being processing right now. Buffers coming after that(have greater index) are pending buffers.
+ // which means: totalBuffers[0] - buffersProcessed[0] = pending buffers
+
+ bool buffersRefilled = false;
+
+ // We should wait queue to be cleared to loop track, because position calculation relies on queue.
+ if (m_nLoopCount != 1 && m_bActive && totalBuffers[0] == 0)
{
- ALuint buffer[2];
-
- alSourceUnqueueBuffers(m_pAlSources[0], 1, &buffer[0]);
- alSourceUnqueueBuffers(m_pAlSources[1], 1, &buffer[1]);
-
- if (m_bActive && FillBuffer(buffer))
- {
- alSourceQueueBuffers(m_pAlSources[0], 1, &buffer[0]);
- alSourceQueueBuffers(m_pAlSources[1], 1, &buffer[1]);
- }
+ Setup(true);
+ buffersRefilled = FillBuffers() != 0;
+ if (m_nLoopCount != 0)
+ m_nLoopCount--;
}
-
- if ( sourceState[0] != AL_PLAYING )
+ else
{
- alGetSourcei(m_pAlSources[0], AL_BUFFERS_PROCESSED, &buffersProcessed[0]);
- SetPlay(buffersProcessed[0]!=0);
+ while( buffersProcessed[0]-- )
+ {
+ ALuint buffer[2];
+
+ alSourceUnqueueBuffers(m_pAlSources[0], 1, &buffer[0]);
+ alSourceUnqueueBuffers(m_pAlSources[1], 1, &buffer[1]);
+
+ if (m_bActive && FillBuffer(buffer))
+ {
+ buffersRefilled = true;
+ alSourceQueueBuffers(m_pAlSources[0], 1, &buffer[0]);
+ alSourceQueueBuffers(m_pAlSources[1], 1, &buffer[1]);
+ }
+ }
}
+
+ // Two reasons: 1-Source may be starved to audio and stopped itself, 2- We're already waiting it to starve and die for looping track!
+ if (m_bActive && (buffersRefilled || (totalBuffers[1] - buffersProcessed[1] != 0)))
+ SetPlay(true);
}
}
@@ -1358,10 +1385,11 @@ void CStream::ProviderInit()
{
if ( m_bReset )
{
- if ( Setup() )
+ if ( Setup(true) )
{
SetPan(m_nPan);
SetVolume(m_nVolume);
+ SetLoopCount(m_nLoopCount);
SetPosMS(m_nPosBeforeReset);
if (m_bActive)
FillBuffers();
diff --git a/src/audio/oal/stream.h b/src/audio/oal/stream.h
index bcbc5e54..9a2a2fbe 100644
--- a/src/audio/oal/stream.h
+++ b/src/audio/oal/stream.h
@@ -69,6 +69,7 @@ class CStream
uint32 m_nVolume;
uint8 m_nPan;
uint32 m_nPosBeforeReset;
+ int32 m_nLoopCount;
IDecoder *m_pSoundFile;
@@ -99,10 +100,12 @@ public:
uint32 GetPosMS();
uint32 GetLengthMS();
- bool Setup();
+ bool Setup(bool imSureQueueIsEmpty = false);
void Start();
void Stop();
void Update(void);
+ void SetLoopCount(int32);
+
void ProviderInit();
void ProviderTerm();