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 Feb 21, 2021
1 parent 8756ee2 commit ffe7beb
Show file tree
Hide file tree
Showing 7 changed files with 316 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
109 changes: 12 additions & 97 deletions src/scripting/game_lua_kernel.cpp
Expand Up @@ -2668,23 +2668,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 @@ -2699,26 +2682,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 @@ -3979,60 +3942,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 @@ -4213,13 +4122,10 @@ 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_fog", &dispatch2<&game_lua_kernel::intf_toggle_fog, false > },
{ "add_time_area", &dispatch<&game_lua_kernel::intf_add_time_area > },
{ "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 @@ -4236,7 +4142,6 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports
{ "get_end_level_data", &dispatch<&game_lua_kernel::intf_get_end_level_data > },
{ "get_locations", &dispatch<&game_lua_kernel::intf_get_locations > },
{ "get_map_size", &dispatch<&game_lua_kernel::intf_get_map_size > },
{ "get_sound_source", &dispatch<&game_lua_kernel::intf_get_sound_source > },
{ "get_terrain", &dispatch<&game_lua_kernel::intf_get_terrain > },
{ "get_terrain_info", &dispatch<&game_lua_kernel::intf_get_terrain_info > },
{ "get_time_of_day", &dispatch<&game_lua_kernel::intf_get_time_of_day > },
Expand All @@ -4250,13 +4155,11 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports
{ "match_location", &dispatch<&game_lua_kernel::intf_match_location > },
{ "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_fog", &dispatch2<&game_lua_kernel::intf_toggle_fog, true > },
{ "remove_time_area", &dispatch<&game_lua_kernel::intf_remove_time_area > },
{ "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 @@ -4436,6 +4339,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 @@ -168,9 +168,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 ffe7beb

Please sign in to comment.