Update XAudio driver to use floating point. Nits here and there.

This commit is contained in:
Themaister 2011-08-21 19:27:11 +02:00
parent ad85ccb42f
commit cb140c3691
3 changed files with 135 additions and 119 deletions

View File

@ -11,149 +11,154 @@
struct xaudio2 : public IXAudio2VoiceCallback struct xaudio2 : public IXAudio2VoiceCallback
{ {
xaudio2(unsigned samplerate, unsigned channels, unsigned bits, unsigned size) : xaudio2(unsigned samplerate, unsigned channels, unsigned size) :
buf(0), pXAudio2(0), pMasterVoice(0), pSourceVoice(0), buf(0), pXAudio2(0), pMasterVoice(0), pSourceVoice(0),
buffers(0), bufptr(0) buffers(0), bufptr(0)
{ {
CoInitializeEx(0, COINIT_MULTITHREADED); CoInitializeEx(0, COINIT_MULTITHREADED);
HRESULT hr; if (FAILED(XAudio2Create(&pXAudio2, 0)))
if (FAILED(hr = XAudio2Create(&pXAudio2, 0))) throw -1;
throw -1; if (FAILED(pXAudio2->CreateMasteringVoice(
if (FAILED(hr = pXAudio2->CreateMasteringVoice( &pMasterVoice, channels, samplerate, 0, 0, 0)))
&pMasterVoice, channels, samplerate, 0, 0, 0))) throw -1;
throw -1;
WAVEFORMATEX wfx;
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = channels;
wfx.nSamplesPerSec = samplerate;
wfx.nBlockAlign = channels * bits / 8;
wfx.wBitsPerSample = bits;
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
wfx.cbSize = 0;
if (FAILED(hr = pXAudio2->CreateSourceVoice(
&pSourceVoice, &wfx, XAUDIO2_VOICE_NOSRC, XAUDIO2_DEFAULT_FREQ_RATIO, this, 0, 0)))
throw -1;
hEvent = CreateEvent(0, FALSE, FALSE, 0); WAVEFORMATEX wfx;
if (!hEvent) wfx.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
throw -1; wfx.nChannels = channels;
wfx.nSamplesPerSec = samplerate;
wfx.nBlockAlign = channels * sizeof(float);
wfx.wBitsPerSample = sizeof(float) * 8;
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
wfx.cbSize = 0;
if (FAILED(pXAudio2->CreateSourceVoice(
&pSourceVoice, &wfx, XAUDIO2_VOICE_NOSRC, XAUDIO2_DEFAULT_FREQ_RATIO, this, 0, 0)))
throw -1;
pSourceVoice->Start(0); hEvent = CreateEvent(0, FALSE, FALSE, 0);
if (!hEvent)
throw -1;
max_buffers = 8; pSourceVoice->Start(0);
bufsize = size / max_buffers;
buf = new uint8_t[bufsize * max_buffers];
write_buffer = 0;
}
virtual ~xaudio2() bufsize = size / max_buffers;
{ buf = new uint8_t[bufsize * max_buffers];
if (pSourceVoice) write_buffer = 0;
{ }
pSourceVoice->Stop(0);
pSourceVoice->DestroyVoice();
}
if (pMasterVoice) virtual ~xaudio2()
pMasterVoice->DestroyVoice(); {
if (pSourceVoice)
{
pSourceVoice->Stop(0);
pSourceVoice->DestroyVoice();
}
if (pXAudio2) if (pMasterVoice)
pXAudio2->Release(); pMasterVoice->DestroyVoice();
if (hEvent) if (pXAudio2)
CloseHandle(hEvent); pXAudio2->Release();
if (buf) if (hEvent)
delete[] buf; CloseHandle(hEvent);
}
size_t write_avail() const if (buf)
{ delete[] buf;
return bufsize * (max_buffers - buffers - 1); }
}
size_t write(const void *buffer, size_t bytes)
{
size_t written = 0;
while (written < bytes)
{
size_t need = std::min(bytes - written, static_cast<size_t>(bufsize - bufptr));
memcpy(buf + write_buffer * bufsize + bufptr, (const uint8_t*)buffer + written, need);
written += need;
bufptr += need;
if (bufptr == bufsize) size_t write_avail() const
{ {
while (static_cast<volatile unsigned>(buffers) == max_buffers - 1) return bufsize * (max_buffers - buffers - 1);
WaitForSingleObject(hEvent, INFINITE); }
XAUDIO2_BUFFER xa2buffer = {0}; size_t write(const void *buffer_, size_t bytes_)
xa2buffer.AudioBytes = bufsize; {
xa2buffer.pAudioData = buf + write_buffer * bufsize; unsigned bytes = bytes_;
xa2buffer.pContext = 0; const uint8_t *buffer = reinterpret_cast<const uint8_t*>(buffer_);
InterlockedIncrement(&buffers); while (bytes > 0)
pSourceVoice->SubmitSourceBuffer(&xa2buffer); {
bufptr = 0; unsigned need = std::min(bytes, bufsize - bufptr);
write_buffer = (write_buffer + 1) % max_buffers; memcpy(buf + write_buffer * bufsize + bufptr,
} buffer, need);
}
return bytes;
}
STDMETHOD_(void, OnBufferStart) (void *) {} bufptr += need;
STDMETHOD_(void, OnBufferEnd) (void *) buffer += need;
{ bytes -= need;
InterlockedDecrement(&buffers);
SetEvent(hEvent); if (bufptr == bufsize)
} {
WaitForSingleObject(hEvent, 0); // Clear out ready state.
while (buffers == max_buffers - 1)
WaitForSingleObject(hEvent, INFINITE);
XAUDIO2_BUFFER xa2buffer = {0};
xa2buffer.AudioBytes = bufsize;
xa2buffer.pAudioData = buf + write_buffer * bufsize;
xa2buffer.pContext = 0;
if (FAILED(pSourceVoice->SubmitSourceBuffer(&xa2buffer)))
return 0;
InterlockedIncrement(&buffers);
bufptr = 0;
write_buffer = (write_buffer + 1) & (max_buffers_mask);
}
}
return bytes_;
}
enum { max_buffers = 16, max_buffers_mask = max_buffers - 1 };
STDMETHOD_(void, OnBufferStart) (void *) {}
STDMETHOD_(void, OnBufferEnd) (void *)
{
InterlockedDecrement(&buffers);
SetEvent(hEvent);
}
STDMETHOD_(void, OnLoopEnd) (void *) {} STDMETHOD_(void, OnLoopEnd) (void *) {}
STDMETHOD_(void, OnStreamEnd) () {} STDMETHOD_(void, OnStreamEnd) () {}
STDMETHOD_(void, OnVoiceError) (void *, HRESULT) {} STDMETHOD_(void, OnVoiceError) (void *, HRESULT) {}
STDMETHOD_(void, OnVoiceProcessingPassEnd) () STDMETHOD_(void, OnVoiceProcessingPassEnd) () {}
{ STDMETHOD_(void, OnVoiceProcessingPassStart) (UINT32) {}
SetEvent(hEvent);
}
STDMETHOD_(void, OnVoiceProcessingPassStart) (UINT32) {}
uint8_t *buf; uint8_t *buf;
IXAudio2 *pXAudio2; IXAudio2 *pXAudio2;
IXAudio2MasteringVoice *pMasterVoice; IXAudio2MasteringVoice *pMasterVoice;
IXAudio2SourceVoice *pSourceVoice; IXAudio2SourceVoice *pSourceVoice;
HANDLE hEvent; HANDLE hEvent;
volatile long buffers; volatile long buffers;
unsigned max_buffers; unsigned bufsize;
unsigned bufsize; unsigned bufptr;
unsigned bufptr; unsigned write_buffer;
unsigned write_buffer;
}; };
xaudio2_t *xaudio2_new(unsigned samplerate, unsigned channels, unsigned bits, size_t size) xaudio2_t *xaudio2_new(unsigned samplerate, unsigned channels, size_t size)
{ {
xaudio2 *handle = 0; xaudio2 *handle = 0;
try { try {
handle = new xaudio2(samplerate, channels, bits, size); handle = new xaudio2(samplerate, channels, size);
} catch(...) { delete handle; return 0; } } catch(...) { delete handle; return 0; }
return handle; return handle;
} }
size_t xaudio2_write_avail(xaudio2_t *handle) size_t xaudio2_write_avail(xaudio2_t *handle)
{ {
try { try {
return handle->write_avail(); return handle->write_avail();
} catch(...) { return 0; } } catch(...) { return 0; }
} }
size_t xaudio2_write(xaudio2_t *handle, const void *buf, size_t bytes) size_t xaudio2_write(xaudio2_t *handle, const void *buf, size_t bytes)
{ {
try { try {
return handle->write(buf, bytes); return handle->write(buf, bytes);
} catch(...) { return 0; } } catch(...) { return 0; }
} }
void xaudio2_free(xaudio2_t *handle) void xaudio2_free(xaudio2_t *handle)
{ {
delete handle; delete handle;
} }

