Now OpenAL works, at least.
But Dolphin's sound stream system really needs a rethink. Because this is the root cause of constant blocking. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@4711 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
parent
7b6a0f9b72
commit
51163196d3
|
@ -171,3 +171,12 @@ void CMixer::PushSamples(short *samples, int num_stereo_samples, int core_sample
|
||||||
}
|
}
|
||||||
push_sync.Leave();
|
push_sync.Leave();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int CMixer::GetNumSamples()
|
||||||
|
{
|
||||||
|
return m_queueSize / 2;
|
||||||
|
//int ret = (m_queueSize - queue_minlength) / 2;
|
||||||
|
//ret = (ret > 0) ? ret : 0;
|
||||||
|
//return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,8 @@ public:
|
||||||
|
|
||||||
// Called from audio threads
|
// Called from audio threads
|
||||||
virtual int Mix(short *sample, int numSamples);
|
virtual int Mix(short *sample, int numSamples);
|
||||||
|
virtual int GetNumSamples();
|
||||||
|
|
||||||
// Called from main thread
|
// Called from main thread
|
||||||
virtual void PushSamples(short* samples, int num_stereo_samples, int core_sample_rate);
|
virtual void PushSamples(short* samples, int num_stereo_samples, int core_sample_rate);
|
||||||
|
|
||||||
|
@ -41,8 +42,6 @@ public:
|
||||||
|
|
||||||
int GetSampleRate() {return m_sampleRate;}
|
int GetSampleRate() {return m_sampleRate;}
|
||||||
|
|
||||||
int GetDataSize() {return m_queueSize;}
|
|
||||||
|
|
||||||
void SetThrottle(bool use) { m_throttle = use;}
|
void SetThrottle(bool use) { m_throttle = use;}
|
||||||
void SetDTKMusic(bool use) { m_EnableDTKMusic = use;}
|
void SetDTKMusic(bool use) { m_EnableDTKMusic = use;}
|
||||||
|
|
||||||
|
|
|
@ -20,18 +20,12 @@
|
||||||
|
|
||||||
#if defined HAVE_OPENAL && HAVE_OPENAL
|
#if defined HAVE_OPENAL && HAVE_OPENAL
|
||||||
|
|
||||||
#define AUDIO_NUMBUFFERS (4)
|
|
||||||
//#define AUDIO_SERVICE_UPDATE_PERIOD (20)
|
|
||||||
|
|
||||||
bool OpenALStream::Start()
|
bool OpenALStream::Start()
|
||||||
{
|
{
|
||||||
ALDeviceList *pDeviceList = NULL;
|
ALDeviceList *pDeviceList = NULL;
|
||||||
ALCcontext *pContext = NULL;
|
ALCcontext *pContext = NULL;
|
||||||
ALCdevice *pDevice = NULL;
|
ALCdevice *pDevice = NULL;
|
||||||
bool bReturn = false;
|
bool bReturn = false;
|
||||||
|
|
||||||
g_uiSource = 0;
|
|
||||||
g_uiBuffers = NULL;
|
|
||||||
|
|
||||||
soundSyncEvent.Init();
|
soundSyncEvent.Init();
|
||||||
|
|
||||||
|
@ -53,13 +47,15 @@ bool OpenALStream::Start()
|
||||||
alcCloseDevice(pDevice);
|
alcCloseDevice(pDevice);
|
||||||
PanicAlert("OpenAL: can't create context for device %s", pDevice);
|
PanicAlert("OpenAL: can't create context for device %s", pDevice);
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
PanicAlert("OpenAL: can't open device %s", pDevice);
|
PanicAlert("OpenAL: can't open device %s", pDevice);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
delete pDeviceList;
|
delete pDeviceList;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
PanicAlert("OpenAL: can't find sound devices");
|
PanicAlert("OpenAL: can't find sound devices");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,57 +64,52 @@ bool OpenALStream::Start()
|
||||||
|
|
||||||
void OpenALStream::Stop()
|
void OpenALStream::Stop()
|
||||||
{
|
{
|
||||||
ALCcontext *pContext;
|
|
||||||
ALCdevice *pDevice;
|
|
||||||
|
|
||||||
threadData = 1;
|
threadData = 1;
|
||||||
// kick the thread if it's waiting
|
// kick the thread if it's waiting
|
||||||
soundSyncEvent.Set();
|
soundSyncEvent.Set();
|
||||||
|
|
||||||
soundCriticalSection.Enter();
|
// AyuanX: Spec says OpenAL1.1 is thread safe already
|
||||||
|
// soundCriticalSection.Enter();
|
||||||
delete thread;
|
delete thread;
|
||||||
thread = NULL;
|
thread = NULL;
|
||||||
|
|
||||||
alSourceStop(g_uiSource);
|
alSourceStop(uiSource);
|
||||||
alSourcei(g_uiSource, AL_BUFFER, 0);
|
alSourcei(uiSource, AL_BUFFER, 0);
|
||||||
|
|
||||||
// Clean up buffers and sources
|
// Clean up buffers and sources
|
||||||
alDeleteSources(1, &g_uiSource);
|
alDeleteSources(1, &uiSource);
|
||||||
alDeleteBuffers(AUDIO_NUMBUFFERS, g_uiBuffers);
|
alDeleteBuffers(OAL_NUM_BUFFERS, uiBuffers);
|
||||||
|
|
||||||
pContext = alcGetCurrentContext();
|
ALCcontext *pContext = alcGetCurrentContext();
|
||||||
pDevice = alcGetContextsDevice(pContext);
|
ALCdevice *pDevice = alcGetContextsDevice(pContext);
|
||||||
|
|
||||||
alcMakeContextCurrent(NULL);
|
alcMakeContextCurrent(NULL);
|
||||||
alcDestroyContext(pContext);
|
alcDestroyContext(pContext);
|
||||||
alcCloseDevice(pDevice);
|
alcCloseDevice(pDevice);
|
||||||
soundCriticalSection.Leave();
|
// soundCriticalSection.Leave();
|
||||||
|
|
||||||
soundSyncEvent.Shutdown();
|
soundSyncEvent.Shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenALStream::Update()
|
void OpenALStream::Update()
|
||||||
{
|
{
|
||||||
//if (m_mixer->GetDataSize()) //here need debug
|
soundSyncEvent.Set();
|
||||||
{
|
|
||||||
soundSyncEvent.Set();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenALStream::Clear(bool mute)
|
void OpenALStream::Clear(bool mute)
|
||||||
{
|
{
|
||||||
m_muted = mute;
|
m_muted = mute;
|
||||||
|
|
||||||
soundCriticalSection.Enter();
|
// soundCriticalSection.Enter();
|
||||||
if(m_muted)
|
if(m_muted)
|
||||||
{
|
{
|
||||||
alSourceStop(g_uiSource);
|
alSourceStop(uiSource);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
alSourcePlay(g_uiSource);
|
alSourcePlay(uiSource);
|
||||||
}
|
}
|
||||||
soundCriticalSection.Leave();
|
// soundCriticalSection.Leave();
|
||||||
}
|
}
|
||||||
|
|
||||||
THREAD_RETURN OpenALStream::ThreadFunc(void* args)
|
THREAD_RETURN OpenALStream::ThreadFunc(void* args)
|
||||||
|
@ -129,64 +120,64 @@ THREAD_RETURN OpenALStream::ThreadFunc(void* args)
|
||||||
|
|
||||||
void OpenALStream::SoundLoop()
|
void OpenALStream::SoundLoop()
|
||||||
{
|
{
|
||||||
ALuint uiBuffers[AUDIO_NUMBUFFERS] = {0};
|
|
||||||
ALuint uiSource = 0;
|
|
||||||
ALenum err;
|
ALenum err;
|
||||||
u32 ulFrequency = m_mixer->GetSampleRate();
|
u32 ulFrequency = m_mixer->GetSampleRate();
|
||||||
|
|
||||||
|
memset(uiBuffers, 0, OAL_NUM_BUFFERS * sizeof(ALuint));
|
||||||
|
uiSource = 0;
|
||||||
|
|
||||||
// Generate some AL Buffers for streaming
|
// Generate some AL Buffers for streaming
|
||||||
alGenBuffers(AUDIO_NUMBUFFERS, (ALuint *)uiBuffers);
|
alGenBuffers(OAL_NUM_BUFFERS, (ALuint *)uiBuffers);
|
||||||
// Generate a Source to playback the Buffers
|
// Generate a Source to playback the Buffers
|
||||||
alGenSources(1, &uiSource);
|
alGenSources(1, &uiSource);
|
||||||
|
|
||||||
memset(realtimeBuffer, 0, OAL_BUFFER_SIZE * sizeof(short));
|
memset(realtimeBuffer, 0, OAL_BUFFER_SIZE);
|
||||||
//*
|
for (int i = 0; i < OAL_NUM_BUFFERS; i++)
|
||||||
for (int iLoop = 0; iLoop < AUDIO_NUMBUFFERS; iLoop++)
|
alBufferData(uiBuffers[i], AL_FORMAT_STEREO16, realtimeBuffer, OAL_BUFFER_SIZE, ulFrequency);
|
||||||
{
|
|
||||||
// pay load fake data
|
|
||||||
alBufferData(uiBuffers[iLoop], AL_FORMAT_STEREO16, realtimeBuffer, 1024, ulFrequency);
|
|
||||||
alSourceQueueBuffers(uiSource, 1, &uiBuffers[iLoop]);
|
|
||||||
}
|
|
||||||
//*/
|
|
||||||
|
|
||||||
g_uiSource = uiSource;
|
|
||||||
g_uiBuffers = (ALuint *)uiBuffers;
|
|
||||||
|
|
||||||
|
alSourceQueueBuffers(uiSource, OAL_NUM_BUFFERS, uiBuffers);
|
||||||
alSourcePlay(uiSource);
|
alSourcePlay(uiSource);
|
||||||
err = alGetError();
|
err = alGetError();
|
||||||
|
// TODO: Error handling
|
||||||
|
|
||||||
|
ALint iBuffersFilled = 0;
|
||||||
|
ALint iBuffersProcessed = 0;
|
||||||
|
ALuint uiBufferTemp[OAL_NUM_BUFFERS] = {0};
|
||||||
|
|
||||||
while (!threadData)
|
while (!threadData)
|
||||||
{
|
{
|
||||||
int numBytesToRender = 32768; //ya, this is a hack, we need real data count
|
// soundCriticalSection.Enter();
|
||||||
/*int numBytesRender =*/ m_mixer->Mix(realtimeBuffer, numBytesToRender >> 2);
|
if (iBuffersProcessed == iBuffersFilled)
|
||||||
|
|
||||||
//if (numBytesRender) //here need debug
|
|
||||||
{
|
{
|
||||||
soundCriticalSection.Enter();
|
|
||||||
ALint iBuffersProcessed = 0;
|
|
||||||
alGetSourcei(uiSource, AL_BUFFERS_PROCESSED, &iBuffersProcessed);
|
alGetSourcei(uiSource, AL_BUFFERS_PROCESSED, &iBuffersProcessed);
|
||||||
|
iBuffersFilled = 0;
|
||||||
if (iBuffersProcessed)
|
|
||||||
{
|
|
||||||
// Remove the Buffer from the Queue. (uiBuffer contains the Buffer ID for the unqueued Buffer)
|
|
||||||
ALuint uiTempBuffer = 0;
|
|
||||||
alSourceUnqueueBuffers(uiSource, 1, &uiTempBuffer);
|
|
||||||
/*
|
|
||||||
soundCriticalSection.Enter();
|
|
||||||
int numBytesToRender = 32768; //ya, this is a hack, we need real data count
|
|
||||||
m_mixer->Mix(realtimeBuffer, numBytesToRender >> 2);
|
|
||||||
soundCriticalSection.Leave();
|
|
||||||
|
|
||||||
unsigned long ulBytesWritten = 0;
|
|
||||||
*/
|
|
||||||
//if (numBytesRender)
|
|
||||||
{
|
|
||||||
alBufferData(uiTempBuffer, AL_FORMAT_STEREO16, realtimeBuffer, numBytesToRender, ulFrequency);
|
|
||||||
}
|
|
||||||
alSourceQueueBuffers(uiSource, 1, &uiTempBuffer);
|
|
||||||
}
|
|
||||||
soundCriticalSection.Leave();
|
|
||||||
}
|
}
|
||||||
|
int numSamples = m_mixer->GetNumSamples();
|
||||||
|
numSamples &= ~0x400;
|
||||||
|
|
||||||
|
if (iBuffersProcessed && numSamples)
|
||||||
|
{
|
||||||
|
numSamples = (numSamples > OAL_BUFFER_SIZE / 4) ? OAL_BUFFER_SIZE / 4 : numSamples;
|
||||||
|
// Remove the Buffer from the Queue. (uiBuffer contains the Buffer ID for the unqueued Buffer)
|
||||||
|
if (iBuffersFilled == 0)
|
||||||
|
alSourceUnqueueBuffers(uiSource, iBuffersProcessed, uiBufferTemp);
|
||||||
|
|
||||||
|
m_mixer->Mix(realtimeBuffer, numSamples);
|
||||||
|
alBufferData(uiBufferTemp[iBuffersFilled], AL_FORMAT_STEREO16, realtimeBuffer, numSamples * 4, ulFrequency);
|
||||||
|
alSourceQueueBuffers(uiSource, 1, &uiBufferTemp[iBuffersFilled]);
|
||||||
|
iBuffersFilled++;
|
||||||
|
|
||||||
|
if (iBuffersFilled == OAL_NUM_BUFFERS)
|
||||||
|
alSourcePlay(uiSource);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ALint state = 0;
|
||||||
|
alGetSourcei(uiSource, AL_SOURCE_STATE, &state);
|
||||||
|
if (state != AL_PLAYING)
|
||||||
|
alSourcePlay(uiSource);
|
||||||
|
}
|
||||||
|
// soundCriticalSection.Leave();
|
||||||
soundSyncEvent.Wait();
|
soundSyncEvent.Wait();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,8 +34,9 @@
|
||||||
#include "AL/alc.h"
|
#include "AL/alc.h"
|
||||||
#endif // WIN32
|
#endif // WIN32
|
||||||
// public use
|
// public use
|
||||||
#define SFX_MAX_SOURCE 1
|
#define SFX_MAX_SOURCE 1
|
||||||
#define OAL_BUFFER_SIZE 1024*1024
|
#define OAL_NUM_BUFFERS 2
|
||||||
|
#define OAL_BUFFER_SIZE (1024 * 8)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
class OpenALStream: public SoundStream
|
class OpenALStream: public SoundStream
|
||||||
|
@ -60,9 +61,9 @@ private:
|
||||||
Common::CriticalSection soundCriticalSection;
|
Common::CriticalSection soundCriticalSection;
|
||||||
Common::Event soundSyncEvent;
|
Common::Event soundSyncEvent;
|
||||||
|
|
||||||
short realtimeBuffer[OAL_BUFFER_SIZE];
|
short realtimeBuffer[OAL_BUFFER_SIZE/sizeof(short)];
|
||||||
ALuint g_uiSource;
|
ALuint uiBuffers[OAL_NUM_BUFFERS];
|
||||||
ALuint *g_uiBuffers;
|
ALuint uiSource;
|
||||||
#else
|
#else
|
||||||
public:
|
public:
|
||||||
OpenALStream(CMixer *mixer, void *hWnd = NULL): SoundStream(mixer) {}
|
OpenALStream(CMixer *mixer, void *hWnd = NULL): SoundStream(mixer) {}
|
||||||
|
|
Loading…
Reference in New Issue