Skip to content

Commit

Permalink
Port [volume] to Lua
Browse files Browse the repository at this point in the history
This also fixes [volume] considering 100% to be an invalid volume.
  • Loading branch information
CelticMinstrel committed Apr 18, 2017
1 parent 8d44cfd commit 98e68d6
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 28 deletions.
3 changes: 3 additions & 0 deletions changelog
Expand Up @@ -25,9 +25,11 @@ Version 1.13.7+dev:
* wesnoth.playlist.add appends a track to the playlist (as [music]append=yes)
* wesnoth.playlist.clear clears the current playlist
* wesnoth.playlist.commit forces any pending playlist changes to be immediately applied
* wesnoth.playlist.volume attribute gets/sets the current music volume, as [volume]music=
* Each track has modifiable shuffle, once, ms_before, ms_after attributes and
read-only append, immediate, once, name, title attributes. They are also
comparable with == or ~=
* New wesnoth.sound_volume function gets/sets the current sound volume, as [volume]sound=
* Performance:
* Greatly speeded up switching between add-ons in the add-on manager (bug #25523)
* User Interface:
Expand All @@ -42,6 +44,7 @@ Version 1.13.7+dev:
* [image] has new resize_mode=tile_center
* Canvas colors can now be expressed as formulas. The formula must return
the color as a list of its components, eg "([r, g, b, a])"
* Fix [volume] not accepting 100% as the new volume.

Version 1.13.7:
* AI:
Expand Down
11 changes: 11 additions & 0 deletions data/lua/wml-tags.lua
Expand Up @@ -285,6 +285,17 @@ function wml_actions.music(cfg)
end
end

function wml_actions.volume(cfg)
if cfg.music then
local rel = tonumber(cfg.music) or 100.0
wesnoth.playlist.volume = rel
end
if cfg.sound then
local rel = tonumber(cfg.sound) or 100.0
wesnoth.sound_volume(rel)
end
end

-- This is mainly for use in unit test macros, but maybe it can be useful elsewhere too
function wml_actions.test_condition(cfg)
local logger = cfg.logger or "warning"
Expand Down
28 changes: 0 additions & 28 deletions src/game_events/action_wml.cpp
Expand Up @@ -929,34 +929,6 @@ WML_HANDLER_FUNCTION(unit,, cfg)

}

WML_HANDLER_FUNCTION(volume,, cfg)
{

int vol;
float rel;
std::string music = cfg["music"];
std::string sound = cfg["sound"];

if(!music.empty()) {
vol = preferences::music_volume();
rel = atof(music.c_str());
if (rel >= 0.0f && rel < 100.0f) {
vol = static_cast<int>(rel*vol/100.0f);
}
sound::set_music_volume(vol);
}

if(!sound.empty()) {
vol = preferences::sound_volume();
rel = atof(sound.c_str());
if (rel >= 0.0f && rel < 100.0f) {
vol = static_cast<int>(rel*vol/100.0f);
}
sound::set_sound_volume(vol);
}

}

WML_HANDLER_FUNCTION(on_undo, event_info, cfg)
{
if(cfg["delayed_variable_substitution"].to_bool(false)) {
Expand Down
21 changes: 21 additions & 0 deletions src/scripting/game_lua_kernel.cpp
Expand Up @@ -2505,6 +2505,26 @@ 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.0f / 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 @@ -3910,6 +3930,7 @@ game_lua_kernel::game_lua_kernel(game_state & gs, play_controller & pc, reports
{ "modify_ai", &intf_modify_ai_old },
{ "remove_modifications", &intf_remove_modifications },
{ "set_music", &intf_set_music },
{ "sound_volume", &intf_sound_volume },
{ "transform_unit", &intf_transform_unit },
{ "unit_defense", &intf_unit_defense },
{ "unit_movement_cost", &intf_unit_movement_cost },
Expand Down
6 changes: 6 additions & 0 deletions src/scripting/lua_audio.cpp
Expand Up @@ -19,6 +19,7 @@ See the COPYING file for more details.
#include "sound.hpp"
#include "sound_music_track.hpp"
#include "config_assign.hpp"
#include "preferences.hpp"

static const char* Track = "music track";

Expand Down Expand Up @@ -63,6 +64,9 @@ static int impl_music_get(lua_State* L) {
return 1;
}
return_int_attrib("current_i", sound::get_current_track() + 1);
// This calculation reverses the one used in [volume] to get back the relative volume level.
// (Which is the same calculation that's duplicated in impl_music_set.)
return_float_attrib("volume", sound::get_music_volume() * 100.0f / preferences::music_volume());
return luaW_getmetafield(L, 1, m);
}

Expand All @@ -72,6 +76,8 @@ static int impl_music_set(lua_State* L) {
sound::set_track(lua_tointeger(L, 2), *track);
return 0;
}
const char* m = luaL_checkstring(L, 2);
modify_float_attrib_check_range("volume", sound::set_music_volume(value * preferences::music_volume() / 100.0f), 0.0, 100.0)
// TODO: Set "current" and "current_i"
return 0;
}
Expand Down
17 changes: 17 additions & 0 deletions src/sound.cpp
Expand Up @@ -895,6 +895,14 @@ void play_UI_sound(const std::string& files)
}
}

int get_music_volume()
{
if(mix_ok) {
return Mix_VolumeMusic(-1);
}
return 0;
}

void set_music_volume(int vol)
{
if(mix_ok && vol >= 0) {
Expand All @@ -905,6 +913,15 @@ void set_music_volume(int vol)
}
}

int get_sound_volume()
{
if(mix_ok) {
// Since set_sound_volume sets all main channels to the same, just return the volume of any main channel
return Mix_Volume(source_channel_start, -1);
}
return 0;
}

void set_sound_volume(int vol)
{
if(mix_ok && vol >= 0) {
Expand Down
2 changes: 2 additions & 0 deletions src/sound.hpp
Expand Up @@ -96,6 +96,8 @@ class music_muter : public events::sdl_handler {
// Save music playlist for snapshot
void write_music_play_list(config& snapshot);

int get_music_volume();
int get_sound_volume();
void set_music_volume(int vol);
void set_sound_volume(int vol);
void set_bell_volume(int vol);
Expand Down

0 comments on commit 98e68d6

Please sign in to comment.