Skip to content
Permalink
Browse files

Sound API: Add fading sounds

  • Loading branch information
Bremaweb authored and paramat committed May 3, 2017
1 parent f1d7a26 commit bd921a7916f0fafc493b1c4d0eeb5e2bb1d6a7c2
@@ -456,11 +456,13 @@ Examples of sound parameter tables:
-- Play locationless on all clients
{
gain = 1.0, -- default
fade = 0.0, -- default, change to a value > 0 to fade the sound in
}
-- Play locationless to one player
{
to_player = name,
gain = 1.0, -- default
fade = 0.0, -- default, change to a value > 0 to fade the sound in
}
-- Play locationless to one player, looped
{
@@ -2587,6 +2589,11 @@ These functions return the leftover itemstack.
* `spec` is a `SimpleSoundSpec`
* `parameters` is a sound parameter table
* `minetest.sound_stop(handle)`
* `minetest.sound_fade(handle, step, gain)`
* `handle` is a handle returned by minetest.sound_play
* `step` determines how fast a sound will fade.
Negative step will lower the sound volume, positive step will increase the sound volume
* `gain` the target gain for the fade.

### Timing
* `minetest.after(time, func, ...)`
@@ -407,6 +407,7 @@ void Client::step(float dtime)

// Step environment
m_env.step(dtime);
m_sound->step(dtime);

/*
Get events
@@ -328,6 +328,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
void handleCommand_ItemDef(NetworkPacket* pkt);
void handleCommand_PlaySound(NetworkPacket* pkt);
void handleCommand_StopSound(NetworkPacket* pkt);
void handleCommand_FadeSound(NetworkPacket *pkt);
void handleCommand_Privileges(NetworkPacket* pkt);
void handleCommand_InventoryFormSpec(NetworkPacket* pkt);
void handleCommand_DetachedInventory(NetworkPacket* pkt);
@@ -109,7 +109,7 @@ const ToClientCommandHandler toClientCommandTable[TOCLIENT_NUM_MSG_TYPES] =
{ "TOCLIENT_EYE_OFFSET", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_EyeOffset }, // 0x52
{ "TOCLIENT_DELETE_PARTICLESPAWNER", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_DeleteParticleSpawner }, // 0x53
{ "TOCLIENT_CLOUD_PARAMS", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_CloudParams }, // 0x54
null_command_handler,
{ "TOCLIENT_FADE_SOUND", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_FadeSound }, // 0x55
null_command_handler,
null_command_handler,
null_command_handler,
@@ -755,21 +755,39 @@ void Client::handleCommand_ItemDef(NetworkPacket* pkt)

void Client::handleCommand_PlaySound(NetworkPacket* pkt)
{
/*
[0] u32 server_id
[4] u16 name length
[6] char name[len]
[ 6 + len] f32 gain
[10 + len] u8 type
[11 + len] (f32 * 3) pos
[23 + len] u16 object_id
[25 + len] bool loop
[26 + len] f32 fade
*/

s32 server_id;
std::string name;

float gain;
u8 type; // 0=local, 1=positional, 2=object
v3f pos;
u16 object_id;
bool loop;
float fade = 0;

*pkt >> server_id >> name >> gain >> type >> pos >> object_id >> loop;

try {
*pkt >> fade;
} catch (SerializationError &e) {};

// Start playing
int client_id = -1;
switch(type) {
case 0: // local
client_id = m_sound->playSound(name, loop, gain);
client_id = m_sound->playSound(name, loop, gain, fade);
break;
case 1: // positional
client_id = m_sound->playSoundAt(name, loop, gain, pos);
@@ -808,6 +826,21 @@ void Client::handleCommand_StopSound(NetworkPacket* pkt)
}
}

void Client::handleCommand_FadeSound(NetworkPacket *pkt)
{
s32 sound_id;
float step;
float gain;

*pkt >> sound_id >> step >> gain;

UNORDERED_MAP<s32, int>::iterator i =
m_sounds_server_to_client.find(sound_id);

if (i != m_sounds_server_to_client.end())
m_sound->fadeSound(i->second, step, gain);
}

