The Mega Change Of Doom - or, fixing Stop. Almost. At least it's better than before. However, the OpenGL plugin seems to lose textures a lot between game restarts :P I think the GL plugin needs to do a lot more cleanup.

This change also includes tons of minor code formatting cleanup. Yeah, should've separated it ... sorry :(

Kills the old CPUCompare support. I'll resurrect it if I need it again, right now it mostly clutters the code.

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@2321 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
hrydgard
2009-02-20 22:04:52 +00:00
parent f992dae50d
commit 6cd34b318f
47 changed files with 685 additions and 1088 deletions

View File

@ -31,7 +31,8 @@ void AOSound::SoundLoop()
format.byte_format = AO_FMT_LITTLE;
device = ao_open_live(default_driver, &format, NULL /* no options */);
if (device == NULL) {
if (!device)
{
PanicAlert("DSP_HLE: Error opening AO device.\n");
ao_shutdown();
Stop();
@ -40,23 +41,26 @@ void AOSound::SoundLoop()
buf_size = format.bits/8 * format.channels * format.rate;
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();
}
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();
}
}
void *soundThread(void *args) {
void *soundThread(void *args)
{
((AOSound *)args)->SoundLoop();
return NULL;
}
bool AOSound::Start() {
bool AOSound::Start()
{
memset(realtimeBuffer, 0, sizeof(realtimeBuffer));
soundSyncEvent = new Common::Event();
@ -68,7 +72,8 @@ bool AOSound::Start() {
return true;
}
void AOSound::Update() {
void AOSound::Update()
{
soundSyncEvent->Set();
}

View File

@ -26,61 +26,52 @@
#include "Thread.h"
class AOSound : public SoundStream
{
#if defined(HAVE_AO) && HAVE_AO
Common::Thread *thread;
Common::Thread *thread;
Common::CriticalSection *soundCriticalSection;
Common::Event *soundSyncEvent;
Common::CriticalSection *soundCriticalSection;
Common::Event *soundSyncEvent;
int buf_size;
ao_device *device;
ao_sample_format format;
int default_driver;
short realtimeBuffer[1024 * 1024];
int buf_size;
ao_device *device;
ao_sample_format format;
int default_driver;
short realtimeBuffer[1024 * 1024];
public:
AOSound(int _sampleRate, StreamCallback _callback) :
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 ~AOSound() {}
virtual bool Start();
virtual void SoundLoop();
virtual void Stop();
static bool isValid() {
return true;
}
virtual bool usesMixer() const {
return true;
}
virtual void Update();
virtual int GetSampleRate() {
return sampleRate;
}
virtual int GetSampleRate() {
return sampleRate;
}
#else
public:
AOSound(int _sampleRate, StreamCallback _callback) :
AOSound(int _sampleRate, StreamCallback _callback) :
SoundStream(_sampleRate, _callback) {}
#endif
};
#endif //__AOSOUNDSTREAM_H__

View File

@ -16,13 +16,9 @@
// http://code.google.com/p/dolphin-emu/
#include <dxerr.h>
#include "DSoundStream.h"
#include "../main.h"
#include "WaveFile.h"
extern bool log_ai;
extern WaveFileWriter g_wave_writer;
bool DSound::CreateBuffer()
{
@ -39,9 +35,9 @@ bool DSound::CreateBuffer()
pcmwf.wf.nAvgBytesPerSec = pcmwf.wf.nSamplesPerSec * pcmwf.wf.nBlockAlign;
pcmwf.wBitsPerSample = 16;
//buffer description
// Fill out DSound buffer description.
dsbdesc.dwSize = sizeof(DSBUFFERDESC);
dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_STICKYFOCUS; //VIKTIGT //DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLFREQUENCY;
dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_STICKYFOCUS;
dsbdesc.dwBufferBytes = bufferSize = BUFSIZE;
dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&pcmwf;
@ -51,7 +47,8 @@ bool DSound::CreateBuffer()
dsBuffer->SetCurrentPosition(0);
return true;
}
else {
else
{
// Failed.
PanicAlert("Sound buffer creation failed: %s", DXGetErrorString(res));
dsBuffer = NULL;
@ -64,11 +61,12 @@ bool DSound::WriteDataToBuffer(DWORD dwOffset, // Our own write
DWORD dwSoundBytes) // Size of block to copy.
{
// I want to record the regular audio to, how do I do that?
// Well, it's gonna be a bit tricky. For future work :)
//std::string Data = ArrayToString((const u8*)soundData, dwSoundBytes);
//Console::Print("Data: %s\n\n", Data.c_str());
//if (log_ai) g_wave_writer.AddStereoSamples((const short*)soundData, dwSoundBytes);
void* ptr1, * ptr2;
void *ptr1, *ptr2;
DWORD numBytes1, numBytes2;
// Obtain memory address of write block. This will be in two parts if the block wraps around.
HRESULT hr = dsBuffer->Lock(dwOffset, dwSoundBytes, &ptr1, &numBytes1, &ptr2, &numBytes2, 0);
@ -79,15 +77,11 @@ bool DSound::WriteDataToBuffer(DWORD dwOffset, // Our own write
dsBuffer->Restore();
hr = dsBuffer->Lock(dwOffset, dwSoundBytes, &ptr1, &numBytes1, &ptr2, &numBytes2, 0);
}
if (SUCCEEDED(hr))
{
memcpy(ptr1, soundData, numBytes1);
if (ptr2 != 0)
{
memcpy(ptr2, soundData + numBytes1, numBytes2);
}
// Release the data back to DirectSound.
dsBuffer->Unlock(ptr1, numBytes1, ptr2, numBytes2);
@ -100,43 +94,33 @@ bool DSound::WriteDataToBuffer(DWORD dwOffset, // Our own write
// The audio thread.
DWORD WINAPI soundThread(void* args)
{
((DSound *)args)->SoundLoop();
return 0; //huzzah! :D
(reinterpret_cast<DSound *>(args))->SoundLoop();
return 0;
}
void DSound::SoundLoop() {
void DSound::SoundLoop()
{
currentPos = 0;
lastPos = 0;
// Prefill buffer?
//writeDataToBuffer(0,realtimeBuffer,bufferSize);
// 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
soundCriticalSection->Enter();
dsBuffer->GetCurrentPosition((DWORD*)&currentPos, 0);
int numBytesToRender = FIX128(ModBufferSize(currentPos - lastPos));
if (numBytesToRender >= 256)
{
if (numBytesToRender > sizeof(realtimeBuffer))
PanicAlert("soundThread: too big render call");
(*callback)(realtimeBuffer, numBytesToRender >> 2, 16,
sampleRate, 2);
(*callback)(realtimeBuffer, numBytesToRender >> 2, 16, sampleRate, 2);
WriteDataToBuffer(lastPos, (char*)realtimeBuffer, numBytesToRender);
currentPos = ModBufferSize(lastPos + numBytesToRender);
totalRenderedBytes += numBytesToRender;
lastPos = currentPos;
}
soundCriticalSection->Leave();
soundSyncEvent->Wait();
}
@ -145,20 +129,14 @@ void DSound::SoundLoop() {
bool DSound::Start()
{
//no security attributes, automatic resetting, init state nonset, untitled
soundSyncEvent = new Common::Event();
soundSyncEvent->Init();
//vi initierar den...........
soundCriticalSection = new Common::CriticalSection();
//vi vill ha access till DSOUND s<>...
if (FAILED(DirectSoundCreate8(0, &ds, 0)))
return false;
return false;
if(hWnd)
ds->SetCooperativeLevel((HWND)hWnd, DSSCL_NORMAL);
if (!CreateBuffer())
return false;
@ -168,7 +146,6 @@ bool DSound::Start()
memset(p1, 0, num1);
dsBuffer->Unlock(p1, num1, 0, 0);
totalRenderedBytes = -bufferSize;
thread = new Common::Thread(soundThread, (void *)this);
return true;
}
@ -196,15 +173,3 @@ void DSound::Stop()
soundSyncEvent = NULL;
thread = NULL;
}
/* Unused, is it needed?
int DSound::GetCurSample()
{
soundCriticalSection->Enter();
int playCursor;
dsBuffer->GetCurrentPosition((DWORD*)&playCursor, 0);
playCursor = ModBufferSize(playCursor - lastPos) + totalRenderedBytes;
soundCriticalSection->Leave();
return playCursor;
}
*/

View File

@ -34,11 +34,8 @@
class DSound : public SoundStream
{
#ifdef _WIN32
Common::Thread *thread;
Common::CriticalSection *soundCriticalSection;
Common::Event *soundSyncEvent;
void *hWnd;
@ -55,20 +52,18 @@ class DSound : public SoundStream
short realtimeBuffer[1024 * 1024];
inline int FIX128(int x) {
return x & (~127);
return x & (~127);
}
inline int ModBufferSize(int x) {
return (x + bufferSize) % bufferSize;
return (x + bufferSize) % bufferSize;
}
bool CreateBuffer();
bool WriteDataToBuffer(DWORD dwOffset, char* soundData,
DWORD dwSoundBytes);
public:
public:
DSound(int _sampleRate, StreamCallback _callback) :
SoundStream(_sampleRate, _callback) {}
@ -76,29 +71,19 @@ public:
SoundStream(_sampleRate, _callback), hWnd(_hWnd) {}
virtual ~DSound() {}
virtual bool Start();
virtual bool Start();
virtual void SoundLoop();
virtual void Stop();
static bool isValid() { return true; }
virtual bool usesMixer() { return true; }
virtual bool usesMixer() const { return true; }
virtual void Update();
#else
public:
DSound(int _sampleRate, StreamCallback _callback, void *hWnd = NULL) :
SoundStream(_sampleRate, _callback) {}
#endif
};
#endif //__DSOUNDSTREAM_H__

View File

@ -31,9 +31,8 @@
#include "Mixer.h"
#include "FixedSizeQueue.h"
namespace {
Common::CriticalSection push_sync;
// On real hardware, this fifo is much, much smaller. But timing is also tighter than under Windows, so...
@ -46,13 +45,6 @@ FixedSizeQueue<s16, queue_maxlength> sample_queue;
volatile bool mixer_HLEready = false;
volatile int queue_size = 0;
bool bThrottling = false;
/* What is this for?
void UpdateThrottle(bool update)
{
bThrottling = update;
}*/
void Mixer(short *buffer, int numSamples, int bits, int rate, int channels)
{
@ -105,11 +97,8 @@ void Mixer_MixUCode(short *buffer, int numSamples, int bits, int rate,
void Mixer_PushSamples(short *buffer, int num_stereo_samples, int sample_rate)
{
// We alredady do this with the WaveFileWriter right? So no need for this to?
// static FILE *f;
// if (!f)
// f = fopen("d:\\hello.raw", "wb");
// fwrite(buffer, num_stereo_samples * 4, 1, f);
if (!soundStream)
return;
if (queue_size == 0)
{
@ -128,14 +117,18 @@ void Mixer_PushSamples(short *buffer, int num_stereo_samples, int sample_rate)
#endif
// Write Other Audio
//bThrottling = g_Config.m_EnableThrottle;
if (g_Config.m_EnableThrottle)
{
/* 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();
// Urgh.
if (g_dspInitialize.pEmulatorState) {
if (*g_dspInitialize.pEmulatorState != 0)
return;
}
soundStream->Update();
Common::SleepCurrentThread(0);
}
@ -144,64 +137,64 @@ void Mixer_PushSamples(short *buffer, int num_stereo_samples, int sample_rate)
push_sync.Enter();
while (num_stereo_samples)
{
acc += sample_rate;
while (num_stereo_samples && (acc >= 48000))
{
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;
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));
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;
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;
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));
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();
}
}

View File

@ -22,37 +22,29 @@
typedef void (*StreamCallback)(short* buffer, int numSamples, int bits, int rate, int channels);
class SoundStream {
protected:
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:
public:
SoundStream(int _sampleRate, StreamCallback _callback) :
sampleRate(_sampleRate), callback(_callback), threadData(0) {}
sampleRate(_sampleRate), callback(_callback), threadData(0) {}
virtual ~SoundStream() {}
static bool isValid() { return false; }
virtual bool usesMixer() { return false; }
virtual bool usesMixer() const { return false; }
virtual bool Start() { return false; }
virtual void SoundLoop() { }
virtual void SoundLoop() {}
virtual void Stop() {}
virtual void Update() {}
virtual int GetSampleRate() { return sampleRate; }
virtual int GetSampleRate() const { return sampleRate; }
};
#endif

View File

@ -47,7 +47,6 @@ std::string gpName;
SoundStream *soundStream = NULL;
// Set this if you want to log audio. search for log_ai in this file to see the filename.
bool log_ai = false;
WaveFileWriter g_wave_writer;
@ -155,17 +154,16 @@ void CloseConsole()
void DllDebugger(HWND _hParent, bool Show)
{
#if defined(HAVE_WX) && HAVE_WX
if(m_frame && Show) // if we have created it, let us show it again
if (m_frame && Show) // if we have created it, let us show it again
{
m_frame->DoShow();
}
else if(!m_frame && Show)
else if (!m_frame && Show)
{
m_frame = new CDebugger(NULL);
m_frame->Show();
}
else if(m_frame && !Show)
else if (m_frame && !Show)
{
m_frame->DoHide();
}
@ -213,10 +211,9 @@ void Initialize(void *init)
{
//Console::Open(80, 5000);
g_Config.Load();
g_dspInitialize = *(DSPInitialize*)init;
g_Config.Load();
g_pMemory = g_dspInitialize.pGetMemoryPointer(0);
#if defined(_DEBUG) || defined(DEBUGFAST)
@ -237,12 +234,12 @@ void Initialize(void *init)
if (DSound::isValid())
soundStream = new DSound(48000, Mixer, g_dspInitialize.hWnd);
}
else if(g_Config.sBackend == "AOSound")
else if (g_Config.sBackend == "AOSound")
{
if (AOSound::isValid())
soundStream = new AOSound(48000, Mixer);
}
else if(g_Config.sBackend == "NullSound")
else if (g_Config.sBackend == "NullSound")
{
soundStream = new NullSound(48000, Mixer_MixUCode);
}
@ -258,15 +255,19 @@ void Initialize(void *init)
_CrtSetDbgFlag(tmpflag);
#endif
if (soundStream) {
if(!soundStream->Start()) {
if (soundStream)
{
if (!soundStream->Start())
{
PanicAlert("Could not initialize backend %s, falling back to NULL",
g_Config.sBackend);
delete soundStream;
soundStream = new NullSound(48000, Mixer);
soundStream->Start();
}
} else {
}
else
{
PanicAlert("Sound backend %s is not valid, falling back to NULL",
g_Config.sBackend);
delete soundStream;
@ -282,28 +283,38 @@ void Initialize(void *init)
}
}
void Shutdown()
void DSP_StopSoundStream()
{
// Stop the sound recording
if (log_ai) g_wave_writer.Stop();
// Delete the UCodes
if (!soundStream)
PanicAlert("Can't stop non running SoundStream!");
soundStream->Stop();
delete soundStream;
soundStream = NULL;
}
void Shutdown()
{
// Check that soundstream already is stopped.
if (soundStream)
PanicAlert("SoundStream alive in DSP::Shutdown!");
// Stop the sound recording
if (log_ai)
g_wave_writer.Stop();
// Delete the UCodes
CDSPHandler::Destroy();
#if defined(HAVE_WX) && HAVE_WX
// Reset mails
if(m_frame)
{
sMailLog.clear();
sMailTime.clear();
m_frame->sMail.clear();
m_frame->sMailEnd.clear();
}
#endif
#if defined(HAVE_WX) && HAVE_WX
// Reset mails
if (m_frame)
{
sMailLog.clear();
sMailTime.clear();
m_frame->sMail.clear();
m_frame->sMailEnd.clear();
}
#endif
}
void DoState(unsigned char **ptr, int mode)
@ -406,7 +417,12 @@ void DSP_Update(int cycles)
game has started. */
void DSP_SendAIBuffer(unsigned int address, int sample_rate)
{
if(soundStream->usesMixer())
// TODO: This is not yet fully threadsafe.
if (!soundStream) {
return;
}
if (soundStream->usesMixer())
{
short samples[16] = {0}; // interleaved stereo
if (address)
@ -417,21 +433,19 @@ void DSP_SendAIBuffer(unsigned int address, int sample_rate)
}
// Write the audio to a file
if (log_ai) g_wave_writer.AddStereoSamples(samples, 8);
if (log_ai)
g_wave_writer.AddStereoSamples(samples, 8);
}
Mixer_PushSamples(samples, 32 / 4, sample_rate);
}
/* If I don't use this in Wario Land Shake It I get bad sound, it's a lot of static and noise
in the sound. It's the same both with an without Enable Other Audio. I can't really say why
this occurs because I don't know what SoundSyncEvent->Set() does. */
#ifdef SETUP_AVOID_SOUND_ARTIFACTS
static int counter = 0;
counter++;
if ((counter & 255) == 0)
#endif
// SoundStream is updated only when necessary (there is no 70 ms limit
// so each sample now triggers the sound stream)
soundStream->Update();
// TODO: think about this.
static int counter = 0;
counter++;
if ((counter & 31) == 0 && soundStream)
soundStream->Update();
}
/////////////////////////////////////