More XAudio2 junk.

This commit is contained in:
gibbed 2014-01-13 02:45:18 -08:00
parent 24e857cc03
commit f47ebb5010
2 changed files with 38 additions and 22 deletions

View File

@ -41,6 +41,7 @@ private:
XAudio2AudioSystem::XAudio2AudioSystem(Emulator* emulator) : XAudio2AudioSystem::XAudio2AudioSystem(Emulator* emulator) :
audio_(0), mastering_voice_(0), pcm_voice_(0), audio_(0), mastering_voice_(0), pcm_voice_(0),
active_channels_(0),
wait_handle_(NULL), voice_callback_(0), wait_handle_(NULL), voice_callback_(0),
AudioSystem(emulator) { AudioSystem(emulator) {
} }
@ -48,6 +49,18 @@ XAudio2AudioSystem::XAudio2AudioSystem(Emulator* emulator) :
XAudio2AudioSystem::~XAudio2AudioSystem() { XAudio2AudioSystem::~XAudio2AudioSystem() {
} }
const DWORD ChannelMasks[] =
{
0, // TODO: fixme
0, // TODO: fixme
SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_LOW_FREQUENCY,
0, // TODO: fixme
0, // TODO: fixme
0, // TODO: fixme
SPEAKER_FRONT_LEFT | SPEAKER_FRONT_CENTER | SPEAKER_FRONT_RIGHT | SPEAKER_LOW_FREQUENCY | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT,
0, // TODO: fixme
};
void XAudio2AudioSystem::Initialize() { void XAudio2AudioSystem::Initialize() {
AudioSystem::Initialize(); AudioSystem::Initialize();
@ -64,8 +77,10 @@ void XAudio2AudioSystem::Initialize() {
return; return;
} }
active_channels_ = 6;
XAUDIO2_DEBUG_CONFIGURATION config; XAUDIO2_DEBUG_CONFIGURATION config;
config.TraceMask = XAUDIO2_LOG_ERRORS; config.TraceMask = XAUDIO2_LOG_ERRORS | XAUDIO2_LOG_WARNINGS | XAUDIO2_LOG_INFO | XAUDIO2_LOG_DETAIL | XAUDIO2_LOG_STREAMING;
config.BreakMask = 0; config.BreakMask = 0;
config.LogThreadID = FALSE; config.LogThreadID = FALSE;
config.LogTiming = TRUE; config.LogTiming = TRUE;
@ -73,7 +88,7 @@ void XAudio2AudioSystem::Initialize() {
config.LogFileline = TRUE; config.LogFileline = TRUE;
audio_->SetDebugConfiguration(&config); audio_->SetDebugConfiguration(&config);
hr = audio_->CreateMasteringVoice(&mastering_voice_); hr = audio_->CreateMasteringVoice(&mastering_voice_, active_channels_, 48000);
if (FAILED(hr)) { if (FAILED(hr)) {
XELOGE("CreateMasteringVoice failed with %.8X", hr); XELOGE("CreateMasteringVoice failed with %.8X", hr);
XEASSERTALWAYS(); XEASSERTALWAYS();
@ -81,30 +96,28 @@ void XAudio2AudioSystem::Initialize() {
} }
WAVEFORMATIEEEFLOATEX waveformat; WAVEFORMATIEEEFLOATEX waveformat;
waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE; waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
waveformat.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT; waveformat.Format.nChannels = active_channels_;
waveformat.Format.nChannels = 6;
waveformat.Format.nSamplesPerSec = 48000; waveformat.Format.nSamplesPerSec = 48000;
waveformat.Format.wBitsPerSample = 32; waveformat.Format.wBitsPerSample = 32;
waveformat.Format.nBlockAlign = (waveformat.Format.nChannels * waveformat.Format.wBitsPerSample) / 8; waveformat.Format.nBlockAlign = (waveformat.Format.nChannels * waveformat.Format.wBitsPerSample) / 8;
waveformat.Format.nAvgBytesPerSec = waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign; waveformat.Format.nAvgBytesPerSec = waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
waveformat.Format.cbSize = sizeof(waveformat) - sizeof(WAVEFORMATEX); waveformat.Format.cbSize = sizeof(waveformat)-sizeof(WAVEFORMATEX);
waveformat.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
waveformat.Samples.wValidBitsPerSample = waveformat.Format.wBitsPerSample; waveformat.Samples.wValidBitsPerSample = waveformat.Format.wBitsPerSample;
waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_CENTER | SPEAKER_FRONT_RIGHT | waveformat.dwChannelMask = ChannelMasks[waveformat.Format.nChannels];
SPEAKER_LOW_FREQUENCY |
SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT; hr = audio_->CreateSourceVoice(&pcm_voice_, &waveformat.Format,
hr = audio_->CreateSourceVoice( XAUDIO2_VOICE_NOPITCH | XAUDIO2_VOICE_NOSRC,
&pcm_voice_, (WAVEFORMATEX*)&waveformat, XAUDIO2_DEFAULT_FREQ_RATIO, voice_callback_);
XAUDIO2_VOICE_NOPITCH | XAUDIO2_VOICE_NOSRC, XAUDIO2_DEFAULT_FREQ_RATIO,
voice_callback_);
if (FAILED(hr)) { if (FAILED(hr)) {
XELOGE("CreateSourceVoice failed with %.8X", hr); XELOGE("CreateSourceVoice failed with %.8X", hr);
XEASSERTALWAYS(); XEASSERTALWAYS();
return; return;
} }
//
pcm_voice_->Start(); pcm_voice_->Start();
} }
@ -112,7 +125,7 @@ void XAudio2AudioSystem::Pump() {
XAUDIO2_VOICE_STATE state; XAUDIO2_VOICE_STATE state;
pcm_voice_->GetState(&state); pcm_voice_->GetState(&state);
auto n = state.BuffersQueued; auto n = state.BuffersQueued;
if (n > 60) { if (n > 1) {
// A lot of buffers are queued up, and until we use them block. // A lot of buffers are queued up, and until we use them block.
ResetEvent(wait_handle_); ResetEvent(wait_handle_);
} }
@ -124,20 +137,22 @@ void XAudio2AudioSystem::SubmitFrame(uint32_t samples_ptr) {
// Process samples! They are big-endian floats. // Process samples! They are big-endian floats.
HRESULT hr; HRESULT hr;
int sample_count = 6 * 256;
auto samples = reinterpret_cast<float*>( auto samples = reinterpret_cast<float*>(
emulator_->memory()->membase() + samples_ptr); emulator_->memory()->membase() + samples_ptr);
for (int i = 0; i < sample_count; ++i) {
samples_[i] = XESWAPF32BE(*(samples + i)); // interleave the data
for (int i = 0, o = 0; i < 256; ++i) {
for (int j = 0; j < 6 && j < active_channels_; ++j) {
samples_[o++] = XESWAPF32BE(*(samples + (j * 256) + i));
}
} }
// this is dumb and not right.
XAUDIO2_BUFFER buffer; XAUDIO2_BUFFER buffer;
buffer.Flags = 0; buffer.Flags = XAUDIO2_END_OF_STREAM;
buffer.AudioBytes = sample_count * sizeof(float);
buffer.pAudioData = reinterpret_cast<BYTE*>(samples_); buffer.pAudioData = reinterpret_cast<BYTE*>(samples_);
buffer.AudioBytes = sizeof(samples_);
buffer.PlayBegin = 0; buffer.PlayBegin = 0;
buffer.PlayLength = 0; buffer.PlayLength = 256;
buffer.LoopBegin = 0; buffer.LoopBegin = 0;
buffer.LoopLength = 0; buffer.LoopLength = 0;
buffer.LoopCount = 0; buffer.LoopCount = 0;

View File

@ -40,6 +40,7 @@ private:
IXAudio2* audio_; IXAudio2* audio_;
IXAudio2MasteringVoice* mastering_voice_; IXAudio2MasteringVoice* mastering_voice_;
IXAudio2SourceVoice* pcm_voice_; IXAudio2SourceVoice* pcm_voice_;
int active_channels_;
float samples_[1536]; float samples_[1536];
HANDLE wait_handle_; HANDLE wait_handle_;