Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DSound: use DSound notifications to produce sound. #256

Merged
merged 1 commit into from May 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 18 additions & 8 deletions Source/Core/AudioCommon/DSoundStream.cpp
Expand Up @@ -27,7 +27,7 @@ bool DSound::CreateBuffer()

// Fill out DSound buffer description.
dsbdesc.dwSize = sizeof(DSBUFFERDESC);
dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRLVOLUME | DSBCAPS_GLOBALFOCUS;
dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRLVOLUME | DSBCAPS_GLOBALFOCUS | DSBCAPS_CTRLPOSITIONNOTIFY;
dsbdesc.dwBufferBytes = bufferSize = BUFSIZE;
dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&pcmwf;
dsbdesc.guid3DAlgorithm = DS3DALG_DEFAULT;
Expand All @@ -37,6 +37,20 @@ bool DSound::CreateBuffer()
{
dsBuffer->SetCurrentPosition(0);
dsBuffer->SetVolume(m_volume);

soundSyncEvent = CreateEvent(NULL, TRUE, FALSE, TEXT("DSound Buffer Notification"));

IDirectSoundNotify *dsnotify;
dsBuffer->QueryInterface(IID_IDirectSoundNotify, (void**)&dsnotify);
DSBPOSITIONNOTIFY notify_positions[3];
for (unsigned i = 0; i < ARRAYSIZE(notify_positions); ++i)
{
notify_positions[i].dwOffset = i * (BUFSIZE / ARRAYSIZE(notify_positions));
notify_positions[i].hEventNotify = soundSyncEvent;
}
dsnotify->SetNotificationPositions(ARRAYSIZE(notify_positions), notify_positions);
dsnotify->Release();

return true;
}
else
Expand Down Expand Up @@ -101,7 +115,7 @@ void DSound::SoundLoop()
WriteDataToBuffer(lastPos, (char*)realtimeBuffer, numBytesToRender);
lastPos = ModBufferSize(lastPos + numBytesToRender);
}
soundSyncEvent.Wait();
WaitForSingleObject(soundSyncEvent, INFINITE);
}
}

Expand Down Expand Up @@ -134,11 +148,6 @@ void DSound::SetVolume(int volume)
dsBuffer->SetVolume(m_volume);
}

void DSound::Update()
{
soundSyncEvent.Set();
}

void DSound::Clear(bool mute)
{
m_muted = mute;
Expand All @@ -160,11 +169,12 @@ void DSound::Stop()
{
threadData = 1;
// kick the thread if it's waiting
soundSyncEvent.Set();
SetEvent(soundSyncEvent);

thread.join();
dsBuffer->Stop();
dsBuffer->Release();
ds->Release();
CloseHandle(soundSyncEvent);
}

5 changes: 2 additions & 3 deletions Source/Core/AudioCommon/DSoundStream.h
Expand Up @@ -12,14 +12,14 @@
#include <mmsystem.h>
#include <dsound.h>

#define BUFSIZE (1024 * 8 * 4)
#define BUFSIZE (256 * 8 * 4)
#endif

class DSound final : public SoundStream
{
#ifdef _WIN32
std::thread thread;
Common::Event soundSyncEvent;
HANDLE soundSyncEvent;
void *hWnd;

IDirectSound8* ds;
Expand Down Expand Up @@ -65,7 +65,6 @@ class DSound final : public SoundStream
virtual void Stop();
virtual void Clear(bool mute);
static bool isValid() { return true; }
virtual void Update();

#else
public:
Expand Down