Skip to content

Commit

Permalink
provide an internal rand() replacement and use it in libmodplug/snd_fx.c
Browse files Browse the repository at this point in the history
because SDL doesn't provide a rand() replacement..
  • Loading branch information
sezero committed Mar 20, 2021
1 parent d0340e6 commit 4702d4a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/SDL_sound.c
Expand Up @@ -798,6 +798,19 @@ Sint32 Sound_GetDuration(Sound_Sample *sample)

/* Utility functions ... */

/* The following uses the implementation suggested by
* the standard document, assumes RAND_MAX == 32767 */
static unsigned long __Sound_seed = 1;
int __Sound_rand(void)
{
__Sound_seed = __Sound_seed * 1103515245 + 12345;
return (__Sound_seed / 65536) % 32768;
}
void __Sound_srand(unsigned int seed)
{
__Sound_seed = seed;
}

#if !defined(HAVE_SDL_STRTOKR)
/* Adapted from _PDCLIB_strtok() of PDClib library at
* https://github.com/DevSolar/pdclib.git
Expand Down
6 changes: 6 additions & 0 deletions src/SDL_sound_internal.h
Expand Up @@ -304,6 +304,12 @@ Uint32 __Sound_convertMsToBytePos(Sound_AudioInfo *info, Uint32 ms);
extern char *SDL_strtokr(char *s1, const char *s2, char **saveptr);
#endif

/* SDL doesn't provide a rand() replacement */
#define SDL_rand __Sound_rand
#define SDL_srand __Sound_srand
extern int SDL_rand(void);
extern void SDL_srand(unsigned int seed);

#endif /* defined _INCLUDE_SDL_SOUND_INTERNAL_H_ */

/* end of SDL_sound_internal.h ... */
Expand Down
2 changes: 2 additions & 0 deletions src/libmodplug/libmodplug.h
Expand Up @@ -83,6 +83,8 @@ typedef void VOID;
* Adam Goode <adam@evdebs.org> (endian and char fixes for PPC)
*/

extern void init_modplug_filters(void);

#ifndef LPCBYTE
typedef const BYTE * LPCBYTE;
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/libmodplug/modplug.c
Expand Up @@ -9,10 +9,10 @@

void ModPlug_Quit(void) { /* does nothing. */ }

extern void init_modplug_filters(void);
int ModPlug_Init(void)
{
init_modplug_filters();
SDL_srand(SDL_GetTicks());
return 1;
}

Expand Down
5 changes: 2 additions & 3 deletions src/libmodplug/snd_fx.c
Expand Up @@ -5,7 +5,6 @@
*/

#include "libmodplug.h"
#include <stdlib.h> /* for rand() */
#define SNDFX_C
#include "tables.h"

Expand Down Expand Up @@ -523,13 +522,13 @@ void CSoundFile_NoteChange(CSoundFile *_this, UINT nChn, int note, BOOL bPorta,
// Volume Swing
if (penv->nVolSwing)
{
int d = ((LONG)penv->nVolSwing*(LONG)((rand() & 0xFF) - 0x7F)) / 128;
int d = ((LONG)penv->nVolSwing*(LONG)((SDL_rand() & 0xFF) - 0x7F)) / 128;
pChn->nVolSwing = (signed short)((d * pChn->nVolume + 1)/128);
}
// Pan Swing
if (penv->nPanSwing)
{
int d = ((LONG)penv->nPanSwing*(LONG)((rand() & 0xFF) - 0x7F)) / 128;
int d = ((LONG)penv->nPanSwing*(LONG)((SDL_rand() & 0xFF) - 0x7F)) / 128;
pChn->nPanSwing = (signed short)d;
}
}
Expand Down

0 comments on commit 4702d4a

Please sign in to comment.