Browse Source

Add PRNG and use it instead of rand()

master
Yury Popov 2 years ago
parent
commit
1e279d3cb8
No account linked to committer's email address

+ 13
- 0
src/libespeak-ng/common.c View File

@@ -326,4 +326,17 @@ int towlower2(unsigned int c, Translator *translator)
return ucd_tolower(c);
}

static uint32_t espeak_rand_state = 0;

long espeak_rand(long min, long max) {
// Ref: https://github.com/bminor/glibc/blob/glibc-2.36/stdlib/random_r.c#L364
espeak_rand_state = (((uint64_t)espeak_rand_state * 1103515245) + 12345) % 0x7fffffff;
long res = (long)espeak_rand_state;
return (res % (max-min+1))-min;
}

void espeak_srand(long seed) {
espeak_rand_state = (uint32_t)(seed);
(void)espeak_rand(0, 1); // Dummy flush a generator
}


+ 3
- 0
src/libespeak-ng/common.h View File

@@ -26,6 +26,9 @@
extern ESPEAK_NG_API int GetFileLength(const char *filename);
extern ESPEAK_NG_API void strncpy0(char *to, const char *from, int size);

void espeak_srand(long seed);
long espeak_rand(long min, long max);

int IsAlpha(unsigned int c);
int IsBracket(int c);
int IsDigit(unsigned int c);

+ 2
- 5
src/libespeak-ng/klatt.c View File

@@ -35,6 +35,7 @@
#include <espeak-ng/speak_lib.h>

#include "klatt.h"
#include "common.h" // for espeak_rand
#include "synthesize.h" // for frame_t, WGEN_DATA, STEPSIZE, N_KLATTP, echo...
#include "voice.h" // for voice_t, N_PEAKS
#ifdef INCLUDE_SPEECHPLAYER
@@ -46,11 +47,7 @@ extern unsigned char *out_end;
static int nsamples;
static int sample_count;

#ifdef _MSC_VER
#define getrandom(min, max) ((rand()%(int)(((max)+1)-(min)))+(min))
#else
#define getrandom(min, max) ((rand()%(long)(((max)+1)-(min)))+(min))
#endif
#define getrandom(min, max) espeak_rand((min), (max))

// function prototypes for functions private to this file


+ 3
- 0
src/libespeak-ng/speech.c View File

@@ -404,6 +404,9 @@ ESPEAK_NG_API espeak_ng_STATUS espeak_ng_Initialize(espeak_ng_ERROR_CONTEXT *con
option_phonemes = 0;
option_phoneme_events = 0;

// Seed random generator
espeak_srand(time(NULL));

return ENS_OK;
}


+ 2
- 1
src/libespeak-ng/wavegen.c View File

@@ -33,6 +33,7 @@
#include <espeak-ng/speak_lib.h>

#include "wavegen.h"
#include "common.h" // for espeak_rand
#include "synthesize.h" // for WGEN_DATA, RESONATOR, frame_t
#include "mbrola.h" // for MbrolaFill, MbrolaReset, mbrola...

@@ -668,7 +669,7 @@ static int ApplyBreath(void)
int ix;

// use two random numbers, for alternate formants
noise = (rand() & 0x3fff) - 0x2000;
noise = espeak_rand(-0x2000, 0x1fff);

for (ix = 1; ix < N_PEAKS; ix++) {
int amp;

Loading…
Cancel
Save