Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Spring.GetSoundDevices. #1841

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion rts/Lua/LuaUnsyncedRead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
#include "System/Log/DefaultFilter.h"
#include "System/Platform/SDL1_keysym.h"
#include "System/Platform/Misc.h"
#include "System/Sound/ISound.h"
#include "System/Sound/ISoundChannels.h"
#include "System/StringUtil.h"
#include "System/Misc/SpringTime.h"
Expand Down Expand Up @@ -220,6 +221,7 @@ bool LuaUnsyncedRead::PushEntries(lua_State* L)

REGISTER_LUA_CFUNC(GetDrawSeconds);

REGISTER_LUA_CFUNC(GetSoundDevices);
REGISTER_LUA_CFUNC(GetSoundStreamTime);
REGISTER_LUA_CFUNC(GetSoundEffectParams);

Expand Down Expand Up @@ -3120,6 +3122,41 @@ int LuaUnsyncedRead::GetDrawSeconds(lua_State* L)
* @section sound
******************************************************************************/

/*** Sound device spec
*
* @table soundDeviceSpec
*
* Contains data about a sound device
*
* @string name
*/


/***
*
* @function Spring.GetSoundDevices
* @treturn {[soundDeviceSpec],...} devices Sound devices
*/
int LuaUnsyncedRead::GetSoundDevices(lua_State* L)
{
std::vector<std::string> devices = sound->GetSoundDevices();

lua_createtable(L, 0, devices.size());

for (size_t i = 0; i < devices.size(); ++i) {
std::string &device = devices[i];

lua_createtable(L, 0, 1); {
lua_pushliteral(L, "name");
lua_pushsstring(L, device);
lua_rawset(L, -3);
}

lua_rawseti(L, -2, i + 1);
}

return 1;
}

/***
*
Expand Down Expand Up @@ -4890,4 +4927,4 @@ int LuaUnsyncedRead::SolveNURBSCurve(lua_State* L)
lua_rawseti(L, -2, ++i);
}
return 1;
}
}
1 change: 1 addition & 0 deletions rts/Lua/LuaUnsyncedRead.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class LuaUnsyncedRead {

static int GetSoundStreamTime(lua_State* L);
static int GetSoundEffectParams(lua_State* L);
static int GetSoundDevices(lua_State* L);

static int GetFPS(lua_State* L);
static int GetGameSpeed(lua_State* L);
Expand Down
2 changes: 2 additions & 0 deletions rts/System/Sound/ISound.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <cstdint>
#include <string>
#include <vector>

class float3;
class LuaParser;
Expand Down Expand Up @@ -70,6 +71,7 @@ class ISound {

virtual const float3& GetListenerPos() const = 0;

virtual std::vector<std::string> GetSoundDevices() = 0;
public:
unsigned numEmptyPlayRequests = 0;
unsigned numAbortedPlays = 0;
Expand Down
2 changes: 2 additions & 0 deletions rts/System/Sound/Null/NullSound.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class NullSound : public ISound
bool LoadSoundDefsImpl(LuaParser* defsParser) { return false; }

const float3& GetListenerPos() const override { return ZeroVector; }

std::vector<std::string> GetSoundDevices() override { return std::vector<std::string>(); };
};

#endif // _NULL_SOUND_H_
30 changes: 21 additions & 9 deletions rts/System/Sound/OpenAL/Sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,16 +669,11 @@ void CSound::InitThread(int cfgMaxSounds)
// LOG(" Implementation: %s", (const char*) alcGetString(curDevice, ALC_DEVICE_SPECIFIER));
LOG(" Devices:");

const bool hasAllEnumExt = alcIsExtensionPresent(nullptr, "ALC_ENUMERATE_ALL_EXT");
const bool hasDefEnumExt = alcIsExtensionPresent(nullptr, "ALC_ENUMERATION_EXT");
std::vector<std::string> devices = GetSoundDevices();

if (hasAllEnumExt || hasDefEnumExt) {
const char* deviceSpecStr = alcGetString(nullptr, hasAllEnumExt? ALC_ALL_DEVICES_SPECIFIER: ALC_DEVICE_SPECIFIER);

while (*deviceSpecStr != '\0') {
LOG(" [%s]", deviceSpecStr);
while (*deviceSpecStr++ != '\0');
}
if (devices.size()) {
for(const std::string& deviceName: devices)
LOG(" [%s]", deviceName.c_str());
} else {
LOG(" [N/A]");
}
Expand Down Expand Up @@ -1054,3 +1049,20 @@ void CSound::GenSources(int alMaxSounds)
}
}

std::vector<std::string> CSound::GetSoundDevices()
{
std::vector<std::string> devices;
const bool hasAllEnumExt = alcIsExtensionPresent(nullptr, "ALC_ENUMERATE_ALL_EXT");
const bool hasDefEnumExt = alcIsExtensionPresent(nullptr, "ALC_ENUMERATION_EXT");

if (hasAllEnumExt || hasDefEnumExt) {
const char* deviceSpecStr = alcGetString(nullptr, hasAllEnumExt? ALC_ALL_DEVICES_SPECIFIER: ALC_DEVICE_SPECIFIER);

while (*deviceSpecStr != '\0') {
devices.emplace_back(deviceSpecStr);

while (*deviceSpecStr++ != '\0');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's a copy-paste code from the previous implementation, but it's hard to read and comprehend if it works as it should. Would be great to rewrite it to something more comprehensible like a combo of std::find_if / std::find_if_not

}
}
return devices;
}
1 change: 1 addition & 0 deletions rts/System/Sound/OpenAL/Sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class CSound : public ISound
ALCdevice* GetCurrentDevice() { return curDevice; }
int GetFrameSize() const { return frameSize; }

std::vector<std::string> GetSoundDevices() override;
private:
typedef spring::unordered_map<std::string, std::string> SoundItemNameMap;
typedef spring::unordered_map<std::string, SoundItemNameMap> SoundItemDefsMap;
Expand Down