foxygit / doom Log in
commit bf67262a1fa5a49fbf384a2c08f74d930080308b
Author:     Jonathan Dowland <jon+github@alcopop.org>
AuthorDate: Tue Jun 23 21:43:26 2015 +0100
Commit:     Jonathan Dowland <jon+github@alcopop.org>
CommitDate: Mon Jul 6 20:00:47 2015 +0100

    update I_StartSound, S_StartSound with pitch

    Re-introduce removed code for pitch shifting. In the case of Doom,
    bring back the code from the linux doom sources. In the case of
    Heretic and Hexen, uncomment existing code.

    The original implementation passed a pitch value around through
    several of the sound routines, including I_StartSound and
    I_UpdateSoundParams, likely because DMX required it. We won't
    need it in I_UpdateSoundParams, so I haven't brought those bits
    back.

    Adjust the sound driver prototypes for the pitch variable.
---
 src/doom/s_sound.c    | 34 +++++++++++++++++++++++++++++++++-
 src/heretic/s_sound.c |  7 ++-----
 src/hexen/s_sound.c   |  3 ++-
 src/i_pcsound.c       |  3 ++-
 src/i_sdlsound.c      |  2 +-
 src/i_sound.c         |  4 ++--
 src/i_sound.h         |  4 ++--
 src/strife/s_sound.c  |  8 ++++++--
 8 files changed, 50 insertions(+), 15 deletions(-)

diff --git a/src/doom/s_sound.c b/src/doom/s_sound.c
index 83f691e0..90f1a99c 100644
--- a/src/doom/s_sound.c
+++ b/src/doom/s_sound.c
@@ -73,6 +73,8 @@ typedef struct
     // handle of the sound being played
     int handle;

+    int pitch;
+
 } channel_t;

 // The set of channels available
@@ -389,12 +391,28 @@ static int S_AdjustSoundParams(mobj_t *listener, mobj_t *source,
     return (*vol > 0);
 }

+// clamp supplied integer to the range 0 <= x <= 255.
+
+static int Clamp(int x)
+{
+    if (x < 0)
+    {
+        return 0;
+    }
+    else if (x > 255)
+    {
+        return 255;
+    }
+    return x;
+}
+
 void S_StartSound(void *origin_p, int sfx_id)
 {
     sfxinfo_t *sfx;
     mobj_t *origin;
     int rc;
     int sep;
+    int pitch;
     int cnum;
     int volume;

@@ -410,9 +428,11 @@ void S_StartSound(void *origin_p, int sfx_id)
     sfx = &S_sfx[sfx_id];

     // Initialize sound parameters
+    pitch = NORM_PITCH;
     if (sfx->link)
     {
         volume += sfx->volume;
+        pitch = sfx->pitch;

         if (volume < 1)
         {
@@ -451,6 +471,17 @@ void S_StartSound(void *origin_p, int sfx_id)
         sep = NORM_SEP;
     }

+    // hacks to vary the sfx pitches
+    if (sfx_id >= sfx_sawup && sfx_id <= sfx_sawhit)
+    {
+        pitch += 8 - (M_Random()&15);
+    }
+    else if (sfx_id != sfx_itemup && sfx_id != sfx_tink)
+    {
+        pitch += 16 - (M_Random()&31);
+    }
+    pitch = Clamp(pitch);
+
     // kill old sound
     S_StopSound(origin);

@@ -473,7 +504,8 @@ void S_StartSound(void *origin_p, int sfx_id)
         sfx->lumpnum = I_GetSfxLumpNum(sfx);
     }

-    channels[cnum].handle = I_StartSound(sfx, cnum, volume, sep);
+    channels[cnum].pitch = pitch;
+    channels[cnum].handle = I_StartSound(sfx, cnum, volume, sep, channels[cnum].pitch);
 }

 //
diff --git a/src/heretic/s_sound.c b/src/heretic/s_sound.c
index 23f341e4..fb9e6988 100644
--- a/src/heretic/s_sound.c
+++ b/src/heretic/s_sound.c
@@ -268,10 +268,8 @@ void S_StartSound(void *_origin, int sound_id)
             sep = 512 - sep;
     }

-    // TODO: Play pitch-shifted sounds as in Vanilla Heretic
-
     channel[i].pitch = (byte) (NORM_PITCH + (M_Random() & 7) - (M_Random() & 7));
-    channel[i].handle = I_StartSound(&S_sfx[sound_id], i, vol, sep);
+    channel[i].handle = I_StartSound(&S_sfx[sound_id], i, vol, sep, channel[i].pitch);
     channel[i].mo = origin;
     channel[i].sound_id = sound_id;
     channel[i].priority = priority;
@@ -327,9 +325,8 @@ void S_StartSoundAtVolume(void *_origin, int sound_id, int volume)
         S_sfx[sound_id].lumpnum = I_GetSfxLumpNum(&S_sfx[sound_id]);
     }

-    // TODO: Pitch shifting.
     channel[i].pitch = (byte) (NORM_PITCH - (M_Random() & 3) + (M_Random() & 3));
-    channel[i].handle = I_StartSound(&S_sfx[sound_id], i, volume, 128);
+    channel[i].handle = I_StartSound(&S_sfx[sound_id], i, volume, 128, channel[i].pitch);
     channel[i].mo = origin;
     channel[i].sound_id = sound_id;
     channel[i].priority = 1;    //super low priority.