void Client::handleCommand_Privileges(NetworkPacket* pkt)
{
m_privileges.clear();
@@ -153,9 +153,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
PROTOCOL VERSION 31:
Add tile overlay
Stop sending TOSERVER_CLIENT_READY
PROTOCOL VERSION 32:
Add fading sounds
*/

#define LATEST_PROTOCOL_VERSION 31
#define LATEST_PROTOCOL_VERSION 32

// Server's supported network protocol range
#define SERVER_PROTOCOL_VERSION_MIN 24
@@ -620,6 +622,13 @@ enum ToClientCommand
v2f1000 speed
*/

TOCLIENT_FADE_SOUND = 0x55,
/*
s32 sound_id
float step
float gain
*/

TOCLIENT_SRP_BYTES_S_B = 0x60,
/*
Belonging to AUTH_MECHANISM_LEGACY_PASSWORD and AUTH_MECHANISM_SRP.
@@ -680,6 +680,7 @@ void read_server_sound_params(lua_State *L, int index,
if(lua_istable(L, index)){
getfloatfield(L, index, "gain", params.gain);
getstringfield(L, index, "to_player", params.to_player);
getfloatfield(L, index, "fade", params.fade);
lua_getfield(L, index, "pos");
if(!lua_isnil(L, -1)){
v3f p = read_v3f(L, -1)*BS;
@@ -712,6 +713,7 @@ void read_soundspec(lua_State *L, int index, SimpleSoundSpec &spec)
} else if(lua_istable(L, index)){
getstringfield(L, index, "name", spec.name);
getfloatfield(L, index, "gain", spec.gain);
getfloatfield(L, index, "fade", spec.fade);
} else if(lua_isstring(L, index)){
spec.name = lua_tostring(L, index);
}
@@ -77,6 +77,8 @@ void setfloatfield(lua_State *L, int table,
const char *fieldname, float value);
void setboolfield(lua_State *L, int table,
const char *fieldname, bool value);
void setstringfield(lua_State *L, int table,
const char *fieldname, const char *value);

v3f checkFloatPos (lua_State *L, int index);
v2f check_v2f (lua_State *L, int index);
@@ -455,6 +455,16 @@ int ModApiServer::l_sound_stop(lua_State *L)
return 0;
}

int ModApiServer::l_sound_fade(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
s32 handle = luaL_checkinteger(L, 1);
float step = luaL_checknumber(L, 2);
float gain = luaL_checknumber(L, 3);
getServer(L)->fadeSound(handle, step, gain);
return 0;
}

// is_singleplayer()
int ModApiServer::l_is_singleplayer(lua_State *L)
{
@@ -518,6 +528,7 @@ void ModApiServer::Initialize(lua_State *L, int top)
API_FCT(show_formspec);
API_FCT(sound_play);
API_FCT(sound_stop);
API_FCT(sound_fade);

API_FCT(get_player_information);
API_FCT(get_player_privs);
@@ -68,6 +68,9 @@ class ModApiServer : public ModApiBase
// 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_player_privs(name, text)
static int l_get_player_privs(lua_State *L);

@@ -2100,15 +2100,23 @@ s32 Server::playSound(const SimpleSoundSpec &spec,
m_playing_sounds[id] = ServerPlayingSound();
ServerPlayingSound &psound = m_playing_sounds[id];
psound.params = params;
psound.spec = spec;

float gain = params.gain * spec.gain;
NetworkPacket pkt(TOCLIENT_PLAY_SOUND, 0);
pkt << id << spec.name << (float) (spec.gain * params.gain)
<< (u8) params.type << pos << params.object << params.loop;
pkt << id << spec.name << gain
<< (u8) params.type << pos << params.object
<< params.loop << params.fade;

for(std::vector<u16>::iterator i = dst_clients.begin();
// Backwards compability
bool play_sound = gain > 0;

for (std::vector<u16>::iterator i = dst_clients.begin();
i != dst_clients.end(); ++i) {
psound.clients.insert(*i);
m_clients.send(*i, 0, &pkt, true);
if (play_sound || m_clients.getProtocolVersion(*i) >= 32) {
psound.clients.insert(*i);
m_clients.send(*i, 0, &pkt, true);
}
}
return id;
}
@@ -2132,6 +2140,52 @@ void Server::stopSound(s32 handle)
m_playing_sounds.erase(i);
}

void Server::fadeSound(s32 handle, float step, float gain)
{
// Get sound reference
UNORDERED_MAP<s32, ServerPlayingSound>::iterator i =
m_playing_sounds.find(handle);
if (i == m_playing_sounds.end())
return;

ServerPlayingSound &psound = i->second;
psound.params.gain = gain;

NetworkPacket pkt(TOCLIENT_FADE_SOUND, 4);
pkt << handle << step << gain;

// Backwards compability
bool play_sound = gain > 0;
ServerPlayingSound compat_psound = psound;
compat_psound.clients.clear();

NetworkPacket compat_pkt(TOCLIENT_STOP_SOUND, 4);
compat_pkt << handle;

for (UNORDERED_SET<u16>::iterator it = psound.clients.begin();
it != psound.clients.end();) {
if (m_clients.getProtocolVersion(*it) >= 32) {
// Send as reliable
m_clients.send(*it, 0, &pkt, true);
++it;
} else {
compat_psound.clients.insert(*it);
// Stop old sound
m_clients.send(*it, 0, &compat_pkt, true);
psound.clients.erase(it++);
}
}

// Remove sound reference
if (!play_sound || psound.clients.size() == 0)
m_playing_sounds.erase(i);

if (play_sound && compat_psound.clients.size() > 0) {
// Play new sound volume on older clients
playSound(compat_psound.spec, compat_psound.params);
}
}

void Server::sendRemoveNode(v3s16 p, u16 ignore_id,
std::vector<u16> *far_players, float far_d_nodes)
{
@@ -115,6 +115,7 @@ struct ServerSoundParams
u16 object;
float max_hear_distance;
bool loop;
float fade;

ServerSoundParams():
gain(1.0),
@@ -123,7 +124,8 @@ struct ServerSoundParams
pos(0,0,0),
object(0),
max_hear_distance(32*BS),
loop(false)
loop(false),
fade(0)
{}

v3f getPos(ServerEnvironment *env, bool *pos_exists) const;
@@ -132,6 +134,7 @@ struct ServerSoundParams
struct ServerPlayingSound
{
ServerSoundParams params;
SimpleSoundSpec spec;
UNORDERED_SET<u16> clients; // peer ids
};

@@ -231,6 +234,7 @@ class Server : public con::PeerHandler, public MapEventReceiver,
// Envlock
s32 playSound(const SimpleSoundSpec &spec, const ServerSoundParams &params);
void stopSound(s32 handle);
void fadeSound(s32 handle, float step, float gain);

// Envlock
std::set<std::string> getPlayerEffectivePrivs(const std::string &name);
@@ -34,22 +34,22 @@ class OnDemandSoundFetcher

struct SimpleSoundSpec
{
SimpleSoundSpec(const std::string &name = "", float gain = 1.0)
: name(name), gain(gain)
SimpleSoundSpec(const std::string &name = "", float gain = 1.0, float fade = 0.0)
: name(name), gain(gain), fade(fade)
{
}

bool exists() const { return name != ""; }

std::string name;
float gain;
float fade;
};

class ISoundManager
{
public:
virtual ~ISoundManager() {}

// Multiple sounds can be loaded per name; when played, the sound
// should be chosen randomly from alternatives
// Return value determines success/failure
@@ -63,16 +63,21 @@ class ISoundManager

// playSound functions return -1 on failure, otherwise a handle to the
// sound. If name=="", call should be ignored without error.
virtual int playSound(const std::string &name, bool loop, float volume) = 0;
virtual int playSoundAt(
const std::string &name, bool loop, float volume, v3f pos) = 0;
virtual int playSound(const std::string &name, bool loop, float volume,
float fade = 0) = 0;
virtual int playSoundAt(const std::string &name, bool loop, float volume,
v3f pos) = 0;
virtual void stopSound(int sound) = 0;
virtual bool soundExists(int sound) = 0;
virtual void updateSoundPosition(int sound, v3f pos) = 0;
virtual bool updateSoundGain(int id, float gain) = 0;
virtual float getSoundGain(int id) = 0;
virtual void step(float dtime) = 0;
virtual void fadeSound(int sound, float step, float gain) = 0;

int playSound(const SimpleSoundSpec &spec, bool loop)
{
return playSound(spec.name, loop, spec.gain);
return playSound(spec.name, loop, spec.gain, spec.fade);
}
int playSoundAt(const SimpleSoundSpec &spec, bool loop, v3f pos)
{
@@ -93,14 +98,21 @@ class DummySoundManager : public ISoundManager
}
void updateListener(v3f pos, v3f vel, v3f at, v3f up) {}
void setListenerGain(float gain) {}
int playSound(const std::string &name, bool loop, float volume) { return 0; }
int playSound(const std::string &name, bool loop, float volume, float fade)
{
return 0;
}
int playSoundAt(const std::string &name, bool loop, float volume, v3f pos)
{
return 0;
}
void stopSound(int sound) {}
bool soundExists(int sound) { return false; }
void updateSoundPosition(int sound, v3f pos) {}
bool updateSoundGain(int id, float gain) { return false; }
float getSoundGain(int id) { return 0; }
void step(float dtime) { }
void fadeSound(int sound, float step, float gain) { }
};

// Global DummySoundManager singleton

0 comments on commit bd921a7

Please sign in to comment.