Skip to content

Commit

Permalink
Merge pull request #12 from EdFuentetaja/master
Browse files Browse the repository at this point in the history
Fixed random number generation that assumed that RAND_MAX == 2147483647.
  • Loading branch information
szechyjs committed Apr 30, 2014
2 parents fa0fed4 + fbe9d4e commit 1e343f4
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions mbelib.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@
#include "mbelib.h"
#include "mbelib_const.h"

/**
* \return A pseudo-random float between [0.0, 1.0].
* See http://www.azillionmonkeys.com/qed/random.html for further improvements
*/
static float
mbe_rand()
{
return ((float) rand () / (float) RAND_MAX);
}

/**
* \return A pseudo-random float between [-pi, +pi].
*/
static float
mbe_rand_phase()
{
return mbe_rand() * (((float)M_PI) * 2.0F) - ((float)M_PI);
}

void
mbe_printVersion (char *str)
{
Expand Down Expand Up @@ -291,7 +310,7 @@ mbe_synthesizeSpeechf (float *aout_buf, mbe_parms * cur_mp, mbe_parms * prev_mp,
}
else
{
cur_mp->PHIl[l] = cur_mp->PSIl[l] + ((numUv * ((((float) random () / (float) 2147483647) * M_PI * (float) 2) - M_PI)) / cur_mp->L);
cur_mp->PHIl[l] = cur_mp->PSIl[l] + ((numUv * mbe_rand_phase()) / cur_mp->L);
}
}

Expand All @@ -305,7 +324,7 @@ mbe_synthesizeSpeechf (float *aout_buf, mbe_parms * cur_mp, mbe_parms * prev_mp,
// init random phase
for (i = 0; i < uvquality; i++)
{
rphase[i] = ((((float) random () / (float) 2147483647) * M_PI * (float) 2) - M_PI);
rphase[i] = mbe_rand_phase();
}
for (n = 0; n < N; n++)
{
Expand All @@ -319,7 +338,7 @@ mbe_synthesizeSpeechf (float *aout_buf, mbe_parms * cur_mp, mbe_parms * prev_mp,
C3 = C3 + cosf ((cw0 * (float) n * ((float) l + ((float) i * uvstep) - uvoffset)) + rphase[i]);
if (cw0l > uvthreshold)
{
C3 = C3 + ((cw0l - uvthreshold) * uvrand * ((float) random () / (float) 2147483647));
C3 = C3 + ((cw0l - uvthreshold) * uvrand * mbe_rand());
}
}
C3 = C3 * uvsine * Ws[n] * cur_mp->Ml[l] * qfactor;
Expand All @@ -333,7 +352,7 @@ mbe_synthesizeSpeechf (float *aout_buf, mbe_parms * cur_mp, mbe_parms * prev_mp,
// init random phase
for (i = 0; i < uvquality; i++)
{
rphase[i] = ((((float) random () / (float) 2147483647) * M_PI * (float) 2) - M_PI);
rphase[i] = mbe_rand_phase();
}
for (n = 0; n < N; n++)
{
Expand All @@ -347,7 +366,7 @@ mbe_synthesizeSpeechf (float *aout_buf, mbe_parms * cur_mp, mbe_parms * prev_mp,
C3 = C3 + cosf ((pw0 * (float) n * ((float) l + ((float) i * uvstep) - uvoffset)) + rphase[i]);
if (pw0l > uvthreshold)
{
C3 = C3 + ((pw0l - uvthreshold) * uvrand * ((float) random () / (float) 2147483647));
C3 = C3 + ((pw0l - uvthreshold) * uvrand * mbe_rand());
}
}
C3 = C3 * uvsine * Ws[n + N] * prev_mp->Ml[l] * qfactor;
Expand Down Expand Up @@ -398,12 +417,12 @@ mbe_synthesizeSpeechf (float *aout_buf, mbe_parms * cur_mp, mbe_parms * prev_mp,
// init random phase
for (i = 0; i < uvquality; i++)
{
rphase[i] = ((((float) random () / (float) 2147483647) * M_PI * (float) 2) - M_PI);
rphase[i] = mbe_rand_phase();
}
// init random phase
for (i = 0; i < uvquality; i++)
{
rphase2[i] = ((((float) random () / (float) 2147483647) * M_PI * (float) 2) - M_PI);
rphase2[i] = mbe_rand_phase();
}
for (n = 0; n < N; n++)
{
Expand All @@ -414,7 +433,7 @@ mbe_synthesizeSpeechf (float *aout_buf, mbe_parms * cur_mp, mbe_parms * prev_mp,
C3 = C3 + cosf ((pw0 * (float) n * ((float) l + ((float) i * uvstep) - uvoffset)) + rphase[i]);
if (pw0l > uvthreshold)
{
C3 = C3 + ((pw0l - uvthreshold) * uvrand * ((float) random () / (float) 2147483647));
C3 = C3 + ((pw0l - uvthreshold) * uvrand * mbe_rand());
}
}
C3 = C3 * uvsine * Ws[n + N] * prev_mp->Ml[l] * qfactor;
Expand All @@ -425,7 +444,7 @@ mbe_synthesizeSpeechf (float *aout_buf, mbe_parms * cur_mp, mbe_parms * prev_mp,
C4 = C4 + cosf ((cw0 * (float) n * ((float) l + ((float) i * uvstep) - uvoffset)) + rphase2[i]);
if (cw0l > uvthreshold)
{
C4 = C4 + ((cw0l - uvthreshold) * uvrand * ((float) random () / (float) 2147483647));
C4 = C4 + ((cw0l - uvthreshold) * uvrand * mbe_rand());
}
}
C4 = C4 * uvsine * Ws[n] * cur_mp->Ml[l] * qfactor;
Expand Down

0 comments on commit 1e343f4

Please sign in to comment.