diff --git a/src/hexen/s_sound.c b/src/hexen/s_sound.c
index 189d59f9..eb3e0465 100644
--- a/src/hexen/s_sound.c
+++ b/src/hexen/s_sound.c
@@ -517,7 +517,8 @@ void S_StartSoundAtVolume(mobj_t * origin, int sound_id, int volume)
     Channel[i].handle = I_StartSound(&S_sfx[sound_id],
                                      i,
                                      vol,
-                                     sep /* , Channel[i].pitch] */);
+                                     sep,
+                                     NORM_PITCH);
     Channel[i].sound_id = sound_id;
     Channel[i].priority = priority;
     Channel[i].volume = volume;
diff --git a/src/i_pcsound.c b/src/i_pcsound.c
index a7a83817..ff5e0b0e 100644
--- a/src/i_pcsound.c
+++ b/src/i_pcsound.c
@@ -174,7 +174,8 @@ static boolean IsDisabledSound(sfxinfo_t *sfxinfo)
 static int I_PCS_StartSound(sfxinfo_t *sfxinfo,
                             int channel,
                             int vol,
-                            int sep)
+                            int sep,
+                            int pitch)
 {
     int result;

diff --git a/src/i_sdlsound.c b/src/i_sdlsound.c
index b379c365..105ae64c 100644
--- a/src/i_sdlsound.c
+++ b/src/i_sdlsound.c
@@ -858,7 +858,7 @@ static void I_SDL_UpdateSoundParams(int handle, int vol, int sep)
 //  is set, but currently not used by mixing.
 //

-static int I_SDL_StartSound(sfxinfo_t *sfxinfo, int channel, int vol, int sep)
+static int I_SDL_StartSound(sfxinfo_t *sfxinfo, int channel, int vol, int sep, int pitch)
 {
     allocated_sound_t *snd;

diff --git a/src/i_sound.c b/src/i_sound.c
index 10b3f0ca..d27d253e 100644
--- a/src/i_sound.c
+++ b/src/i_sound.c
@@ -308,12 +308,12 @@ void I_UpdateSoundParams(int channel, int vol, int sep)
     }
 }

-int I_StartSound(sfxinfo_t *sfxinfo, int channel, int vol, int sep)
+int I_StartSound(sfxinfo_t *sfxinfo, int channel, int vol, int sep, int pitch)
 {
     if (sound_module != NULL)
     {
         CheckVolumeSeparation(&vol, &sep);
-        return sound_module->StartSound(sfxinfo, channel, vol, sep);
+        return sound_module->StartSound(sfxinfo, channel, vol, sep, pitch);
     }
     else
     {
diff --git a/src/i_sound.h b/src/i_sound.h
index e3f29c4f..b8b03c80 100644
--- a/src/i_sound.h
+++ b/src/i_sound.h
@@ -135,7 +135,7 @@ typedef struct
     // Start a sound on a given channel.  Returns the channel id
     // or -1 on failure.

-    int (*StartSound)(sfxinfo_t *sfxinfo, int channel, int vol, int sep);
+    int (*StartSound)(sfxinfo_t *sfxinfo, int channel, int vol, int sep, int pitch);

     // Stop the sound playing on the given channel.

@@ -156,7 +156,7 @@ void I_ShutdownSound(void);
 int I_GetSfxLumpNum(sfxinfo_t *sfxinfo);
 void I_UpdateSound(void);
 void I_UpdateSoundParams(int channel, int vol, int sep);
-int I_StartSound(sfxinfo_t *sfxinfo, int channel, int vol, int sep);
+int I_StartSound(sfxinfo_t *sfxinfo, int channel, int vol, int sep, int pitch);
 void I_StopSound(int channel);
 boolean I_SoundIsPlaying(int channel);
 void I_PrecacheSounds(sfxinfo_t *sounds, int num_sounds);
diff --git a/src/strife/s_sound.c b/src/strife/s_sound.c
index 3ef6e31c..b0008d67 100644
--- a/src/strife/s_sound.c
+++ b/src/strife/s_sound.c
@@ -73,6 +73,8 @@ typedef struct

     // handle of the sound being played
     int handle;
+
+    int pitch;

 } channel_t;

@@ -407,6 +409,7 @@ void S_StartSound(void *origin_p, int sfx_id)
     mobj_t *origin;
     int rc;
     int sep;
+    int pitch;
     int cnum;
     int volume;

@@ -466,6 +469,7 @@ void S_StartSound(void *origin_p, int sfx_id)
     {
         sep = NORM_SEP;
     }
+    pitch = NORM_PITCH;

     // kill old sound [STRIFE] - nope!
     //S_StopSound(origin);
@@ -489,7 +493,7 @@ void S_StartSound(void *origin_p, int sfx_id)
         sfx->lumpnum = I_GetSfxLumpNum(sfx);
     }

-    channels[cnum].handle = I_StartSound(sfx, cnum, volume, sep);
+    channels[cnum].handle = I_StartSound(sfx, cnum, volume, sep, pitch);
 }


@@ -616,7 +620,7 @@ void I_StartVoice(const char *lumpname)
         i_voicehandle = S_GetChannel(NULL, &voice->sfx, true);

         channels[i_voicehandle].handle
-            = I_StartSound(&voice->sfx, i_voicehandle, snd_VoiceVolume, NORM_SEP);
+            = I_StartSound(&voice->sfx, i_voicehandle, snd_VoiceVolume, NORM_SEP, NORM_PITCH);
     }
 }