Sound: Add pitch option (#5960)
authorRui <rui.minetest@gmail.com>
Sun, 11 Jun 2017 11:58:26 +0000 (20:58 +0900)
committerLoïc Blot <nerzhul@users.noreply.github.com>
Sun, 11 Jun 2017 11:58:26 +0000 (13:58 +0200)
* Sound: Add pitch option

doc/lua_api.txt
src/itemdef.cpp
src/network/clientpackethandler.cpp
src/nodedef.cpp
src/script/common/c_content.cpp
src/script/lua_api/l_client.cpp
src/server.cpp
src/server.h
src/sound.h
src/sound_openal.cpp

index 2bbf18310cd4d8f0c2f54c9b0ef18f31ccb09338..28067eef161676922823bda20048385a41461813 100644 (file)
@@ -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
     {
@@ -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
 -------------------------------
index f1cc03c4b652ade4519796cad5511c76cb2c8ea1..ca6020e149d817b459ad635ba998739a78ffd5e7 100644 (file)
@@ -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)
@@ -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) {};
 }
 
index 9eb6d8dca2747bdae4ab7bdcaec65cf082285b67..caaf24d80f23b8296e93d80eebbbafafa38bbbf8 100644 (file)
@@ -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;
@@ -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;
                }
index 6b2718636cde7d143c9a56ad9f12946b9347c8bc..02e96738425d07dade1bce44721a0cbc4220cf12 100644 (file)
@@ -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()
index 9be9d77178a2ce924b4021c393b3a7f4423ba401..3ee6913c97815ce66e26f218442002993d2b5cbf 100644 (file)
@@ -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;
@@ -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);
        }
@@ -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");
 }
 
 /******************************************************************************/
index eab7bdfae0c07acc45991f5b510bae065aa2ac4b..b4dfe91749574131ebba29bee2b6e83cb7f4aee8 100644 (file)
@@ -227,12 +227,14 @@ 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");
@@ -240,13 +242,13 @@ int ModApiClient::l_sound_play(lua_State *L)
                        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;
index 4d7947919c06a51d2bbf76877031717a31d2bbf1..5911bbaf18f22bd861906bfa6f4f7a0d365a0538 100644 (file)
@@ -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;
index 0ad5dd97acf2d9d1ee8d9cd10cf3b15d9ea82132..3086e87621345332def05297327ce6fc4282e342 100644 (file)
@@ -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;
 };
index 76c0d1be4cfd5e8d9a8ff76fd176c680de800dcd..9447b77ef1ae2e71ba7e9ece62afc77d43ae0cb2 100644 (file)
@@ -34,16 +34,18 @@ public:
 
 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
@@ -64,9 +66,9 @@ public:
        // 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;
@@ -77,11 +79,11 @@ public:
 
        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);
        }
 };
 
@@ -98,11 +100,13 @@ public:
        }
        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;
        }
index d1a5279b309af0d30b2cef936ad1d791a83d1490..0b53572c428b645a8d87e7c3798db8f1959a88d7 100644 (file)
@@ -394,7 +394,7 @@ public:
        }
 
        PlayingSound* createPlayingSound(SoundBuffer *buf, bool loop,
-                       float volume)
+                       float volume, float pitch)
        {
                infostream<<"OpenALSoundManager: Creating playing sound"<<std::endl;
                assert(buf);
@@ -409,13 +409,14 @@ public:
                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;
@@ -432,15 +433,16 @@ public:
                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++;
@@ -448,10 +450,10 @@ public:
                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++;
@@ -561,7 +563,7 @@ public:
                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 == "")
@@ -574,15 +576,15 @@ public:
                }
                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 == "")
@@ -593,7 +595,7 @@ public:
                                        <<std::endl;
                        return -1;
                }
-               return playSoundRawAt(buf, loop, volume, pos);
+               return playSoundRawAt(buf, loop, volume, pos, pitch);
        }
 
        void stopSound(int sound)