1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Sound refactor and improvements (#12764)

This commit is contained in:
DS 2023-06-16 20:15:21 +02:00 committed by GitHub
parent 8e1af25738
commit edcbfa31c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
52 changed files with 2802 additions and 1211 deletions

View file

@ -28,10 +28,11 @@ set(common_SCRIPT_LUA_API_SRCS
set(client_SCRIPT_LUA_API_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/l_camera.cpp
${CMAKE_CURRENT_SOURCE_DIR}/l_client.cpp
${CMAKE_CURRENT_SOURCE_DIR}/l_client_sound.cpp
${CMAKE_CURRENT_SOURCE_DIR}/l_localplayer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/l_mainmenu.cpp
${CMAKE_CURRENT_SOURCE_DIR}/l_mainmenu_sound.cpp
${CMAKE_CURRENT_SOURCE_DIR}/l_minimap.cpp
${CMAKE_CURRENT_SOURCE_DIR}/l_particles_local.cpp
${CMAKE_CURRENT_SOURCE_DIR}/l_sound.cpp
${CMAKE_CURRENT_SOURCE_DIR}/l_storage.cpp
PARENT_SCOPE)

View file

@ -260,63 +260,6 @@ int ModApiClient::l_get_meta(lua_State *L)
return 1;
}
// sound_play(spec, parameters)
int ModApiClient::l_sound_play(lua_State *L)
{
ISoundManager *sound = getClient(L)->getSoundManager();
SimpleSoundSpec spec;
read_soundspec(L, 1, spec);
SoundLocation type = SoundLocation::Local;
float gain = 1.0f;
v3f position;
if (lua_istable(L, 2)) {
getfloatfield(L, 2, "gain", gain);
getfloatfield(L, 2, "pitch", spec.pitch);
getboolfield(L, 2, "loop", spec.loop);
lua_getfield(L, 2, "pos");
if (!lua_isnil(L, -1)) {
position = read_v3f(L, -1) * BS;
type = SoundLocation::Position;
lua_pop(L, 1);
}
}
spec.gain *= gain;
s32 handle;
if (type == SoundLocation::Local)
handle = sound->playSound(spec);
else
handle = sound->playSoundAt(spec, position);
lua_pushinteger(L, handle);
return 1;
}
// sound_stop(handle)
int ModApiClient::l_sound_stop(lua_State *L)
{
s32 handle = luaL_checkinteger(L, 1);
getClient(L)->getSoundManager()->stopSound(handle);
return 0;
}
// sound_fade(handle, step, gain)
int ModApiClient::l_sound_fade(lua_State *L)
{
s32 handle = luaL_checkinteger(L, 1);
float step = readParam<float>(L, 2);
float gain = readParam<float>(L, 3);
getClient(L)->getSoundManager()->fadeSound(handle, step, gain);
return 0;
}
// get_server_info()
int ModApiClient::l_get_server_info(lua_State *L)
{
@ -433,9 +376,6 @@ void ModApiClient::Initialize(lua_State *L, int top)
API_FCT(get_node_or_nil);
API_FCT(disconnect);
API_FCT(get_meta);
API_FCT(sound_play);
API_FCT(sound_stop);
API_FCT(sound_fade);
API_FCT(get_server_info);
API_FCT(get_item_def);
API_FCT(get_node_def);

View file

@ -78,15 +78,6 @@ private:
// get_meta(pos)
static int l_get_meta(lua_State *L);
// sound_play(spec, parameters)
static int l_sound_play(lua_State *L);
// sound_stop(handle)
static int l_sound_stop(lua_State *L);
// sound_fade(handle, step, gain)
static int l_sound_fade(lua_State *L);
// get_server_info()
static int l_get_server_info(lua_State *L);

View file

@ -0,0 +1,150 @@
/*
Minetest
Copyright (C) 2023 DS
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
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_client_sound.h"
#include "l_internal.h"
#include "common/c_content.h"
#include "common/c_converter.h"
#include "client/client.h"
#include "client/sound.h"
/* ModApiClientSound */
// sound_play(spec, parameters)
int ModApiClientSound::l_sound_play(lua_State *L)
{
ISoundManager *sound_manager = getClient(L)->getSoundManager();
SoundSpec spec;
read_simplesoundspec(L, 1, spec);
SoundLocation type = SoundLocation::Local;
float gain = 1.0f;
v3f position;
if (lua_istable(L, 2)) {
getfloatfield(L, 2, "gain", gain);
getfloatfield(L, 2, "pitch", spec.pitch);
getboolfield(L, 2, "loop", spec.loop);
lua_getfield(L, 2, "pos");
if (!lua_isnil(L, -1)) {
position = read_v3f(L, -1);
type = SoundLocation::Position;
lua_pop(L, 1);
}
}
spec.gain *= gain;
sound_handle_t handle = sound_manager->allocateId(2);
if (type == SoundLocation::Local)
sound_manager->playSound(handle, spec);
else
sound_manager->playSoundAt(handle, spec, position, v3f(0.0f));
ClientSoundHandle::create(L, handle);
return 1;
}
void ModApiClientSound::Initialize(lua_State *L, int top)
{
API_FCT(sound_play);
}
/* ClientSoundHandle */
ClientSoundHandle *ClientSoundHandle::checkobject(lua_State *L, int narg)
{
luaL_checktype(L, narg, LUA_TUSERDATA);
void *ud = luaL_checkudata(L, narg, className);
if (!ud)
luaL_typerror(L, narg, className);
return *(ClientSoundHandle**)ud; // unbox pointer
}
int ClientSoundHandle::gc_object(lua_State *L)
{
ClientSoundHandle *o = *(ClientSoundHandle **)(lua_touserdata(L, 1));
if (getClient(L) && getClient(L)->getSoundManager())
getClient(L)->getSoundManager()->freeId(o->m_handle);
delete o;
return 0;
}
// :stop()
int ClientSoundHandle::l_stop(lua_State *L)
{
ClientSoundHandle *o = checkobject(L, 1);
getClient(L)->getSoundManager()->stopSound(o->m_handle);
return 0;
}
// :fade(step, gain)
int ClientSoundHandle::l_fade(lua_State *L)
{
ClientSoundHandle *o = checkobject(L, 1);
float step = readParam<float>(L, 2);
float gain = readParam<float>(L, 3);
getClient(L)->getSoundManager()->fadeSound(o->m_handle, step, gain);
return 0;
}
void ClientSoundHandle::create(lua_State *L, sound_handle_t handle)
{
ClientSoundHandle *o = new ClientSoundHandle(handle);
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
luaL_getmetatable(L, className);
lua_setmetatable(L, -2);
}
void ClientSoundHandle::Register(lua_State *L)
{
lua_newtable(L);
int methodtable = lua_gettop(L);
luaL_newmetatable(L, className);
int metatable = lua_gettop(L);
lua_pushliteral(L, "__metatable");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable); // hide metatable from Lua getmetatable()
lua_pushliteral(L, "__index");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);
lua_pushliteral(L, "__gc");
lua_pushcfunction(L, gc_object);
lua_settable(L, metatable);
lua_pop(L, 1); // drop metatable
luaL_register(L, nullptr, methods); // fill methodtable
lua_pop(L, 1); // drop methodtable
}
const char ClientSoundHandle::className[] = "ClientSoundHandle";
const luaL_Reg ClientSoundHandle::methods[] = {
luamethod(ClientSoundHandle, stop),
luamethod(ClientSoundHandle, fade),
{0,0}
};

