Merge pull request #5631 from LAGonauta/openal-real-latency

Fixed latency setting and cleaned-up OpenAL backend
This commit is contained in:
shuffle2 2017-06-27 11:31:16 -07:00 committed by GitHub
commit ce4d514542
5 changed files with 115 additions and 129 deletions

View File

@ -100,27 +100,27 @@ bool OpenALStream::Start()
return false; return false;
} }
const char* defaultDeviceName = palcGetString(nullptr, ALC_DEFAULT_DEVICE_SPECIFIER); const char* default_device_dame = palcGetString(nullptr, ALC_DEFAULT_DEVICE_SPECIFIER);
INFO_LOG(AUDIO, "Found OpenAL device %s", defaultDeviceName); INFO_LOG(AUDIO, "Found OpenAL device %s", default_device_dame);
ALCdevice* pDevice = palcOpenDevice(defaultDeviceName); ALCdevice* device = palcOpenDevice(default_device_dame);
if (!pDevice) if (!device)
{ {
PanicAlertT("OpenAL: can't open device %s", defaultDeviceName); PanicAlertT("OpenAL: can't open device %s", default_device_dame);
return false; return false;
} }
ALCcontext* pContext = palcCreateContext(pDevice, nullptr); ALCcontext* context = palcCreateContext(device, nullptr);
if (!pContext) if (!context)
{ {
palcCloseDevice(pDevice); palcCloseDevice(device);
PanicAlertT("OpenAL: can't create context for device %s", defaultDeviceName); PanicAlertT("OpenAL: can't create context for device %s", default_device_dame);
return false; return false;
} }
palcMakeContextCurrent(pContext); palcMakeContextCurrent(context);
m_run_thread.Set(); m_run_thread.Set();
thread = std::thread(&OpenALStream::SoundLoop, this); m_thread = std::thread(&OpenALStream::SoundLoop, this);
return true; return true;
} }
@ -128,37 +128,37 @@ void OpenALStream::Stop()
{ {
m_run_thread.Clear(); m_run_thread.Clear();
// kick the thread if it's waiting // kick the thread if it's waiting
soundSyncEvent.Set(); m_sound_sync_event.Set();
thread.join(); m_thread.join();
palSourceStop(uiSource); palSourceStop(m_source);
palSourcei(uiSource, AL_BUFFER, 0); palSourcei(m_source, AL_BUFFER, 0);
// Clean up buffers and sources // Clean up buffers and sources
palDeleteSources(1, &uiSource); palDeleteSources(1, &m_source);
uiSource = 0; m_source = 0;
palDeleteBuffers(numBuffers, uiBuffers); palDeleteBuffers(OAL_BUFFERS, m_buffers.data());
ALCcontext* pContext = palcGetCurrentContext(); ALCcontext* context = palcGetCurrentContext();
ALCdevice* pDevice = palcGetContextsDevice(pContext); ALCdevice* device = palcGetContextsDevice(context);
palcMakeContextCurrent(nullptr); palcMakeContextCurrent(nullptr);
palcDestroyContext(pContext); palcDestroyContext(context);
palcCloseDevice(pDevice); palcCloseDevice(device);
} }
void OpenALStream::SetVolume(int volume) void OpenALStream::SetVolume(int volume)
{ {
fVolume = (float)volume / 100.0f; m_volume = (float)volume / 100.0f;
if (uiSource) if (m_source)
palSourcef(uiSource, AL_GAIN, fVolume); palSourcef(m_source, AL_GAIN, m_volume);
} }
void OpenALStream::Update() void OpenALStream::Update()
{ {
soundSyncEvent.Set(); m_sound_sync_event.Set();
} }
void OpenALStream::Clear(bool mute) void OpenALStream::Clear(bool mute)
@ -167,11 +167,11 @@ void OpenALStream::Clear(bool mute)
if (m_muted) if (m_muted)
{ {
palSourceStop(uiSource); palSourceStop(m_source);
} }
else else
{ {
palSourcePlay(uiSource); palSourcePlay(m_source);
} }
} }
@ -229,86 +229,109 @@ void OpenALStream::SoundLoop()
// we just check if one is being used. // we just check if one is being used.
bool fixed32_capable = IsCreativeXFi(); bool fixed32_capable = IsCreativeXFi();
u32 ulFrequency = m_mixer->GetSampleRate(); u32 frequency = m_mixer->GetSampleRate();
numBuffers = SConfig::GetInstance().iLatency + 2; // OpenAL requires a minimum of two buffers
memset(uiBuffers, 0, numBuffers * sizeof(ALuint)); u32 frames_per_buffer;
uiSource = 0; // Can't have zero samples per buffer
if (SConfig::GetInstance().iLatency > 0)
{
frames_per_buffer = frequency / 1000 * SConfig::GetInstance().iLatency / OAL_BUFFERS;
}
else
{
frames_per_buffer = frequency / 1000 * 1 / OAL_BUFFERS;
}
if (frames_per_buffer > OAL_MAX_FRAMES)
{
frames_per_buffer = OAL_MAX_FRAMES;
}
// DPL2 needs a minimum number of samples to work (FWRDURATION)
if (use_surround && frames_per_buffer < 240)
{
frames_per_buffer = 240;
}
INFO_LOG(AUDIO, "Using %d buffers, each with %d audio frames for a total of %d.", OAL_BUFFERS,
frames_per_buffer, frames_per_buffer * OAL_BUFFERS);
// Should we make these larger just in case the mixer ever sends more samples
// than what we request?
m_realtime_buffer.resize(frames_per_buffer * STEREO_CHANNELS);
m_source = 0;
// Clear error state before querying or else we get false positives. // Clear error state before querying or else we get false positives.
ALenum err = palGetError(); ALenum err = palGetError();
// Generate some AL Buffers for streaming // Generate some AL Buffers for streaming
palGenBuffers(numBuffers, (ALuint*)uiBuffers); palGenBuffers(OAL_BUFFERS, (ALuint*)m_buffers.data());
err = CheckALError("generating buffers"); err = CheckALError("generating buffers");
// Generate a Source to playback the Buffers // Generate a Source to playback the Buffers
palGenSources(1, &uiSource); palGenSources(1, &m_source);
err = CheckALError("generating sources"); err = CheckALError("generating sources");
// Set the default sound volume as saved in the config file. // Set the default sound volume as saved in the config file.
palSourcef(uiSource, AL_GAIN, fVolume); palSourcef(m_source, AL_GAIN, m_volume);
// TODO: Error handling // TODO: Error handling
// ALenum err = alGetError(); // ALenum err = alGetError();
unsigned int nextBuffer = 0; unsigned int next_buffer = 0;
unsigned int numBuffersQueued = 0; unsigned int num_buffers_queued = 0;
ALint iState = 0; ALint state = 0;
while (m_run_thread.IsSet()) while (m_run_thread.IsSet())
{ {
// Block until we have a free buffer // Block until we have a free buffer
int numBuffersProcessed; int num_buffers_processed;
palGetSourcei(uiSource, AL_BUFFERS_PROCESSED, &numBuffersProcessed); palGetSourcei(m_source, AL_BUFFERS_PROCESSED, &num_buffers_processed);
if (numBuffers == numBuffersQueued && !numBuffersProcessed) if (num_buffers_queued == OAL_BUFFERS && !num_buffers_processed)
{ {
soundSyncEvent.Wait(); std::this_thread::sleep_for(std::chrono::milliseconds(1));
continue; continue;
} }
// Remove the Buffer from the Queue. // Remove the Buffer from the Queue.
if (numBuffersProcessed) if (num_buffers_processed)
{ {
ALuint unqueuedBufferIds[OAL_MAX_BUFFERS]; std::array<ALuint, OAL_BUFFERS> unqueued_buffer_ids;
palSourceUnqueueBuffers(uiSource, numBuffersProcessed, unqueuedBufferIds); palSourceUnqueueBuffers(m_source, num_buffers_processed, unqueued_buffer_ids.data());
err = CheckALError("unqueuing buffers"); err = CheckALError("unqueuing buffers");
numBuffersQueued -= numBuffersProcessed; num_buffers_queued -= num_buffers_processed;
} }
unsigned int numSamples = OAL_MAX_SAMPLES; unsigned int min_frames = frames_per_buffer;
if (use_surround) if (use_surround)
{ {
// DPL2 accepts 240 samples minimum (FWRDURATION) std::array<float, OAL_MAX_FRAMES * SURROUND_CHANNELS> dpl2;
unsigned int minSamples = 240; u32 rendered_frames = m_mixer->MixSurround(dpl2.data(), min_frames);
float dpl2[OAL_MAX_SAMPLES * OAL_MAX_BUFFERS * SURROUND_CHANNELS]; if (rendered_frames < min_frames)
numSamples = m_mixer->MixSurround(dpl2, numSamples);
if (numSamples < minSamples)
continue; continue;
// zero-out the subwoofer channel - DPL2Decode generates a pretty // zero-out the subwoofer channel - DPL2Decode generates a pretty
// good 5.0 but not a good 5.1 output. Sadly there is not a 5.0 // good 5.0 but not a good 5.1 output. Sadly there is not a 5.0
// AL_FORMAT_50CHN32 to make this super-explicit. // AL_FORMAT_50CHN32 to make this super-explicit.
// DPL2Decode output: LEFTFRONT, RIGHTFRONT, CENTREFRONT, (sub), LEFTREAR, RIGHTREAR // DPL2Decode output: LEFTFRONT, RIGHTFRONT, CENTREFRONT, (sub), LEFTREAR, RIGHTREAR
for (u32 i = 0; i < numSamples; ++i) for (u32 i = 0; i < rendered_frames; ++i)
{ {
dpl2[i * SURROUND_CHANNELS + 3 /*sub/lfe*/] = 0.0f; dpl2[i * SURROUND_CHANNELS + 3 /*sub/lfe*/] = 0.0f;
} }
if (float32_capable) if (float32_capable)
{ {
palBufferData(uiBuffers[nextBuffer], AL_FORMAT_51CHN32, dpl2, palBufferData(m_buffers[next_buffer], AL_FORMAT_51CHN32, dpl2.data(),
numSamples * FRAME_SURROUND_FLOAT, ulFrequency); rendered_frames * FRAME_SURROUND_FLOAT, frequency);
} }
else if (fixed32_capable) else if (fixed32_capable)
{ {
int surround_int32[OAL_MAX_SAMPLES * SURROUND_CHANNELS * OAL_MAX_BUFFERS]; std::array<int, OAL_MAX_FRAMES * SURROUND_CHANNELS> surround_int32;
for (u32 i = 0; i < numSamples * SURROUND_CHANNELS; ++i) for (u32 i = 0; i < rendered_frames * SURROUND_CHANNELS; ++i)
{ {
// For some reason the ffdshow's DPL2 decoder outputs samples bigger than 1. // For some reason the ffdshow's DPL2 decoder outputs samples bigger than 1.
// Most are close to 2.5 and some go up to 8. Hard clamping here, we need to // Most are close to 2.5 and some go up to 8. Hard clamping here, we need to
@ -319,17 +342,17 @@ void OpenALStream::SoundLoop()
else if (dpl2[i] < INT_MIN) else if (dpl2[i] < INT_MIN)
surround_int32[i] = INT_MIN; surround_int32[i] = INT_MIN;
else else
surround_int32[i] = (int)dpl2[i]; surround_int32[i] = static_cast<int>(dpl2[i]);
} }
palBufferData(uiBuffers[nextBuffer], AL_FORMAT_51CHN32, surround_int32, palBufferData(m_buffers[next_buffer], AL_FORMAT_51CHN32, surround_int32.data(),
numSamples * FRAME_SURROUND_INT32, ulFrequency); rendered_frames * FRAME_SURROUND_INT32, frequency);
} }
else else
{ {
short surround_short[OAL_MAX_SAMPLES * SURROUND_CHANNELS * OAL_MAX_BUFFERS]; std::array<short, OAL_MAX_FRAMES * SURROUND_CHANNELS> surround_short;
for (u32 i = 0; i < numSamples * SURROUND_CHANNELS; ++i) for (u32 i = 0; i < rendered_frames * SURROUND_CHANNELS; ++i)
{ {
dpl2[i] = dpl2[i] * (1 << 15); dpl2[i] = dpl2[i] * (1 << 15);
if (dpl2[i] > SHRT_MAX) if (dpl2[i] > SHRT_MAX)
@ -337,11 +360,11 @@ void OpenALStream::SoundLoop()
else if (dpl2[i] < SHRT_MIN) else if (dpl2[i] < SHRT_MIN)
surround_short[i] = SHRT_MIN; surround_short[i] = SHRT_MIN;
else else
surround_short[i] = (int)dpl2[i]; surround_short[i] = static_cast<int>(dpl2[i]);
} }
palBufferData(uiBuffers[nextBuffer], AL_FORMAT_51CHN16, surround_short, palBufferData(m_buffers[next_buffer], AL_FORMAT_51CHN16, surround_short.data(),
numSamples * FRAME_SURROUND_SHORT, ulFrequency); rendered_frames * FRAME_SURROUND_SHORT, frequency);
} }
err = CheckALError("buffering data"); err = CheckALError("buffering data");
@ -355,59 +378,26 @@ void OpenALStream::SoundLoop()
} }
else else
{ {
numSamples = m_mixer->Mix(realtimeBuffer, numSamples); u32 rendered_frames = m_mixer->Mix(m_realtime_buffer.data(), min_frames);
// Convert the samples from short to float if (!rendered_frames)
for (u32 i = 0; i < numSamples * STEREO_CHANNELS; ++i)
sampleBuffer[i] = static_cast<float>(realtimeBuffer[i]) / (1 << 15);
if (!numSamples)
continue; continue;
if (float32_capable) palBufferData(m_buffers[next_buffer], AL_FORMAT_STEREO16, m_realtime_buffer.data(),
{ rendered_frames * FRAME_STEREO_SHORT, frequency);
palBufferData(uiBuffers[nextBuffer], AL_FORMAT_STEREO_FLOAT32, sampleBuffer,
numSamples * FRAME_STEREO_FLOAT, ulFrequency);
err = CheckALError("buffering float32 data");
if (err == AL_INVALID_ENUM)
{
float32_capable = false;
}
}
else if (fixed32_capable)
{
// Clamping is not necessary here, samples are always between (-1,1)
int stereo_int32[OAL_MAX_SAMPLES * STEREO_CHANNELS * OAL_MAX_BUFFERS];
for (u32 i = 0; i < numSamples * STEREO_CHANNELS; ++i)
stereo_int32[i] = (int)((float)sampleBuffer[i] * (INT64_C(1) << 31));
palBufferData(uiBuffers[nextBuffer], AL_FORMAT_STEREO32, stereo_int32,
numSamples * FRAME_STEREO_INT32, ulFrequency);
}
else
{
// Convert the samples from float to short
short stereo[OAL_MAX_SAMPLES * STEREO_CHANNELS * OAL_MAX_BUFFERS];
for (u32 i = 0; i < numSamples * STEREO_CHANNELS; ++i)
stereo[i] = (short)((float)sampleBuffer[i] * (1 << 15));
palBufferData(uiBuffers[nextBuffer], AL_FORMAT_STEREO16, stereo,
numSamples * FRAME_STEREO_SHORT, ulFrequency);
}
} }
palSourceQueueBuffers(uiSource, 1, &uiBuffers[nextBuffer]); palSourceQueueBuffers(m_source, 1, &m_buffers[next_buffer]);
err = CheckALError("queuing buffers"); err = CheckALError("queuing buffers");
numBuffersQueued++; num_buffers_queued++;
nextBuffer = (nextBuffer + 1) % numBuffers; next_buffer = (next_buffer + 1) % OAL_BUFFERS;
palGetSourcei(uiSource, AL_SOURCE_STATE, &iState); palGetSourcei(m_source, AL_SOURCE_STATE, &state);
if (iState != AL_PLAYING) if (state != AL_PLAYING)
{ {
// Buffer underrun occurred, resume playback // Buffer underrun occurred, resume playback
palSourcePlay(uiSource); palSourcePlay(m_source);
err = CheckALError("occurred resuming playback"); err = CheckALError("occurred resuming playback");
} }
} }

