Permalink
Browse files

Merge pull request chocolate-doom#567 from jmtd/pitch-shifting

Implementation of pitch shifting.

This appeared in early versions of Doom and (more importantly) in Heretic
and Hexen. Sound effects get pitched up and down to sound less repetitive.
  • Loading branch information...
fragglet committed Jul 9, 2015
2 parents 4780944 + 8a3621b commit 93fe1124119faafe0704ea913ab4d5977b1f24b6
Showing with 238 additions and 57 deletions.
  1. +39 −2 src/doom/s_sound.c
  2. +10 −7 src/heretic/s_sound.c
  3. +14 −8 src/hexen/s_sound.c
  4. +2 −1 src/i_pcsound.c
  5. +124 −29 src/i_sdlsound.c
  6. +8 −2 src/i_sound.c
  7. +8 −5 src/i_sound.h
  8. +8 −0 src/m_config.c
  9. +19 −0 src/setup/sound.c
  10. +6 −3 src/strife/s_sound.c
@@ -59,7 +59,6 @@

#define S_STEREO_SWING (96 * FRACUNIT)

#define NORM_PITCH 128
#define NORM_PRIORITY 64
#define NORM_SEP 128

@@ -74,6 +73,8 @@ typedef struct
// handle of the sound being played
int handle;

int pitch;

} channel_t;

// The set of channels available
@@ -141,6 +142,12 @@ void S_Init(int sfxVolume, int musicVolume)
S_sfx[i].lumpnum = S_sfx[i].usefulness = -1;
}

// Doom defaults to pitch-shifting off.
if(snd_pitchshift == -1)
{
snd_pitchshift = 0;
}

I_AtExit(S_Shutdown, true);
}

@@ -390,12 +397,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;

@@ -411,9 +434,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)
{
@@ -452,6 +477,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);

@@ -474,7 +510,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);
}

//
@@ -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) (127 + (M_Random() & 7) - (M_Random() & 7));
channel[i].handle = I_StartSound(&S_sfx[sound_id], i, vol, sep);
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].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) (127 - (M_Random() & 3) + (M_Random() & 3));
channel[i].handle = I_StartSound(&S_sfx[sound_id], i, volume, 128);
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].pitch);
channel[i].mo = origin;
channel[i].sound_id = sound_id;
channel[i].priority = 1; //super low priority.
@@ -527,6 +524,12 @@ void S_Init(void)

I_AtExit(S_ShutDown, true);

// Heretic defaults to pitch-shifting on
if(snd_pitchshift == -1)
{
snd_pitchshift = 1;
}

I_PrecacheSounds(S_sfx, NUMSFX);
}

@@ -498,17 +498,16 @@ void S_StartSoundAtVolume(mobj_t * origin, int sound_id, int volume)
// vol = SoundCurve[dist];
}

#if 0
// TODO
if (S_sfx[sound_id].changePitch)
// if the sfxinfo_t is marked as 'can be pitch shifted'
if (S_sfx[sound_id].pitch)
{
Channel[i].pitch = (byte) (127 + (M_Random() & 7) - (M_Random() & 7));
Channel[i].pitch = (byte) (NORM_PITCH + (M_Random() & 7) - (M_Random() & 7));
}
else
{
Channel[i].pitch = 127;
Channel[i].pitch = NORM_PITCH;
}
#endif

if (S_sfx[sound_id].lumpnum == 0)
{
S_sfx[sound_id].lumpnum = I_GetSfxLumpNum(&S_sfx[sound_id]);
@@ -517,7 +516,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,
Channel[i].pitch);
Channel[i].sound_id = sound_id;
Channel[i].priority = priority;
Channel[i].volume = volume;
@@ -771,7 +771,7 @@ void S_UpdateSounds(mobj_t * listener)
if (sep > 192)
sep = 512 - sep;
}
I_UpdateSoundParams(i, vol, sep /*, Channel[i].pitch */);
I_UpdateSoundParams(i, vol, sep);
priority = S_sfx[Channel[i].sound_id].priority;
priority *= PRIORITY_MAX_ADJUST - (dist / DIST_ADJUST);
Channel[i].priority = priority;
@@ -799,6 +799,12 @@ void S_Init(void)

I_AtExit(S_ShutDown, true);

// Hexen defaults to pitch-shifting on
if(snd_pitchshift == -1)
{
snd_pitchshift = 1;
}

I_PrecacheSounds(S_sfx, NUMSFX);

// Attempt to setup CD music
@@ -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;

Oops, something went wrong.

0 comments on commit 93fe112

Please sign in to comment.