View file

@ -0,0 +1,66 @@
/*
Minetest
Copyright (C) 2023 DS
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
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.
*/
#pragma once
#include "lua_api/l_base.h"
#include "util/basic_macros.h"
using sound_handle_t = int;
class ModApiClientSound : public ModApiBase
{
private:
// sound_play(spec, parameters)
static int l_sound_play(lua_State *L);
public:
static void Initialize(lua_State *L, int top);
};
class ClientSoundHandle final : public ModApiBase
{
private:
sound_handle_t m_handle;
static const char className[];
static const luaL_Reg methods[];
ClientSoundHandle(sound_handle_t handle) : m_handle(handle) {}
DISABLE_CLASS_COPY(ClientSoundHandle)
static ClientSoundHandle *checkobject(lua_State *L, int narg);
static int gc_object(lua_State *L);
// :stop()
static int l_stop(lua_State *L);
// :fade(step, gain)
static int l_fade(lua_State *L);
public:
~ClientSoundHandle() = default;
static void create(lua_State *L, sound_handle_t handle);
static void Register(lua_State *L);
};

View file

@ -0,0 +1,116 @@
/*
Minetest
Copyright (C) 2023 DS
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
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_mainmenu_sound.h"
#include "l_internal.h"
#include "common/c_content.h"
#include "gui/guiEngine.h"
/* ModApiMainMenuSound */
// sound_play(spec, loop)
int ModApiMainMenuSound::l_sound_play(lua_State *L)
{
SoundSpec spec;
read_simplesoundspec(L, 1, spec);
spec.loop = readParam<bool>(L, 2);
ISoundManager &sound_manager = *getGuiEngine(L)->m_sound_manager;
sound_handle_t handle = sound_manager.allocateId(2);
sound_manager.playSound(handle, spec);
MainMenuSoundHandle::create(L, handle);
return 1;
}
void ModApiMainMenuSound::Initialize(lua_State *L, int top)
{
API_FCT(sound_play);
}
/* MainMenuSoundHandle */
MainMenuSoundHandle *MainMenuSoundHandle::checkobject(lua_State *L, int narg)
{
luaL_checktype(L, narg, LUA_TUSERDATA);
void *ud = luaL_checkudata(L, narg, className);
if (!ud)
luaL_typerror(L, narg, className);
return *(MainMenuSoundHandle**)ud; // unbox pointer
}
int MainMenuSoundHandle::gc_object(lua_State *L)
{
MainMenuSoundHandle *o = *(MainMenuSoundHandle **)(lua_touserdata(L, 1));
if (getGuiEngine(L) && getGuiEngine(L)->m_sound_manager)
getGuiEngine(L)->m_sound_manager->freeId(o->m_handle);
delete o;
return 0;
}
// :stop()
int MainMenuSoundHandle::l_stop(lua_State *L)
{
MainMenuSoundHandle *o = checkobject(L, 1);
getGuiEngine(L)->m_sound_manager->stopSound(o->m_handle);
return 0;
}
void MainMenuSoundHandle::create(lua_State *L, sound_handle_t handle)
{
MainMenuSoundHandle *o = new MainMenuSoundHandle(handle);
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
luaL_getmetatable(L, className);
lua_setmetatable(L, -2);
}
void MainMenuSoundHandle::Register(lua_State *L)
{
lua_newtable(L);
int methodtable = lua_gettop(L);
luaL_newmetatable(L, className);
int metatable = lua_gettop(L);
lua_pushliteral(L, "__metatable");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable); // hide metatable from Lua getmetatable()
lua_pushliteral(L, "__index");
lua_pushvalue(L, methodtable);
lua_settable(L, metatable);
lua_pushliteral(L, "__gc");
lua_pushcfunction(L, gc_object);
lua_settable(L, metatable);
lua_pop(L, 1); // drop metatable
luaL_register(L, nullptr, methods); // fill methodtable
lua_pop(L, 1); // drop methodtable
}
const char MainMenuSoundHandle::className[] = "MainMenuSoundHandle";
const luaL_Reg MainMenuSoundHandle::methods[] = {
luamethod(MainMenuSoundHandle, stop),
{0,0}
};

