2010-08-16 14:10:50 +10:00
|
|
|
#include "xaudio2.hpp"
|
2013-05-02 21:25:45 +10:00
|
|
|
#include <windows.h>
|
2010-08-16 14:10:50 +10:00
|
|
|
|
2018-08-05 19:00:15 +10:00
|
|
|
struct AudioXAudio2 : AudioDriver, public IXAudio2VoiceCallback {
|
|
|
|
AudioXAudio2& self = *this;
|
|
|
|
AudioXAudio2(Audio& super) : AudioDriver(super) {}
|
2017-07-24 15:23:40 +10:00
|
|
|
~AudioXAudio2() { terminate(); }
|
|
|
|
|
2018-08-05 19:00:15 +10:00
|
|
|
auto create() -> bool override {
|
|
|
|
super.setFrequency(48000);
|
|
|
|
super.setLatency(40);
|
|
|
|
return initialize();
|
|
|
|
}
|
|
|
|
|
Update to 20180729 release.
byuu wrote:
Sigh ...
asio.hpp needs #include <nall/windows/registry.hpp>
[Since the last WIP, byuu also posted the following message. -Ed.]
ruby drivers have all been updated (but not tested outside of BSD), and
I redesigned the settings window. The driver functionality all exists on
a new "Drivers" panel, the emulator/hack settings go to a
"Configuration" panel, and the video/audio panels lose driver settings.
As does the settings menu and its synchronize options.
I want to start pushing toward a v107 release. Critically, I will need
DirectSound and ALSA to support dynamic rate control. I'd also like to
eliminate the other system manifest.bml files. I need to update the
cheat code database format, and bundle at least a few quark shaders --
although I still need to default to Direct3D on Windows.
Turbo keys would be nice, if it's not too much effort. Aside from
netplay, it's the last significant feature I'm missing.
I think for v107, higan is going to be a bit rough around the edges
compared to bsnes. And I don't think it's practical to finish the bsnes
localization support.
I'm thinking we probably want another WIP to iron out any critical
issues, but this time there should be a feature freeze with the next
WIP.
2018-07-29 23:24:38 +10:00
|
|
|
auto driver() -> string override { return "XAudio2"; }
|
|
|
|
auto ready() -> bool override { return _ready; }
|
|
|
|
|
|
|
|
auto hasBlocking() -> bool override { return true; }
|
Update to v104r06 release.
byuu says:
Changelog:
- gba,ws: removed Thread::step() override¹
- processor/m68k: move.b (a7)+ and move.b (a7)- adjust a7 by two, not
by one²
- tomoko: created new initialize(Video,Audio,Input)Driver() functions³
- ruby/audio: split Audio::information into
Audio::available(Devices,Frequencies,Latencies,Channels)³
- ws: added Model::(WonderSwan,WonderSwanColor,SwanCrystal)()
functions for consistency with other cores
¹: this should hopefully fix GBA Pokemon Pinball. Thanks to
SuperMikeMan for pointing out the underlying cause.
²: this fixes A Ressaha de Ikou, Mega Bomberman, and probably more
games.
³: this is the big change: so there was a problem with WASAPI where
you might change your device under the audio settings panel. And your
new device may not support the frequency that your old device used. This
would end up not updating the frequency, and the pitch would be
distorted.
The old Audio::information() couldn't tell you what frequencies,
latencies, or channels were available for all devices simultaneously, so
I had to split them up. The new initializeAudioDriver() function
validates you have a correct driver, or it defaults to none. Then it
validates a correct device name, or it defaults to the first entry in
the list. Then it validates a correct frequency, or defaults to the
first in the list. Then finally it validates a correct latency, or
defaults to the first in the list.
In this way ... we have a clear path now with no API changes required to
select default devices, frequencies, latencies, channel counts: they
need to be the first items in their respective lists.
So, what we need to do now is go through and for every audio driver that
enumerates devices, we need to make sure the default device gets added
to the top of the list. I'm ... not really sure how to do this with most
drivers, so this is definitely going to take some time.
Also, when you change a device, initializeAudioDriver() is called again,
so if it's a bad device, it will disable the audio driver instead of
continuing to send samples at it and hoping that the driver blocked
those API calls when it failed to initialize properly.
Now then ... since it was a decently-sized API change, it's possible
I've broken compilation of the Linux drivers, so please report any
compilation errors so that I can fix them.
2017-08-26 11:15:49 +10:00
|
|
|
|
2018-08-05 19:00:15 +10:00
|
|
|
auto hasFrequencies() -> vector<uint> override {
|
|
|
|
return {44100, 48000, 96000};
|
Update to v104r06 release.
byuu says:
Changelog:
- gba,ws: removed Thread::step() override¹
- processor/m68k: move.b (a7)+ and move.b (a7)- adjust a7 by two, not
by one²
- tomoko: created new initialize(Video,Audio,Input)Driver() functions³
- ruby/audio: split Audio::information into
Audio::available(Devices,Frequencies,Latencies,Channels)³
- ws: added Model::(WonderSwan,WonderSwanColor,SwanCrystal)()
functions for consistency with other cores
¹: this should hopefully fix GBA Pokemon Pinball. Thanks to
SuperMikeMan for pointing out the underlying cause.
²: this fixes A Ressaha de Ikou, Mega Bomberman, and probably more
games.
³: this is the big change: so there was a problem with WASAPI where
you might change your device under the audio settings panel. And your
new device may not support the frequency that your old device used. This
would end up not updating the frequency, and the pitch would be
distorted.
The old Audio::information() couldn't tell you what frequencies,
latencies, or channels were available for all devices simultaneously, so
I had to split them up. The new initializeAudioDriver() function
validates you have a correct driver, or it defaults to none. Then it
validates a correct device name, or it defaults to the first entry in
the list. Then it validates a correct frequency, or defaults to the
first in the list. Then finally it validates a correct latency, or
defaults to the first in the list.
In this way ... we have a clear path now with no API changes required to
select default devices, frequencies, latencies, channel counts: they
need to be the first items in their respective lists.
So, what we need to do now is go through and for every audio driver that
enumerates devices, we need to make sure the default device gets added
to the top of the list. I'm ... not really sure how to do this with most
drivers, so this is definitely going to take some time.
Also, when you change a device, initializeAudioDriver() is called again,
so if it's a bad device, it will disable the audio driver instead of
continuing to send samples at it and hoping that the driver blocked
those API calls when it failed to initialize properly.
Now then ... since it was a decently-sized API change, it's possible
I've broken compilation of the Linux drivers, so please report any
compilation errors so that I can fix them.
2017-08-26 11:15:49 +10:00
|
|
|
}
|
2017-07-24 15:23:40 +10:00
|
|
|
|
2018-08-05 19:00:15 +10:00
|
|
|
auto hasLatencies() -> vector<uint> override {
|
Update to v104r06 release.
byuu says:
Changelog:
- gba,ws: removed Thread::step() override¹
- processor/m68k: move.b (a7)+ and move.b (a7)- adjust a7 by two, not
by one²
- tomoko: created new initialize(Video,Audio,Input)Driver() functions³
- ruby/audio: split Audio::information into
Audio::available(Devices,Frequencies,Latencies,Channels)³
- ws: added Model::(WonderSwan,WonderSwanColor,SwanCrystal)()
functions for consistency with other cores
¹: this should hopefully fix GBA Pokemon Pinball. Thanks to
SuperMikeMan for pointing out the underlying cause.
²: this fixes A Ressaha de Ikou, Mega Bomberman, and probably more
games.
³: this is the big change: so there was a problem with WASAPI where
you might change your device under the audio settings panel. And your
new device may not support the frequency that your old device used. This
would end up not updating the frequency, and the pitch would be
distorted.
The old Audio::information() couldn't tell you what frequencies,
latencies, or channels were available for all devices simultaneously, so
I had to split them up. The new initializeAudioDriver() function
validates you have a correct driver, or it defaults to none. Then it
validates a correct device name, or it defaults to the first entry in
the list. Then it validates a correct frequency, or defaults to the
first in the list. Then finally it validates a correct latency, or
defaults to the first in the list.
In this way ... we have a clear path now with no API changes required to
select default devices, frequencies, latencies, channel counts: they
need to be the first items in their respective lists.
So, what we need to do now is go through and for every audio driver that
enumerates devices, we need to make sure the default device gets added
to the top of the list. I'm ... not really sure how to do this with most
drivers, so this is definitely going to take some time.
Also, when you change a device, initializeAudioDriver() is called again,
so if it's a bad device, it will disable the audio driver instead of
continuing to send samples at it and hoping that the driver blocked
those API calls when it failed to initialize properly.
Now then ... since it was a decently-sized API change, it's possible
I've broken compilation of the Linux drivers, so please report any
compilation errors so that I can fix them.
2017-08-26 11:15:49 +10:00
|
|
|
return {20, 40, 60, 80, 100};
|
2017-07-24 15:23:40 +10:00
|
|
|
}
|
2010-08-16 14:10:50 +10:00
|
|
|
|
2018-08-05 19:00:15 +10:00
|
|
|
auto setBlocking(bool blocking) -> bool override { return true; }
|
|
|
|
auto setFrequency(uint frequency) -> bool override { return initialize(); }
|
|
|
|
auto setLatency(uint latency) -> bool override { return initialize(); }
|
2010-08-16 14:10:50 +10:00
|
|
|
|
Update to 20180729 release.
byuu wrote:
Sigh ...
asio.hpp needs #include <nall/windows/registry.hpp>
[Since the last WIP, byuu also posted the following message. -Ed.]
ruby drivers have all been updated (but not tested outside of BSD), and
I redesigned the settings window. The driver functionality all exists on
a new "Drivers" panel, the emulator/hack settings go to a
"Configuration" panel, and the video/audio panels lose driver settings.
As does the settings menu and its synchronize options.
I want to start pushing toward a v107 release. Critically, I will need
DirectSound and ALSA to support dynamic rate control. I'd also like to
eliminate the other system manifest.bml files. I need to update the
cheat code database format, and bundle at least a few quark shaders --
although I still need to default to Direct3D on Windows.
Turbo keys would be nice, if it's not too much effort. Aside from
netplay, it's the last significant feature I'm missing.
I think for v107, higan is going to be a bit rough around the edges
compared to bsnes. And I don't think it's practical to finish the bsnes
localization support.
I'm thinking we probably want another WIP to iron out any critical
issues, but this time there should be a feature freeze with the next
WIP.
2018-07-29 23:24:38 +10:00
|
|
|
auto clear() -> void override {
|
2017-07-24 15:23:40 +10:00
|
|
|
if(!_sourceVoice) return;
|
|
|
|
_sourceVoice->Stop(0);
|
|
|
|
_sourceVoice->FlushSourceBuffers(); //calls OnBufferEnd for all currently submitted buffers
|
2010-08-16 14:10:50 +10:00
|
|
|
|
2017-07-24 15:23:40 +10:00
|
|
|
_bufferIndex = 0;
|
2010-08-16 14:10:50 +10:00
|
|
|
|
2017-07-24 15:23:40 +10:00
|
|
|
_bufferOffset = 0;
|
2018-05-28 11:16:27 +10:00
|
|
|
if(_buffer) memory::fill<uint32_t>(_buffer, _period * _bufferCount);
|
2013-05-02 21:25:45 +10:00
|
|
|
|
2017-07-24 15:23:40 +10:00
|
|
|
_sourceVoice->Start(0);
|
2010-08-16 14:10:50 +10:00
|
|
|
}
|
|
|
|
|
Update to 20180729 release.
byuu wrote:
Sigh ...
asio.hpp needs #include <nall/windows/registry.hpp>
[Since the last WIP, byuu also posted the following message. -Ed.]
ruby drivers have all been updated (but not tested outside of BSD), and
I redesigned the settings window. The driver functionality all exists on
a new "Drivers" panel, the emulator/hack settings go to a
"Configuration" panel, and the video/audio panels lose driver settings.
As does the settings menu and its synchronize options.
I want to start pushing toward a v107 release. Critically, I will need
DirectSound and ALSA to support dynamic rate control. I'd also like to
eliminate the other system manifest.bml files. I need to update the
cheat code database format, and bundle at least a few quark shaders --
although I still need to default to Direct3D on Windows.
Turbo keys would be nice, if it's not too much effort. Aside from
netplay, it's the last significant feature I'm missing.
I think for v107, higan is going to be a bit rough around the edges
compared to bsnes. And I don't think it's practical to finish the bsnes
localization support.
I'm thinking we probably want another WIP to iron out any critical
issues, but this time there should be a feature freeze with the next
WIP.
2018-07-29 23:24:38 +10:00
|
|
|
auto output(const double samples[]) -> void override {
|
2017-10-07 18:28:12 +11:00
|
|
|
_buffer[_bufferIndex * _period + _bufferOffset] = (uint16_t)sclamp<16>(samples[0] * 32767.0) << 0;
|
|
|
|
_buffer[_bufferIndex * _period + _bufferOffset] |= (uint16_t)sclamp<16>(samples[1] * 32767.0) << 16;
|
|
|
|
if(++_bufferOffset < _period) return;
|
2017-07-24 15:23:40 +10:00
|
|
|
_bufferOffset = 0;
|
2010-08-16 14:10:50 +10:00
|
|
|
|
2017-07-24 15:23:40 +10:00
|
|
|
if(_bufferQueue == _bufferCount - 1) {
|
2018-08-05 19:00:15 +10:00
|
|
|
if(self.blocking) {
|
2010-08-16 14:10:50 +10:00
|
|
|
//wait until there is at least one other free buffer for the next sample
|
2017-07-24 15:23:40 +10:00
|
|
|
while(_bufferQueue == _bufferCount - 1);
|
2013-05-02 21:25:45 +10:00
|
|
|
} else { //we need one free buffer for the next sample, so ignore the current contents
|
2010-08-16 14:10:50 +10:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-05-02 21:25:45 +10:00
|
|
|
|
2017-07-24 15:23:40 +10:00
|
|
|
pushBuffer(_period * 4, _buffer + _bufferIndex * _period);
|
|
|
|
_bufferIndex = (_bufferIndex + 1) % _bufferCount;
|
2010-08-16 14:10:50 +10:00
|
|
|
}
|
|
|
|
|
2017-07-24 15:23:40 +10:00
|
|
|
private:
|
|
|
|
auto initialize() -> bool {
|
|
|
|
terminate();
|
2010-08-16 14:10:50 +10:00
|
|
|
|
2017-07-24 15:23:40 +10:00
|
|
|
_bufferCount = 8;
|
2018-08-05 19:00:15 +10:00
|
|
|
_period = self.frequency * self.latency / _bufferCount / 1000.0 + 0.5;
|
2017-07-24 15:23:40 +10:00
|
|
|
_buffer = new uint32_t[_period * _bufferCount];
|
|
|
|
_bufferOffset = 0;
|
|
|
|
_bufferIndex = 0;
|
|
|
|
_bufferQueue = 0;
|
2010-08-16 14:10:50 +10:00
|
|
|
|
2017-07-24 15:23:40 +10:00
|
|
|
if(FAILED(XAudio2Create(&_interface, 0 , XAUDIO2_DEFAULT_PROCESSOR))) return false;
|
2010-08-16 14:10:50 +10:00
|
|
|
|
2017-07-24 15:23:40 +10:00
|
|
|
uint deviceCount = 0;
|
|
|
|
_interface->GetDeviceCount(&deviceCount);
|
|
|
|
if(deviceCount == 0) return terminate(), false;
|
2010-08-16 14:10:50 +10:00
|
|
|
|
2017-07-24 15:23:40 +10:00
|
|
|
uint deviceID = 0;
|
|
|
|
for(uint deviceIndex : range(deviceCount)) {
|
|
|
|
XAUDIO2_DEVICE_DETAILS deviceDetails = {};
|
|
|
|
_interface->GetDeviceDetails(deviceIndex, &deviceDetails);
|
2014-01-28 21:04:58 +11:00
|
|
|
if(deviceDetails.Role & DefaultGameDevice) deviceID = deviceIndex;
|
|
|
|
}
|
|
|
|
|
2018-08-05 19:00:15 +10:00
|
|
|
if(FAILED(_interface->CreateMasteringVoice(&_masterVoice, _channels, self.frequency, 0, deviceID, nullptr))) return terminate(), false;
|
2010-08-16 14:10:50 +10:00
|
|
|
|
2017-07-24 15:23:40 +10:00
|
|
|
WAVEFORMATEX waveFormat;
|
|
|
|
waveFormat.wFormatTag = WAVE_FORMAT_PCM;
|
|
|
|
waveFormat.nChannels = _channels;
|
2018-08-05 19:00:15 +10:00
|
|
|
waveFormat.nSamplesPerSec = self.frequency;
|
2017-07-24 15:23:40 +10:00
|
|
|
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 14:10:50 +10:00
|
|
|
|
|
|
|
clear();
|
2017-07-24 15:23:40 +10:00
|
|
|
return _ready = true;
|
2010-08-16 14:10:50 +10:00
|
|
|
}
|
|
|
|
|
2017-07-24 15:23:40 +10:00
|
|
|
auto terminate() -> void {
|
|
|
|
_ready = false;
|
|
|
|
|
|
|
|
if(_sourceVoice) {
|
|
|
|
_sourceVoice->Stop(0);
|
|
|
|
_sourceVoice->DestroyVoice();
|
|
|
|
_sourceVoice = nullptr;
|
2010-08-16 14:10:50 +10:00
|
|
|
}
|
2017-07-24 15:23:40 +10:00
|
|
|
|
|
|
|
if(_masterVoice) {
|
|
|
|
_masterVoice->DestroyVoice();
|
|
|
|
_masterVoice = nullptr;
|
2010-08-16 14:10:50 +10:00
|
|
|
}
|
2017-07-24 15:23:40 +10:00
|
|
|
|
|
|
|
if(_interface) {
|
|
|
|
_interface->Release();
|
|
|
|
_interface = nullptr;
|
2010-08-16 14:10:50 +10:00
|
|
|
}
|
2017-07-24 15:23:40 +10: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 14:10:50 +10:00
|
|
|
}
|
2013-05-02 21:25:45 +10:00
|
|
|
|
2017-07-24 15:23:40 +10:00
|
|
|
bool _ready = false;
|
|
|
|
|
|
|
|
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 21:25:45 +10:00
|
|
|
STDMETHODIMP_(void) OnBufferEnd(void* pBufferContext) {
|
2017-07-24 15:23:40 +10:00
|
|
|
InterlockedDecrement(&_bufferQueue);
|
2010-08-16 14:10:50 +10:00
|
|
|
}
|
|
|
|
};
|