2015-02-01 17:31:51 +00:00
|
|
|
#include "Common/CommonWindows.h"
|
2015-02-01 10:56:44 +00:00
|
|
|
#include <dsound.h>
|
|
|
|
|
2013-08-19 06:25:14 +00:00
|
|
|
#include "native/thread/threadutil.h"
|
2015-01-11 22:19:49 +00:00
|
|
|
#include "Core/Reporting.h"
|
2015-01-11 22:31:21 +00:00
|
|
|
#include "Core/Util/AudioFormat.h"
|
2015-02-01 10:56:44 +00:00
|
|
|
#include "Windows/W32Util/Misc.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
#include "dsoundstream.h"
|
|
|
|
|
2015-01-11 20:44:31 +00:00
|
|
|
// WASAPI begin
|
|
|
|
#include <Objbase.h>
|
|
|
|
#include <Mmreg.h>
|
|
|
|
#include <MMDeviceAPI.h>
|
|
|
|
#include <AudioClient.h>
|
|
|
|
#include <AudioPolicy.h>
|
|
|
|
|
|
|
|
#pragma comment(lib, "ole32.lib")
|
|
|
|
|
|
|
|
const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
|
|
|
|
const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
|
|
|
|
const IID IID_IAudioClient = __uuidof(IAudioClient);
|
|
|
|
const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient);
|
|
|
|
// WASAPI end
|
|
|
|
|
2013-05-10 19:36:28 +00:00
|
|
|
#define BUFSIZE 0x4000
|
2012-11-01 15:19:01 +00:00
|
|
|
#define MAXWAIT 20 //ms
|
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
class DSoundAudioBackend : public WindowsAudioBackend {
|
2015-01-11 13:11:09 +00:00
|
|
|
public:
|
2015-01-11 20:00:56 +00:00
|
|
|
DSoundAudioBackend();
|
|
|
|
~DSoundAudioBackend() override;
|
2015-01-11 13:11:09 +00:00
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
bool Init(HWND window, StreamCallback callback, int sampleRate) override; // If fails, can safely delete the object
|
|
|
|
void Update() override;
|
|
|
|
int GetSampleRate() override { return sampleRate_; }
|
2015-01-11 13:11:09 +00:00
|
|
|
|
|
|
|
private:
|
2015-01-11 20:00:56 +00:00
|
|
|
inline int ModBufferSize(int x) { return (x + bufferSize_) % bufferSize_; }
|
|
|
|
int RunThread();
|
|
|
|
static unsigned int WINAPI soundThread(void *param);
|
2015-01-11 18:30:25 +00:00
|
|
|
bool CreateBuffer();
|
2015-01-11 20:00:56 +00:00
|
|
|
bool WriteDataToBuffer(DWORD offset, // Our own write cursor.
|
2015-01-11 18:30:25 +00:00
|
|
|
char* soundData, // Start of our data.
|
2015-01-11 20:00:56 +00:00
|
|
|
DWORD soundBytes); // Size of block to copy.
|
2015-01-11 18:30:25 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
CRITICAL_SECTION soundCriticalSection;
|
2015-01-11 13:11:09 +00:00
|
|
|
HWND window_;
|
2015-01-11 20:00:56 +00:00
|
|
|
HANDLE soundSyncEvent_ = NULL;
|
|
|
|
HANDLE hThread_ = NULL;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
StreamCallback callback_;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
IDirectSound8 *ds_ = NULL;
|
|
|
|
IDirectSoundBuffer *dsBuffer_ = NULL;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
int bufferSize_; // bytes
|
|
|
|
int totalRenderedBytes_;
|
|
|
|
int sampleRate_;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
volatile int threadData_;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
int currentPos_;
|
|
|
|
int lastPos_;
|
|
|
|
short realtimeBuffer_[BUFSIZE * 2];
|
2015-01-11 13:11:09 +00:00
|
|
|
};
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-01-11 13:11:09 +00:00
|
|
|
// TODO: Get rid of this
|
2015-01-11 20:00:56 +00:00
|
|
|
static DSoundAudioBackend *g_dsound;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-01-11 13:11:09 +00:00
|
|
|
inline int RoundDown128(int x) {
|
|
|
|
return x & (~127);
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-02-01 10:56:44 +00:00
|
|
|
unsigned int WINAPI DSoundAudioBackend::soundThread(void *param) {
|
|
|
|
DSoundAudioBackend *dsound = (DSoundAudioBackend *)param;
|
|
|
|
return dsound->RunThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DSoundAudioBackend::WriteDataToBuffer(DWORD offset, // Our own write cursor.
|
|
|
|
char* soundData, // Start of our data.
|
|
|
|
DWORD soundBytes) { // 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(offset, soundBytes, &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;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
bool DSoundAudioBackend::CreateBuffer() {
|
2015-01-11 13:11:09 +00:00
|
|
|
PCMWAVEFORMAT pcmwf;
|
|
|
|
DSBUFFERDESC dsbdesc;
|
|
|
|
|
|
|
|
memset(&pcmwf, 0, sizeof(PCMWAVEFORMAT));
|
|
|
|
memset(&dsbdesc, 0, sizeof(DSBUFFERDESC));
|
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
bufferSize_ = BUFSIZE;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-01-11 13:11:09 +00:00
|
|
|
pcmwf.wf.wFormatTag = WAVE_FORMAT_PCM;
|
|
|
|
pcmwf.wf.nChannels = 2;
|
2015-01-11 20:00:56 +00:00
|
|
|
pcmwf.wf.nSamplesPerSec = sampleRate_;
|
2015-01-11 13:11:09 +00:00
|
|
|
pcmwf.wf.nBlockAlign = 4;
|
|
|
|
pcmwf.wf.nAvgBytesPerSec = pcmwf.wf.nSamplesPerSec * pcmwf.wf.nBlockAlign;
|
|
|
|
pcmwf.wBitsPerSample = 16;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-01-11 13:11:09 +00:00
|
|
|
dsbdesc.dwSize = sizeof(DSBUFFERDESC);
|
|
|
|
dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS; // //DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLFREQUENCY;
|
2015-01-11 20:00:56 +00:00
|
|
|
dsbdesc.dwBufferBytes = bufferSize_; //FIX32(pcmwf.wf.nAvgBytesPerSec); //change to set buffer size
|
2015-01-11 13:11:09 +00:00
|
|
|
dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&pcmwf;
|
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
if (SUCCEEDED(ds_->CreateSoundBuffer(&dsbdesc, &dsBuffer_, NULL))) {
|
|
|
|
dsBuffer_->SetCurrentPosition(0);
|
2015-01-11 13:11:09 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
2015-01-11 20:00:56 +00:00
|
|
|
dsBuffer_ = NULL;
|
2015-01-11 13:11:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-02-01 10:56:44 +00:00
|
|
|
int DSoundAudioBackend::RunThread() {
|
|
|
|
if (FAILED(DirectSoundCreate8(0, &ds_, 0))) {
|
|
|
|
ds_ = NULL;
|
|
|
|
threadData_ = 2;
|
|
|
|
return 1;
|
|
|
|
}
|
2015-01-11 13:11:09 +00:00
|
|
|
|
2015-02-01 10:56:44 +00:00
|
|
|
ds_->SetCooperativeLevel(window_, DSSCL_PRIORITY);
|
|
|
|
if (!CreateBuffer()) {
|
|
|
|
ds_->Release();
|
|
|
|
ds_ = NULL;
|
|
|
|
threadData_ = 2;
|
|
|
|
return 1;
|
|
|
|
}
|
2015-01-11 13:11:09 +00:00
|
|
|
|
2015-02-01 10:56:44 +00:00
|
|
|
soundSyncEvent_ = CreateEvent(0, false, false, 0);
|
|
|
|
InitializeCriticalSection(&soundCriticalSection);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-02-01 10:56:44 +00:00
|
|
|
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_;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-01-11 13:11:09 +00:00
|
|
|
setCurrentThreadName("DSound");
|
2015-01-11 20:00:56 +00:00
|
|
|
currentPos_ = 0;
|
|
|
|
lastPos_ = 0;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
dsBuffer_->Play(0,0,DSBPLAY_LOOPING);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
while (!threadData_) {
|
2015-01-11 13:11:09 +00:00
|
|
|
EnterCriticalSection(&soundCriticalSection);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
dsBuffer_->GetCurrentPosition((DWORD *)¤tPos_, 0);
|
|
|
|
int numBytesToRender = RoundDown128(ModBufferSize(currentPos_ - lastPos_));
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-01-11 13:11:09 +00:00
|
|
|
if (numBytesToRender >= 256) {
|
2015-01-11 20:00:56 +00:00
|
|
|
int numBytesRendered = 4 * (*callback_)(realtimeBuffer_, numBytesToRender >> 2, 16, 44100, 2);
|
2015-01-11 13:11:09 +00:00
|
|
|
//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;
|
2015-01-11 20:00:56 +00:00
|
|
|
WriteDataToBuffer(lastPos_, (char *) realtimeBuffer_, numBytesRendered);
|
2015-01-11 13:11:09 +00:00
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
currentPos_ = ModBufferSize(lastPos_ + numBytesRendered);
|
|
|
|
totalRenderedBytes_ += numBytesRendered;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
lastPos_ = currentPos_;
|
2015-01-11 13:11:09 +00:00
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-01-11 13:11:09 +00:00
|
|
|
LeaveCriticalSection(&soundCriticalSection);
|
2015-01-11 20:00:56 +00:00
|
|
|
WaitForSingleObject(soundSyncEvent_, MAXWAIT);
|
2015-01-11 13:11:09 +00:00
|
|
|
}
|
2015-01-11 20:00:56 +00:00
|
|
|
dsBuffer_->Stop();
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-02-01 10:56:44 +00:00
|
|
|
dsBuffer_->Release();
|
|
|
|
ds_->Release();
|
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
threadData_ = 2;
|
2015-01-11 13:11:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
DSoundAudioBackend::DSoundAudioBackend() : threadData_(0), ds_(nullptr) {
|
2015-01-11 13:11:09 +00:00
|
|
|
}
|
2013-10-13 06:05:06 +00:00
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
DSoundAudioBackend::~DSoundAudioBackend() {
|
|
|
|
if (!ds_)
|
|
|
|
return;
|
2013-08-19 06:25:14 +00:00
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
if (!dsBuffer_)
|
2015-01-11 13:11:09 +00:00
|
|
|
return;
|
2013-08-19 06:25:14 +00:00
|
|
|
|
2015-01-11 13:11:09 +00:00
|
|
|
EnterCriticalSection(&soundCriticalSection);
|
2013-08-19 06:25:14 +00:00
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
if (threadData_ == 0) {
|
|
|
|
threadData_ = 1;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
if (hThread_ != NULL) {
|
|
|
|
WaitForSingleObject(hThread_, 1000);
|
|
|
|
CloseHandle(hThread_);
|
|
|
|
hThread_ = NULL;
|
2015-01-11 13:11:09 +00:00
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
if (soundSyncEvent_ != NULL) {
|
|
|
|
CloseHandle(soundSyncEvent_);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2015-01-11 20:00:56 +00:00
|
|
|
soundSyncEvent_ = NULL;
|
2015-01-11 13:11:09 +00:00
|
|
|
LeaveCriticalSection(&soundCriticalSection);
|
|
|
|
DeleteCriticalSection(&soundCriticalSection);
|
|
|
|
}
|
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
bool DSoundAudioBackend::Init(HWND window, StreamCallback _callback, int sampleRate) {
|
|
|
|
window_ = window;
|
|
|
|
callback_ = _callback;
|
|
|
|
sampleRate_ = sampleRate;
|
|
|
|
threadData_ = 0;
|
|
|
|
hThread_ = (HANDLE)_beginthreadex(0, 0, soundThread, (void *)this, 0, 0);
|
|
|
|
SetThreadPriority(hThread_, THREAD_PRIORITY_ABOVE_NORMAL);
|
2015-01-11 13:11:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
void DSoundAudioBackend::Update() {
|
|
|
|
if (soundSyncEvent_ != NULL)
|
|
|
|
SetEvent(soundSyncEvent_);
|
2015-01-11 13:11:09 +00:00
|
|
|
}
|
2015-01-11 20:00:56 +00:00
|
|
|
|
2015-01-11 20:44:31 +00:00
|
|
|
class WASAPIAudioBackend : public WindowsAudioBackend {
|
|
|
|
public:
|
|
|
|
WASAPIAudioBackend();
|
|
|
|
~WASAPIAudioBackend() override;
|
|
|
|
|
|
|
|
bool Init(HWND window, StreamCallback callback, int sampleRate) override; // If fails, can safely delete the object
|
|
|
|
void Update() override {}
|
|
|
|
int GetSampleRate() override { return sampleRate_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int RunThread();
|
|
|
|
static unsigned int WINAPI soundThread(void *param);
|
|
|
|
|
|
|
|
HANDLE hThread_;
|
|
|
|
|
|
|
|
StreamCallback callback_;
|
|
|
|
int sampleRate_;
|
|
|
|
|
|
|
|
volatile int threadData_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: Make these adjustable. This is from the example in MSDN.
|
|
|
|
// 200 times/sec = 5ms, pretty good :) Wonder if all computers can handle it though.
|
|
|
|
#define REFTIMES_PER_SEC (10000000/200)
|
|
|
|
#define REFTIMES_PER_MILLISEC (REFTIMES_PER_SEC / 1000)
|
|
|
|
|
|
|
|
WASAPIAudioBackend::WASAPIAudioBackend() : hThread_(NULL), sampleRate_(0), callback_(nullptr), threadData_(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
WASAPIAudioBackend::~WASAPIAudioBackend() {
|
|
|
|
if (threadData_ == 0) {
|
|
|
|
threadData_ = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hThread_ != NULL) {
|
|
|
|
WaitForSingleObject(hThread_, 1000);
|
|
|
|
CloseHandle(hThread_);
|
|
|
|
hThread_ = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (threadData_ == 2) {
|
|
|
|
// blah.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int WINAPI WASAPIAudioBackend::soundThread(void *param) {
|
|
|
|
WASAPIAudioBackend *backend = (WASAPIAudioBackend *)param;
|
|
|
|
return backend->RunThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WASAPIAudioBackend::Init(HWND window, StreamCallback callback, int sampleRate) {
|
|
|
|
threadData_ = 0;
|
|
|
|
callback_ = callback;
|
|
|
|
sampleRate_ = sampleRate;
|
|
|
|
hThread_ = (HANDLE)_beginthreadex(0, 0, soundThread, (void *)this, 0, 0);
|
|
|
|
SetThreadPriority(hThread_, THREAD_PRIORITY_ABOVE_NORMAL);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WASAPIAudioBackend::RunThread() {
|
2015-01-11 22:19:49 +00:00
|
|
|
// Adapted from http://msdn.microsoft.com/en-us/library/windows/desktop/dd316756(v=vs.85).aspx
|
|
|
|
|
2015-02-01 10:56:44 +00:00
|
|
|
CoInitializeEx(NULL, COINIT_MULTITHREADED);
|
2015-01-11 22:19:49 +00:00
|
|
|
setCurrentThreadName("WASAPI_audio");
|
|
|
|
|
2015-01-11 20:44:31 +00:00
|
|
|
IMMDeviceEnumerator *pDeviceEnumerator;
|
|
|
|
IMMDevice *pDevice;
|
|
|
|
IAudioClient *pAudioInterface;
|
|
|
|
IAudioRenderClient *pAudioRenderClient;
|
|
|
|
WAVEFORMATEXTENSIBLE *pDeviceFormat;
|
|
|
|
DWORD flags = 0;
|
|
|
|
REFERENCE_TIME hnsBufferDuration, hnsActualDuration;
|
|
|
|
UINT32 pNumBufferFrames;
|
|
|
|
UINT32 pNumPaddingFrames, pNumAvFrames;
|
|
|
|
hnsBufferDuration = REFTIMES_PER_SEC;
|
|
|
|
|
|
|
|
HRESULT hresult;
|
|
|
|
hresult = CoCreateInstance(CLSID_MMDeviceEnumerator,
|
|
|
|
NULL, /*Object is not created as the part of the aggregate */
|
|
|
|
CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&pDeviceEnumerator);
|
2015-02-01 17:31:51 +00:00
|
|
|
if (FAILED(hresult)) goto bail;
|
2015-01-11 20:44:31 +00:00
|
|
|
|
|
|
|
hresult = pDeviceEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &pDevice);
|
2015-02-01 17:31:51 +00:00
|
|
|
if (FAILED(hresult)) {
|
|
|
|
pDeviceEnumerator->Release();
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2015-01-11 20:44:31 +00:00
|
|
|
hresult = pDevice->Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void**)&pAudioInterface);
|
2015-02-01 17:31:51 +00:00
|
|
|
if (FAILED(hresult)) {
|
|
|
|
pDevice->Release();
|
|
|
|
pDeviceEnumerator->Release();
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
2015-01-11 20:44:31 +00:00
|
|
|
hresult = pAudioInterface->GetMixFormat((WAVEFORMATEX**)&pDeviceFormat);
|
|
|
|
hresult = pAudioInterface->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, hnsBufferDuration, 0, &pDeviceFormat->Format, NULL);
|
|
|
|
hresult = pAudioInterface->GetService(IID_IAudioRenderClient, (void**)&pAudioRenderClient);
|
2015-02-01 17:31:51 +00:00
|
|
|
if (FAILED(hresult)) {
|
|
|
|
pDevice->Release();
|
|
|
|
pDeviceEnumerator->Release();
|
|
|
|
pAudioInterface->Release();
|
|
|
|
goto bail;
|
|
|
|
}
|
2015-01-11 20:44:31 +00:00
|
|
|
hresult = pAudioInterface->GetBufferSize(&pNumBufferFrames);
|
2015-02-01 17:31:51 +00:00
|
|
|
if (FAILED(hresult)) {
|
|
|
|
pDevice->Release();
|
|
|
|
pDeviceEnumerator->Release();
|
|
|
|
pAudioInterface->Release();
|
|
|
|
goto bail;
|
|
|
|
}
|
2015-01-11 20:44:31 +00:00
|
|
|
|
|
|
|
sampleRate_ = pDeviceFormat->Format.nSamplesPerSec;
|
|
|
|
|
2015-01-12 19:24:09 +00:00
|
|
|
enum {
|
|
|
|
UNKNOWN_FORMAT = 0,
|
|
|
|
IEEE_FLOAT = 1,
|
|
|
|
PCM16 = 2,
|
|
|
|
} format = UNKNOWN_FORMAT;
|
|
|
|
|
|
|
|
// Don't know if PCM16 ever shows up here, the documentation only talks about float... but let's blindly
|
|
|
|
// try to support it :P
|
|
|
|
|
|
|
|
if (pDeviceFormat->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
|
2015-01-11 20:44:31 +00:00
|
|
|
if (!memcmp(&pDeviceFormat->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, sizeof(pDeviceFormat->SubFormat))) {
|
2015-01-12 19:24:09 +00:00
|
|
|
format = IEEE_FLOAT;
|
2015-01-11 20:44:31 +00:00
|
|
|
// printf("float format\n");
|
|
|
|
} else {
|
2015-01-12 19:24:09 +00:00
|
|
|
ERROR_LOG_REPORT_ONCE(unexpectedformat, SCEAUDIO, "Got unexpected WASAPI 0xFFFE stream format, expected float!");
|
|
|
|
if (pDeviceFormat->Format.wBitsPerSample == 16 && pDeviceFormat->Format.nChannels == 2) {
|
|
|
|
format = PCM16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ERROR_LOG_REPORT_ONCE(unexpectedformat2, SCEAUDIO, "Got unexpected non-extensible WASAPI stream format, expected extensible float!");
|
|
|
|
if (pDeviceFormat->Format.wBitsPerSample == 16 && pDeviceFormat->Format.nChannels == 2) {
|
|
|
|
format = PCM16;
|
2015-01-11 20:44:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 19:24:09 +00:00
|
|
|
short *shortBuf = nullptr;
|
|
|
|
|
2015-01-11 20:44:31 +00:00
|
|
|
BYTE *pData;
|
|
|
|
hresult = pAudioRenderClient->GetBuffer(pNumBufferFrames, &pData);
|
2015-01-12 19:24:09 +00:00
|
|
|
int numSamples = pNumBufferFrames * pDeviceFormat->Format.nChannels;
|
|
|
|
if (format == IEEE_FLOAT) {
|
|
|
|
memset(pData, 0, sizeof(float) * numSamples);
|
|
|
|
shortBuf = new short[pNumBufferFrames * pDeviceFormat->Format.nChannels];
|
|
|
|
} else if (format == PCM16) {
|
|
|
|
memset(pData, 0, sizeof(short) * numSamples);
|
2015-01-11 20:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hresult = pAudioRenderClient->ReleaseBuffer(pNumBufferFrames, flags);
|
2015-01-11 22:19:49 +00:00
|
|
|
hnsActualDuration = (REFERENCE_TIME)((double)REFTIMES_PER_SEC * pNumBufferFrames / pDeviceFormat->Format.nSamplesPerSec);
|
2015-01-11 20:44:31 +00:00
|
|
|
|
|
|
|
hresult = pAudioInterface->Start();
|
|
|
|
|
2015-01-12 20:34:17 +00:00
|
|
|
while (flags != AUDCLNT_BUFFERFLAGS_SILENT) {
|
2015-01-11 20:44:31 +00:00
|
|
|
Sleep((DWORD)(hnsActualDuration / REFTIMES_PER_MILLISEC / 2));
|
|
|
|
|
|
|
|
hresult = pAudioInterface->GetCurrentPadding(&pNumPaddingFrames);
|
|
|
|
if (FAILED(hresult)) {
|
|
|
|
// What to do?
|
|
|
|
pNumPaddingFrames = 0;
|
|
|
|
}
|
|
|
|
pNumAvFrames = pNumBufferFrames - pNumPaddingFrames;
|
|
|
|
|
|
|
|
hresult = pAudioRenderClient->GetBuffer(pNumAvFrames, &pData);
|
|
|
|
if (FAILED(hresult)) {
|
|
|
|
// What to do?
|
|
|
|
} else if (pNumAvFrames) {
|
2015-01-12 19:24:09 +00:00
|
|
|
switch (format) {
|
|
|
|
case IEEE_FLOAT:
|
|
|
|
callback_(shortBuf, pNumAvFrames, 16, sampleRate_, 2);
|
2015-01-12 19:56:57 +00:00
|
|
|
if (pDeviceFormat->Format.nChannels == 2) {
|
|
|
|
ConvertS16ToF32((float *)pData, shortBuf, pNumAvFrames * pDeviceFormat->Format.nChannels);
|
|
|
|
} else {
|
|
|
|
float *ptr = (float *)pData;
|
|
|
|
int chans = pDeviceFormat->Format.nChannels;
|
|
|
|
memset(ptr, 0, pNumAvFrames * chans * sizeof(float));
|
2015-01-15 20:27:01 +00:00
|
|
|
for (UINT32 i = 0; i < pNumAvFrames; i++) {
|
2015-01-12 19:56:57 +00:00
|
|
|
ptr[i * chans + 0] = (float)shortBuf[i * 2] * (1.0f / 32768.0f);
|
|
|
|
ptr[i * chans + 1] = (float)shortBuf[i * 2 + 1] * (1.0f / 32768.0f);
|
|
|
|
}
|
|
|
|
}
|
2015-01-12 19:24:09 +00:00
|
|
|
break;
|
|
|
|
case PCM16:
|
|
|
|
callback_((short *)pData, pNumAvFrames, 16, sampleRate_, 2);
|
|
|
|
break;
|
|
|
|
}
|
2015-01-11 20:44:31 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 20:34:17 +00:00
|
|
|
if (threadData_ != 0) {
|
|
|
|
flags = AUDCLNT_BUFFERFLAGS_SILENT;
|
|
|
|
}
|
|
|
|
|
2015-01-11 20:44:31 +00:00
|
|
|
hresult = pAudioRenderClient->ReleaseBuffer(pNumAvFrames, flags);
|
|
|
|
if (FAILED(hresult)) {
|
|
|
|
// Not much to do here either...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 20:34:17 +00:00
|
|
|
// Wait for last data in buffer to play before stopping.
|
|
|
|
Sleep((DWORD)(hnsActualDuration / REFTIMES_PER_MILLISEC / 2));
|
|
|
|
|
2015-01-12 19:24:09 +00:00
|
|
|
delete[] shortBuf;
|
2015-01-11 20:44:31 +00:00
|
|
|
hresult = pAudioInterface->Stop();
|
|
|
|
|
|
|
|
CoTaskMemFree(pDeviceFormat);
|
2015-01-13 19:51:38 +00:00
|
|
|
pDeviceEnumerator->Release();
|
|
|
|
pDevice->Release();
|
|
|
|
pAudioInterface->Release();
|
|
|
|
pAudioRenderClient->Release();
|
2015-01-11 20:44:31 +00:00
|
|
|
|
2015-02-01 17:31:51 +00:00
|
|
|
bail:
|
2015-01-11 20:44:31 +00:00
|
|
|
threadData_ = 2;
|
2015-02-01 10:56:44 +00:00
|
|
|
CoUninitialize();
|
2015-01-11 20:44:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-11 20:00:56 +00:00
|
|
|
WindowsAudioBackend *CreateAudioBackend(AudioBackendType type) {
|
2015-02-01 10:56:44 +00:00
|
|
|
if (IsVistaOrHigher()) {
|
|
|
|
switch (type) {
|
|
|
|
case AUDIO_BACKEND_WASAPI:
|
|
|
|
case AUDIO_BACKEND_AUTO:
|
|
|
|
return new WASAPIAudioBackend();
|
|
|
|
case AUDIO_BACKEND_DSOUND:
|
|
|
|
default:
|
|
|
|
return new DSoundAudioBackend();
|
2015-01-11 20:44:31 +00:00
|
|
|
}
|
2015-02-01 10:56:44 +00:00
|
|
|
} else {
|
|
|
|
return new DSoundAudioBackend();
|
2015-01-11 20:44:31 +00:00
|
|
|
}
|
2015-01-11 20:00:56 +00:00
|
|
|
}
|