This is a joined work of XK and me on improving the HLE plugin interface.

It allows run time selection of backends (AOSound, DSound and NullSound).
It replaces the DSP_NULL plugin (works even better!)
It also includes improved thread handling on asound, and using some common functions on both
asound and windows.


git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@2027 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
nakeee
2009-01-29 00:57:55 +00:00
parent 121be22532
commit 7219bcd4d5
46 changed files with 705 additions and 4584 deletions

View File

@ -1,4 +1,4 @@
// Copyright (C) 2003-2008 Dolphin Project.
// Copyright (C) 2003-2009 Dolphin Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
@ -15,78 +15,74 @@
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#include <ao/ao.h>
#include <pthread.h>
#include <string.h>
#include "AOSoundStream.h"
namespace AOSound
#if defined(HAVE_AO) && HAVE_AO
void AOSound::SoundLoop()
{
pthread_t thread;
StreamCallback callback;
int buf_size;
ao_device *device;
ao_sample_format format;
int default_driver;
int sampleRate;
volatile int threadData;
short realtimeBuffer[1024 * 1024];
int AOSound_GetSampleRate()
{
return sampleRate;
}
void *soundThread(void *)
{
ao_initialize();
default_driver = ao_default_driver_id();
format.bits = 16;
format.channels = 2;
format.rate = sampleRate;
format.byte_format = AO_FMT_LITTLE;
ao_initialize();
default_driver = ao_default_driver_id();
format.bits = 16;
format.channels = 2;
format.rate = sampleRate;
format.byte_format = AO_FMT_LITTLE;
device = ao_open_live(default_driver, &format, NULL /* no options */);
if (device == NULL)
{
fprintf(stderr, "DSP_HLE: Error opening AO device.\n");
return false;
}
buf_size = format.bits/8 * format.channels * format.rate;
device = ao_open_live(default_driver, &format, NULL /* no options */);
if (device == NULL) {
PanicAlert("DSP_HLE: Error opening AO device.\n");
ao_shutdown();
Stop();
return;
}
while (!threadData)
{
uint_32 numBytesToRender = 256;
(*callback)(realtimeBuffer, numBytesToRender >> 2, 16, sampleRate, 2);
ao_play(device, (char*)realtimeBuffer, numBytesToRender);
}
buf_size = format.bits/8 * format.channels * format.rate;
ao_close(device);
device = 0;
ao_shutdown();
while (!threadData) {
soundCriticalSection->Enter();
uint_32 numBytesToRender = 256;
(*callback)(realtimeBuffer, numBytesToRender >> 2, 16, sampleRate, 2);
ao_play(device, (char*)realtimeBuffer, numBytesToRender);
soundCriticalSection->Leave();
soundSyncEvent->Wait();
}
return 0;
}
bool AOSound_StartSound(int _sampleRate, StreamCallback _callback)
{
callback = _callback;
threadData = 0;
sampleRate = _sampleRate;
memset(realtimeBuffer, 0, sizeof(realtimeBuffer));
pthread_create(&thread, NULL, soundThread, (void *)NULL);
return true;
}
void AOSound_StopSound()
{
threadData = 1;
void *retval;
pthread_join(thread, &retval);
}
ao_close(device);
device = NULL;
ao_shutdown();
}
void *soundThread(void *args) {
((AOSound *)args)->SoundLoop();
return NULL;
}
bool AOSound::Start() {
memset(realtimeBuffer, 0, sizeof(realtimeBuffer));
soundCriticalSection = new Common::CriticalSection(1);
thread = new Common::Thread(soundThread, (void *)this);
soundSyncEvent = new Common::Event();
soundSyncEvent->Init();
return true;
}
void AOSound::Update() {
soundSyncEvent->Set();
}
void AOSound::Stop()
{
soundCriticalSection->Enter();
threadData = 1;
soundSyncEvent->Set();
soundCriticalSection->Leave();
soundSyncEvent->Shutdown();
delete soundCriticalSection;
delete thread;
delete soundSyncEvent;
}
#endif