View file

@ -1,5 +1,6 @@
/*
Minetest
Copyright (C) 2023 DS
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
@ -21,13 +22,42 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
#include "lua_api/l_base.h"
#include "util/basic_macros.h"
class ModApiSound : public ModApiBase
using sound_handle_t = int;
class ModApiMainMenuSound : public ModApiBase
{
private:
// sound_play(spec, loop)
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);
};
class MainMenuSoundHandle final : public ModApiBase
{
private:
sound_handle_t m_handle;
static const char className[];
static const luaL_Reg methods[];
MainMenuSoundHandle(sound_handle_t handle) : m_handle(handle) {}
DISABLE_CLASS_COPY(MainMenuSoundHandle)
static MainMenuSoundHandle *checkobject(lua_State *L, int narg);
static int gc_object(lua_State *L);
// :stop()
static int l_stop(lua_State *L);
public:
~MainMenuSoundHandle() = default;
static void create(lua_State *L, sound_handle_t handle);
static void Register(lua_State *L);
};

View file

@ -503,7 +503,7 @@ int ModApiServer::l_sound_play(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ServerPlayingSound params;
read_soundspec(L, 1, params.spec);
read_simplesoundspec(L, 1, params.spec);
read_server_sound_params(L, 2, params);
bool ephemeral = lua_gettop(L) > 2 && readParam<bool>(L, 3);
if (ephemeral) {

View file

@ -1,53 +0,0 @@
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
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 "gui/guiEngine.h"
int ModApiSound::l_sound_play(lua_State *L)
{
SimpleSoundSpec spec;
read_soundspec(L, 1, spec);
spec.loop = readParam<bool>(L, 2);
s32 handle = getGuiEngine(L)->playSound(spec);
lua_pushinteger(L, handle);
return 1;
}
int ModApiSound::l_sound_stop(lua_State *L)
{
u32 handle = luaL_checkinteger(L, 1);
getGuiEngine(L)->stopSound(handle);
return 1;
}
void ModApiSound::Initialize(lua_State *L, int top)
{
API_FCT(sound_play);
API_FCT(sound_stop);
}