Skip to content

Commit

Permalink
Add an audio module
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Mar 23, 2021
1 parent 692f1f5 commit 4db16b5
Show file tree
Hide file tree
Showing 8 changed files with 321 additions and 111 deletions.
6 changes: 0 additions & 6 deletions data/lua/core/_initial.lua
Expand Up @@ -74,9 +74,3 @@ end
unpack = wesnoth.deprecate_api('unpack', 'table.unpack', 3, '1.17', table.unpack)
math.pow = wesnoth.deprecate_api('math.pow', '^', 3, '1.17', function(a,b) return a ^ b end)
wesnoth.compare_versions = wesnoth.deprecate_api('wesnoth.compare_versions', 'versions.compare', 1, nil, versions.compare)
if wesnoth.kernel_type() == "Game Lua Kernel" then
-- wesnoth.wml_actions.music doesn't exist yet at this point, so create a helper function instead.
wesnoth.set_music = wesnoth.deprecate_api('wesnoth.set_music', 'wesnoth.music_list', 1, nil, function(cfg)
wesnoth.wml_actions.music(cfg)
end)
end
30 changes: 30 additions & 0 deletions data/lua/core/audio.lua
@@ -0,0 +1,30 @@

if wesnoth.kernel_type() == "Game Lua Kernel" then
-- Only deprecation stubs for now
wesnoth.play_sound = wesnoth.deprecate_api('wesnoth.play_sound', 'wesnoth.audio.play', 1, nil, wesnoth.audio.play)
wesnoth.sound_volume = wesnoth.deprecate_api('wesnoth.sound_volume', 'wesnoth.audio.volume', 1, nil, function(volume)
local old_volume = wesnoth.audio.volume
if type(volume) == 'number' then
wesnoth.audio.volume = volume
end
return old_volume
end)
wesnoth.add_sound_source = wesnoth.deprecate_api('wesnoth.add_sound_source', 'wesnoth.audio.sources', 1, nil, function(cfg)
wesnoth.audio.sources[cfg.id] = cfg
end, 'Assign a config to the sound source ID')
wesnoth.get_sound_source = wesnoth.deprecate_api('wesnoth.get_sound_source', 'wesnoth.audio.sources', 1, nil, function(id)
return wesnoth.audio.sources[id]
end, 'Index by the sound source ID')
wesnoth.remove_sound_source = wesnoth.deprecate_api('wesnoth.remove_sound_source', 'wesnoth.audio.sources', 1, nil, function(id)
wesnoth.audio.sources[id] = nil
end, 'Assign nil to the sound source ID')
wesnoth.music_list = wesnoth.deprecate_api('wesnoth.music_list', 'wesnoth.audio.music_list', 1, nil, setmetatable({}, {
__len = function() return #wesnoth.audio.music_list end,
__index = function(self, key) return wesnoth.audio.music_list[key] end,
__newindex = function(self, key, value) wesnoth.audio.music_list[key] = value end,
}))
-- wesnoth.wml_actions.music doesn't exist yet at this point, so create a helper function instead.
wesnoth.set_music = wesnoth.deprecate_api('wesnoth.set_music', 'wesnoth.audio.music_list', 1, nil, function(cfg)
wesnoth.wml_actions.music(cfg)
end)
end
Expand Up @@ -1035,6 +1035,7 @@
91E3570B1CACC9B200774252 /* singleplayer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EC2F600C1A048E220018C9D6 /* singleplayer.cpp */; };
91ECD5D21BA11A5200B25CF1 /* unit_creator.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91ECD5D01BA11A5200B25CF1 /* unit_creator.cpp */; };
91F8E12E260A25E2002312BA /* lua_mathx.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91F8E12D260A25E1002312BA /* lua_mathx.cpp */; };
91F8E135260A2676002312BA /* lua_audio.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91F8E133260A2676002312BA /* lua_audio.cpp */; };
91FAC70A1C7FBC3400DAB2C3 /* lua_formula_bridge.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91FAC7091C7FBC2C00DAB2C3 /* lua_formula_bridge.cpp */; };
91FBBAD81CB6BC3F00470BFE /* filesystem_sdl.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91FBBAD71CB6BC3F00470BFE /* filesystem_sdl.cpp */; };
91FBBADB1CB6D1B700470BFE /* markov_generator.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 91FBBAD91CB6D1B700470BFE /* markov_generator.cpp */; };
Expand Down Expand Up @@ -2210,6 +2211,7 @@
91EF6C001C9E22E400E2A733 /* reference_counter.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = reference_counter.hpp; sourceTree = "<group>"; };
91F8E12C260A25E1002312BA /* lua_mathx.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = lua_mathx.hpp; sourceTree = "<group>"; };
91F8E12D260A25E1002312BA /* lua_mathx.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lua_mathx.cpp; sourceTree = "<group>"; };
91F8E133260A2676002312BA /* lua_audio.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lua_audio.cpp; sourceTree = "<group>"; };
91FAC7081C7F931900DAB2C3 /* lua_formula_bridge.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = lua_formula_bridge.hpp; sourceTree = "<group>"; };
91FAC7091C7FBC2C00DAB2C3 /* lua_formula_bridge.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lua_formula_bridge.cpp; sourceTree = "<group>"; };
91FBBAD71CB6BC3F00470BFE /* filesystem_sdl.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = filesystem_sdl.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -4615,6 +4617,7 @@
91B621E21B76BAF300B00E0F /* context.hpp */,
EC218EA21A106673007C910C /* game_lua_kernel.cpp */,
91B621E41B76BB0100B00E0F /* game_lua_kernel.hpp */,
91F8E133260A2676002312BA /* lua_audio.cpp */,
EC3863621EB6286E0048B0C8 /* lua_audio.cpp */,
EC3863631EB6286E0048B0C8 /* lua_audio.hpp */,
461DC52B241F836200B9DD10 /* lua_color.cpp */,
Expand Down Expand Up @@ -4668,6 +4671,7 @@
ECA1E1001A12755B00426E00 /* mapgen_lua_kernel.cpp */,
91B621EF1B76BB3200B00E0F /* mapgen_lua_kernel.hpp */,
91B621F01B76BB3500B00E0F /* push_check.hpp */,
EC3863631EB6286E0048B0C8 /* lua_audio.hpp */,
);
path = scripting;
sourceTree = "<group>";
Expand Down Expand Up @@ -5473,6 +5477,7 @@
46F92E732174F6A400602C1C /* addon_list.cpp in Sources */,
46F92E8D2174F6A400602C1C /* password_box.cpp in Sources */,
46F92DF92174F6A400602C1C /* unit_list.cpp in Sources */,
91F8E135260A2676002312BA /* lua_audio.cpp in Sources */,
46F92E952174F6A400602C1C /* spacer.cpp in Sources */,
627F1EDB175AF35C000042E0 /* aspect_advancements.cpp in Sources */,
EC386CF1195119AA006004ED /* animation_component.cpp in Sources */,
Expand Down
109 changes: 12 additions & 97 deletions src/scripting/game_lua_kernel.cpp
Expand Up @@ -2436,23 +2436,6 @@ int game_lua_kernel::intf_simulate_combat(lua_State *L)
return 4;
}

