Add a sample rate parameter to DSound, clean it up a bit

This commit is contained in:
Henrik Rydgard 2015-01-11 14:11:09 +01:00
parent 62d86f3246
commit cac7a2feed
5 changed files with 263 additions and 214 deletions

View file

@ -1,3 +1,4 @@
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.
@ -10,4 +11,21 @@ namespace MathUtil
#define ROUND_UP(x, a) (((x) + (a) - 1) & ~((a) - 1))
#define ROUND_DOWN(x, a) ((x) & ~((a) - 1))
template<class T>
inline void Clamp(T* val, const T& min, const T& max)
{
if (*val < min)
*val = min;
else if (*val > max)
*val = max;
}
template<class T>
inline T Clamp(const T val, const T& min, const T& max)
{
T ret = val;
Clamp(&ret, min, max);
return ret;
}
}

View file

@ -229,11 +229,10 @@ int NativeMix(short *audio, int num_samples) {
num_samples = __AudioMix(audio, num_samples);
} else {
MixBackgroundAudio(audio, num_samples);
// memset(audio, 0, num_samples * 2 * sizeof(short));
}
#ifdef _WIN32
DSound::DSound_UpdateSound();
DSound_UpdateSound();
#endif
return num_samples;
@ -569,13 +568,13 @@ void NativeInitGraphics() {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
#ifdef _WIN32
DSound::DSound_StartSound(MainWindow::GetHWND(), &Win32Mix);
DSound_StartSound(MainWindow::GetHWND(), &Win32Mix, 44100);
#endif
}
void NativeShutdownGraphics() {
#ifdef _WIN32
DSound::DSound_StopSound();
DSound_StopSound();
#endif
screenManager->deviceLost();

View file

@ -1,15 +1,33 @@
#include "native/thread/threadutil.h"
#include "Common/CommonWindows.h"
#include <dsound.h>
#include "dsoundstream.h"
namespace DSound
{
#define BUFSIZE 0x4000
#define MAXWAIT 20 //ms
class DSoundState {
public:
DSoundState(HWND window, StreamCallback _callback, int sampleRate);
bool Init(); // If fails, can safely delete the object
bool createBuffer();
bool writeDataToBuffer(DWORD dwOffset, // Our own write cursor.
char* soundData, // Start of our data.
DWORD dwSoundBytes); // Size of block to copy.
inline int ModBufferSize(int x) { return (x + bufferSize) % bufferSize; }
int RunThread();
void UpdateSound();
void StopSound();
int GetCurSample();
int GetSampleRate() { return sampleRate; }
private:
CRITICAL_SECTION soundCriticalSection;
HWND window_;
HANDLE soundSyncEvent = NULL;
HANDLE hThread = NULL;
@ -24,215 +42,233 @@ namespace DSound
volatile int threadData;
inline int RoundDown128(int x)
{
return x & (~127);
}
int DSound_GetSampleRate()
{
return sampleRate;
}
bool createBuffer()
{
PCMWAVEFORMAT pcmwf;
DSBUFFERDESC dsbdesc;
memset(&pcmwf, 0, sizeof(PCMWAVEFORMAT));
memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
pcmwf.wf.wFormatTag = WAVE_FORMAT_PCM;
pcmwf.wf.nChannels = 2;
pcmwf.wf.nSamplesPerSec = sampleRate = 44100;
pcmwf.wf.nBlockAlign = 4;
pcmwf.wf.nAvgBytesPerSec = pcmwf.wf.nSamplesPerSec * pcmwf.wf.nBlockAlign;
pcmwf.wBitsPerSample = 16;
dsbdesc.dwSize = sizeof(DSBUFFERDESC);
dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS; // //DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLFREQUENCY;
dsbdesc.dwBufferBytes = bufferSize = BUFSIZE; //FIX32(pcmwf.wf.nAvgBytesPerSec); //change to set buffer size
dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&pcmwf;
if (SUCCEEDED(ds->CreateSoundBuffer(&dsbdesc, &dsBuffer, NULL)))
{
dsBuffer->SetCurrentPosition(0);
return true;
}
else
{
dsBuffer = NULL;
return false;
}
}
bool writeDataToBuffer(DWORD dwOffset, // Our own write cursor.
char* soundData, // Start of our data.
DWORD dwSoundBytes) // Size of block to copy.
{
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);
// If the buffer was lost, restore and retry lock.
/*
if (DSERR_BUFFERLOST == hr) {
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);
return true;
}/*
else
{
char temp[8];
sprintf(temp,"%i\n",hr);
OutputDebugStringUTF8(temp);
}*/
return false;
}
inline int ModBufferSize(int x)
{
return (x+bufferSize)%bufferSize;
}
int currentPos;
int lastPos;
short realtimeBuffer[BUFSIZE * 2];
};
unsigned int WINAPI soundThread(void *)
{
setCurrentThreadName("DSound");
currentPos = 0;
lastPos = 0;
//writeDataToBuffer(0,realtimeBuffer,bufferSize);
// dsBuffer->Lock(0, bufferSize, (void **)&p1, &num1, (void **)&p2, &num2, 0);
// TODO: Get rid of this
static DSoundState *g_dsound;
dsBuffer->Play(0,0,DSBPLAY_LOOPING);
while (!threadData)
{
EnterCriticalSection(&soundCriticalSection);
inline int RoundDown128(int x) {
return x & (~127);
}
dsBuffer->GetCurrentPosition((DWORD *)&currentPos, 0);
int numBytesToRender = RoundDown128(ModBufferSize(currentPos - lastPos));
int DSound_GetSampleRate() {
return g_dsound->GetSampleRate();
}
if (numBytesToRender >= 256)
{
int numBytesRendered = 4 * (*callback)(realtimeBuffer, numBytesToRender >> 2, 16, 44100, 2);
//We need to copy the full buffer, regardless of what the mixer claims to have filled
//If we don't do this then the sound will loop if the sound stops and the mixer writes only zeroes
numBytesRendered = numBytesToRender;
writeDataToBuffer(lastPos, (char *) realtimeBuffer, numBytesRendered);
bool DSoundState::createBuffer() {
PCMWAVEFORMAT pcmwf;
DSBUFFERDESC dsbdesc;
currentPos = ModBufferSize(lastPos + numBytesRendered);
totalRenderedBytes += numBytesRendered;
memset(&pcmwf, 0, sizeof(PCMWAVEFORMAT));
memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
lastPos = currentPos;
}
bufferSize = BUFSIZE;
LeaveCriticalSection(&soundCriticalSection);
WaitForSingleObject(soundSyncEvent, MAXWAIT);
}
dsBuffer->Stop();
pcmwf.wf.wFormatTag = WAVE_FORMAT_PCM;
pcmwf.wf.nChannels = 2;
pcmwf.wf.nSamplesPerSec = sampleRate;
pcmwf.wf.nBlockAlign = 4;
pcmwf.wf.nAvgBytesPerSec = pcmwf.wf.nSamplesPerSec * pcmwf.wf.nBlockAlign;
pcmwf.wBitsPerSample = 16;
threadData = 2;
return 0;
}
dsbdesc.dwSize = sizeof(DSBUFFERDESC);
dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS; // //DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLFREQUENCY;
dsbdesc.dwBufferBytes = bufferSize; //FIX32(pcmwf.wf.nAvgBytesPerSec); //change to set buffer size
dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&pcmwf;
bool DSound_StartSound(HWND window, StreamCallback _callback)
{
callback = _callback;
threadData=0;
soundSyncEvent=CreateEvent(0,false,false,0);
InitializeCriticalSection(&soundCriticalSection);
if (FAILED(DirectSoundCreate8(0,&ds,0)))
return false;
ds->SetCooperativeLevel(window,DSSCL_PRIORITY);
if (!createBuffer())
return false;
DWORD num1;
short *p1;
dsBuffer->Lock(0, bufferSize, (void **)&p1, &num1, 0, 0, 0);
memset(p1,0,num1);
dsBuffer->Unlock(p1,num1,0,0);
totalRenderedBytes = -bufferSize;
hThread = (HANDLE)_beginthreadex(0, 0, soundThread, 0, 0, 0);
SetThreadPriority(hThread, THREAD_PRIORITY_ABOVE_NORMAL);
if (SUCCEEDED(ds->CreateSoundBuffer(&dsbdesc, &dsBuffer, NULL))) {
dsBuffer->SetCurrentPosition(0);
return true;
}
void DSound_UpdateSound()
{
if (soundSyncEvent != NULL)
SetEvent(soundSyncEvent);
}
void DSound_StopSound()
{
if (!dsBuffer)
return;
EnterCriticalSection(&soundCriticalSection);
if (threadData == 0)
threadData = 1;
if (hThread != NULL)
{
WaitForSingleObject(hThread, 1000);
CloseHandle(hThread);
hThread = NULL;
}
if (threadData == 2)
{
if (dsBuffer != NULL)
dsBuffer->Release();
dsBuffer = NULL;
if (ds != NULL)
ds->Release();
ds = NULL;
}
if (soundSyncEvent != NULL)
CloseHandle(soundSyncEvent);
soundSyncEvent = NULL;
LeaveCriticalSection(&soundCriticalSection);
}
int DSound_GetCurSample()
{
EnterCriticalSection(&soundCriticalSection);
int playCursor;
dsBuffer->GetCurrentPosition((DWORD *)&playCursor,0);
playCursor = ModBufferSize(playCursor-lastPos)+totalRenderedBytes;
LeaveCriticalSection(&soundCriticalSection);
return playCursor;
}
float DSound_GetTimer()
{
return (float)DSound_GetCurSample()*(1.0f/(4.0f*44100.0f));
} else {
dsBuffer = NULL;
return false;
}
}
bool DSoundState::writeDataToBuffer(DWORD dwOffset, // Our own write cursor.
char* soundData, // Start of our data.
DWORD dwSoundBytes) { // Size of block to copy.
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);
// If the buffer was lost, restore and retry lock.
/*
if (DSERR_BUFFERLOST == hr) {
dsBuffer->Restore();
hr=dsBuffer->Lock(dwOffset, dwSoundBytes, &ptr1, &numBytes1, &ptr2, &numBytes2, 0);
} */
if (SUCCEEDED(hr)) {
memcpy(ptr1, soundData, numBytes1);
if (ptr2)
memcpy(ptr2, soundData+numBytes1, numBytes2);
// Release the data back to DirectSound.
dsBuffer->Unlock(ptr1, numBytes1, ptr2, numBytes2);
return true;
}/*
else
{
char temp[8];
sprintf(temp,"%i\n",hr);
OutputDebugStringUTF8(temp);
}*/
return false;
}
unsigned int WINAPI soundThread(void *param) {
DSoundState *state = (DSoundState *)param;
return state->RunThread();
}
int DSoundState::RunThread() {
setCurrentThreadName("DSound");
currentPos = 0;
lastPos = 0;
//writeDataToBuffer(0,realtimeBuffer,bufferSize);
// dsBuffer->Lock(0, bufferSize, (void **)&p1, &num1, (void **)&p2, &num2, 0);
dsBuffer->Play(0,0,DSBPLAY_LOOPING);
while (!threadData) {
EnterCriticalSection(&soundCriticalSection);
dsBuffer->GetCurrentPosition((DWORD *)&currentPos, 0);
int numBytesToRender = RoundDown128(ModBufferSize(currentPos - lastPos));
if (numBytesToRender >= 256) {
int numBytesRendered = 4 * (*callback)(realtimeBuffer, numBytesToRender >> 2, 16, 44100, 2);
//We need to copy the full buffer, regardless of what the mixer claims to have filled
//If we don't do this then the sound will loop if the sound stops and the mixer writes only zeroes
numBytesRendered = numBytesToRender;
writeDataToBuffer(lastPos, (char *) realtimeBuffer, numBytesRendered);
currentPos = ModBufferSize(lastPos + numBytesRendered);
totalRenderedBytes += numBytesRendered;
lastPos = currentPos;
}
LeaveCriticalSection(&soundCriticalSection);
WaitForSingleObject(soundSyncEvent, MAXWAIT);
}
dsBuffer->Stop();
threadData = 2;
return 0;
}
DSoundState::DSoundState(HWND window, StreamCallback _callback, int sampleRate)
: window_(window), callback(_callback), sampleRate(sampleRate) {
callback = _callback;
threadData=0;
}
bool DSoundState::Init() {
soundSyncEvent = CreateEvent(0, false, false, 0);
InitializeCriticalSection(&soundCriticalSection);
if (FAILED(DirectSoundCreate8(0,&ds,0))) {
CloseHandle(soundSyncEvent);
DeleteCriticalSection(&soundCriticalSection);
return false;
}
ds->SetCooperativeLevel(window_, DSSCL_PRIORITY);
if (!createBuffer())
return false;
DWORD num1;
short *p1;
dsBuffer->Lock(0, bufferSize, (void **)&p1, &num1, 0, 0, 0);
memset(p1,0,num1);
dsBuffer->Unlock(p1,num1,0,0);
totalRenderedBytes = -bufferSize;
hThread = (HANDLE)_beginthreadex(0, 0, soundThread, (void *)this, 0, 0);
SetThreadPriority(hThread, THREAD_PRIORITY_ABOVE_NORMAL);
return true;
}
void DSoundState::UpdateSound() {
if (soundSyncEvent != NULL)
SetEvent(soundSyncEvent);
}
void DSoundState::StopSound() {
if (!dsBuffer)
return;
EnterCriticalSection(&soundCriticalSection);
if (threadData == 0) {
threadData = 1;
}
if (hThread != NULL) {
WaitForSingleObject(hThread, 1000);
CloseHandle(hThread);
hThread = NULL;
}
if (threadData == 2) {
if (dsBuffer != NULL)
dsBuffer->Release();
dsBuffer = NULL;
if (ds != NULL)
ds->Release();
ds = NULL;
}
if (soundSyncEvent != NULL) {
CloseHandle(soundSyncEvent);
}
soundSyncEvent = NULL;
LeaveCriticalSection(&soundCriticalSection);
DeleteCriticalSection(&soundCriticalSection);
}
int DSoundState::GetCurSample() {
EnterCriticalSection(&soundCriticalSection);
int playCursor;
dsBuffer->GetCurrentPosition((DWORD *)&playCursor,0);
playCursor = ModBufferSize(playCursor-lastPos)+totalRenderedBytes;
LeaveCriticalSection(&soundCriticalSection);
return playCursor;
}
void DSound_UpdateSound() {
g_dsound->UpdateSound();
}
bool DSound_StartSound(HWND window, StreamCallback _callback, int sampleRate) {
g_dsound = new DSoundState(window, _callback, sampleRate);
if (!g_dsound->Init()) {
delete g_dsound;
g_dsound = NULL;
return false;
}
return true;
}
void DSound_StopSound() {
g_dsound->StopSound();
delete g_dsound;
g_dsound = NULL;
}
int DSound_GetCurSample() {
return g_dsound->GetCurSample();
}
float DSound_GetTimer() {
return (float)g_dsound->GetCurSample()*(1.0f/(4.0f*44100.0f));
}

View file

@ -3,18 +3,14 @@
#include "Common/CommonWindows.h"
namespace DSound
{
typedef int (*StreamCallback)(short *buffer, int numSamples, int bits, int rate, int channels);
typedef int (*StreamCallback)(short *buffer, int numSamples, int bits, int rate, int channels);
bool DSound_StartSound(HWND window, StreamCallback _callback);
void DSound_UpdateSound();
void DSound_StopSound();
float DSound_GetTimer();
int DSound_GetCurSample();
int DSound_GetSampleRate();
}
bool DSound_StartSound(HWND window, StreamCallback _callback, int sampleRate = 44100);
void DSound_UpdateSound();
void DSound_StopSound();
float DSound_GetTimer();
int DSound_GetCurSample();
int DSound_GetSampleRate();
#endif //__SOUNDSTREAM_H__

View file

@ -128,7 +128,7 @@ void WindowsHost::InitSound()
void WindowsHost::UpdateSound()
{
DSound::DSound_UpdateSound();
DSound_UpdateSound();
}
void WindowsHost::ShutdownSound()