shithub: sox

Download patch

ref: 226fe1c2a6758de617c080ff740427213ec212be
parent: 0891ccec364edf8db07e0314a616260d9d8a28b8
author: robs <robs>
date: Sun Dec 17 04:17:07 EST 2006

Ability to not invoke srand.

--- a/src/misc.c
+++ b/src/misc.c
@@ -16,7 +16,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stddef.h>
-#include <time.h>
 #include <string.h>
 #include <ctype.h>
 #include <errno.h>
@@ -265,7 +264,7 @@
 {
         if (ft->info.swap)
                 u3 = st_swap24(u3);
-        if (st_writebuf(ft, &u3, 2, 1) != 1)
+        if (st_writebuf(ft, &u3, 3, 1) != 1)
         {
                 st_fail_errno(ft,errno,writerr);
                 return (ST_EOF);
@@ -496,14 +495,6 @@
     return strcpy((char *)xmalloc(strlen(s) + 1), s);
 }
 #endif
-
-/* Util to set initial seed so that we are a little less non-random */
-void st_initrand(void) {
-    time_t t;
-
-    time(&t);
-    srand(t);
-}
 
 
 
--- a/src/polyphas.c
+++ b/src/polyphas.c
@@ -380,8 +380,6 @@
     if (effp->ininfo.rate == effp->outinfo.rate)
       return ST_EFF_NULL;
 
-    st_initrand();
-
     rate->lcmrate = st_lcm((st_sample_t)effp->ininfo.rate,
                            (st_sample_t)effp->outinfo.rate);
 
--- a/src/sox.c
+++ b/src/sox.c
@@ -39,6 +39,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <signal.h>
+#include <time.h>
 #include <errno.h>
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>             /* for unlink() */
@@ -74,6 +75,7 @@
 static enum {SOX_CONCAT, SOX_MIX, SOX_MERGE} mode = SOX_CONCAT;
 static int clipped = 0;         /* Volume change clipping errors */
 static int writing = 1;         /* are we writing to a file? assume yes. */
+static bool repeatable_random = false;  /* Whether to invoke srand. */
 static st_globalinfo_t globalinfo = {false, 1};
 
 static int user_abort = 0;
@@ -280,6 +282,15 @@
       writing = 0;
     }
 
+    if (repeatable_random)
+      st_debug("Not reseeding PRNG; randomness is repeatable");
+    else {
+      time_t t;
+
+      time(&t);
+      srand(t);
+    }
+
     signal(SIGINT, sigint);
     signal(SIGTERM, sigint);
 
@@ -333,7 +344,7 @@
   return result;
 }
 
-static char *getoptstr = "+r:v:t:c:C:phsuUAaig1b2w34lf8dxV::SqoenmM";
+static char *getoptstr = "+r:v:t:c:C:phsuUAaig1b2w34lf8dxV::SqoenmMR";
 
 static struct option long_options[] =
 {
@@ -382,12 +393,16 @@
         break;
 
       case 'm':
-         mode = SOX_MIX;
-         break;
+        mode = SOX_MIX;
+        break;
 
       case 'M':
-         mode = SOX_MERGE;
-         break;
+        mode = SOX_MERGE;
+        break;
+
+      case 'R': /* Useful for regression testing; not in man page. */
+        repeatable_random = true;
+        break;
 
       case 'e': case 'n':
         return true;            /* Is null file. */
--- a/src/synth.c
+++ b/src/synth.c
@@ -376,8 +376,6 @@
 
     synth->max = (ST_SAMPLE_MAX >> shift_for_max) << shift_for_max;
 
-    st_initrand();
-
     if (synth->length_str)
     {
         if (st_parsesamples(effp->ininfo.rate, synth->length_str,
--- a/src/vorbis.c
+++ b/src/vorbis.c
@@ -365,7 +365,6 @@
         vorbis_analysis_init(&ve->vd, &ve->vi);
         vorbis_block_init(&ve->vd, &ve->vb);
 
-        st_initrand();
         ogg_stream_init(&ve->os, rand()); /* Random serial number */
 
         if (write_vorbis_header(ft, ve) == HEADER_ERROR)