RetroArch/360/xdk360_audio.cpp

228 lines
5.6 KiB
C++
Raw Normal View History

2012-04-21 21:13:50 +00:00
/* RetroArch - A frontend for libretro.
2013-01-01 00:37:37 +00:00
* Copyright (C) 2010-2013 - Hans-Kristian Arntzen
* Copyright (C) 2011-2013 - Daniel De Matteis
2012-01-05 13:17:35 +00:00
*
2012-04-21 21:13:50 +00:00
* RetroArch is free software: you can redistribute it and/or modify it under the terms
2012-01-05 13:17:35 +00:00
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
2012-04-21 21:13:50 +00:00
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
2012-01-05 13:17:35 +00:00
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
2012-04-21 21:31:57 +00:00
* You should have received a copy of the GNU General Public License along with RetroArch.
2012-01-05 13:17:35 +00:00
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "../driver.h"
#include <stdlib.h>
#include <xtl.h>
2012-03-11 05:58:56 +00:00
#include "xaudio-c/xaudio.h"
2012-01-05 13:17:35 +00:00
#include "../general.h"
2012-01-05 13:48:54 +00:00
#define MAX_BUFFERS 16
#define MAX_BUFFERS_MASK 15
2012-01-05 18:27:58 +00:00
struct XAudio : public IXAudio2VoiceCallback
{
2012-01-05 18:27:58 +00:00
XAudio() :
buf(0), pXAudio2(0), pMasteringVoice(0),
pSourceVoice(0), nonblock(false), bufsize(0),
bufptr(0), write_buffer(0), buffers(0), hEvent(0)
{}
2012-01-05 18:27:58 +00:00
~XAudio()
{
if (pSourceVoice)
{
pSourceVoice->Stop(0, XAUDIO2_COMMIT_NOW);
pSourceVoice->DestroyVoice();
}
2012-01-05 13:48:54 +00:00
2012-01-05 18:27:58 +00:00
if (pMasteringVoice)
pMasteringVoice->DestroyVoice();
2012-01-05 18:27:58 +00:00
if (pXAudio2)
pXAudio2->Release();
2012-01-05 18:27:58 +00:00
if (hEvent)
CloseHandle(hEvent);
2012-03-11 05:58:56 +00:00
free(buf);
2012-01-06 21:29:34 +00:00
}
2012-01-05 18:27:58 +00:00
bool init(unsigned rate, unsigned latency)
{
size_t bufsize_ = latency * rate / 1000;
2012-01-06 21:29:34 +00:00
size_t size = bufsize_ * 2 * sizeof(int16_t);
2012-01-05 13:17:35 +00:00
2012-04-21 21:25:32 +00:00
RARCH_LOG("XAudio2: Requesting %d ms latency, using %d ms latency.\n", latency, (int)bufsize_ * 1000 / rate);
if (FAILED(XAudio2Create(&pXAudio2, 0, XAUDIO2_DEFAULT_PROCESSOR)))
2012-01-05 18:27:58 +00:00
return false;
2012-01-05 13:17:35 +00:00
2012-01-05 18:27:58 +00:00
if (FAILED(pXAudio2->CreateMasteringVoice(&pMasteringVoice, 2, rate, 0, 0, NULL)))
return false;
2012-01-05 18:27:58 +00:00
WAVEFORMATEX wfx = {0};
2012-01-06 21:29:34 +00:00
wfx.wFormatTag = WAVE_FORMAT_PCM;
2012-01-05 18:27:58 +00:00
wfx.nChannels = 2;
wfx.nSamplesPerSec = rate;
2012-01-06 21:29:34 +00:00
wfx.nBlockAlign = 2 * sizeof(int16_t);
wfx.wBitsPerSample = sizeof(int16_t) * 8;
2012-01-05 18:27:58 +00:00
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
wfx.cbSize = 0;
2012-01-05 18:33:38 +00:00
if (FAILED(pXAudio2->CreateSourceVoice(&pSourceVoice, &wfx,
2012-01-05 18:27:58 +00:00
XAUDIO2_VOICE_NOSRC, XAUDIO2_DEFAULT_FREQ_RATIO, this, 0, 0)))
return false;
2012-01-06 18:45:27 +00:00
hEvent = CreateEvent(0, FALSE, FALSE, 0);
2012-01-05 18:27:58 +00:00
bufsize = size / MAX_BUFFERS;
2012-03-11 05:58:56 +00:00
buf = (uint8_t*)malloc(bufsize * MAX_BUFFERS);
2012-01-05 18:27:58 +00:00
memset(buf, 0, bufsize * MAX_BUFFERS);
if (FAILED(pSourceVoice->Start(0, XAUDIO2_COMMIT_NOW)))
2012-01-05 18:27:58 +00:00
return false;
2012-01-05 18:27:58 +00:00
return true;
}
2012-01-06 21:29:34 +00:00
// It's really 16-bit, but we have to byteswap.
2012-01-06 21:49:30 +00:00
size_t write(const uint8_t *buffer, size_t size)
2012-01-05 18:27:58 +00:00
{
2012-01-05 18:33:38 +00:00
if (nonblock)
2012-01-05 18:27:58 +00:00
{
2012-01-05 18:33:38 +00:00
size_t avail = bufsize * (MAX_BUFFERS - buffers - 1);
2012-01-05 18:27:58 +00:00
if (avail == 0)
return 0;
if (avail < size)
size = avail;
}
2012-01-05 13:17:35 +00:00
2012-01-05 18:27:58 +00:00
unsigned bytes = size;
while (bytes)
{
unsigned need = min(bytes, bufsize - bufptr);
2012-01-06 21:49:30 +00:00
uint8_t *base_write = buf + write_buffer * bufsize + bufptr;
memcpy(base_write, buffer, need);
2012-01-05 18:27:58 +00:00
bufptr += need;
buffer += need;
bytes -= need;
if (bufptr == bufsize)
{
while (buffers == MAX_BUFFERS - 1)
WaitForSingleObject(hEvent, INFINITE);
XAUDIO2_BUFFER xa2buffer = {0};
xa2buffer.AudioBytes = bufsize;
2012-01-06 21:49:30 +00:00
xa2buffer.pAudioData = buf + write_buffer * bufsize;
2012-01-05 18:27:58 +00:00
if (FAILED(pSourceVoice->SubmitSourceBuffer(&xa2buffer, NULL)))
2012-01-05 18:27:58 +00:00
return 0;
InterlockedIncrement(&buffers);
bufptr = 0;
write_buffer = (write_buffer + 1) & MAX_BUFFERS_MASK;
}
}
2012-01-05 18:33:38 +00:00
return size;
2012-01-05 18:27:58 +00:00
}
2012-01-05 18:27:58 +00:00
STDMETHOD_(void, OnBufferStart) (void *) {}
STDMETHOD_(void, OnBufferEnd) (void *)
{
2012-01-05 18:27:58 +00:00
InterlockedDecrement(&buffers);
SetEvent(hEvent);
}
STDMETHOD_(void, OnLoopEnd) (void *) {}
STDMETHOD_(void, OnStreamEnd) () {}
STDMETHOD_(void, OnVoiceError) (void *, HRESULT) {}
STDMETHOD_(void, OnVoiceProcessingPassEnd) () {}
STDMETHOD_(void, OnVoiceProcessingPassStart) (UINT32) {}
2012-01-06 21:55:07 +00:00
uint8_t *buf;
2012-01-05 18:27:58 +00:00
IXAudio2 *pXAudio2;
IXAudio2MasteringVoice *pMasteringVoice;
IXAudio2SourceVoice *pSourceVoice;
bool nonblock;
unsigned bufsize;
unsigned bufptr;
unsigned write_buffer;
volatile long buffers;
HANDLE hEvent;
};
2012-01-05 18:27:58 +00:00
static void *xa_init(const char *device, unsigned rate, unsigned latency)
{
if (latency < 8)
latency = 8; // Do not allow shenanigans.
2012-01-05 18:27:58 +00:00
XAudio *xa = new XAudio;
if (!xa->init(rate, latency))
goto error;
2012-01-05 18:27:58 +00:00
return xa;
2012-01-05 18:27:58 +00:00
error:
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to init XAudio2.\n");
2012-01-05 18:27:58 +00:00
delete xa;
return NULL;
}
2012-01-05 13:17:35 +00:00
static ssize_t xa_write(void *data, const void *buf, size_t size)
{
2012-01-05 18:27:58 +00:00
XAudio *xa = (XAudio*)data;
2012-01-06 21:55:07 +00:00
size_t ret = xa->write((const uint8_t*)buf, size);
2012-01-05 13:17:35 +00:00
2012-02-12 20:12:55 +00:00
if (ret == 0 && !xa->nonblock)
2012-01-05 13:17:35 +00:00
return -1;
return ret;
}
static bool xa_stop(void *data)
{
(void)data;
return true;
}
static void xa_set_nonblock_state(void *data, bool state)
{
2012-01-05 18:27:58 +00:00
XAudio *xa = (XAudio*)data;
2012-01-05 13:17:35 +00:00
xa->nonblock = state;
}
static bool xa_start(void *data)
{
(void)data;
return true;
}
static void xa_free(void *data)
{
2012-01-05 18:27:58 +00:00
XAudio *xa = (XAudio*)data;
2012-01-05 13:17:35 +00:00
if (xa)
2012-01-05 18:27:58 +00:00
delete xa;
2012-01-05 13:17:35 +00:00
}
static bool xa_use_float(void *data)
{
(void)data;
return false;
}
2012-01-05 17:38:29 +00:00
const audio_driver_t audio_xdk360 = {
2012-01-05 13:17:35 +00:00
xa_init,
xa_write,
xa_stop,
xa_start,
xa_set_nonblock_state,
xa_free,
xa_use_float,
2012-01-06 13:12:12 +00:00
"xdk360"
2012-01-05 13:17:35 +00:00
};