Skip to content

Commit

Permalink
Sound: Add pitch option (#5960)
Browse files Browse the repository at this point in the history
* Sound: Add pitch option
  • Loading branch information
Rui authored and nerzhul committed Jun 11, 2017
1 parent 03ff53e commit ff73c7a
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 54 deletions.
3 changes: 3 additions & 0 deletions doc/lua_api.txt
Expand Up @@ -622,12 +622,14 @@ Examples of sound parameter tables:
{
gain = 1.0, -- default
fade = 0.0, -- default, change to a value > 0 to fade the sound in
pitch = 1.0, -- default
}
-- 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
pitch = 1.0, -- default
}
-- Play locationless to one player, looped
{
Expand Down Expand Up @@ -658,6 +660,7 @@ one player using `to_player = name,`
* e.g. `{}`
* e.g. `{name = "default_place_node"}`
* e.g. `{name = "default_place_node", gain = 1.0}`
* e.g. `{name = "default_place_node", gain = 1.0, pitch = 1.0}`

Registered definitions of stuff
-------------------------------
Expand Down
4 changes: 4 additions & 0 deletions src/itemdef.cpp
Expand Up @@ -156,6 +156,8 @@ void ItemDefinition::serialize(std::ostream &os, u16 protocol_version) const
writeF1000(os, sound_place_failed.gain);
os << serializeString(palette_image);
writeU32(os, color.color);
writeF1000(os, sound_place.pitch);
writeF1000(os, sound_place_failed.pitch);
}

void ItemDefinition::deSerialize(std::istream &is)
Expand Down Expand Up @@ -214,6 +216,8 @@ void ItemDefinition::deSerialize(std::istream &is)
sound_place_failed.gain = readF1000(is);
palette_image = deSerializeString(is);
color.set(readU32(is));
sound_place.pitch = readF1000(is);
sound_place_failed.pitch = readF1000(is);
} catch(SerializationError &e) {};
}

Expand Down
11 changes: 7 additions & 4 deletions src/network/clientpackethandler.cpp
Expand Up @@ -764,6 +764,7 @@ void Client::handleCommand_PlaySound(NetworkPacket* pkt)
[23 + len] u16 object_id
[25 + len] bool loop
[26 + len] f32 fade
[30 + len] f32 pitch
*/

s32 server_id;
Expand All @@ -774,29 +775,31 @@ void Client::handleCommand_PlaySound(NetworkPacket* pkt)
v3f pos;
u16 object_id;
bool loop;
float fade = 0;
float fade = 0.0f;
float pitch = 1.0f;

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

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

// Start playing
int client_id = -1;
switch(type) {
case 0: // local
client_id = m_sound->playSound(name, loop, gain, fade);
client_id = m_sound->playSound(name, loop, gain, fade, pitch);
break;
case 1: // positional
client_id = m_sound->playSoundAt(name, loop, gain, pos);
client_id = m_sound->playSoundAt(name, loop, gain, pos, pitch);
break;
case 2:
{ // object
ClientActiveObject *cao = m_env.getActiveObject(object_id);
if (cao)
pos = cao->getPosition();
client_id = m_sound->playSoundAt(name, loop, gain, pos);
client_id = m_sound->playSoundAt(name, loop, gain, pos, pitch);
// TODO: Set up sound to move with object
break;
}
Expand Down
2 changes: 2 additions & 0 deletions src/nodedef.cpp
Expand Up @@ -252,11 +252,13 @@ static void serializeSimpleSoundSpec(const SimpleSoundSpec &ss,
{
os<<serializeString(ss.name);
writeF1000(os, ss.gain);
writeF1000(os, ss.pitch);
}
static void deSerializeSimpleSoundSpec(SimpleSoundSpec &ss, std::istream &is)
{
ss.name = deSerializeString(is);
ss.gain = readF1000(is);
ss.pitch = readF1000(is);
}

void TextureSettings::readSettings()
Expand Down
4 changes: 4 additions & 0 deletions src/script/common/c_content.cpp
Expand Up @@ -925,6 +925,7 @@ void read_server_sound_params(lua_State *L, int index,
getfloatfield(L, index, "gain", params.gain);
getstringfield(L, index, "to_player", params.to_player);
getfloatfield(L, index, "fade", params.fade);
getfloatfield(L, index, "pitch", params.pitch);
lua_getfield(L, index, "pos");
if(!lua_isnil(L, -1)){
v3f p = read_v3f(L, -1)*BS;
Expand Down Expand Up @@ -958,6 +959,7 @@ void read_soundspec(lua_State *L, int index, SimpleSoundSpec &spec)
getstringfield(L, index, "name", spec.name);
getfloatfield(L, index, "gain", spec.gain);
getfloatfield(L, index, "fade", spec.fade);
getfloatfield(L, index, "pitch", spec.pitch);
} else if(lua_isstring(L, index)){
spec.name = lua_tostring(L, index);
}
Expand All @@ -972,6 +974,8 @@ void push_soundspec(lua_State *L, const SimpleSoundSpec &spec)
lua_setfield(L, -2, "gain");
lua_pushnumber(L, spec.fade);
lua_setfield(L, -2, "fade");
lua_pushnumber(L, spec.pitch);
lua_setfield(L, -2, "pitch");
}

/******************************************************************************/
Expand Down
8 changes: 5 additions & 3 deletions src/script/lua_api/l_client.cpp
Expand Up @@ -227,26 +227,28 @@ int ModApiClient::l_sound_play(lua_State *L)

SimpleSoundSpec spec;
read_soundspec(L, 1, spec);
float gain = 1.0;
float gain = 1.0f;
float pitch = 1.0f;
bool looped = false;
s32 handle;

if (lua_istable(L, 2)) {
getfloatfield(L, 2, "gain", gain);
getfloatfield(L, 2, "pitch", pitch);
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);
spec.name, looped, gain * spec.gain, pos, pitch);
lua_pushinteger(L, handle);
return 1;
}
}