View File

@ -17,17 +17,15 @@
#include <OpenAL/include/alc.h> #include <OpenAL/include/alc.h>
#include <OpenAL/include/alext.h> #include <OpenAL/include/alext.h>
#define SFX_MAX_SOURCE 1 // OpenAL requires a minimum of two buffers, three or more recommended
#define OAL_MAX_BUFFERS 32 #define OAL_BUFFERS 3
#define OAL_MAX_SAMPLES 256 #define OAL_MAX_FRAMES 4096
#define STEREO_CHANNELS 2 #define STEREO_CHANNELS 2
#define SURROUND_CHANNELS 6 // number of channels in surround mode #define SURROUND_CHANNELS 6 // number of channels in surround mode
#define SIZE_SHORT 2 #define SIZE_SHORT 2
#define SIZE_INT32 4 #define SIZE_INT32 4
#define SIZE_FLOAT 4 // size of a float in bytes #define SIZE_FLOAT 4 // size of a float in bytes
#define FRAME_STEREO_SHORT STEREO_CHANNELS* SIZE_SHORT #define FRAME_STEREO_SHORT STEREO_CHANNELS* SIZE_SHORT
#define FRAME_STEREO_FLOAT STEREO_CHANNELS* SIZE_FLOAT
#define FRAME_STEREO_INT32 STEREO_CHANNELS* SIZE_INT32
#define FRAME_SURROUND_FLOAT SURROUND_CHANNELS* SIZE_FLOAT #define FRAME_SURROUND_FLOAT SURROUND_CHANNELS* SIZE_FLOAT
#define FRAME_SURROUND_SHORT SURROUND_CHANNELS* SIZE_SHORT #define FRAME_SURROUND_SHORT SURROUND_CHANNELS* SIZE_SHORT
#define FRAME_SURROUND_INT32 SURROUND_CHANNELS* SIZE_INT32 #define FRAME_SURROUND_INT32 SURROUND_CHANNELS* SIZE_INT32
@ -56,7 +54,7 @@ class OpenALStream final : public SoundStream
{ {
#ifdef _WIN32 #ifdef _WIN32
public: public:
OpenALStream() : uiSource(0) {} OpenALStream() : m_source(0) {}
bool Start() override; bool Start() override;
void SoundLoop() override; void SoundLoop() override;
void SetVolume(int volume) override; void SetVolume(int volume) override;
@ -67,17 +65,15 @@ public:
static bool isValid(); static bool isValid();
private: private:
std::thread thread; std::thread m_thread;
Common::Flag m_run_thread; Common::Flag m_run_thread;
Common::Event soundSyncEvent; Common::Event m_sound_sync_event;
short realtimeBuffer[OAL_MAX_SAMPLES * STEREO_CHANNELS]; std::vector<short> m_realtime_buffer;
float sampleBuffer[OAL_MAX_SAMPLES * SURROUND_CHANNELS * OAL_MAX_BUFFERS]; std::array<ALuint, OAL_BUFFERS> m_buffers;
ALuint uiBuffers[OAL_MAX_BUFFERS]; ALuint m_source;
ALuint uiSource; ALfloat m_volume;
ALfloat fVolume;
u8 numBuffers;
#endif // _WIN32 #endif // _WIN32
}; };