/**
* Modifies the music playlist.
* - Arg 1: WML table, or nil to force changes.
*/
static int intf_set_music(lua_State *L)
{
deprecated_message("wesnoth.set_music", DEP_LEVEL::INDEFINITE, "", "Use the wesnoth.playlist table instead!");
if (lua_isnoneornil(L, 1)) {
sound::commit_music_changes();
return 0;
}

config cfg = luaW_checkconfig(L, 1);
sound::play_music_config(cfg);
return 0;
}

/**
* Plays a sound, possibly repeated.
* - Arg 1: string.
Expand All @@ -2467,26 +2450,6 @@ int game_lua_kernel::intf_play_sound(lua_State *L)
return 0;
}

/**
* Gets/sets the current sound volume
* - Arg 1: (optional) New volume to set
* - Return: Original volume
*/
static int intf_sound_volume(lua_State* L)
{
int vol = preferences::sound_volume();
lua_pushnumber(L, sound::get_sound_volume() * 100.0 / vol);
if(lua_isnumber(L, 1)) {
float rel = lua_tonumber(L, 1);
if(rel < 0.0f || rel > 100.0f) {
return luaL_argerror(L, 1, "volume must be in range 0..100");
}
vol = static_cast<int>(rel*vol / 100.0f);
sound::set_sound_volume(vol);
}
return 1;
}

/**
* Scrolls to given tile.
* - Arg 1: location.
Expand Down Expand Up @@ -3783,60 +3746,6 @@ int game_lua_kernel::intf_teleport(lua_State *L)
return 0;
}

/**
* Removes a sound source by its ID
* Arg 1: sound source ID
*/
int game_lua_kernel::intf_remove_sound_source(lua_State *L)
{
soundsource::manager* man = play_controller_.get_soundsource_man();
std::string id = luaL_checkstring(L, 1);
man->remove(id);
return 0;
}

/**
* Add a new sound source
* Arg 1: Table containing keyword arguments
*/
int game_lua_kernel::intf_add_sound_source(lua_State *L)
{
soundsource::manager* man = play_controller_.get_soundsource_man();
config cfg = luaW_checkconfig(L, 1);
try {
soundsource::sourcespec spec(cfg);
man->add(spec);
man->update();
} catch (const bad_lexical_cast &) {
ERR_LUA << "Error when parsing sound_source config: invalid parameter." << std::endl;
ERR_LUA << "sound_source config was: " << cfg.debug() << std::endl;
ERR_LUA << "Skipping this sound source..." << std::endl;
}
return 0;
}