View File

@ -1,4 +1,4 @@
// Copyright (C) 2003-2008 Dolphin Project.
// Copyright (C) 2003-2009 Dolphin Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
@ -18,18 +18,69 @@
#ifndef __AOSOUNDSTREAM_H__
#define __AOSOUNDSTREAM_H__
namespace AOSound
#include "SoundStream.h"
#if defined(HAVE_AO) && HAVE_AO
#include <ao/ao.h>
#endif
#include "Thread.h"
class AOSound : public SoundStream
{
typedef void (*StreamCallback)(short* buffer, int numSamples, int bits, int rate, int channels);
bool AOSound_StartSound(int sampleRate, StreamCallback _callback);
void AOSound_UpdateSound();
void AOSound_StopSound();
#if defined(HAVE_AO) && HAVE_AO
Common::Thread *thread;
float AOSound_GetTimer();
int AOSound_GetCurSample();
int AOSound_GetSampleRate();
}
Common::CriticalSection *soundCriticalSection;
Common::Event *soundSyncEvent;
int buf_size;
ao_device *device;
ao_sample_format format;
int default_driver;
short realtimeBuffer[1024 * 1024];
public:
AOSound(int _sampleRate, StreamCallback _callback) :
SoundStream(_sampleRate, _callback) {}
virtual ~AOSound() {}
virtual bool Start();
virtual void SoundLoop();
virtual void Stop();
static bool isValid() {
return true;
}
virtual bool usesMixer() {
return true;
}
virtual void Update();
virtual int GetSampleRate() {
return sampleRate;
}
#else
public:
AOSound(int _sampleRate, StreamCallback _callback) :
SoundStream(_sampleRate, _callback) {}
#endif
};
#endif //__AOSOUNDSTREAM_H__

View File

