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
|
|
|
|
2015-06-20 05:44:05 +00:00
|
|
|
struct AudioXAudio2 : Audio, public IXAudio2VoiceCallback {
|
2017-07-24 05:23:40 +00:00
|
|
|
AudioXAudio2() { initialize(); }
|
|
|
|
~AudioXAudio2() { terminate(); }
|
|
|
|
|
|
|
|
auto ready() -> bool { return _ready; }
|
|
|
|
|
|
|
|
auto information() -> Information {
|
|
|
|
Information information;
|
|
|
|
information.devices = {"Default"};
|
|
|
|
information.channels = {2};
|
|
|
|
information.frequencies = {44100.0, 48000.0, 96000.0};
|
|
|
|
information.latencies = {20, 40, 60, 80, 100};
|
|
|
|
return information;
|
|
|
|
}
|
2010-08-16 04:10:50 +00:00
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
auto blocking() -> bool { return _blocking; }
|
|
|
|
auto channels() -> uint { return _channels; }
|
|
|
|
auto frequency() -> double { return _frequency; }
|
|
|
|
auto latency() -> uint { return _latency; }
|
2010-08-16 04:10:50 +00:00
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
auto setBlocking(bool blocking) -> bool {
|
|
|
|
if(_blocking == blocking) return true;
|
|
|
|
_blocking = blocking;
|
|
|
|
return true;
|
2010-08-16 04:10:50 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
auto setFrequency(double frequency) -> bool {
|
|
|
|
if(_frequency == frequency) return true;
|
|
|
|
_frequency = frequency;
|
|
|
|
return initialize();
|
2010-08-16 04:10:50 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
auto setLatency(uint latency) -> bool {
|
|
|
|
if(_latency == latency) return true;
|
|
|
|
_latency = latency;
|
|
|
|
return initialize();
|
|
|
|
}
|
2010-08-16 04:10:50 +00:00
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
auto clear() -> void {
|
|
|
|
if(!_sourceVoice) return;
|
|
|
|
_sourceVoice->Stop(0);
|
|
|
|
_sourceVoice->FlushSourceBuffers(); //calls OnBufferEnd for all currently submitted buffers
|
2010-08-16 04:10:50 +00:00
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
_bufferIndex = 0;
|
2010-08-16 04:10:50 +00:00
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
_bufferOffset = 0;
|
|
|
|
if(_buffer) memory::fill(_buffer, _period * _bufferCount * sizeof(uint32_t));
|
2013-05-02 11:25:45 +00:00
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
_sourceVoice->Start(0);
|
2010-08-16 04:10:50 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
auto output(const double samples[]) -> void {
|
|
|
|
_buffer[_bufferIndex * _period + _bufferOffset++] = uint16_t(samples[0] * 32768.0) << 0 | uint16_t(samples[1] * 32768.0) << 16;
|
|
|
|
if(_bufferOffset < _period) return;
|
|
|
|
_bufferOffset = 0;
|
2010-08-16 04:10:50 +00:00
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
if(_bufferQueue == _bufferCount - 1) {
|
|
|
|
if(_blocking) {
|
2010-08-16 04:10:50 +00:00
|
|
|
//wait until there is at least one other free buffer for the next sample
|
2017-07-24 05:23:40 +00:00
|
|
|
while(_bufferQueue == _bufferCount - 1);
|
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
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
pushBuffer(_period * 4, _buffer + _bufferIndex * _period);
|
|
|
|
_bufferIndex = (_bufferIndex + 1) % _bufferCount;
|
2010-08-16 04:10:50 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
private:
|
|
|
|
auto initialize() -> bool {
|
|
|
|
terminate();
|
2010-08-16 04:10:50 +00:00
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
_bufferCount = 8;
|
|
|
|
_period = _frequency * _latency / _bufferCount / 1000.0 + 0.5;
|
|
|
|
_buffer = new uint32_t[_period * _bufferCount];
|
|
|
|
_bufferOffset = 0;
|
|
|
|
_bufferIndex = 0;
|
|
|
|
_bufferQueue = 0;
|
2010-08-16 04:10:50 +00:00
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
if(FAILED(XAudio2Create(&_interface, 0 , XAUDIO2_DEFAULT_PROCESSOR))) return false;
|
2010-08-16 04:10:50 +00:00
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
uint deviceCount = 0;
|
|
|
|
_interface->GetDeviceCount(&deviceCount);
|
|
|
|
if(deviceCount == 0) return terminate(), false;
|
2010-08-16 04:10:50 +00:00
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
uint deviceID = 0;
|
|
|
|
for(uint deviceIndex : range(deviceCount)) {
|
|
|
|
XAUDIO2_DEVICE_DETAILS deviceDetails = {};
|
|
|
|
_interface->GetDeviceDetails(deviceIndex, &deviceDetails);
|
2014-01-28 10:04:58 +00:00
|
|
|
if(deviceDetails.Role & DefaultGameDevice) deviceID = deviceIndex;
|
|
|
|
}
|
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
if(FAILED(_interface->CreateMasteringVoice(&_masterVoice, _channels, (uint)_frequency, 0, deviceID, nullptr))) return terminate(), false;
|
2010-08-16 04:10:50 +00:00
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
WAVEFORMATEX waveFormat;
|
|
|
|
waveFormat.wFormatTag = WAVE_FORMAT_PCM;
|
|
|
|
waveFormat.nChannels = _channels;
|
|
|
|
waveFormat.nSamplesPerSec = (uint)_frequency;
|
|
|
|
waveFormat.nBlockAlign = 4;
|
|
|
|
waveFormat.wBitsPerSample = 16;
|
|
|
|
waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec * waveFormat.nBlockAlign;
|
|
|
|
waveFormat.cbSize = 0;
|
|
|
|
|
|
|
|
if(FAILED(_interface->CreateSourceVoice(&_sourceVoice, (WAVEFORMATEX*)&waveFormat, XAUDIO2_VOICE_NOSRC, XAUDIO2_DEFAULT_FREQ_RATIO, this, nullptr, nullptr))) return terminate(), false;
|
2010-08-16 04:10:50 +00:00
|
|
|
|
|
|
|
clear();
|
2017-07-24 05:23:40 +00:00
|
|
|
return _ready = true;
|
2010-08-16 04:10:50 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
auto terminate() -> void {
|
|
|
|
_ready = false;
|
|
|
|
|
|
|
|
if(_sourceVoice) {
|
|
|
|
_sourceVoice->Stop(0);
|
|
|
|
_sourceVoice->DestroyVoice();
|
|
|
|
_sourceVoice = nullptr;
|
2010-08-16 04:10:50 +00:00
|
|
|
}
|
2017-07-24 05:23:40 +00:00
|
|
|
|
|
|
|
if(_masterVoice) {
|
|
|
|
_masterVoice->DestroyVoice();
|
|
|
|
_masterVoice = nullptr;
|
2010-08-16 04:10:50 +00:00
|
|
|
}
|
2017-07-24 05:23:40 +00:00
|
|
|
|
|
|
|
if(_interface) {
|
|
|
|
_interface->Release();
|
|
|
|
_interface = nullptr;
|
2010-08-16 04:10:50 +00:00
|
|
|
}
|
2017-07-24 05:23:40 +00:00
|
|
|
|
|
|
|
delete[] _buffer;
|
|
|
|
_buffer = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto pushBuffer(uint bytes, uint32_t* _audioData) -> void {
|
|
|
|
XAUDIO2_BUFFER buffer = {};
|
|
|
|
buffer.AudioBytes = bytes;
|
|
|
|
buffer.pAudioData = reinterpret_cast<BYTE*>(_audioData);
|
|
|
|
buffer.pContext = 0;
|
|
|
|
InterlockedIncrement(&_bufferQueue);
|
|
|
|
_sourceVoice->SubmitSourceBuffer(&buffer);
|
2010-08-16 04:10:50 +00:00
|
|
|
}
|
2013-05-02 11:25:45 +00:00
|
|
|
|
2017-07-24 05:23:40 +00:00
|
|
|
bool _ready = false;
|
|
|
|
bool _blocking = true;
|
|
|
|
uint _channels = 2;
|
|
|
|
double _frequency = 48000.0;
|
|
|
|
uint _latency = 80;
|
|
|
|
|
|
|
|
uint32_t* _buffer = nullptr;
|
|
|
|
uint _period = 0;
|
|
|
|
uint _bufferCount = 0;
|
|
|
|
uint _bufferOffset = 0;
|
|
|
|
uint _bufferIndex = 0;
|
|
|
|
volatile long _bufferQueue = 0; //how many buffers are queued and ready for playback
|
|
|
|
|
|
|
|
IXAudio2* _interface = nullptr;
|
|
|
|
IXAudio2MasteringVoice* _masterVoice = nullptr;
|
|
|
|
IXAudio2SourceVoice* _sourceVoice = nullptr;
|
|
|
|
|
|
|
|
//inherited from IXAudio2VoiceCallback
|
|
|
|
STDMETHODIMP_(void) OnBufferStart(void* pBufferContext){}
|
|
|
|
STDMETHODIMP_(void) OnLoopEnd(void* pBufferContext){}
|
|
|
|
STDMETHODIMP_(void) OnStreamEnd() {}
|
|
|
|
STDMETHODIMP_(void) OnVoiceError(void* pBufferContext, HRESULT Error) {}
|
|
|
|
STDMETHODIMP_(void) OnVoiceProcessingPassEnd() {}
|
|
|
|
STDMETHODIMP_(void) OnVoiceProcessingPassStart(UINT32 BytesRequired) {}
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
STDMETHODIMP_(void) OnBufferEnd(void* pBufferContext) {
|
2017-07-24 05:23:40 +00:00
|
|
|
InterlockedDecrement(&_bufferQueue);
|
2010-08-16 04:10:50 +00:00
|
|
|
}
|
|
|
|
};
|