2010-08-16 04:10:50 +00:00
|
|
|
#include "xaudio2.hpp"
|
2013-05-02 11:25:45 +00:00
|
|
|
#include <windows.h>
|
2010-08-16 04:10:50 +00:00
|
|
|
|
|
|
|
namespace ruby {
|
|
|
|
|
|
|
|
class pAudioXAudio2: public IXAudio2VoiceCallback {
|
|
|
|
public:
|
2013-05-02 11:25:45 +00:00
|
|
|
IXAudio2* pXAudio2;
|
2010-08-16 04:10:50 +00:00
|
|
|
IXAudio2MasteringVoice* pMasterVoice;
|
2013-05-02 11:25:45 +00:00
|
|
|
IXAudio2SourceVoice* pSourceVoice;
|
|
|
|
|
|
|
|
//inherited from IXAudio2VoiceCallback
|
|
|
|
STDMETHODIMP_(void) OnBufferStart(void* pBufferContext){}
|
|
|
|
STDMETHODIMP_(void) OnLoopEnd(void* pBufferContext){}
|
2010-08-16 04:10:50 +00:00
|
|
|
STDMETHODIMP_(void) OnStreamEnd() {}
|
2013-05-02 11:25:45 +00:00
|
|
|
STDMETHODIMP_(void) OnVoiceError(void* pBufferContext, HRESULT Error) {}
|
2010-08-16 04:10:50 +00:00
|
|
|
STDMETHODIMP_(void) OnVoiceProcessingPassEnd() {}
|
|
|
|
STDMETHODIMP_(void) OnVoiceProcessingPassStart(UINT32 BytesRequired) {}
|
|
|
|
|
|
|
|
struct {
|
|
|
|
unsigned buffers;
|
|
|
|
unsigned latency;
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
uint32_t* buffer;
|
2010-08-16 04:10:50 +00:00
|
|
|
unsigned bufferoffset;
|
|
|
|
|
|
|
|
volatile long submitbuffers;
|
|
|
|
unsigned writebuffer;
|
|
|
|
} device;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
bool synchronize;
|
|
|
|
unsigned frequency;
|
|
|
|
unsigned latency;
|
|
|
|
} settings;
|
|
|
|
|
|
|
|
bool cap(const string& name) {
|
|
|
|
if(name == Audio::Synchronize) return true;
|
|
|
|
if(name == Audio::Frequency) return true;
|
|
|
|
if(name == Audio::Latency) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
any get(const string& name) {
|
|
|
|
if(name == Audio::Synchronize) return settings.synchronize;
|
|
|
|
if(name == Audio::Frequency) return settings.frequency;
|
|
|
|
if(name == Audio::Latency) return settings.latency;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool set(const string& name, const any& value) {
|
|
|
|
if(name == Audio::Synchronize) {
|
|
|
|
settings.synchronize = any_cast<bool>(value);
|
|
|
|
if(pXAudio2) clear();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(name == Audio::Frequency) {
|
|
|
|
settings.frequency = any_cast<unsigned>(value);
|
|
|
|
if(pXAudio2) init();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(name == Audio::Latency) {
|
|
|
|
settings.latency = any_cast<unsigned>(value);
|
|
|
|
if(pXAudio2) init();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-02 11:25:45 +00:00
|
|
|
|
|
|
|
void pushbuffer(unsigned bytes, uint32_t* pAudioData) {
|
|
|
|
XAUDIO2_BUFFER xa2buffer = {0};
|
|
|
|
xa2buffer.AudioBytes = bytes;
|
|
|
|
xa2buffer.pAudioData = reinterpret_cast<BYTE*>(pAudioData);
|
|
|
|
xa2buffer.pContext = 0;
|
2010-08-16 04:10:50 +00:00
|
|
|
InterlockedIncrement(&device.submitbuffers);
|
|
|
|
pSourceVoice->SubmitSourceBuffer(&xa2buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sample(uint16_t left, uint16_t right) {
|
|
|
|
device.buffer[device.writebuffer * device.latency + device.bufferoffset++] = left + (right << 16);
|
|
|
|
if(device.bufferoffset < device.latency) return;
|
|
|
|
device.bufferoffset = 0;
|
|
|
|
|
|
|
|
if(device.submitbuffers == device.buffers - 1) {
|
|
|
|
if(settings.synchronize == true) {
|
|
|
|
//wait until there is at least one other free buffer for the next sample
|
|
|
|
while(device.submitbuffers == device.buffers - 1) {
|
|
|
|
//Sleep(0);
|
|
|
|
}
|
2013-05-02 11:25:45 +00:00
|
|
|
} else { //we need one free buffer for the next sample, so ignore the current contents
|
2010-08-16 04:10:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-05-02 11:25:45 +00:00
|
|
|
|
2010-08-16 04:10:50 +00:00
|
|
|
pushbuffer(device.latency * 4,device.buffer + device.writebuffer * device.latency);
|
|
|
|
|
|
|
|
device.writebuffer = (device.writebuffer + 1) % device.buffers;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
if(!pSourceVoice) return;
|
|
|
|
pSourceVoice->Stop(0);
|
2013-05-02 11:25:45 +00:00
|
|
|
pSourceVoice->FlushSourceBuffers(); //calls OnBufferEnd for all currently submitted buffers
|
|
|
|
|
2010-08-16 04:10:50 +00:00
|
|
|
device.writebuffer = 0;
|
|
|
|
|
|
|
|
device.bufferoffset = 0;
|
|
|
|
if(device.buffer) memset(device.buffer, 0, device.latency * device.buffers * 4);
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
pSourceVoice->Start(0);
|
2010-08-16 04:10:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool init() {
|
|
|
|
term();
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
device.buffers = 8;
|
2010-08-16 04:10:50 +00:00
|
|
|
device.latency = settings.frequency * settings.latency / device.buffers / 1000.0 + 0.5;
|
2013-05-02 11:25:45 +00:00
|
|
|
device.buffer = new uint32_t[device.latency * device.buffers];
|
2010-08-16 04:10:50 +00:00
|
|
|
device.bufferoffset = 0;
|
|
|
|
device.submitbuffers = 0;
|
|
|
|
|
|
|
|
HRESULT hr;
|
|
|
|
if(FAILED(hr = XAudio2Create(&pXAudio2, 0 , XAUDIO2_DEFAULT_PROCESSOR))) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-02 11:25:45 +00:00
|
|
|
|
2014-01-28 10:04:58 +00:00
|
|
|
unsigned deviceCount = 0;
|
|
|
|
pXAudio2->GetDeviceCount(&deviceCount);
|
|
|
|
if(deviceCount == 0) { term(); return false; }
|
|
|
|
|
|
|
|
unsigned deviceID = 0;
|
|
|
|
for(unsigned deviceIndex = 0; deviceIndex < deviceCount; deviceIndex++) {
|
|
|
|
XAUDIO2_DEVICE_DETAILS deviceDetails;
|
|
|
|
memset(&deviceDetails, 0, sizeof(XAUDIO2_DEVICE_DETAILS));
|
|
|
|
pXAudio2->GetDeviceDetails(deviceIndex, &deviceDetails);
|
|
|
|
if(deviceDetails.Role & DefaultGameDevice) deviceID = deviceIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(FAILED(hr = pXAudio2->CreateMasteringVoice(&pMasterVoice, 2, settings.frequency, 0, deviceID, NULL))) {
|
2010-08-16 04:10:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
WAVEFORMATEX wfx;
|
|
|
|
wfx.wFormatTag = WAVE_FORMAT_PCM;
|
|
|
|
wfx.nChannels = 2;
|
|
|
|
wfx.nSamplesPerSec = settings.frequency;
|
|
|
|
wfx.nBlockAlign = 4;
|
|
|
|
wfx.wBitsPerSample = 16;
|
|
|
|
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
|
|
|
|
wfx.cbSize = 0;
|
|
|
|
|
2014-01-28 10:04:58 +00:00
|
|
|
if(FAILED(hr = pXAudio2->CreateSourceVoice(&pSourceVoice, (WAVEFORMATEX*)&wfx, XAUDIO2_VOICE_NOSRC, XAUDIO2_DEFAULT_FREQ_RATIO, this, NULL, NULL))) {
|
2010-08-16 04:10:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
clear();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void term() {
|
|
|
|
if(pSourceVoice) {
|
|
|
|
pSourceVoice->Stop(0);
|
|
|
|
pSourceVoice->DestroyVoice();
|
2013-05-02 11:25:45 +00:00
|
|
|
pSourceVoice = nullptr;
|
2010-08-16 04:10:50 +00:00
|
|
|
}
|
|
|
|
if(pMasterVoice) {
|
|
|
|
pMasterVoice->DestroyVoice();
|
2013-05-02 11:25:45 +00:00
|
|
|
pMasterVoice = nullptr;
|
2010-08-16 04:10:50 +00:00
|
|
|
}
|
|
|
|
if(pXAudio2) {
|
|
|
|
pXAudio2->Release();
|
2013-05-02 11:25:45 +00:00
|
|
|
pXAudio2 = nullptr;
|
2010-08-16 04:10:50 +00:00
|
|
|
}
|
|
|
|
if(device.buffer) {
|
|
|
|
delete[] device.buffer;
|
2013-05-02 11:25:45 +00:00
|
|
|
device.buffer = nullptr;
|
2010-08-16 04:10:50 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-02 11:25:45 +00:00
|
|
|
|
|
|
|
STDMETHODIMP_(void) OnBufferEnd(void* pBufferContext) {
|
2010-08-16 04:10:50 +00:00
|
|
|
InterlockedDecrement(&device.submitbuffers);
|
|
|
|
}
|
|
|
|
|
|
|
|
pAudioXAudio2() {
|
2013-05-02 11:25:45 +00:00
|
|
|
pXAudio2 = nullptr;
|
|
|
|
pMasterVoice = nullptr;
|
|
|
|
pSourceVoice = nullptr;
|
2010-08-16 04:10:50 +00:00
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
device.buffer = nullptr;
|
2010-08-16 04:10:50 +00:00
|
|
|
device.bufferoffset = 0;
|
|
|
|
device.submitbuffers = 0;
|
|
|
|
device.writebuffer = 0;
|
|
|
|
|
|
|
|
settings.synchronize = false;
|
|
|
|
settings.frequency = 22050;
|
|
|
|
settings.latency = 120;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DeclareAudio(XAudio2)
|
|
|
|
|
|
|
|
};
|