History

PRNG clean-up

robs committed 17 years ago

Commit dec07291 parent 1915e8212 files · +33 −33Browse files

Unified · Split

12 files
  1. ChangeLog +2 −1
  2. scripts/synth.sh +10 −8
  3. src/dither.c +1 −5
  4. src/dither_fir.h +1 −1
  5. src/dither_iir.h +1 −1
  6. src/libsox.c +1 −0
  7. src/sox.c +3 −7
  8. src/sox.h +2 −1
  9. src/sox_i.h +4 −0
  10. src/synth.c +5 −7
  11. src/util.h +2 −1
  12. src/vorbis.c +1 −1
ChangeLog+2−1
@@ -39,7 +39,8 @@ Other bug fixes: · 38 unmodified lines
3939
4040 Internal improvements:
4141
42− o
42+ o Rationalise use of and make repeatable across different platforms
43+ pseudo random number generators. (robs)
4344
4445
4546 sox-14.2.0 2008-11-09
scripts/synth.sh+10−8
@@ -29,7 +29,7 @@ Bb0="pl %-23 pl %-16 pl %-11 pl %-7 pl %-4 pl %1" · 28 unmodified lines
2929 o="overdrive 40 gain -8"
3030 e="delay 0 .02 .04 .06 .08 .1 remix - $o fade 0"
3131 s="$sox -q -n -p synth 0 0 0 80 87"
32−l="$sox -q -n -p synth 0 0 0 80 0"
32+l="$sox -q -n -p synth 0 0 0 80 0 0"
3333
3434 $play -m -v .8 \
3535 "|$sox \
@@ -41,7 +41,7 @@ equalizer 9500 10 8 \ · 5 unmodified lines
4141 equalizer 7000 10 8 \
4242 equalizer 5200 10 8 \
4343 equalizer 3800 10 8 \
44−equalizer 1500 10 8 pad 0 .21 remix 1 1 reverb 20 repeat 59" \
44+equalizer 1500 10 8 pad 0 .21 remix 1 1 reverb 10 repeat 56" \
4545 "|$sox \
4646 \"|$sox -n -p trim 0 1.4\" \
4747 \"|$s $G0 $e 2.6 .1 bend .5,200,.2\" \
@@ -63,15 +63,17 @@ equalizer 1500 10 8 pad 0 .21 remix 1 1 reverb 20 repeat 59" \ · 15 unmodified lines
6363 \"|$l pl %10 $o trim 0 .5 bend .2,-300,.1\" \
6464 \"|$l pl %5 $o trim 0 .5 bend .2,-200,.1\" \
6565 \"|$l pl %0 $o fade 0 .55 .1 bend .2,-200,.1\" \
66−\"|$l pl %12 $o trim 0 5.95\" \
66+\"|$l pl %0 $o fade 0 2 .1\" \
67+\"|$sox -n -p trim 0 3.95\" \
6768 \"|$l pl %12 $o trim 0 .5 bend .2,-200,.1\" \
6869 \"|$l pl %12 $o trim 0 .5 bend .2,-200,.1\" \
69−\"|$l pl %12 $o trim 0 .5 bend .2,-200,.1\" \
70−\"|$l pl %12 $o fade 0 .6 .1 bend .2,-200,.1\" \
71−\"|$l pl %12 $o trim 0 1.95 bend .3,-100,1\" \
70+\"|$l pl %12 $o fade 0 .8 .1 bend .2,-200,.1\" \
71+\"|$l pl %12 $o trim 0 .3 bend .1,-200,.1\" \
72+\"|$l pl %12 $o trim 0 1.95 bend .3,-50,1\" \
7273 \"|$l pl %10 $o trim 0 2 bend .3,-50,1\" \
73−\"|$l pl %9 $o trim 0 2 gain -3 \" \
74−\"|$l pl %8 $o trim 0 2 gain -6 \" \
74+\"|$l pl %9 $o trim 0 2 gain -3\" \
75+\"|$l pl %8 $o fade h 0 1 .3\" \
76+\"|$l pl %8 $o fade h 0 1 .1 gain 1.5\" \
7577 \"|$l pl %2 pl %7 delay 0 .02 remix - $o trim 0 .25\" \
7678 \"|$l pl %-5 $o trim 0 .25\" \
7779 \"|$l pl %-5 $o trim 0 .25\" \
src/dither.c+1−5
@@ -25,10 +25,6 @@ · 24 unmodified lines
2525
2626 #define PREC effp->out_signal.precision
2727
28−#define RAND_CONST 140359821
29−static int32_t rand_ = ~RAND_CONST;
30−#define RAND_ (rand_ = rand_ * RAND_CONST + 1)
31−
3228 typedef enum {Pdf_rectangular, Pdf_triangular, Pdf_gaussian} pdf_type_t;
3329 static lsx_enum_item const pdf_types[] = {
3430 LSX_ENUM_ITEM(Pdf_,rectangular)
@@ -190,7 +186,7 @@ static int flow_no_shape(sox_effect_t * effp, const sox_sample_t * ibuf, · 155 unmodified lines
190186 int dummy = 0;
191187
192188 while (len--) {
193− double d = *ibuf++ + p->am0 * RAND_ + p->am1 * RAND_;
189+ double d = *ibuf++ + p->am0 * RANQD1 + p->am1 * RANQD1;
194190 *obuf++ = SOX_ROUND_CLIP_COUNT(d, dummy);
195191 }
196192 return SOX_SUCCESS;
src/dither_fir.h+1−1
@@ -7,7 +7,7 @@ static int NAME(sox_effect_t * effp, const sox_sample_t * ibuf, · 6 unmodified lines
77 int dummy = 0;
88
99 while (len--) {
10− double r = p->am0 * RAND_ + p->am1 * RAND_;
10+ double r = p->am0 * RANQD1 + p->am1 * RANQD1;
1111 double error, d = *ibuf++;
1212 int j = 0;
1313 CONVOLVE
src/dither_iir.h+1−1
@@ -8,7 +8,7 @@ static int NAME(sox_effect_t * effp, const sox_sample_t * ibuf, · 7 unmodified lines
88 int dummy = 0;
99
1010 while (len--) {
11− double r = p->am0 * RAND_ + p->am1 * RAND_;
11+ double r = p->am0 * RANQD1 + p->am1 * RANQD1;
1212 double error, d, output = 0;
1313 int j = 0;
1414 CONVOLVE
src/libsox.c+1−0
@@ -48,6 +48,7 @@ sox_globals_t sox_globals = { · 47 unmodified lines
4848 sox_false,
4949 8192,
5050 0,
51+ 0,
5152 NULL, NULL, NULL};
5253
5354 void sox_output_message(FILE *file, const char *filename, const char *fmt, va_list ap)
src/sox.c+3−7
@@ -2485,13 +2485,9 @@ int main(int argc, char **argv) · 2484 unmodified lines
24852485 exit(0);
24862486 }
24872487
2488− if (sox_globals.repeatable)
2489− lsx_debug("Not reseeding PRNG; randomness is repeatable");
2490− else {
2491− time_t t;
2492−
2493− time(&t);
2494− srand((unsigned)t);
2488+ if (!sox_globals.repeatable) { /* Re-seed PRNG? */
2489+ sox_globals.ranqd1 = (int32_t)time(NULL);
2490+ srand((unsigned int)sox_globals.ranqd1); /* srand only for polyphase */
24952491 }
24962492
24972493 /* Save things that sox_sequence needs to be reinitialised for each segued
src/sox.h+2−1
@@ -179,7 +179,8 @@ typedef struct { /* Global parameters (for effects & formats) */ · 178 unmodified lines
179179 * to perform file I/O. It can be useful to pass in similar sized
180180 * data to get max performance.
181181 */
182− size_t bufsiz, input_bufsiz;
182+ size_t bufsiz, input_bufsiz;
183+ int32_t ranqd1; /* Can be used to re-seed libSoX's PRNG */
183184
184185 /* private: */
185186 char const * stdin_in_use_by;
src/sox_i.h+4−0
@@ -18,6 +18,10 @@ · 17 unmodified lines
1818 #include "util.h"
1919
2020 #include <errno.h>
21+
22+#define RANQD1 ranqd1(sox_globals.ranqd1)
23+#define DRANQD1 dranqd1(sox_globals.ranqd1)
24+
2125 typedef enum {SOX_SHORT, SOX_INT, SOX_FLOAT, SOX_DOUBLE} sox_data_t;
2226 typedef enum {SOX_WAVE_SINE, SOX_WAVE_TRIANGLE} lsx_wave_t;
2327 extern lsx_enum_item const lsx_wave_enum[];
src/synth.c+5−7
@@ -13,9 +13,6 @@ · 12 unmodified lines
1313 #include <string.h>
1414 #include <ctype.h>
1515
16−#define RAND (2. * rand() * (1. / RAND_MAX) - 1)
17−#define RAND_ ranqd1(r) * (1. / (65536. * 32768.)) /* [-1,1) */
18−
1916 typedef enum {
2017 synth_sine,
2118 synth_square,
@@ -405,7 +402,8 @@ static int start(sox_effect_t * effp) · 383 unmodified lines
405402 chan->buffer = malloc(chan->buffer_len * sizeof(*chan->buffer));
406403 chan->buffer[0] = 0;
407404 for (j = 1; j < chan->buffer_len; dc += chan->buffer[j++])
408− do chan->buffer[j] = chan->buffer[j - 1] + RAND_ * colour;
405+ do chan->buffer[j] =
406+ chan->buffer[j - 1] + (chan->p3 == 0? DRANQD1:dranqd1(r)) * colour;
409407 while (fabs(chan->buffer[j]) > 1);
410408 for (dc /= chan->buffer_len, j = 0; j < chan->buffer_len; ++j)
411409 chan->buffer[j] = range_limit(chan->buffer[j] - dc, -1, 1) * a;
@@ -570,11 +568,11 @@ static int flow(sox_effect_t * effp, const sox_sample_t * ibuf, sox_sample_t * o · 158 unmodified lines
570568 }
571569 } else switch (chan->type) {
572570 case synth_whitenoise:
573− synth_out = RAND;
571+ synth_out = DRANQD1;
574572 break;
575573
576574 case synth_tpdfnoise:
577− synth_out = .5 * (RAND + RAND);
575+ synth_out = .5 * (DRANQD1 + DRANQD1);
578576 break;
579577
580578 case synth_pinknoise:
@@ -582,7 +580,7 @@ static int flow(sox_effect_t * effp, const sox_sample_t * ibuf, sox_sample_t * o · 1 unmodified line
582580 break;
583581
584582 case synth_brownnoise:
585− do synth_out = chan->last_out + RAND * (1. / 16);
583+ do synth_out = chan->last_out + DRANQD1 * (1. / 16);
586584 while (fabs(synth_out) > 1);
587585 chan->last_out = synth_out;
588586 break;
src/util.h+2−1
@@ -92,7 +92,8 @@ · 91 unmodified lines
9292 #endif
9393
9494 /* Numerical Recipes in C, p. 284 */
95−#define ranqd1(x) ((x) = 1664525L * (x) + 1013904223L)
95+#define ranqd1(x) ((x) = 1664525L * (x) + 1013904223L) /* int32_t x */
96+#define dranqd1(x) (ranqd1(x) * (1. / (65536. * 32768.))) /* [-1,1) */
9697
9798 #ifdef WORDS_BIGENDIAN
9899 #define MACHINE_IS_BIGENDIAN 1
src/vorbis.c+1−1
@@ -305,7 +305,7 @@ static int startwrite(sox_format_t * ft) · 304 unmodified lines
305305 vorbis_analysis_init(&ve->vd, &ve->vi);
306306 vorbis_block_init(&ve->vd, &ve->vb);
307307
308− ogg_stream_init(&ve->os, rand()); /* Random serial number */
308+ ogg_stream_init(&ve->os, INT_MAX & (int)RANQD1); /* Random serial number */
309309
310310 if (write_vorbis_header(ft, ve) == HEADER_ERROR) {
311311 lsx_fail_errno(ft, SOX_EHDR,

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t