From 1745e5d4adf5b17b2a605413a760109e48fdb06c Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Thu, 11 Aug 2022 13:37:00 +0200 Subject: [PATCH] feat(volume): Add conversion functions --- src/lua_libpulse_glib/volume.c | 30 ++++++++++++++++++ src/lua_libpulse_glib/volume.h | 58 +++++++++++++++++++++++++++++++--- 2 files changed, 84 insertions(+), 4 deletions(-) diff --git a/src/lua_libpulse_glib/volume.c b/src/lua_libpulse_glib/volume.c index 5183aeb..d4aecab 100644 --- a/src/lua_libpulse_glib/volume.c +++ b/src/lua_libpulse_glib/volume.c @@ -1,5 +1,7 @@ #include "volume.h" +#include "lua_util.h" + #include #include #include @@ -311,3 +313,31 @@ int volume_divide(lua_State* L) { } } } + + +int volume_from_dB(lua_State* L) { + double value = (double) luaL_checknumber(L, 1); + lua_pushinteger(L, pa_sw_volume_from_dB(value)); + return 1; +} + + +int volume_to_dB(lua_State* L) { + pa_volume_t value = luaL_checkinteger(L, 1); + lua_pushnumber(L, pa_sw_volume_to_dB(value)); + return 1; +} + + +int volume_from_linear(lua_State* L) { + double value = (double) luaL_checknumber(L, 1); + lua_pushinteger(L, pa_sw_volume_from_linear(value)); + return 1; +} + + +int volume_to_linear(lua_State* L) { + pa_volume_t value = luaL_checkinteger(L, 1); + lua_pushnumber(L, pa_sw_volume_to_linear(value)); + return 1; +} diff --git a/src/lua_libpulse_glib/volume.h b/src/lua_libpulse_glib/volume.h index c07ad47..95c2870 100644 --- a/src/lua_libpulse_glib/volume.h +++ b/src/lua_libpulse_glib/volume.h @@ -55,7 +55,6 @@ int volume__newindex(lua_State*); */ int volume_new(lua_State*); - /** Checks whether a value is a valid @{Volume}. * * @function is_valid @@ -64,6 +63,53 @@ int volume_new(lua_State*); */ int volume_is_valid(lua_State*); +/** Converts a decibel value to an integer volume value. + * + * This is only valid for software volumes. It does not operate + * on instances of @{Volume}. + * + * @function from_dB + * @tparam number value + * @treturn number + */ +int volume_from_dB(lua_State*); + +/** Converts an integer volume value to a decibel value. + * + * This is only valid for software volumes. It does not operate + * on instances of @{Volume}. + * + * @function to_dB + * @tparam number value + * @treturn number + */ +int volume_to_dB(lua_State*); + +/** Converts a linear factor to an integer volume value. + * + * `0.0` and less is muted, `1.0` is normal volume. + * + * This is only valid for software volumes. It does not operate + * on instances of @{Volume}. + * + * @function from_linear + * @tparam number value + * @treturn number + */ +int volume_from_linear(lua_State*); + +/** Converts an integer volume value to linear factor. + * + * This is only valid for software volumes. It does not operate + * on instances of @{Volume}. + * + * @function to_linear + * @tparam number value + * @treturn number + */ +int volume_to_linear(lua_State*); + + /// Volume /// @type Volume @@ -236,9 +282,13 @@ static const struct luaL_Reg volume_mt[] = { static const struct luaL_Reg volume_lib[] = { - {"new", volume_new }, - { "is_valid", volume_is_valid}, - { NULL, NULL } + {"new", volume_new }, + { "from_dB", volume_from_dB }, + { "to_dB", volume_to_dB }, + { "from_linear", volume_from_linear}, + { "to_linear", volume_to_linear }, + { "is_valid", volume_is_valid }, + { NULL, NULL } };