handle = sound->playSound(spec.name, looped, gain * spec.gain);
handle = sound->playSound(spec.name, looped, gain * spec.gain, 0.0f, pitch);
lua_pushinteger(L, handle);

return 1;
Expand Down
2 changes: 1 addition & 1 deletion src/server.cpp
Expand Up @@ -2098,7 +2098,7 @@ s32 Server::playSound(const SimpleSoundSpec &spec,
NetworkPacket pkt(TOCLIENT_PLAY_SOUND, 0);
pkt << id << spec.name << gain
<< (u8) params.type << pos << params.object
<< params.loop << params.fade;
<< params.loop << params.fade << params.pitch;

// Backwards compability
bool play_sound = gain > 0;
Expand Down
36 changes: 13 additions & 23 deletions src/server.h
Expand Up @@ -104,29 +104,19 @@ struct MediaInfo

struct ServerSoundParams
{
float gain;
std::string to_player;
enum Type{
SSP_LOCAL=0,
SSP_POSITIONAL=1,
SSP_OBJECT=2
} type;
v3f pos;
u16 object;
float max_hear_distance;
bool loop;
float fade;

ServerSoundParams():
gain(1.0),
to_player(""),
type(SSP_LOCAL),
pos(0,0,0),
object(0),
max_hear_distance(32*BS),
loop(false),
fade(0)
{}
enum Type {
SSP_LOCAL,
SSP_POSITIONAL,
SSP_OBJECT
} type = SSP_LOCAL;
float gain = 1.0f;
float fade = 0.0f;
float pitch = 1.0f;
bool loop = false;
float max_hear_distance = 32*BS;
v3f pos = v3f(0, 0, 0);
u16 object = 0;
std::string to_player = "";

v3f getPos(ServerEnvironment *env, bool *pos_exists) const;
};
Expand Down
28 changes: 16 additions & 12 deletions src/sound.h
Expand Up @@ -34,16 +34,18 @@ class OnDemandSoundFetcher

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

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

std::string name;
float gain;
float fade;
std::string name = "";
float gain = 1.0f;
float fade = 0.0f;
float pitch = 1.0f;
};

class ISoundManager
Expand All @@ -64,9 +66,9 @@ 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,
float fade = 0) = 0;
virtual int playSoundAt(
const std::string &name, bool loop, float volume, v3f pos) = 0;
float fade = 0.0f, float pitch = 1.0f) = 0;
virtual int playSoundAt(const std::string &name, bool loop, float volume, v3f pos,
float pitch = 1.0f) = 0;
virtual void stopSound(int sound) = 0;
virtual bool soundExists(int sound) = 0;
virtual void updateSoundPosition(int sound, v3f pos) = 0;
Expand All @@ -77,11 +79,11 @@ class ISoundManager

int playSound(const SimpleSoundSpec &spec, bool loop)
{
return playSound(spec.name, loop, spec.gain, spec.fade);
return playSound(spec.name, loop, spec.gain, spec.fade, spec.pitch);
}
int playSoundAt(const SimpleSoundSpec &spec, bool loop, v3f pos)
{
return playSoundAt(spec.name, loop, spec.gain, pos);
return playSoundAt(spec.name, loop, spec.gain, pos, spec.pitch);
}
};

