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
39 39
40Internal improvements:40Internal improvements:
41 41
42 o 42 o Rationalise use of and make repeatable across different platforms
43 pseudo random number generators. (robs)
43 44
44 45
45sox-14.2.0 2008-11-0946sox-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
29o="overdrive 40 gain -8"29o="overdrive 40 gain -8"
30e="delay 0 .02 .04 .06 .08 .1 remix - $o fade 0"30e="delay 0 .02 .04 .06 .08 .1 remix - $o fade 0"
31s="$sox -q -n -p synth 0 0 0 80 87"31s="$sox -q -n -p synth 0 0 0 80 87"
32l="$sox -q -n -p synth 0 0 0 80 0"32l="$sox -q -n -p synth 0 0 0 80 0 0"
33 33
34$play -m -v .8 \34$play -m -v .8 \
35"|$sox \35"|$sox \
@@ -41,7 +41,7 @@ equalizer 9500 10 8 \ · 5 unmodified lines
41equalizer 7000 10 8 \41equalizer 7000 10 8 \
42equalizer 5200 10 8 \42equalizer 5200 10 8 \
43equalizer 3800 10 8 \43equalizer 3800 10 8 \
44equalizer 1500 10 8 pad 0 .21 remix 1 1 reverb 20 repeat 59" \44equalizer 1500 10 8 pad 0 .21 remix 1 1 reverb 10 repeat 56" \
45"|$sox \45"|$sox \
46\"|$sox -n -p trim 0 1.4\" \46\"|$sox -n -p trim 0 1.4\" \
47\"|$s $G0 $e 2.6 .1 bend .5,200,.2\" \47\"|$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
63\"|$l pl %10 $o trim 0 .5 bend .2,-300,.1\" \63\"|$l pl %10 $o trim 0 .5 bend .2,-300,.1\" \
64\"|$l pl %5 $o trim 0 .5 bend .2,-200,.1\" \64\"|$l pl %5 $o trim 0 .5 bend .2,-200,.1\" \
65\"|$l pl %0 $o fade 0 .55 .1 bend .2,-200,.1\" \65\"|$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\" \
67\"|$l pl %12 $o trim 0 .5 bend .2,-200,.1\" \68\"|$l pl %12 $o trim 0 .5 bend .2,-200,.1\" \
68\"|$l pl %12 $o trim 0 .5 bend .2,-200,.1\" \69\"|$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 .8 .1 bend .2,-200,.1\" \
70\"|$l pl %12 $o fade 0 .6 .1 bend .2,-200,.1\" \71\"|$l pl %12 $o trim 0 .3 bend .1,-200,.1\" \
71\"|$l pl %12 $o trim 0 1.95 bend .3,-100,1\" \72\"|$l pl %12 $o trim 0 1.95 bend .3,-50,1\" \
72\"|$l pl %10 $o trim 0 2 bend .3,-50,1\" \73\"|$l pl %10 $o trim 0 2 bend .3,-50,1\" \
73\"|$l pl %9 $o trim 0 2 gain -3 \" \74\"|$l pl %9 $o trim 0 2 gain -3\" \
74\"|$l pl %8 $o trim 0 2 gain -6 \" \75\"|$l pl %8 $o fade h 0 1 .3\" \
76\"|$l pl %8 $o fade h 0 1 .1 gain 1.5\" \
75\"|$l pl %2 pl %7 delay 0 .02 remix - $o trim 0 .25\" \77\"|$l pl %2 pl %7 delay 0 .02 remix - $o trim 0 .25\" \
76\"|$l pl %-5 $o trim 0 .25\" \78\"|$l pl %-5 $o trim 0 .25\" \
77\"|$l pl %-5 $o trim 0 .25\" \79\"|$l pl %-5 $o trim 0 .25\" \
src/dither.c+1−5
@@ -25,10 +25,6 @@ · 24 unmodified lines
25 25
26#define PREC effp->out_signal.precision26#define PREC effp->out_signal.precision
27 27
28#define RAND_CONST 140359821
29static int32_t rand_ = ~RAND_CONST;
30#define RAND_ (rand_ = rand_ * RAND_CONST + 1)
31
32typedef enum {Pdf_rectangular, Pdf_triangular, Pdf_gaussian} pdf_type_t;28typedef enum {Pdf_rectangular, Pdf_triangular, Pdf_gaussian} pdf_type_t;
33static lsx_enum_item const pdf_types[] = {29static lsx_enum_item const pdf_types[] = {
34 LSX_ENUM_ITEM(Pdf_,rectangular)30 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
190 int dummy = 0;186 int dummy = 0;
191 187
192 while (len--) {188 while (len--) {
193 double d = *ibuf++ + p->am0 * RAND_ + p->am1 * RAND_;189 double d = *ibuf++ + p->am0 * RANQD1 + p->am1 * RANQD1;
194 *obuf++ = SOX_ROUND_CLIP_COUNT(d, dummy);190 *obuf++ = SOX_ROUND_CLIP_COUNT(d, dummy);
195 }191 }
196 return SOX_SUCCESS;192 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
7 int dummy = 0;7 int dummy = 0;
8 8
9 while (len--) {9 while (len--) {
10 double r = p->am0 * RAND_ + p->am1 * RAND_;10 double r = p->am0 * RANQD1 + p->am1 * RANQD1;
11 double error, d = *ibuf++;11 double error, d = *ibuf++;
12 int j = 0;12 int j = 0;
13 CONVOLVE13 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
8 int dummy = 0;8 int dummy = 0;
9 9
10 while (len--) {10 while (len--) {
11 double r = p->am0 * RAND_ + p->am1 * RAND_;11 double r = p->am0 * RANQD1 + p->am1 * RANQD1;
12 double error, d, output = 0;12 double error, d, output = 0;
13 int j = 0;13 int j = 0;
14 CONVOLVE14 CONVOLVE
src/libsox.c+1−0
@@ -48,6 +48,7 @@ sox_globals_t sox_globals = { · 47 unmodified lines
48 sox_false,48 sox_false,
49 8192,49 8192,
50 0,50 0,
51 0,
51 NULL, NULL, NULL};52 NULL, NULL, NULL};
52 53
53void sox_output_message(FILE *file, const char *filename, const char *fmt, va_list ap)54void 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
2485 exit(0);2485 exit(0);
2486 }2486 }
2487 2487
2488 if (sox_globals.repeatable)2488 if (!sox_globals.repeatable) { /* Re-seed PRNG? */
2489 lsx_debug("Not reseeding PRNG; randomness is repeatable");2489 sox_globals.ranqd1 = (int32_t)time(NULL);
2490 else {2490 srand((unsigned int)sox_globals.ranqd1); /* srand only for polyphase */
2491 time_t t;
2492
2493 time(&t);
2494 srand((unsigned)t);
2495 }2491 }
2496 2492
2497 /* Save things that sox_sequence needs to be reinitialised for each segued2493 /* 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
179 * to perform file I/O. It can be useful to pass in similar sized179 * to perform file I/O. It can be useful to pass in similar sized
180 * data to get max performance.180 * data to get max performance.
181 */181 */
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 */
183 184
184/* private: */185/* private: */
185 char const * stdin_in_use_by;186 char const * stdin_in_use_by;
src/sox_i.h+4−0
@@ -18,6 +18,10 @@ · 17 unmodified lines
18#include "util.h"18#include "util.h"
19 19
20#include <errno.h>20#include <errno.h>
21
22#define RANQD1 ranqd1(sox_globals.ranqd1)
23#define DRANQD1 dranqd1(sox_globals.ranqd1)
24
21typedef enum {SOX_SHORT, SOX_INT, SOX_FLOAT, SOX_DOUBLE} sox_data_t;25typedef enum {SOX_SHORT, SOX_INT, SOX_FLOAT, SOX_DOUBLE} sox_data_t;
22typedef enum {SOX_WAVE_SINE, SOX_WAVE_TRIANGLE} lsx_wave_t;26typedef enum {SOX_WAVE_SINE, SOX_WAVE_TRIANGLE} lsx_wave_t;
23extern lsx_enum_item const lsx_wave_enum[];27extern lsx_enum_item const lsx_wave_enum[];
src/synth.c+5−7
@@ -13,9 +13,6 @@ · 12 unmodified lines
13#include <string.h>13#include <string.h>
14#include <ctype.h>14#include <ctype.h>
15 15
16#define RAND (2. * rand() * (1. / RAND_MAX) - 1)
17#define RAND_ ranqd1(r) * (1. / (65536. * 32768.)) /* [-1,1) */
18
19typedef enum {16typedef enum {
20 synth_sine,17 synth_sine,
21 synth_square,18 synth_square,
@@ -405,7 +402,8 @@ static int start(sox_effect_t * effp) · 383 unmodified lines
405 chan->buffer = malloc(chan->buffer_len * sizeof(*chan->buffer));402 chan->buffer = malloc(chan->buffer_len * sizeof(*chan->buffer));
406 chan->buffer[0] = 0;403 chan->buffer[0] = 0;
407 for (j = 1; j < chan->buffer_len; dc += chan->buffer[j++])404 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;
409 while (fabs(chan->buffer[j]) > 1);407 while (fabs(chan->buffer[j]) > 1);
410 for (dc /= chan->buffer_len, j = 0; j < chan->buffer_len; ++j)408 for (dc /= chan->buffer_len, j = 0; j < chan->buffer_len; ++j)
411 chan->buffer[j] = range_limit(chan->buffer[j] - dc, -1, 1) * a;409 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
570 }568 }
571 } else switch (chan->type) {569 } else switch (chan->type) {
572 case synth_whitenoise:570 case synth_whitenoise:
573 synth_out = RAND;571 synth_out = DRANQD1;
574 break;572 break;
575 573
576 case synth_tpdfnoise:574 case synth_tpdfnoise:
577 synth_out = .5 * (RAND + RAND);575 synth_out = .5 * (DRANQD1 + DRANQD1);
578 break;576 break;
579 577
580 case synth_pinknoise:578 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
582 break;580 break;
583 581
584 case synth_brownnoise:582 case synth_brownnoise:
585 do synth_out = chan->last_out + RAND * (1. / 16);583 do synth_out = chan->last_out + DRANQD1 * (1. / 16);
586 while (fabs(synth_out) > 1);584 while (fabs(synth_out) > 1);
587 chan->last_out = synth_out;585 chan->last_out = synth_out;
588 break;586 break;
src/util.h+2−1
@@ -92,7 +92,8 @@ · 91 unmodified lines
92#endif92#endif
93 93
94/* Numerical Recipes in C, p. 284 */94/* 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) */
96 97
97#ifdef WORDS_BIGENDIAN98#ifdef WORDS_BIGENDIAN
98#define MACHINE_IS_BIGENDIAN 199#define MACHINE_IS_BIGENDIAN 1
src/vorbis.c+1−1
@@ -305,7 +305,7 @@ static int startwrite(sox_format_t * ft) · 304 unmodified lines
305 vorbis_analysis_init(&ve->vd, &ve->vi);305 vorbis_analysis_init(&ve->vd, &ve->vi);
306 vorbis_block_init(&ve->vd, &ve->vb);306 vorbis_block_init(&ve->vd, &ve->vb);
307 307
308 ogg_stream_init(&ve->os, rand()); /* Random serial number */308 ogg_stream_init(&ve->os, INT_MAX & (int)RANQD1); /* Random serial number */
309 309
310 if (write_vorbis_header(ft, ve) == HEADER_ERROR) {310 if (write_vorbis_header(ft, ve) == HEADER_ERROR) {
311 lsx_fail_errno(ft, SOX_EHDR,311 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