From 2c19d51409ca903021e0b508e5bc15299c4e51dc Mon Sep 17 00:00:00 2001 From: Loïc Blot Date: Sun, 22 Jan 2017 11:17:41 +0100 Subject: [CSM] sound_play & sound_stop support + client_lua_api doc (#5096) * squashed: CSM: Implement register_globalstep * Re-use fatal error mechanism from server to disconnect client on CSM error * Little client functions cleanups * squashed: CSM: add core.after function * core.after is shared code between client & server * ModApiUtil get_us_time feature enabled for client --- src/script/lua_api/l_sound.cpp | 62 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/script/lua_api/l_sound.cpp (limited to 'src/script/lua_api/l_sound.cpp') diff --git a/src/script/lua_api/l_sound.cpp b/src/script/lua_api/l_sound.cpp new file mode 100644 index 000000000..774b5be0c --- /dev/null +++ b/src/script/lua_api/l_sound.cpp @@ -0,0 +1,62 @@ +/* +Minetest +Copyright (C) 2013 celeron55, Perttu Ahola +Copyright (C) 2017 nerzhul, Loic Blot + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "l_sound.h" +#include "l_internal.h" +#include "common/c_content.h" +#include "guiEngine.h" + + +int ModApiSound::l_sound_play(lua_State *L) +{ + SimpleSoundSpec spec; + read_soundspec(L, 1, spec); + bool looped = lua_toboolean(L, 2); + + s32 handle; + if (Client *client = getClient(L)) + handle = client->getSoundManager()->playSound(spec, looped); + // Main menu doesn't have access to client, use guiEngine + else + handle = getGuiEngine(L)->playSound(spec, looped); + + lua_pushinteger(L, handle); + + return 1; +} + +int ModApiSound::l_sound_stop(lua_State *L) +{ + u32 handle = luaL_checkinteger(L, 1); + + if (Client *client = getClient(L)) + client->getSoundManager()->stopSound(handle); + // Main menu doesn't have access to client, use guiEngine + else + getGuiEngine(L)->stopSound(handle); + + return 1; +} + +void ModApiSound::Initialize(lua_State *L, int top) +{ + API_FCT(sound_play); + API_FCT(sound_stop); +} -- cgit v1.2.3 From 4ee6be856d435dff010244c910d5dafe2bfbeb1e Mon Sep 17 00:00:00 2001 From: red-001 Date: Thu, 6 Apr 2017 07:14:31 +0100 Subject: [CSM] Add support for positional audio. (#5516) Fixes parts of #5389. --- doc/client_lua_api.md | 15 ++++----------- src/script/clientscripting.cpp | 1 - src/script/lua_api/l_client.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ src/script/lua_api/l_client.h | 4 ++++ src/script/lua_api/l_sound.cpp | 13 ++----------- 5 files changed, 51 insertions(+), 23 deletions(-) (limited to 'src/script/lua_api/l_sound.cpp') diff --git a/doc/client_lua_api.md b/doc/client_lua_api.md index 68156efd3..25055c98a 100644 --- a/doc/client_lua_api.md +++ b/doc/client_lua_api.md @@ -131,7 +131,7 @@ The `:` prefix can also be used for maintaining backwards compatibility. Sounds ------ -**NOTE: Not fully implemented yet.** +**NOTE: max_hear_distance and connecting to objects is not implemented.** Only Ogg Vorbis files are supported. @@ -158,18 +158,12 @@ from the available ones of the following files: Examples of sound parameter tables: - -- Play locationless on all clients + -- Play locationless { gain = 1.0, -- default } - -- Play locationless to one player + -- Play locationless, looped { - to_player = name, - gain = 1.0, -- default - } - -- Play locationless to one player, looped - { - to_player = name, gain = 1.0, -- default loop = true, } @@ -187,8 +181,7 @@ Examples of sound parameter tables: loop = true, } -Looped sounds must either be connected to an object or played locationless to -one player using `to_player = name,` +Looped sounds must either be connected to an object or played locationless. ### SimpleSoundSpec * e.g. `""` diff --git a/src/script/clientscripting.cpp b/src/script/clientscripting.cpp index 17c53985d..6f91b82ec 100644 --- a/src/script/clientscripting.cpp +++ b/src/script/clientscripting.cpp @@ -62,7 +62,6 @@ void ClientScripting::InitializeModApi(lua_State *L, int top) { ModApiUtil::InitializeClient(L, top); ModApiClient::Initialize(L, top); - ModApiSound::Initialize(L, top); ModApiStorage::Initialize(L, top); ModApiEnvMod::InitializeClient(L, top); diff --git a/src/script/lua_api/l_client.cpp b/src/script/lua_api/l_client.cpp index 5a0cd5cc3..2d906985f 100644 --- a/src/script/lua_api/l_client.cpp +++ b/src/script/lua_api/l_client.cpp @@ -194,6 +194,45 @@ int ModApiClient::l_get_meta(lua_State *L) return 1; } +int ModApiClient::l_sound_play(lua_State *L) +{ + ISoundManager *sound = getClient(L)->getSoundManager(); + + SimpleSoundSpec spec; + read_soundspec(L, 1, spec); + float gain = 1.0 ; + bool looped = false; + s32 handle; + + if (lua_istable(L, 2)) { + getfloatfield(L, 2, "gain", gain); + getboolfield(L, 2, "loop", looped); + + lua_getfield(L, 2, "pos"); + if (!lua_isnil(L, -1)) { + v3f pos = read_v3f(L, -1) * BS; + lua_pop(L, 1); + handle = sound->playSoundAt(spec.name, looped, gain * spec.gain, pos); + lua_pushinteger(L, handle); + return 1; + } + } + + handle = sound->playSound(spec.name, looped, gain * spec.gain); + lua_pushinteger(L, handle); + + return 1; +} + +int ModApiClient::l_sound_stop(lua_State *L) +{ + u32 handle = luaL_checkinteger(L, 1); + + getClient(L)->getSoundManager()->stopSound(handle); + + return 0; +} + void ModApiClient::Initialize(lua_State *L, int top) { API_FCT(get_current_modname); @@ -209,4 +248,6 @@ void ModApiClient::Initialize(lua_State *L, int top) API_FCT(get_wielded_item); API_FCT(disconnect); API_FCT(get_meta); + API_FCT(sound_play); + API_FCT(sound_stop); } diff --git a/src/script/lua_api/l_client.h b/src/script/lua_api/l_client.h index 94b154bb4..4e7f63d6a 100644 --- a/src/script/lua_api/l_client.h +++ b/src/script/lua_api/l_client.h @@ -65,6 +65,10 @@ private: // get_meta(pos) static int l_get_meta(lua_State *L); + static int l_sound_play(lua_State *L); + + static int l_sound_stop(lua_State *L); + public: static void Initialize(lua_State *L, int top); }; diff --git a/src/script/lua_api/l_sound.cpp b/src/script/lua_api/l_sound.cpp index 774b5be0c..07ce36daa 100644 --- a/src/script/lua_api/l_sound.cpp +++ b/src/script/lua_api/l_sound.cpp @@ -30,12 +30,7 @@ int ModApiSound::l_sound_play(lua_State *L) read_soundspec(L, 1, spec); bool looped = lua_toboolean(L, 2); - s32 handle; - if (Client *client = getClient(L)) - handle = client->getSoundManager()->playSound(spec, looped); - // Main menu doesn't have access to client, use guiEngine - else - handle = getGuiEngine(L)->playSound(spec, looped); + s32 handle = getGuiEngine(L)->playSound(spec, looped); lua_pushinteger(L, handle); @@ -46,11 +41,7 @@ int ModApiSound::l_sound_stop(lua_State *L) { u32 handle = luaL_checkinteger(L, 1); - if (Client *client = getClient(L)) - client->getSoundManager()->stopSound(handle); - // Main menu doesn't have access to client, use guiEngine - else - getGuiEngine(L)->stopSound(handle); + getGuiEngine(L)->stopSound(handle); return 1; } -- cgit v1.2.3