Expand All @@ -98,11 +100,13 @@ 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, float fade)
int playSound(const std::string &name, bool loop, float volume, float fade,
float pitch)
{
return 0;
}
int playSoundAt(const std::string &name, bool loop, float volume, v3f pos)
int playSoundAt(const std::string &name, bool loop, float volume, v3f pos,
float pitch)
{
return 0;
}
Expand Down
24 changes: 13 additions & 11 deletions src/sound_openal.cpp
Expand Up @@ -394,7 +394,7 @@ class OpenALSoundManager: public ISoundManager
}

PlayingSound* createPlayingSound(SoundBuffer *buf, bool loop,
float volume)
float volume, float pitch)
{
infostream<<"OpenALSoundManager: Creating playing sound"<<std::endl;
assert(buf);
Expand All @@ -409,13 +409,14 @@ class OpenALSoundManager: public ISoundManager
alSourcei(sound->source_id, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
volume = MYMAX(0.0, volume);
alSourcef(sound->source_id, AL_GAIN, volume);
alSourcef(sound->source_id, AL_PITCH, pitch);
alSourcePlay(sound->source_id);
warn_if_error(alGetError(), "createPlayingSound");
return sound;
}

PlayingSound* createPlayingSoundAt(SoundBuffer *buf, bool loop,
float volume, v3f pos)
float volume, v3f pos, float pitch)
{
infostream<<"OpenALSoundManager: Creating positional playing sound"
<<std::endl;
Expand All @@ -432,26 +433,27 @@ class OpenALSoundManager: public ISoundManager
alSourcei(sound->source_id, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
volume = MYMAX(0.0, volume);
alSourcef(sound->source_id, AL_GAIN, volume);
alSourcef(sound->source_id, AL_PITCH, pitch);
alSourcePlay(sound->source_id);
warn_if_error(alGetError(), "createPlayingSoundAt");
return sound;
}

int playSoundRaw(SoundBuffer *buf, bool loop, float volume)
int playSoundRaw(SoundBuffer *buf, bool loop, float volume, float pitch)
{
assert(buf);
PlayingSound *sound = createPlayingSound(buf, loop, volume);
PlayingSound *sound = createPlayingSound(buf, loop, volume, pitch);
if(!sound)
return -1;
int id = m_next_id++;
m_sounds_playing[id] = sound;
return id;
}

int playSoundRawAt(SoundBuffer *buf, bool loop, float volume, v3f pos)
int playSoundRawAt(SoundBuffer *buf, bool loop, float volume, v3f pos, float pitch)
{
assert(buf);
PlayingSound *sound = createPlayingSoundAt(buf, loop, volume, pos);
PlayingSound *sound = createPlayingSoundAt(buf, loop, volume, pos, pitch);
if(!sound)
return -1;
int id = m_next_id++;
Expand Down Expand Up @@ -561,7 +563,7 @@ class OpenALSoundManager: public ISoundManager
alListenerf(AL_GAIN, gain);
}

int playSound(const std::string &name, bool loop, float volume, float fade)
int playSound(const std::string &name, bool loop, float volume, float fade, float pitch)
{
maintain();
if(name == "")
Expand All @@ -574,15 +576,15 @@ class OpenALSoundManager: public ISoundManager
}
int handle = -1;
if (fade > 0) {
handle = playSoundRaw(buf, loop, 0);
handle = playSoundRaw(buf, loop, 0.0f, 0.0f);
fadeSound(handle, fade, volume);
} else {
handle = playSoundRaw(buf, loop, volume);
handle = playSoundRaw(buf, loop, volume, pitch);
}
return handle;
}

int playSoundAt(const std::string &name, bool loop, float volume, v3f pos)
int playSoundAt(const std::string &name, bool loop, float volume, v3f pos, float pitch)
{
maintain();
if(name == "")
Expand All @@ -593,7 +595,7 @@ class OpenALSoundManager: public ISoundManager
<<std::endl;
return -1;
}
return playSoundRawAt(buf, loop, volume, pos);
return playSoundRawAt(buf, loop, volume, pos, pitch);
}

void stopSound(int sound)
Expand Down

3 comments on commit ff73c7a

@nerzhul
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Rui-Minetest can you provide very fast a fix adding protocol BUMP + protocol version checking on deserializaiton, we cannot connect to servers atm.

@Fixer-007
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reported and confirmed by me

@Fixer-007
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Fixed the issue for me.

Please sign in to comment.