@ -1,4 +1,4 @@
// Copyright (C) 2003-2008 Dolphin Project.
// Copyright (C) 2003-2009 Dolphin Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
@ -15,53 +15,12 @@
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#include "stdafx.h"
#include <mmsystem.h>
#include <dsound.h>
#include "DSoundStream.h"
#include "../main.h"
namespace DSound
{
#include <dxerr.h>
#define BUFSIZE 32768
#define MAXWAIT 70 //ms
CRITICAL_SECTION soundCriticalSection;
HANDLE soundSyncEvent;
HANDLE hThread;
StreamCallback callback;
IDirectSound8* ds;
IDirectSoundBuffer* dsBuffer;
int bufferSize; //i bytes
int totalRenderedBytes;
int sampleRate;
// playback position
int currentPos;
int lastPos;
short realtimeBuffer[1024 * 1024];
// We set this to shut down the sound thread.
// 0=keep playing, 1=stop playing NOW.
volatile int threadData;
inline int FIX128(int x)
{
return(x & (~127));
}
int DSound_GetSampleRate()
{
return(sampleRate);
}
bool CreateBuffer()
bool DSound::CreateBuffer()
{
PCMWAVEFORMAT pcmwf;
DSBUFFERDESC dsbdesc;
@ -80,22 +39,23 @@ bool CreateBuffer()
dsbdesc.dwSize = sizeof(DSBUFFERDESC);
dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_STICKYFOCUS; //VIKTIGT //DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLFREQUENCY;
dsbdesc.dwBufferBytes = bufferSize = BUFSIZE;
dsbdesc.lpwfxFormat = (WAVEFORMATEX*)&pcmwf;
dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&pcmwf;
if (SUCCEEDED(ds->CreateSoundBuffer(&dsbdesc, &dsBuffer, NULL)))
HRESULT res = ds->CreateSoundBuffer(&dsbdesc, &dsBuffer, NULL);
if (SUCCEEDED(res))
{
dsBuffer->SetCurrentPosition(0);
return(true);
return true;
}
else
{
else {
// Failed.
PanicAlert("Sound buffer creation failed: %s", DXGetErrorString(res));
dsBuffer = NULL;
return(false);
return false;
}
}
bool WriteDataToBuffer(DWORD dwOffset, // Our own write cursor.
bool DSound::WriteDataToBuffer(DWORD dwOffset, // Our own write cursor.
char* soundData, // Start of our data.
DWORD dwSoundBytes) // Size of block to copy.
{
@ -122,20 +82,22 @@ bool WriteDataToBuffer(DWORD dwOffset, // Our own write cursor.
// Release the data back to DirectSound.
dsBuffer->Unlock(ptr1, numBytes1, ptr2, numBytes2);
return(true);
return true;
}
return(false);
}
inline int ModBufferSize(int x)
{
return((x + bufferSize) % bufferSize);
return false;
}
// The audio thread.
DWORD WINAPI soundThread(void*)
DWORD WINAPI soundThread(void* args)
{
((DSound *)args)->SoundLoop();
return 0; //huzzah! :D
}
void DSound::SoundLoop() {
currentPos = 0;
lastPos = 0;
@ -144,18 +106,19 @@ DWORD WINAPI soundThread(void*)
// dsBuffer->Lock(0, bufferSize, (void **)&p1, &num1, (void **)&p2, &num2, 0);
dsBuffer->Play(0, 0, DSBPLAY_LOOPING);
while (!threadData)
{
while (!threadData) {
// No blocking inside the csection
EnterCriticalSection(&soundCriticalSection);
soundCriticalSection->Enter();
dsBuffer->GetCurrentPosition((DWORD*)&currentPos, 0);
int numBytesToRender = FIX128(ModBufferSize(currentPos - lastPos));
int numBytesToRender = FIX128(
ModBufferSize(currentPos - lastPos));
if (numBytesToRender >= 256)
{
if (numBytesToRender > sizeof(realtimeBuffer))
MessageBox(0,"soundThread: too big render call",0,0);
(*callback)(realtimeBuffer, numBytesToRender >> 2, 16, sampleRate, 2);
PanicAlert("soundThread: too big render call");
(*callback)(realtimeBuffer, numBytesToRender >> 2, 16,
sampleRate, 2);
WriteDataToBuffer(lastPos, (char*)realtimeBuffer, numBytesToRender);
currentPos = ModBufferSize(lastPos + numBytesToRender);
@ -164,36 +127,32 @@ DWORD WINAPI soundThread(void*)
lastPos = currentPos;
}
LeaveCriticalSection(&soundCriticalSection);
WaitForSingleObject(soundSyncEvent, MAXWAIT);
soundCriticalSection->Leave();
soundSyncEvent->Wait();
}
dsBuffer->Stop();
return(0); //hurra!
}
bool DSound_StartSound(HWND window, int _sampleRate, StreamCallback _callback)
bool DSound::Start()
{
callback = _callback;
threadData = 0;
sampleRate = _sampleRate;
//no security attributes, automatic resetting, init state nonset, untitled
soundSyncEvent = CreateEvent(0, false, false, 0);
soundSyncEvent = new Common::Event();
soundSyncEvent->Init();
//vi initierar den...........
InitializeCriticalSection(&soundCriticalSection);
soundCriticalSection = new Common::CriticalSection();
//vi vill ha access till DSOUND s<>...
if (FAILED(DirectSoundCreate8(0, &ds, 0)))
return false;
return false;
ds->SetCooperativeLevel(window, DSSCL_NORMAL);
if(hWnd)
ds->SetCooperativeLevel((HWND)hWnd, DSSCL_NORMAL);
if (!CreateBuffer())
{
return false;
}
DWORD num1;
short* p1;
@ -201,48 +160,43 @@ bool DSound_StartSound(HWND window, int _sampleRate, StreamCallback _callback)
memset(p1, 0, num1);
dsBuffer->Unlock(p1, num1, 0, 0);
totalRenderedBytes = -bufferSize;
DWORD h;
hThread = CreateThread(0, 0, soundThread, 0, 0, &h);
SetThreadPriority(hThread, THREAD_PRIORITY_ABOVE_NORMAL);
thread = new Common::Thread(soundThread, (void *)this);
return true;
}
void DSound_UpdateSound()
void DSound::Update()
{
SetEvent(soundSyncEvent);
soundSyncEvent->Set();
}
void DSound_StopSound()
void DSound::Stop()
{
EnterCriticalSection(&soundCriticalSection);
soundCriticalSection->Enter();
threadData = 1;
// kick the thread if it's waiting
SetEvent(soundSyncEvent);
LeaveCriticalSection(&soundCriticalSection);
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
soundSyncEvent->Set();
soundCriticalSection->Leave();
delete soundCriticalSection;
delete thread;
dsBuffer->Release();
ds->Release();
CloseHandle(soundSyncEvent);
soundSyncEvent = INVALID_HANDLE_VALUE;
hThread = INVALID_HANDLE_VALUE;
soundSyncEvent->Shutdown();
delete soundSyncEvent;
soundSyncEvent = NULL;
thread = NULL;
}
int DSound_GetCurSample()
/* Unused, is it needed?
int DSound::GetCurSample()
{
EnterCriticalSection(&soundCriticalSection);
soundCriticalSection->Enter();
int playCursor;
dsBuffer->GetCurrentPosition((DWORD*)&playCursor, 0);
playCursor = ModBufferSize(playCursor - lastPos) + totalRenderedBytes;
LeaveCriticalSection(&soundCriticalSection);
return(playCursor);
soundCriticalSection->Leave();
return playCursor;
}
float DSound_GetTimer()
{
return((float)DSound_GetCurSample() * (1.0f / (4.0f * sampleRate)));
}
} // namespace
*/

View File

@ -1,4 +1,4 @@
// Copyright (C) 2003-2008 Dolphin Project.
// Copyright (C) 2003-2009 Dolphin Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
@ -15,21 +15,90 @@
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#ifndef __SOUNDSTREAM_H__
#define __SOUNDSTREAM_H__
#ifndef __DSOUNDSTREAM_H__
#define __DSOUNDSTREAM_H__
#include "SoundStream.h"
namespace DSound
#include "Thread.h"
#ifdef _WIN32
#include "stdafx.h"
#include <mmsystem.h>
#include <dsound.h>
#define BUFSIZE 32768
#define MAXWAIT 70 //ms
#endif
class DSound : public SoundStream
{
typedef void (*StreamCallback)(short* buffer, int numSamples, int bits, int rate, int channels);
#ifdef _WIN32
bool DSound_StartSound(HWND window, int sampleRate, StreamCallback _callback);
void DSound_UpdateSound();
void DSound_StopSound();
Common::Thread *thread;
Common::CriticalSection *soundCriticalSection;
Common::Event *soundSyncEvent;
void *hWnd;
float DSound_GetTimer();
int DSound_GetCurSample();
int DSound_GetSampleRate();
}
IDirectSound8* ds;
IDirectSoundBuffer* dsBuffer;
int bufferSize; //i bytes
int totalRenderedBytes;
// playback position
int currentPos;
int lastPos;
short realtimeBuffer[1024 * 1024];
inline int FIX128(int x) {
return x & (~127);
}
inline int ModBufferSize(int x) {
return (x + bufferSize) % bufferSize;
}
bool CreateBuffer();
bool WriteDataToBuffer(DWORD dwOffset, char* soundData,
DWORD dwSoundBytes);
public:
DSound(int _sampleRate, StreamCallback _callback) :
SoundStream(_sampleRate, _callback) {}
DSound(int _sampleRate, StreamCallback _callback, void *_hWnd) :
SoundStream(_sampleRate, _callback), hWnd(_hWnd) {}
virtual ~DSound() {}
virtual bool Start();
virtual void SoundLoop();
virtual void Stop();
static bool isValid() { return true; }
virtual bool usesMixer() { return true; }
virtual void Update();
#else
public:
DSound(int _sampleRate, StreamCallback _callback, void *hWnd = NULL) :
SoundStream(_sampleRate, _callback) {}
#endif //__SOUNDSTREAM_H__
#endif
};
#endif //__DSOUNDSTREAM_H__

View File

@ -16,9 +16,6 @@
// http://code.google.com/p/dolphin-emu/
//////////////////////////////////////////////////////////////////////////////////////////
// Includes
// -------------
// This queue solution is temporary. I'll implement something more efficient later.
#include <queue> // System
@ -29,14 +26,11 @@
#include "../Globals.h"
#include "../DSPHandler.h"
#include "../Debugger/File.h"
#include "../main.h"
#include "Mixer.h"
#include "FixedSizeQueue.h"
#ifdef _WIN32
#include "../PCHW/DSoundStream.h"
#endif
///////////////////////
namespace {
@ -52,6 +46,11 @@ FixedSizeQueue<s16, queue_maxlength> sample_queue;
volatile bool mixer_HLEready = false;
volatile int queue_size = 0;
bool bThrottling = false;
void UpdateThrottle(bool update) {
bThrottling = update;
}
void Mixer(short *buffer, int numSamples, int bits, int rate, int channels)
{
@ -111,87 +110,85 @@ void Mixer_PushSamples(short *buffer, int num_stereo_samples, int sample_rate) {
static int PV1l=0,PV2l=0,PV3l=0,PV4l=0;
static int PV1r=0,PV2r=0,PV3r=0,PV4r=0;
static int acc=0;
static int acc=0;
bThrottling = g_Config.m_EnableThrottle;
if(bThrottling) {
/* This is only needed for non-AX sound, currently directly
streamed and DTK sound. For AX we call SoundStream::Update in
AXTask() for example. */
while (queue_size > queue_maxlength / 2) {
soundStream->Update();
Common::SleepCurrentThread(0);
}
#ifdef _WIN32
if (! (GetAsyncKeyState(VK_TAB)) && g_Config.m_EnableThrottle) {
//convert into config option?
const int mode = 2;
/* This is only needed for non-AX sound, currently directly streamed and
DTK sound. For AX we call DSound_UpdateSound in AXTask() for example. */
while (queue_size > queue_maxlength / 2) {
DSound::DSound_UpdateSound();
Sleep(0);
}
} else {
return;
}
#else
while (queue_size > queue_maxlength) {
usleep(1000);
}
#endif
//convert into config option?
const int mode = 2;
push_sync.Enter();
while (num_stereo_samples)
{
acc += sample_rate;
while (num_stereo_samples && (acc >= 48000))
{
PV4l=PV3l;
PV3l=PV2l;
PV2l=PV1l;
PV1l=*(buffer++); //32bit processing
PV4r=PV3r;
PV3r=PV2r;
PV2r=PV1r;
PV1r=*(buffer++); //32bit processing
num_stereo_samples--;
acc-=48000;
}
push_sync.Enter();
while (num_stereo_samples)
{
acc += sample_rate;
while (num_stereo_samples && (acc >= 48000))
{
PV4l=PV3l;
PV3l=PV2l;
PV2l=PV1l;
PV1l=*(buffer++); //32bit processing
PV4r=PV3r;
PV3r=PV2r;
PV2r=PV1r;
PV1r=*(buffer++); //32bit processing
num_stereo_samples--;
acc-=48000;
}
// defaults to nearest
s32 DataL = PV1l;
s32 DataR = PV1r;
// defaults to nearest
s32 DataL = PV1l;
s32 DataR = PV1r;
if (mode == 1) //linear
{
DataL = PV1l + ((PV2l - PV1l)*acc)/48000;
DataR = PV1r + ((PV2r - PV1r)*acc)/48000;
}
else if (mode == 2) //cubic
{
s32 a0l = PV1l - PV2l - PV4l + PV3l;
s32 a0r = PV1r - PV2r - PV4r + PV3r;
s32 a1l = PV4l - PV3l - a0l;
s32 a1r = PV4r - PV3r - a0r;
s32 a2l = PV1l - PV4l;
s32 a2r = PV1r - PV4r;
s32 a3l = PV2l;
s32 a3r = PV2r;
if (mode == 1) //linear
{
DataL = PV1l + ((PV2l - PV1l)*acc)/48000;
DataR = PV1r + ((PV2r - PV1r)*acc)/48000;
}
else if (mode == 2) //cubic
{
s32 a0l = PV1l - PV2l - PV4l + PV3l;
s32 a0r = PV1r - PV2r - PV4r + PV3r;
s32 a1l = PV4l - PV3l - a0l;
s32 a1r = PV4r - PV3r - a0r;
s32 a2l = PV1l - PV4l;
s32 a2r = PV1r - PV4r;
s32 a3l = PV2l;
s32 a3r = PV2r;
s32 t0l = ((a0l )*acc)/48000;
s32 t0r = ((a0r )*acc)/48000;
s32 t1l = ((t0l+a1l)*acc)/48000;
s32 t1r = ((t0r+a1r)*acc)/48000;
s32 t2l = ((t1l+a2l)*acc)/48000;
s32 t2r = ((t1r+a2r)*acc)/48000;
s32 t3l = ((t2l+a3l));
s32 t3r = ((t2r+a3r));
s32 t0l = ((a0l )*acc)/48000;
s32 t0r = ((a0r )*acc)/48000;
s32 t1l = ((t0l+a1l)*acc)/48000;
s32 t1r = ((t0r+a1r)*acc)/48000;
s32 t2l = ((t1l+a2l)*acc)/48000;
s32 t2r = ((t1r+a2r)*acc)/48000;
s32 t3l = ((t2l+a3l));
s32 t3r = ((t2r+a3r));
DataL = t3l;
DataR = t3r;
}
DataL = t3l;
DataR = t3r;
}
int l = DataL, r = DataR;
if (l < -32767) l = -32767;
if (r < -32767) r = -32767;
if (l > 32767) l = 32767;
if (r > 32767) r = 32767;
sample_queue.push(l);
sample_queue.push(r);
queue_size += 2;
}
push_sync.Leave();
}
int l = DataL, r = DataR;
if (l < -32767) l = -32767;
if (r < -32767) r = -32767;
if (l > 32767) l = 32767;
if (r > 32767) r = 32767;
sample_queue.push(l);
sample_queue.push(r);
queue_size += 2;
}
push_sync.Leave();
}

View File

@ -0,0 +1,58 @@
// Copyright (C) 2003-2009 Dolphin Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#ifndef __SOUNDSTREAM_H__
#define __SOUNDSTREAM_H__
#include "Common.h"
typedef void (*StreamCallback)(short* buffer, int numSamples, int bits, int rate, int channels);
class SoundStream {
protected:
int sampleRate;
StreamCallback callback;
// We set this to shut down the sound thread.
// 0=keep playing, 1=stop playing NOW.
volatile int threadData;
public:
SoundStream(int _sampleRate, StreamCallback _callback) :
sampleRate(_sampleRate), callback(_callback), threadData(0) {}
virtual ~SoundStream() {}
static bool isValid() { return false; }
virtual bool usesMixer() { return false; }
virtual bool Start() { return false; }
virtual void SoundLoop() { }
virtual void Stop() {}
virtual void Update() {}
virtual int GetSampleRate() { return sampleRate; }
};
#endif