From 2c0dab7c3282122413e199ce0eb2e9ddd2f93225 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Fri, 18 Mar 2022 12:55:25 +0000 Subject: [PATCH] Choosing the driver via a new submenu instead --- .../Gui/StartupScreen/ConfigViewTabs.as | 76 +++++++++++++++++++ Sources/Audio/ALDevice.cpp | 57 ++++++-------- Sources/Audio/ALDevice.h | 2 + Sources/Gui/StartupScreenHelper.cpp | 14 ++++ Sources/Gui/StartupScreenHelper.h | 4 + .../ScriptBindings/StartupScreenHelper.cpp | 10 +++ 6 files changed, 128 insertions(+), 35 deletions(-) diff --git a/Resources/Scripts/Gui/StartupScreen/ConfigViewTabs.as b/Resources/Scripts/Gui/StartupScreen/ConfigViewTabs.as index 4a11e634b..f39d1bf6a 100644 --- a/Resources/Scripts/Gui/StartupScreen/ConfigViewTabs.as +++ b/Resources/Scripts/Gui/StartupScreen/ConfigViewTabs.as @@ -473,8 +473,11 @@ namespace spades { StartupScreenConfigView @configViewOpenAL; StartupScreenConfigView @configViewYSR; + StartupScreenAudioOpenALEditor @editOpenAL; + private ConfigItem s_audioDriver("s_audioDriver"); private ConfigItem s_eax("s_eax"); + private ConfigItem s_openalDevice("s_openalDevice"); StartupScreenAudioTab(StartupScreenUI @ui, Vector2 size) { super(ui.manager); @@ -536,6 +539,7 @@ namespace spades { { StartupScreenConfigView cfg(Manager); + cfg.AddRow(StartupScreenConfigSliderItemEditor( ui, StartupScreenConfig(ui, "s_maxPolyphonics"), 16.0, 256.0, 8.0, _Tr("StartupScreen", "Polyphonics"), @@ -547,6 +551,13 @@ namespace spades { ui, StartupScreenConfig(ui, "s_eax"), "0", "1", _Tr("StartupScreen", "EAX"), _Tr("StartupScreen", "Enables extended features provided by the OpenAL driver to create " "more ambience."))); + AddLabel(0.f, 90.f, 20.f, _Tr("StartupScreen", "Devices")); + { + StartupScreenAudioOpenALEditor e(ui); + AddChild(e); + @editOpenAL = e; + cfg.AddRow(editOpenAL); + } cfg.Finalize(); cfg.SetHelpTextHandler(HelpTextHandler(this.HandleHelpText)); @@ -609,6 +620,71 @@ namespace spades { ui.helper.CheckConfigCapability("s_audioDriver", "null").length == 0; configViewOpenAL.LoadConfig(); configViewYSR.LoadConfig(); + editOpenAL.LoadConfig(); + + s_openalDevice.StringValue = editOpenAL.openal.StringValue; + } + } + + class StartupScreenAudioOpenALEditor : spades::ui::UIElement, LabelAddable { + StartupScreenUI @ui; + StartupScreenHelper @helper; + ConfigItem openal("openal"); + + spades::ui::Button @dropdownButton; + + StartupScreenAudioOpenALEditor(StartupScreenUI @ui) { + super(ui.manager); + @this.ui = ui; + @helper = ui.helper; + { + StartupScreenDropdownListDropdownButton e(Manager); + AddChild(e); + e.Bounds = AABB2(80.f, 0.f, 400.f, 20.f); + @e.Activated = spades::ui::EventHandler(this.ShowDropdown); + @dropdownButton = e; + } + } + + void LoadConfig() { + string drivername = openal.StringValue; + string name = _Tr("StartupScreen", "Default device", drivername); + if (drivername == "") { + name = _Tr("StartupScreen", "Default device"); + } + + int cnt = helper.GetNumAudioOpenALDevices(); + for (int i = 0; i < cnt; i++) { + if (drivername == helper.GetAudioOpenALDevice(i)) { + name = helper.GetAudioOpenALDevice(i); + } + } + + dropdownButton.Caption = name; + } + + private void ShowDropdown(spades::ui::UIElement @) { + string[] items = {_Tr("StartupScreen", "Default device")}; + int cnt = helper.GetNumAudioOpenALDevices(); + for (int i = 0; i < cnt; i++) { + string s = helper.GetAudioOpenALDevice(i); + items.insertLast(s); + } + spades::ui::ShowDropDownList(this, items, + spades::ui::DropDownListHandler(this.DropdownHandler)); + } + + private void DropdownHandler(int index) { + if (index >= 0) { + if (index == 0) { + openal = "default"; + } else { + openal = helper.GetAudioOpenALDevice(index - 1); + } + + // Reload the startup screen so the language config takes effect + ui.Reload(); + } } } diff --git a/Sources/Audio/ALDevice.cpp b/Sources/Audio/ALDevice.cpp index 06aa859e9..6b50ca87c 100644 --- a/Sources/Audio/ALDevice.cpp +++ b/Sources/Audio/ALDevice.cpp @@ -39,6 +39,7 @@ DEFINE_SPADES_SETTING(s_maxPolyphonics, "96"); DEFINE_SPADES_SETTING(s_eax, "1"); DEFINE_SPADES_SETTING(s_alPreciseErrorCheck, "1"); DEFINE_SPADES_SETTING(s_gain, "1"); +DEFINE_SPADES_SETTING(s_openalDevice, ""); // lm: seems to be missing for me.. #ifndef ALC_ALL_DEVICES_SPECIFIER @@ -421,7 +422,7 @@ namespace spades { Internal() { SPADES_MARK_FUNCTION(); - std::vector devs; + const char *ext, *dev; if (al::qalGetString(AL_EXTENSIONS)) { std::vector strs = Split(al::qalGetString(AL_EXTENSIONS), " "); @@ -431,38 +432,11 @@ namespace spades { } } - SPLog("--- All devices ---"); - const ALCchar *ext = al::qalcGetString(NULL, ALC_ALL_DEVICES_SPECIFIER); - while (ext && *ext) { - SPLog("%s", ext); - devs.push_back(ext); - ext += (std::strlen(ext) + 1); - } - SPLog("-------------------"); - - SPLog("--- Devices ---"); - ext = al::qalcGetString(NULL, ALC_DEVICE_SPECIFIER); - while (ext && *ext) { - SPLog("%s", ext); - devs.push_back(ext); - ext += (std::strlen(ext) + 1); - } - SPLog("---------------"); - const ALCchar *dev = al::qalcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER); - if (dev && *dev) { - SPLog("Default device: %s", dev); - } - - if (!(alDevice = al::qalcOpenDevice(NULL))) { - SPLog("Failed to open default OpenAL device"); - for (const auto &d: devs) { - if (dev && *dev && !strcmp(dev, d.c_str())) - continue; - SPLog("Opening handle attempt on device: %s", d.c_str()); - if ((alDevice = al::qalcOpenDevice(d.c_str()))) - break; - } - } + dev = s_openalDevice.CString(); + SPLog("OpenAL opening device: %s", dev); + if (!strcmp(dev, "default")) + dev = NULL; + alDevice = al::qalcOpenDevice(dev); if (!alDevice) { if ((ext = al::qalcGetString(NULL, ALC_EXTENSIONS))) { @@ -571,7 +545,7 @@ namespace spades { ALSrc *AllocChunk() { SPADES_MARK_FUNCTION(); - size_t start = SampleRandomInt(0, srcs.size() - 1); + size_t start = SampleRandomInt(0, srcs.size() - 1); for (size_t i = 0; i < srcs.size(); i++) { ALSrc *src = srcs[(i + start) % srcs.size()]; if (src->IsPlaying()) @@ -579,7 +553,7 @@ namespace spades { return src; } - ALSrc *src = SampleRandomElement(srcs); + ALSrc *src = SampleRandomElement(srcs); src->Terminate(); return src; } @@ -768,6 +742,19 @@ namespace spades { d = new Internal(); } + std::vector ALDevice::DeviceList() { + std::vector devs; + const ALCchar *ext = al::qalcGetString(NULL, ALC_ALL_DEVICES_SPECIFIER); + if (!ext || *ext == '\0') + ext = al::qalcGetString(NULL, ALC_DEVICE_SPECIFIER); + while (ext && *ext) { + devs.push_back(ext); + ext += std::strlen(ext) + 1; + } + + return devs; + } + bool ALDevice::TryLoad() { try { al::Link(); diff --git a/Sources/Audio/ALDevice.h b/Sources/Audio/ALDevice.h index 17972352f..1ff47e767 100644 --- a/Sources/Audio/ALDevice.h +++ b/Sources/Audio/ALDevice.h @@ -45,6 +45,8 @@ namespace spades { client::IAudioChunk *RegisterSound(const char *name) override; + static std::vector DeviceList(); + void ClearCache() override; void SetGameMap(client::GameMap *) override; diff --git a/Sources/Gui/StartupScreenHelper.cpp b/Sources/Gui/StartupScreenHelper.cpp index a25068ccb..fa7c6bd5c 100644 --- a/Sources/Gui/StartupScreenHelper.cpp +++ b/Sources/Gui/StartupScreenHelper.cpp @@ -166,6 +166,13 @@ namespace spades { })); } + // check openAL drivers + SPLog("Checking OpenAL available drivers"); + openalDevices = audio::ALDevice::DeviceList(); + for (const auto &d: openalDevices) { + SPLog("%s", d.c_str()); + } + // check GL capabilities SPLog("Performing ecapability query"); @@ -754,6 +761,13 @@ namespace spades { report += '\n'; } + int StartupScreenHelper::GetNumAudioOpenALDevices() { return static_cast(openalDevices.size()); } + std::string StartupScreenHelper::GetAudioOpenALDevice(int index) { + if (index < 0 || index >= GetNumAudioOpenALDevices()) + SPInvalidArgument("index"); + return openalDevices[index]; + } + int StartupScreenHelper::GetNumLocales() { return static_cast(locales.size()); } std::string StartupScreenHelper::GetLocale(int index) { if (index < 0 || index >= GetNumLocales()) diff --git a/Sources/Gui/StartupScreenHelper.h b/Sources/Gui/StartupScreenHelper.h index 74bbec278..5710d15c0 100644 --- a/Sources/Gui/StartupScreenHelper.h +++ b/Sources/Gui/StartupScreenHelper.h @@ -50,6 +50,7 @@ namespace spades { void AddReport(const std::string &text = std::string(), Vector4 color = Vector4::Make(1.f, 1.f, 1.f, 1.f)); + std::vector openalDevices; struct LocaleInfo { std::string name; std::string descriptionNative; @@ -78,6 +79,9 @@ namespace spades { int GetVideoModeWidth(int index); int GetVideoModeHeight(int index); + int GetNumAudioOpenALDevices(); + std::string GetAudioOpenALDevice(int index); + int GetNumReportLines(); std::string GetReport() { return report; } std::string GetReportLineText(int line); diff --git a/Sources/ScriptBindings/StartupScreenHelper.cpp b/Sources/ScriptBindings/StartupScreenHelper.cpp index 3dccaff88..587c80e0e 100644 --- a/Sources/ScriptBindings/StartupScreenHelper.cpp +++ b/Sources/ScriptBindings/StartupScreenHelper.cpp @@ -72,6 +72,16 @@ namespace spades { asMETHOD(gui::StartupScreenHelper, GetVideoModeHeight), asCALL_THISCALL); manager->CheckError(r); + r = eng->RegisterObjectMethod("StartupScreenHelper", + "int GetNumAudioOpenALDevices()", + asMETHOD(gui::StartupScreenHelper, GetNumAudioOpenALDevices), + asCALL_THISCALL); + manager->CheckError(r); + r = eng->RegisterObjectMethod("StartupScreenHelper", + "string GetAudioOpenALDevice(int)", + asMETHOD(gui::StartupScreenHelper, GetAudioOpenALDevice), + asCALL_THISCALL); + manager->CheckError(r); r = eng->RegisterObjectMethod("StartupScreenHelper", "int GetNumReportLines()", asMETHOD(gui::StartupScreenHelper, GetNumReportLines),