View File

@ -1,7 +1,7 @@
/* /*
Simple C interface for XAudio2 Simple C interface for XAudio2
Author: Hans-Kristian Arntzen Author: Hans-Kristian Arntzen
License: Public Domain License: Public Domain
*/ */
#ifndef XAUDIO_C_H #ifndef XAUDIO_C_H
@ -15,7 +15,7 @@ extern "C" {
typedef struct xaudio2 xaudio2_t; typedef struct xaudio2 xaudio2_t;
xaudio2_t* xaudio2_new(unsigned samplerate, unsigned channels, unsigned bits, size_t bufsize); xaudio2_t* xaudio2_new(unsigned samplerate, unsigned channels, size_t bufsize);
size_t xaudio2_write_avail(xaudio2_t *handle); size_t xaudio2_write_avail(xaudio2_t *handle);
size_t xaudio2_write(xaudio2_t *handle, const void *data, size_t bytes); size_t xaudio2_write(xaudio2_t *handle, const void *data, size_t bytes);
void xaudio2_free(xaudio2_t *handle); void xaudio2_free(xaudio2_t *handle);

View File

@ -36,11 +36,10 @@ static void* __xa_init(const char* device, unsigned rate, unsigned latency)
return NULL; return NULL;
size_t bufsize = latency * rate / 1000; size_t bufsize = latency * rate / 1000;
bufsize = next_pow2(bufsize);
SSNES_LOG("XAudio2: Requesting %d ms latency, using %d ms latency.\n", latency, (int)bufsize * 1000 / rate); SSNES_LOG("XAudio2: Requesting %d ms latency, using %d ms latency.\n", latency, (int)bufsize * 1000 / rate);
xa->xa = xaudio2_new(rate, 2, 16, bufsize << 2); xa->xa = xaudio2_new(rate, 2, bufsize * 2 * sizeof(float));
if (!xa->xa) if (!xa->xa)
{ {
SSNES_ERR("Failed to init XAudio2.\n"); SSNES_ERR("Failed to init XAudio2.\n");
@ -56,10 +55,15 @@ static ssize_t __xa_write(void* data, const void* buf, size_t size)
if (xa->nonblock) if (xa->nonblock)
{ {
size_t avail = xaudio2_write_avail(xa->xa); size_t avail = xaudio2_write_avail(xa->xa);
if (avail == 0)
return 0;
if (avail < size) if (avail < size)
size = avail; size = avail;
} }
return xaudio2_write(xa->xa, buf, size); size_t ret = xaudio2_write(xa->xa, buf, size);
if (ret == 0)
return -1;
return ret;
} }
static bool __xa_stop(void *data) static bool __xa_stop(void *data)
@ -80,6 +84,12 @@ static bool __xa_start(void *data)
return true; return true;
} }
static bool __xa_use_float(void *data)
{
(void)data;
return true;
}
static void __xa_free(void *data) static void __xa_free(void *data)
{ {
xa_t *xa = data; xa_t *xa = data;
@ -97,6 +107,7 @@ const audio_driver_t audio_xa = {
.stop = __xa_stop, .stop = __xa_stop,
.start = __xa_start, .start = __xa_start,
.set_nonblock_state = __xa_set_nonblock_state, .set_nonblock_state = __xa_set_nonblock_state,
.use_float = __xa_use_float,
.free = __xa_free, .free = __xa_free,
.ident = "xaudio" .ident = "xaudio"
}; };