View File

@ -254,7 +254,7 @@ void SConfig::SaveCoreSettings(IniFile& ini)
core->Set("SelectedLanguage", SelectedLanguage); core->Set("SelectedLanguage", SelectedLanguage);
core->Set("OverrideGCLang", bOverrideGCLanguage); core->Set("OverrideGCLang", bOverrideGCLanguage);
core->Set("DPL2Decoder", bDPL2Decoder); core->Set("DPL2Decoder", bDPL2Decoder);
core->Set("Latency", iLatency); core->Set("AudioLatency", iLatency);
core->Set("AudioStretch", m_audio_stretch); core->Set("AudioStretch", m_audio_stretch);
core->Set("AudioStretchMaxLatency", m_audio_stretch_max_latency); core->Set("AudioStretchMaxLatency", m_audio_stretch_max_latency);
core->Set("MemcardAPath", m_strMemoryCardA); core->Set("MemcardAPath", m_strMemoryCardA);
@ -568,7 +568,7 @@ void SConfig::LoadCoreSettings(IniFile& ini)
core->Get("SelectedLanguage", &SelectedLanguage, 0); core->Get("SelectedLanguage", &SelectedLanguage, 0);
core->Get("OverrideGCLang", &bOverrideGCLanguage, false); core->Get("OverrideGCLang", &bOverrideGCLanguage, false);
core->Get("DPL2Decoder", &bDPL2Decoder, false); core->Get("DPL2Decoder", &bDPL2Decoder, false);
core->Get("Latency", &iLatency, 5); core->Get("AudioLatency", &iLatency, 20);
core->Get("AudioStretch", &m_audio_stretch, false); core->Get("AudioStretch", &m_audio_stretch, false);
core->Get("AudioStretchMaxLatency", &m_audio_stretch_max_latency, 80); core->Get("AudioStretchMaxLatency", &m_audio_stretch_max_latency, 80);
core->Get("MemcardAPath", &m_strMemoryCardA); core->Get("MemcardAPath", &m_strMemoryCardA);
@ -831,7 +831,7 @@ void SConfig::LoadDefaults()
bOverrideGCLanguage = false; bOverrideGCLanguage = false;
bWii = false; bWii = false;
bDPL2Decoder = false; bDPL2Decoder = false;
iLatency = 14; iLatency = 20;
m_audio_stretch = false; m_audio_stretch = false;
m_audio_stretch_max_latency = 80; m_audio_stretch_max_latency = 80;