/**
* Get an existing sound source
* Arg 1: The sound source ID
* Return: Config of sound source info, or nil if it didn't exist
* This is a copy of the sound source info, so you need to call
* add_sound_source again after changing it.
*/
int game_lua_kernel::intf_get_sound_source(lua_State *L)
{
soundsource::manager* man = play_controller_.get_soundsource_man();
std::string id = luaL_checkstring(L, 1);
config cfg = man->get(id);
if(cfg.empty()) {
return 0;
}
// Sound sources do not know their own string ID
// Thus, we need to add this manually
cfg["id"] = id;
luaW_pushconfig(L, cfg);
return 1;
}

/**
* Logs a message
* Arg 1: (optional) Logger; "wml" for WML errors or deprecations
Expand Down Expand Up @@ -4017,11 +3926,8 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports
{ "get_viewing_side", &intf_get_viewing_side },
{ "invoke_synced_command", &intf_invoke_synced_command },
{ "modify_ai", &intf_modify_ai_old },
{ "set_music", &intf_set_music },
{ "sound_volume", &intf_sound_volume },
{ "unsynced", &intf_do_unsynced },
{ "add_event_handler", &dispatch<&game_lua_kernel::intf_add_event > },
{ "add_sound_source", &dispatch<&game_lua_kernel::intf_add_sound_source > },
{ "allow_end_turn", &dispatch<&game_lua_kernel::intf_allow_end_turn > },
{ "allow_undo", &dispatch<&game_lua_kernel::intf_allow_undo > },
{ "cancel_action", &dispatch<&game_lua_kernel::intf_cancel_action > },
Expand All @@ -4036,19 +3942,16 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports
{ "fire_event_by_id", &dispatch2<&game_lua_kernel::intf_fire_event, true > },
{ "get_all_vars", &dispatch<&game_lua_kernel::intf_get_all_vars > },
{ "get_end_level_data", &dispatch<&game_lua_kernel::intf_get_end_level_data > },
{ "get_sound_source", &dispatch<&game_lua_kernel::intf_get_sound_source > },
{ "get_time_of_day", &dispatch<&game_lua_kernel::intf_get_time_of_day > },
{ "get_max_liminal_bonus", &dispatch<&game_lua_kernel::intf_get_max_liminal_bonus > },
{ "get_variable", &dispatch<&game_lua_kernel::intf_get_variable > },
{ "log_replay", &dispatch<&game_lua_kernel::intf_log_replay > },
{ "log", &dispatch<&game_lua_kernel::intf_log > },
{ "message", &dispatch<&game_lua_kernel::intf_message > },
{ "open_help", &dispatch<&game_lua_kernel::intf_open_help > },
{ "play_sound", &dispatch<&game_lua_kernel::intf_play_sound > },
{ "print", &dispatch<&game_lua_kernel::intf_print > },
{ "redraw", &dispatch<&game_lua_kernel::intf_redraw > },
{ "remove_event_handler", &dispatch<&game_lua_kernel::intf_remove_event > },
{ "remove_sound_source", &dispatch<&game_lua_kernel::intf_remove_sound_source > },
{ "replace_schedule", &dispatch<&game_lua_kernel::intf_replace_schedule > },
{ "select_hex", &dispatch<&game_lua_kernel::intf_select_hex > },
{ "set_time_of_day", &dispatch<&game_lua_kernel::intf_set_time_of_day > },
Expand Down Expand Up @@ -4272,6 +4175,18 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports
lua_setfield(L, -2, "interface");
lua_pop(L, 1);

// Create the audio module
cmd_log_ << "Adding audio module...\n";
static luaL_Reg const audio_callbacks[] {
{ "play", &dispatch<&game_lua_kernel::intf_play_sound > },
{ nullptr, nullptr }
};
lua_getglobal(L, "wesnoth");
lua_newtable(L);
luaL_setfuncs(L, audio_callbacks, 0);
lua_setfield(L, -2, "audio");
lua_pop(L, 1);

// Create the playlist table with its metatable
cmd_log_ << lua_audio::register_table(L);

Expand Down
3 changes: 0 additions & 3 deletions src/scripting/game_lua_kernel.hpp
Expand Up @@ -166,9 +166,6 @@ class game_lua_kernel : public lua_kernel_base
int intf_fire_event(lua_State *L, const bool by_id);
int intf_fire_wml_menu_item(lua_State *L);
int intf_teleport(lua_State *L);
int intf_remove_sound_source(lua_State *L);
int intf_add_sound_source(lua_State *L);
int intf_get_sound_source(lua_State *L);
int intf_log(lua_State *L);
int intf_toggle_fog(lua_State *L, const bool clear);
int intf_get_fog_or_shroud(lua_State *L, bool fog);
Expand Down

0 comments on commit 4db16b5

Please sign in to comment.