Added volume control for OpenAL, also improved its performance a bit, but don't expect too much.

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@4712 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
ayuanx 2009-12-20 13:54:14 +00:00
parent 51163196d3
commit c3b196541d
2 changed files with 22 additions and 11 deletions

View File

@ -20,6 +20,9 @@
#if defined HAVE_OPENAL && HAVE_OPENAL #if defined HAVE_OPENAL && HAVE_OPENAL
//
// AyuanX: Spec says OpenAL1.1 is thread safe already
//
bool OpenALStream::Start() bool OpenALStream::Start()
{ {
ALDeviceList *pDeviceList = NULL; ALDeviceList *pDeviceList = NULL;
@ -68,8 +71,6 @@ void OpenALStream::Stop()
// kick the thread if it's waiting // kick the thread if it's waiting
soundSyncEvent.Set(); soundSyncEvent.Set();
// AyuanX: Spec says OpenAL1.1 is thread safe already
// soundCriticalSection.Enter();
delete thread; delete thread;
thread = NULL; thread = NULL;
@ -78,6 +79,7 @@ void OpenALStream::Stop()
// Clean up buffers and sources // Clean up buffers and sources
alDeleteSources(1, &uiSource); alDeleteSources(1, &uiSource);
uiSource = 0;
alDeleteBuffers(OAL_NUM_BUFFERS, uiBuffers); alDeleteBuffers(OAL_NUM_BUFFERS, uiBuffers);
ALCcontext *pContext = alcGetCurrentContext(); ALCcontext *pContext = alcGetCurrentContext();
@ -86,11 +88,18 @@ void OpenALStream::Stop()
alcMakeContextCurrent(NULL); alcMakeContextCurrent(NULL);
alcDestroyContext(pContext); alcDestroyContext(pContext);
alcCloseDevice(pDevice); alcCloseDevice(pDevice);
// soundCriticalSection.Leave();
soundSyncEvent.Shutdown(); soundSyncEvent.Shutdown();
} }
void OpenALStream::SetVolume(int volume)
{
fVolume = (float)volume / 100.0f;
if (uiSource)
alSourcef(uiSource, AL_GAIN, fVolume);
}
void OpenALStream::Update() void OpenALStream::Update()
{ {
soundSyncEvent.Set(); soundSyncEvent.Set();
@ -100,7 +109,6 @@ void OpenALStream::Clear(bool mute)
{ {
m_muted = mute; m_muted = mute;
// soundCriticalSection.Enter();
if(m_muted) if(m_muted)
{ {
alSourceStop(uiSource); alSourceStop(uiSource);
@ -109,7 +117,6 @@ void OpenALStream::Clear(bool mute)
{ {
alSourcePlay(uiSource); alSourcePlay(uiSource);
} }
// soundCriticalSection.Leave();
} }
THREAD_RETURN OpenALStream::ThreadFunc(void* args) THREAD_RETURN OpenALStream::ThreadFunc(void* args)
@ -146,14 +153,13 @@ void OpenALStream::SoundLoop()
while (!threadData) while (!threadData)
{ {
// soundCriticalSection.Enter();
if (iBuffersProcessed == iBuffersFilled) if (iBuffersProcessed == iBuffersFilled)
{ {
alGetSourcei(uiSource, AL_BUFFERS_PROCESSED, &iBuffersProcessed); alGetSourcei(uiSource, AL_BUFFERS_PROCESSED, &iBuffersProcessed);
iBuffersFilled = 0; iBuffersFilled = 0;
} }
int numSamples = m_mixer->GetNumSamples(); int numSamples = m_mixer->GetNumSamples();
numSamples &= ~0x400; numSamples &= ~0x100;
if (iBuffersProcessed && numSamples) if (iBuffersProcessed && numSamples)
{ {
@ -177,7 +183,6 @@ void OpenALStream::SoundLoop()
if (state != AL_PLAYING) if (state != AL_PLAYING)
alSourcePlay(uiSource); alSourcePlay(uiSource);
} }
// soundCriticalSection.Leave();
soundSyncEvent.Wait(); soundSyncEvent.Wait();
} }
} }

View File

@ -35,19 +35,24 @@
#endif // WIN32 #endif // WIN32
// public use // public use
#define SFX_MAX_SOURCE 1 #define SFX_MAX_SOURCE 1
#define OAL_NUM_BUFFERS 2 #define OAL_NUM_BUFFERS 8
#define OAL_BUFFER_SIZE (1024 * 8) #define OAL_BUFFER_SIZE (512 * 4)
#endif #endif
class OpenALStream: public SoundStream class OpenALStream: public SoundStream
{ {
#if defined HAVE_OPENAL && HAVE_OPENAL #if defined HAVE_OPENAL && HAVE_OPENAL
public: public:
OpenALStream(CMixer *mixer, void *hWnd = NULL): SoundStream(mixer) {}; OpenALStream(CMixer *mixer, void *hWnd = NULL)
: SoundStream(mixer)
, uiSource(0)
{};
virtual ~OpenALStream() {}; virtual ~OpenALStream() {};
virtual bool Start(); virtual bool Start();
virtual void SoundLoop(); virtual void SoundLoop();
virtual void SetVolume(int volume);
virtual void Stop(); virtual void Stop();
virtual void Clear(bool mute); virtual void Clear(bool mute);
static bool isValid() { return true; } static bool isValid() { return true; }
@ -64,6 +69,7 @@ private:
short realtimeBuffer[OAL_BUFFER_SIZE/sizeof(short)]; short realtimeBuffer[OAL_BUFFER_SIZE/sizeof(short)];
ALuint uiBuffers[OAL_NUM_BUFFERS]; ALuint uiBuffers[OAL_NUM_BUFFERS];
ALuint uiSource; ALuint uiSource;
ALfloat fVolume;
#else #else
public: public:
OpenALStream(CMixer *mixer, void *hWnd = NULL): SoundStream(mixer) {} OpenALStream(CMixer *mixer, void *hWnd = NULL): SoundStream(mixer) {}