View File

@ -109,7 +109,7 @@ struct SConfig : NonCopyable
bool bCopyWiiSaveNetplay = true; bool bCopyWiiSaveNetplay = true;
bool bDPL2Decoder = false; bool bDPL2Decoder = false;
int iLatency = 14; int iLatency = 20;
bool m_audio_stretch = false; bool m_audio_stretch = false;
int m_audio_stretch_max_latency = 80; int m_audio_stretch_max_latency = 80;

View File

@ -45,8 +45,8 @@ void AudioConfigPane::InitializeGUI()
m_audio_backend_choice = m_audio_backend_choice =
new wxChoice(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, m_audio_backend_strings); new wxChoice(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, m_audio_backend_strings);
m_audio_latency_spinctrl = m_audio_latency_spinctrl =
new wxSpinCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 0, 30); new wxSpinCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 0, 200);
m_audio_latency_label = new wxStaticText(this, wxID_ANY, _("Latency:")); m_audio_latency_label = new wxStaticText(this, wxID_ANY, _("Latency (ms):"));
m_stretch_checkbox = new wxCheckBox(this, wxID_ANY, _("Enable Audio Stretching")); m_stretch_checkbox = new wxCheckBox(this, wxID_ANY, _("Enable Audio Stretching"));
m_stretch_label = new wxStaticText(this, wxID_ANY, _("Buffer Size:")); m_stretch_label = new wxStaticText(this, wxID_ANY, _("Buffer Size:"));