shithub: sox

Download patch

ref: f93dbb2f633ca0bd7da6bbbeff0d6c1ca06a5245
parent: e79e6e1af4b55a69a4f43b47823f13a9ab4349c8
author: rrt <rrt>
date: Fri Dec 22 21:34:52 EST 2006

Use xstrdup.

Various cosmetic fixes, especially to stat.c and skeleton code.

--- a/src/au.c
+++ b/src/au.c
@@ -443,7 +443,7 @@
          * useful when there is no comments.
          */
         if (ft->comment == NULL)
-                ft->comment = strdup("SOX");
+                ft->comment = xstrdup("SOX");
 
         hdr_size = SUN_HDRSIZE;
 
--- a/src/dat.c
+++ b/src/dat.c
@@ -1,7 +1,7 @@
 /*
  * Sound Tools text format file.  Tom Littlejohn, March 93.
  *
- * Reads/writes sound files as text for use with FFT and graph.
+ * Reads/writes sound files as text.
  *
  * Copyright 1998-2006 Chris Bagwell and SoX Contributors
  * This source code is freely redistributable and may be used for
--- a/src/flac.c
+++ b/src/flac.c
@@ -371,14 +371,8 @@
       strcat(comments, format->comment);
     }
     else
-    {
       comments = strdup(format->comment);
-    }
-    if (comments == NULL)
-    {
-      st_fail_errno(format, ST_ENOMEM, "FLAC ERROR initialising encoder");
-      return ST_EOF;
-    }
+
     comment = comments;
 
     do
--- a/src/lua.c
+++ b/src/lua.c
@@ -19,15 +19,15 @@
  * USA.  */
 
  
-/* TODO: If efficiency is still a problem, move the call of the Lua
-   script into the flow phase. Instrument the Lua environment
-   so that scripts can still be written naively: reading beyond the
-   end of the input array yields to read more data, and writing output
-   similarly. In order not to need nonetheless to buffer all input and
-   output until finished, need low-water-marks that the script can
-   update to signal that it has finished reading and writing
-   respectively. Alternatively, assume that each location can only be
-   read/written once. */
+/* TODO: If efficiency is a problem, move the call of the Lua script
+   into the flow phase. Instrument the Lua environment so that scripts
+   can still be written naively: reading beyond the end of the input
+   array yields to read more data, and writing output similarly. In
+   order not to need nonetheless to buffer all input and output until
+   finished, need low-water-marks that the script can update to signal
+   that it has finished reading and writing respectively.
+   Alternatively, assume that each location can only be read/written
+   once. */
 
  
 #include "st_i.h"
@@ -49,22 +49,29 @@
 assert_static(sizeof(struct lua) <= ST_MAX_EFFECT_PRIVSIZE, 
               /* else */ lua_PRIVSIZE_too_big);
 
+
 /*
- * Process options
- *
- * Don't do initialization now.
- * The 'info' fields are not yet filled in.
+ * Process command-line options
  */
 static int st_lua_getopts(eff_t effp, int n, char **argv) 
 {
   lua_t lua = (lua_t)effp->priv;
+  int i;
 
-  if (n != 1) {
+  if (n < 1) {
     st_fail(effp->h->usage);
     return ST_EOF;
   }
 
-  lua->file = strdup(argv[0]);
+  /* Collect options into global arg table */
+  lua_createtable(lua->L, n - 1, 0);
+  for (i = 1; i < n; i++) {
+    lua_pushstring(lua->L, argv[i]);
+    lua_rawseti(lua->L, -2, i);
+  }
+  lua_setglobal(lua->L, "arg");
+
+  lua->file = xstrdup(argv[0]);
   return ST_SUCCESS;
 }
 
@@ -73,8 +80,7 @@
   if (nsize == 0) {
     free(ptr);
     return NULL;
-  }
-  else
+  } else
     return xrealloc(ptr, nsize);
 }
 
@@ -261,7 +267,7 @@
  */
 static st_effect_t st_lua_effect = {
   "lua",
-  "Usage: lua script",
+  "Usage: lua script [options]",
   ST_EFF_MCHAN,
   st_lua_getopts,
   st_lua_start,
--- a/src/rabbit.c
+++ b/src/rabbit.c
@@ -17,6 +17,11 @@
  * Foundation, Fifth Floor, 51 Franklin Street, Boston, MA 02111-1301,
  * USA.  */
 
+/* FIXME: Make more efficient by resampling piece by piece rather than
+   all in one go. The code is as it is at present because before SoX
+   had global clipping detection, rabbit used to do its own based on
+   sndfile-resample. */
+ 
 #include "st_i.h"
 
 #ifdef HAVE_SAMPLERATE
@@ -68,10 +73,10 @@
 
   if (n >= 1) {
     st_fail(st_rabbit_effect.usage);
-    return (ST_EOF);
+    return ST_EOF;
   }
 
-  return (ST_SUCCESS);
+  return ST_SUCCESS;
 }
 
 /*
@@ -81,7 +86,7 @@
 {
   rabbit_t r = (rabbit_t) effp->priv;
   double in_rate = floor(effp->ininfo.rate / effp->globalinfo->speed + .5)
-    * effp->globalinfo->speed;/* Make "speed" more accurate (st_rate_t is int)*/
+    * effp->globalinfo->speed; /* FIXME: Make "speed" more accurate (st_rate_t is int) */
 
   if (effp->ininfo.rate == effp->outinfo.rate)
     return ST_EFF_NULL;
@@ -88,7 +93,7 @@
 
   if (effp->ininfo.channels != effp->outinfo.channels) {
     st_fail("number of Input and Output channels must be equal to use rabbit effect");
-    return (ST_EOF);
+    return ST_EOF;
   }
 
   r->data = (SRC_DATA *)xcalloc(1, sizeof(SRC_DATA));
@@ -96,7 +101,7 @@
   r->data->input_frames_used = 0;
   r->data->output_frames_gen = 0;
 
-  return (ST_SUCCESS);
+  return ST_SUCCESS;
 }
 
 /*
@@ -112,7 +117,7 @@
   newsamples = r->samples + *isamp;
   if (newsamples / channels > INT_MAX) {
       st_fail("input data size %d too large for libsamplerate", newsamples);
-      return (ST_EOF);
+      return ST_EOF;
   }
 
   r->data->data_in = (float *)xrealloc(r->data->data_in, newsamples * sizeof(float));
@@ -126,7 +131,7 @@
 
   *osamp = 0;           /* Signal that we didn't produce any output */
 
-  return (ST_SUCCESS);
+  return ST_SUCCESS;
 }
 
 /*
@@ -146,7 +151,7 @@
 
     if (outframes > INT_MAX) {
       st_fail("too many output frames (%d) for libsamplerate", outframes);
-      return (ST_EOF);
+      return ST_EOF;
     }
     r->data->output_frames = outframes;
     r->data->data_out = (float *)xmalloc(r->data->output_frames * channels * sizeof(float));
@@ -154,7 +159,7 @@
     /* Process the data */
     if ((error = src_simple(r->data, r->converter_type, channels))) {
       st_fail("libsamplerate processing failed: %s", src_strerror(error));
-      return (ST_EOF);
+      return ST_EOF;
     }
   }
 
@@ -161,7 +166,7 @@
   /* Return the data one bufferful at a time */
   if (*osamp > INT_MAX) {
     st_fail("output buffer size %d too large for libsamplerate", *osamp);
-    return (ST_EOF);
+    return ST_EOF;
   }
 
   outsamps = min(r->data->output_frames_gen * channels - r->outsamp, *osamp);
@@ -170,7 +175,7 @@
   *osamp = (st_size_t)outsamps;
   r->outsamp += outsamps;
 
-  return (ST_SUCCESS);
+  return ST_SUCCESS;
 }
 
 /*
@@ -183,7 +188,7 @@
 
   free(r->data);
   src_delete(r->state);
-  return (ST_SUCCESS);
+  return ST_SUCCESS;
 }
 
 static st_effect_t st_rabbit_effect = {
--- a/src/skel.c
+++ b/src/skel.c
@@ -23,7 +23,7 @@
 /* Private data for SKEL file */
 typedef struct skel
 {
-    st_size_t samples_remaining;
+  st_size_t samples_remaining;
 } *skel_t;
 
 /* Note that if any of your methods doesn't need to do anything, you
@@ -38,37 +38,37 @@
  */
 static int st_skelstartread(ft_t ft)
 {
-    skel_t sk = (skel_t)ft->priv;
+  skel_t sk = (skel_t)ft->priv;
 
-    /* If you need to seek around the input file. */
-    if (!ft->seekable) {
-        st_fail_errno(ft,ST_EVALUE,"SKEL input file must be a file, not a pipe");
-        return (ST_EOF);
-    }
+  /* If you need to seek around the input file. */
+  if (!ft->seekable) {
+    st_fail_errno(ft,ST_EVALUE,"SKEL input file must be a file, not a pipe");
+    return (ST_EOF);
+  }
 
-    /*
-     * If your format is headerless and has fixed values for
-     * the following items, you can hard code them here (see cdr.c).
-     * If your format contains a header with format information
-     * then you should set it here.
-     */
-    ft->info.rate =  44100L;
-    ft->info.size = ST_SIZE_BYTE or WORD ...;
-    ft->info.encoding = ST_ENCODING_UNSIGNED or SIGN2 ...;
-    ft->info.channels = 1 or 2 or 4;
-    ft->comment = xmalloc(size_of_comment);
-    strcpy(ft->comment, "any comment in file header.");
+  /*
+   * If your format is headerless and has fixed values for
+   * the following items, you can hard code them here (see cdr.c).
+   * If your format contains a header with format information
+   * then you should set it here.
+   */
+  ft->info.rate =  44100L;
+  ft->info.size = ST_SIZE_BYTE or WORD ...;
+  ft->info.encoding = ST_ENCODING_UNSIGNED or SIGN2 ...;
+  ft->info.channels = 1 or 2 or 4;
+  ft->comment = xmalloc(size_of_comment);
+  strcpy(ft->comment, "any comment in file header.");
 
-    /* If your format doesn't have a header then samples_in_file
-     * can be determined by the file size.
-     */
-    samples_in_file = st_filelength(ft)/ft->info.size;
+  /* If your format doesn't have a header then samples_in_file
+   * can be determined by the file size.
+   */
+  samples_in_file = st_filelength(ft)/ft->info.size;
 
-    /* If you can detect the length of your file, record it here. */
-    ft->length = samples_in_file;
-    sk->remaining_samples = samples_in_file;
+  /* If you can detect the length of your file, record it here. */
+  ft->length = samples_in_file;
+  sk->remaining_samples = samples_in_file;
 
-    return (ST_SUCCESS);
+  return (ST_SUCCESS);
 }
 
 /*
@@ -79,30 +79,30 @@
  */
 static st_size_t st_skelread(ft_t ft, st_sample_t *buf, st_size_t len)
 {
-    skel_t sk = (skel_t)ft->priv;
-    st_size_t done = 0;
-    st_sample_t l;
+  skel_t sk = (skel_t)ft->priv;
+  st_size_t done;
+  st_sample_t l;
 
-    /* Always return a full frame of audio data */
-    if (len % ft->info.size)
-        len -= (len % ft->info.size);
+  /* Always return a full frame of audio data */
+  if (len % ft->info.size)
+    len -= (len % ft->info.size);
 
-    for(; done < len; done++) {
-        if no more samples
+  for (done = 0; done < len; done++) {
+    if no more samples
             break
-        get a sample
-        switch (ft->info.size) {
+            get a sample
+            switch (ft->info.size) {
             case ST_SIZE_BYTE:
-                switch (ft->info.encoding) {
-                    case ST_ENCODING_UNSIGNED;
-                        *buf++ = ST_UNSIGNED_BYTE_TO_SAMPLE(sample);
-                        break;
-                }
+              switch (ft->info.encoding) {
+                case ST_ENCODING_UNSIGNED;
+                *buf++ = ST_UNSIGNED_BYTE_TO_SAMPLE(sample);
                 break;
-        }
-    }
+              }
+              break;
+            }
+  }
 
-    return done;
+  return done;
 }
 
 /*
@@ -111,69 +111,69 @@
  */
 static int st_skelstopread(ft_t ft)
 {
-    return ST_SUCCESS;
+  return ST_SUCCESS;
 }
 
 static int st_skelstartwrite(ft_t ft)
 {
-    skel_t sk = (skel_t)ft->priv;
+  skel_t sk = (skel_t)ft->priv;
 
-    /* If you have to seek around the output file. */
-    /* If header contains a length value then seeking will be
-     * required.  Instead of failing, it's sometimes nice to
-     * just set the length to max value and not fail.
-     */
-    if (!ft->seekable) {
-        st_fail_errno(ft, ST_EVALUE, "Output .skel file must be a file, not a pipe");
-        return ST_EOF;
-    }
+  /* If you have to seek around the output file. */
+  /* If header contains a length value then seeking will be
+   * required.  Instead of failing, it's sometimes nice to
+   * just set the length to max value and not fail.
+   */
+  if (!ft->seekable) {
+    st_fail_errno(ft, ST_EVALUE, "Output .skel file must be a file, not a pipe");
+    return ST_EOF;
+  }
 
-    if (ft->info.rate != 44100L)
-        st_fail_errno(ft, ST_EVALUE, "Output .skel file must have a sample rate of 44100");
+  if (ft->info.rate != 44100L)
+    st_fail_errno(ft, ST_EVALUE, "Output .skel file must have a sample rate of 44100");
 
-    if (ft->info.size == -1) {
-        st_fail_errno(ft, ST_EVALUE, "Did not specify a size for .skel output file");
-        return ST_EOF;
-    }
+  if (ft->info.size == -1) {
+    st_fail_errno(ft, ST_EVALUE, "Did not specify a size for .skel output file");
+    return ST_EOF;
+  }
 
-    error check ft->info.encoding;
-    error check ft->info.channels;
+  error check ft->info.encoding;
+  error check ft->info.channels;
 
-    /* Write file header, if any */
-    /* Write comment field, if any */
+  /* Write file header, if any */
+  /* Write comment field, if any */
 
-    return ST_SUCCESS;
+  return ST_SUCCESS;
 
 }
 
 static st_size_t st_skelwrite(ft_t ft, const st_sample_t *buf, st_size_t len)
 {
-    skel_t sk = (skel_t)ft->priv;
-    st_size_t len = 0;
+  skel_t sk = (skel_t)ft->priv;
+  st_size_t len = 0;
 
-    switch (ft->info.size) {
-        case ST_SIZE_BYTE:
-            switch (ft->info.encoding) {
-                case ST_ENCODING_UNSIGNED:
-                    while (len--) {
-                        len = st_writeb(ft, ST_SAMPLE_TO_UNSIGNED_BYTE(*buff++, ft->clippedCount));
-                        if (len == ST_EOF)
-                            break;
-                    }
-                    break;
-            }
-            break;
-    }
+  switch (ft->info.size) {
+  case ST_SIZE_BYTE:
+    switch (ft->info.encoding) {
+    case ST_ENCODING_UNSIGNED:
+      while (len--) {
+        len = st_writeb(ft, ST_SAMPLE_TO_UNSIGNED_BYTE(*buff++, ft->clippedCount));
+        if (len == ST_EOF)
+          break;
+      }
+      break;
+    }
+    break;
+  }
 
-    return len;
+  return len;
 }
 
 static int st_skelstopwrite(ft_t ft)
 {
-    /* All samples are already written out. */
-    /* If file header needs fixing up, for example it needs the */
-    /* the number of samples in a field, seek back and write them here. */
-    return ST_SUCCESS;
+  /* All samples are already written out. */
+  /* If file header needs fixing up, for example it needs the */
+  /* the number of samples in a field, seek back and write them here. */
+  return ST_SUCCESS;
 }
 
 /* Format file suffixes */
@@ -197,5 +197,5 @@
 
 const st_format_t *st_skel_format_fn()
 {
-    return &st_skel_format;
+  return &st_skel_format;
 }
--- a/src/skeleff.c
+++ b/src/skeleff.c
@@ -22,14 +22,14 @@
 
 /* Private data for effect */
 typedef struct skeleff {
-        int  localdata;
+  int  localdata;
 } *skeleff_t;
 
 assert_static(sizeof(struct skeleff) <= ST_MAX_EFFECT_PRIVSIZE, 
-    /* else */ skeleff_PRIVSIZE_too_big);
+              /* else */ skeleff_PRIVSIZE_too_big);
 
 /*
- * Process options
+ * Process command-line options
  *
  * Don't do initialization now.
  * The 'info' fields are not yet filled in.
@@ -36,14 +36,14 @@
  */
 static int st_skeleff_getopts(eff_t effp, int n, char **argv)
 {
-    skeleff_t skeleff = (skeleff_t)effp->priv;
+  skeleff_t skeleff = (skeleff_t)effp->priv;
 
-    if (n && n != 1) {
-      st_fail(effp->h->usage);
-      return ST_EOF;
-    }
+  if (n && n != 1) {
+    st_fail(effp->h->usage);
+    return ST_EOF;
+  }
 
-    return ST_SUCCESS;
+  return ST_SUCCESS;
 }
 
 /*
@@ -53,12 +53,12 @@
  */
 static int st_skeleff_start(eff_t effp)
 {
-    if (effp->outinfo.channels == 1) {
-        st_fail("Can't run skeleff on mono data.");
-        return ST_EOF;
-    }
+  if (effp->outinfo.channels == 1) {
+    st_fail("Can't run skeleff on mono data.");
+    return ST_EOF;
+  }
 
-    return ST_SUCCESS;
+  return ST_SUCCESS;
 }
 
 /*
@@ -68,31 +68,31 @@
 static int st_skeleff_flow(eff_t effp, const st_sample_t *ibuf, st_sample_t *obuf, 
                            st_size_t *isamp, st_size_t *osamp)
 {
-    skeleff_t skeleff = (skeleff_t)effp->priv;
-    st_size_t len, done;
+  skeleff_t skeleff = (skeleff_t)effp->priv;
+  st_size_t len, done;
 
-    switch (effp->outinfo.channels) {
-    case 2:
-        /* Length to process will be buffer length / 2 since we
-         * work with two samples at a time.
-         */
-        len = ((*isamp > *osamp) ? *osamp : *isamp) / 2;
-        for(done = 0; done < len; done++)
-        {
-            obuf[0] = ibuf[0];
-            obuf[1] = ibuf[1];
-            /* Advance buffer by 2 samples */
-            ibuf += 2;
-            obuf += 2;
-        }
+  switch (effp->outinfo.channels) {
+  case 2:
+    /* Length to process will be buffer length / 2 since we
+     * work with two samples at a time.
+     */
+    len = min(*isamp, *osamp) / 2;
+    for (done = 0; done < len; done++)
+      {
+        obuf[0] = ibuf[0];
+        obuf[1] = ibuf[1];
+        /* Advance buffer by 2 samples */
+        ibuf += 2;
+        obuf += 2;
+      }
         
-        *isamp = len * 2;
-        *osamp = len * 2;
+    *isamp = len * 2;
+    *osamp = len * 2;
         
-        break;
-    }
+    break;
+  }
 
-    return ST_SUCCESS;
+  return ST_SUCCESS;
 }
 
 /*
@@ -101,12 +101,12 @@
  */
 static int st_skeleff_drain(eff_t effp, st_sample_t *obuf, st_size_t *osamp)
 {
-        *osamp = 0;
-        /* Help out application and return ST_EOF when drain
-         * will not return any mre information.  *osamp == 0
-         * also indicates that.
-         */
-        return ST_EOF;
+  *osamp = 0;
+  /* Help out application and return ST_EOF when drain
+   * will not return any mre information.  *osamp == 0
+   * also indicates that.
+   */
+  return ST_EOF;
 }
 
 /*
@@ -116,7 +116,7 @@
  */
 static int st_skeleff_stop(eff_t effp)
 {
-    return ST_SUCCESS;
+  return ST_SUCCESS;
 }
 
 
--- a/src/sox.c
+++ b/src/sox.c
@@ -276,7 +276,7 @@
       if (fo->filetype != NULL && strcmp(fo->filetype, "null") != 0)
         st_warn("Ignoring \"-t %s\".", fo->filetype);
       fo->filetype = "null";
-      fo->filename = strdup(fo->filetype);
+      fo->filename = xstrdup(fo->filetype);
     } else {
       if (optind >= argc) {
         if (memcmp(fo, &fo_none, sizeof(fo_none)) != 0)
@@ -284,7 +284,7 @@
         free(fo);
         continue;
       }
-      fo->filename = strdup(argv[optind++]);
+      fo->filename = xstrdup(argv[optind++]);
     }
     file_opts[file_count++] = fo;
   }
@@ -441,7 +441,7 @@
         break;
 
       case 2:
-        fo->comment = strdup(optarg);
+        fo->comment = xstrdup(optarg);
         break;
 
       case 3:
@@ -653,7 +653,7 @@
     }
     
     file_desc[file_count - 1] = 
-      st_open_write_instr(overwrite_permitted,
+      st_open_write(overwrite_permitted,
                           options->filename,
                           &options->info, 
                           options->filetype,
--- a/src/st.h
+++ b/src/st.h
@@ -379,8 +379,8 @@
 
 extern ft_t st_open_read(const char *path, const st_signalinfo_t *info, 
                          const char *filetype);
-ft_t st_open_write_instr(
-    bool (*overwrite_permitted)(char const * filename),
+ft_t st_open_write(
+    bool (*overwrite_permitted)(const char *filename),
     const char *path,
     const st_signalinfo_t *info,
     const char *filetype,
@@ -387,7 +387,6 @@
     const char *comment,
     const st_instrinfo_t *instr,
     const st_loopinfo_t *loops);
-#define st_open_write(a,b,c,d,e) st_open_write_instr(a,b,c,d,e,NULL,NULL)
 extern st_size_t st_read(ft_t ft, st_sample_t *buf, st_size_t len);
 extern st_size_t st_write(ft_t ft, const st_sample_t *buf, st_size_t len);
 extern int st_close(ft_t ft);
--- a/src/stat.c
+++ b/src/stat.c
@@ -5,9 +5,6 @@
  *
  * Output is unmodified from input.
  *
- */
-
-/*
  * July 5, 1991
  * Copyright 1991 Lance Norskog And Sundry Contributors
  * This source code is freely redistributable and may be used for
@@ -21,24 +18,24 @@
 #include "st_i.h"
 #include "FFT.h"
 
-/* Private data for STAT effect */
+/* Private data for stat effect */
 typedef struct statstuff {
-        double  min, max, mid;
-        double  asum;
-        double  sum1, sum2;     /* amplitudes */
-        double  dmin, dmax;
-        double  dsum1, dsum2;   /* deltas */
-        double  scale;          /* scale-factor    */
-        double  last;           /* previous sample */
-        st_size_t read;         /* samples processed */
-        int     volume;
-        int     srms;
-        int     fft;
-        unsigned long   bin[4];
-        float  *re_in;
-        float  *re_out;
-        unsigned long   fft_size;
-        unsigned long   fft_offset;
+  double min, max, mid;
+  double asum;
+  double sum1, sum2;            /* amplitudes */
+  double dmin, dmax;
+  double dsum1, dsum2;          /* deltas */
+  double scale;                 /* scale-factor */
+  double last;                  /* previous sample */
+  st_size_t read;               /* samples processed */
+  int volume;
+  int srms;
+  int fft;
+  unsigned long bin[4];
+  float *re_in;
+  float *re_out;
+  unsigned long fft_size;
+  unsigned long fft_offset;
 } *stat_t;
 
 
@@ -47,51 +44,40 @@
  */
 static int st_stat_getopts(eff_t effp, int n, char **argv)
 {
-        stat_t stat = (stat_t) effp->priv;
+  stat_t stat = (stat_t) effp->priv;
 
-        stat->scale = ST_SAMPLE_MAX;
-        stat->volume = 0;
-        stat->srms = 0;
-        stat->fft = 0;
+  stat->scale = ST_SAMPLE_MAX;
+  stat->volume = 0;
+  stat->srms = 0;
+  stat->fft = 0;
 
-        while (n>0)
-        {
-                if (!(strcmp(argv[0], "-v")))
-                        stat->volume = 1;
-                else if (!(strcmp(argv[0], "-s")))
-                {
-                        double scale;
+  for (; n > 0; n--, argv++) {
+    if (!(strcmp(*argv, "-v")))
+      stat->volume = 1;
+    else if (!(strcmp(*argv, "-s"))) {
+      if (n <= 1) {
+        st_fail("-s option: invalid argument");
+        return ST_EOF;
+      }
+      n--, argv++;              /* Move to next argument. */
+      if (!sscanf(*argv, "%lf", &stat->scale)) {
+        st_fail("-s option: invalid argument");
+        return ST_EOF;
+      }
+    } else if (!(strcmp(*argv, "-rms")))
+      stat->srms = 1;
+    else if (!(strcmp(*argv, "-freq")))
+      stat->fft = 1;
+    else if (!(strcmp(*argv, "-d")))
+      stat->volume = 2;
+    else {
+      st_fail("Summary effect: unknown option");
+      return ST_EOF;
+    }
+  }
 
-                        if (n <= 1)
-                        {
-                          st_fail("-s option: invalid argument");
-                          return (ST_EOF);
-                        }
-                        if (!sscanf(argv[1], "%lf", &scale))
-                        {
-                          st_fail("-s option: invalid argument");
-                          return (ST_EOF);
-                        }
-                        stat->scale = scale;
-
-                        /* Two argument option.  Account for this */
-                        --n; ++argv;
-                }
-                else if (!(strcmp(argv[0], "-rms")))
-                        stat->srms = 1;
-                else if (!(strcmp(argv[0], "-freq")))
-                        stat->fft = 1;
-                else if (!(strcmp(argv[0], "-d")))
-                        stat->volume = 2;
-                else
-                {
-                        st_fail("Summary effect: unknown option");
-                        return(ST_EOF);
-                }
-                --n; ++argv;
-        }
-        return (ST_SUCCESS);
-}
+  return ST_SUCCESS;
+}
 
 /*
  * Prepare processing.
@@ -98,33 +84,32 @@
  */
 static int st_stat_start(eff_t effp)
 {
-        stat_t stat = (stat_t) effp->priv;
-        int i;
+  stat_t stat = (stat_t) effp->priv;
+  int i;
 
-        stat->min = stat->max = stat->mid = 0;
-        stat->asum = 0;
-        stat->sum1 = stat->sum2 = 0;
+  stat->min = stat->max = stat->mid = 0;
+  stat->asum = 0;
+  stat->sum1 = stat->sum2 = 0;
 
-        stat->dmin = stat->dmax = 0;
-        stat->dsum1 = stat->dsum2 = 0;
+  stat->dmin = stat->dmax = 0;
+  stat->dsum1 = stat->dsum2 = 0;
 
-        stat->last = 0;
-        stat->read = 0;
+  stat->last = 0;
+  stat->read = 0;
 
-        for (i = 0; i < 4; i++)
-                stat->bin[i] = 0;
+  for (i = 0; i < 4; i++)
+    stat->bin[i] = 0;
 
-        stat->fft_size = 4096;
-        stat->re_in = stat->re_out = NULL;
+  stat->fft_size = 4096;
+  stat->re_in = stat->re_out = NULL;
 
-        if (stat->fft)
-        {
-            stat->fft_offset = 0;
-            stat->re_in = (float *)xmalloc(sizeof(float) * stat->fft_size);
-            stat->re_out = (float *)xmalloc(sizeof(float) * (stat->fft_size / 2));
-        }
+  if (stat->fft) {
+    stat->fft_offset = 0;
+    stat->re_in = (float *)xmalloc(sizeof(float) * stat->fft_size);
+    stat->re_out = (float *)xmalloc(sizeof(float) * (stat->fft_size / 2));
+  }
 
-        return (ST_SUCCESS);
+  return ST_SUCCESS;
 }
 
 /*
@@ -144,81 +129,73 @@
  * Processed signed long samples from ibuf to obuf.
  * Return number of samples processed.
  */
-
 static int st_stat_flow(eff_t effp, const st_sample_t *ibuf, st_sample_t *obuf,
-                 st_size_t *isamp, st_size_t *osamp)
+                        st_size_t *isamp, st_size_t *osamp)
 {
-        stat_t stat = (stat_t) effp->priv;
-        int len, done, x;
-        short count = 0;
+  stat_t stat = (stat_t) effp->priv;
+  int done, x, len = min(*isamp, *osamp);
+  short count = 0;
 
-        len = ((*isamp > *osamp) ? *osamp : *isamp);
-        if (len==0)
-          return (ST_SUCCESS);
+  if (len == 0)
+    return ST_SUCCESS;
 
-        if (stat->read == 0)    /* 1st sample */
-                stat->min = stat->max = stat->mid = stat->last = (*ibuf)/stat->scale;
+  if (stat->read == 0)          /* 1st sample */
+    stat->min = stat->max = stat->mid = stat->last = (*ibuf)/stat->scale;
 
-        if (stat->fft)
-        {
-            for (x = 0; x < len; x++)
-            {
-                stat->re_in[stat->fft_offset++] = ST_SAMPLE_TO_FLOAT_DWORD(ibuf[x], effp->clippedCount);
+  if (stat->fft) {
+    for (x = 0; x < len; x++) {
+      stat->re_in[stat->fft_offset++] = ST_SAMPLE_TO_FLOAT_DWORD(ibuf[x], effp->clippedCount);
 
-                if (stat->fft_offset >= stat->fft_size)
-                {
-                    stat->fft_offset = 0;
-                    print_power_spectrum(stat->fft_size, effp->ininfo.rate, stat->re_in, stat->re_out);
-                }
+      if (stat->fft_offset >= stat->fft_size) {
+        stat->fft_offset = 0;
+        print_power_spectrum(stat->fft_size, effp->ininfo.rate, stat->re_in, stat->re_out);
+      }
 
-            }
-        }
+    }
+  }
 
-        for(done = 0; done < len; done++) {
-                long lsamp;
-                double samp, delta;
-                /* work in scaled levels for both sample and delta */
-                lsamp = *ibuf++;
-                samp = (double)lsamp/stat->scale;
-                stat->bin[(lsamp>>30)+2]++;
-                *obuf++ = lsamp;
+  for (done = 0; done < len; done++) {
+    long lsamp = *ibuf++;
+    double delta, samp = (double)lsamp / stat->scale;
+    /* work in scaled levels for both sample and delta */
+    stat->bin[(lsamp >> 30) + 2]++;
+    *obuf++ = lsamp;
 
-                if (stat->volume == 2)
-                {
-                    fprintf(stderr,"%08lx ",lsamp);
-                    if (count++ == 5)
-                    {
-                        fprintf(stderr,"\n");
-                        count = 0;
-                    }
-                }
+    if (stat->volume == 2) {
+        fprintf(stderr,"%08lx ",lsamp);
+        if (count++ == 5) {
+            fprintf(stderr,"\n");
+            count = 0;
+        }
+    }
 
-                /* update min/max */
-                if (stat->min > samp)
-                        stat->min = samp;
-                else if (stat->max < samp)
-                        stat->max = samp;
-                stat->mid = stat->min / 2 + stat->max / 2;
+    /* update min/max */
+    if (stat->min > samp)
+      stat->min = samp;
+    else if (stat->max < samp)
+      stat->max = samp;
+    stat->mid = stat->min / 2 + stat->max / 2;
 
-                stat->sum1 += samp;
-                stat->sum2 += samp*samp;
-                stat->asum += fabs(samp);
+    stat->sum1 += samp;
+    stat->sum2 += samp*samp;
+    stat->asum += fabs(samp);
 
-                delta = fabs(samp - stat->last);
-                if (delta < stat->dmin)
-                        stat->dmin = delta;
-                else if (delta > stat->dmax)
-                        stat->dmax = delta;
+    delta = fabs(samp - stat->last);
+    if (delta < stat->dmin)
+      stat->dmin = delta;
+    else if (delta > stat->dmax)
+      stat->dmax = delta;
 
-                stat->dsum1 += delta;
-                stat->dsum2 += delta*delta;
+    stat->dsum1 += delta;
+    stat->dsum2 += delta*delta;
 
-                stat->last = samp;
-        }
-        stat->read += len;
-        *isamp = *osamp = len;
-        /* Process all samples */
-        return (ST_SUCCESS);
+    stat->last = samp;
+  }
+  stat->read += len;
+  *isamp = *osamp = len;
+  /* Process all samples */
+
+  return ST_SUCCESS;
 }
 
 /*
@@ -226,23 +203,23 @@
  */
 static int st_stat_drain(eff_t effp, st_sample_t *obuf UNUSED, st_size_t *osamp)
 {
-    stat_t stat = (stat_t) effp->priv;
+  stat_t stat = (stat_t) effp->priv;
 
-    /* When we run out of samples, then we need to pad buffer with
-     * zeros and then run FFT one last time to process any unprocessed
-     * samples.
-     */
-    if (stat->fft && stat->fft_offset) {
-      unsigned int x;
+  /* When we run out of samples, then we need to pad buffer with
+   * zeros and then run FFT one last time to process any unprocessed
+   * samples.
+   */
+  if (stat->fft && stat->fft_offset) {
+    unsigned int x;
 
-      for (x = stat->fft_offset; x < stat->fft_size; x++)
-        stat->re_in[x] = 0;
+    for (x = stat->fft_offset; x < stat->fft_size; x++)
+      stat->re_in[x] = 0;
       
-      print_power_spectrum(stat->fft_size, effp->ininfo.rate, stat->re_in, stat->re_out);
-    }
+    print_power_spectrum(stat->fft_size, effp->ininfo.rate, stat->re_in, stat->re_out);
+  }
 
-    *osamp = 0;
-    return (ST_EOF);
+  *osamp = 0;
+  return ST_EOF;
 }
 
 /*
@@ -251,99 +228,94 @@
  */
 static int st_stat_stop(eff_t effp)
 {
-        stat_t stat = (stat_t) effp->priv;
-        double amp, scale, rms = 0, freq;
-        double x, ct;
+  stat_t stat = (stat_t) effp->priv;
+  double amp, scale, rms = 0, freq;
+  double x, ct;
 
-        ct = stat->read;
+  ct = stat->read;
 
-        if (stat->srms) {  /* adjust results to units of rms */
-                double f;
-                rms = sqrt(stat->sum2/ct);
-                f = 1.0/rms;
-                stat->max *= f;
-                stat->min *= f;
-                stat->mid *= f;
-                stat->asum *= f;
-                stat->sum1 *= f;
-                stat->sum2 *= f*f;
-                stat->dmax *= f;
-                stat->dmin *= f;
-                stat->dsum1 *= f;
-                stat->dsum2 *= f*f;
-                stat->scale *= rms;
-        }
+  if (stat->srms) {  /* adjust results to units of rms */
+    double f;
+    rms = sqrt(stat->sum2/ct);
+    f = 1.0/rms;
+    stat->max *= f;
+    stat->min *= f;
+    stat->mid *= f;
+    stat->asum *= f;
+    stat->sum1 *= f;
+    stat->sum2 *= f*f;
+    stat->dmax *= f;
+    stat->dmin *= f;
+    stat->dsum1 *= f;
+    stat->dsum2 *= f*f;
+    stat->scale *= rms;
+  }
 
-        scale = stat->scale;
+  scale = stat->scale;
 
-        amp = -stat->min;
-        if (amp < stat->max)
-                amp = stat->max;
+  amp = -stat->min;
+  if (amp < stat->max)
+    amp = stat->max;
 
-        /* Just print the volume adjustment */
-        if (stat->volume == 1 && amp > 0) {
-                fprintf(stderr, "%.3f\n", ST_SAMPLE_MAX/(amp*scale));
-                return (ST_SUCCESS);
-        }
-        if (stat->volume == 2)
-                fprintf(stderr, "\n\n");
-        /* print out the info */
-        fprintf(stderr, "Samples read:      %12u\n", stat->read);
-        fprintf(stderr, "Length (seconds):  %12.6f\n", (double)stat->read/effp->ininfo.rate/effp->ininfo.channels);
-        if (stat->srms)
-                fprintf(stderr, "Scaled by rms:     %12.6f\n", rms);
-        else
-                fprintf(stderr, "Scaled by:         %12.1f\n", scale);
-        fprintf(stderr, "Maximum amplitude: %12.6f\n", stat->max);
-        fprintf(stderr, "Minimum amplitude: %12.6f\n", stat->min);
-        fprintf(stderr, "Midline amplitude: %12.6f\n", stat->mid);
-        fprintf(stderr, "Mean    norm:      %12.6f\n", stat->asum/ct);
-        fprintf(stderr, "Mean    amplitude: %12.6f\n", stat->sum1/ct);
-        fprintf(stderr, "RMS     amplitude: %12.6f\n", sqrt(stat->sum2/ct));
+  /* Just print the volume adjustment */
+  if (stat->volume == 1 && amp > 0) {
+    fprintf(stderr, "%.3f\n", ST_SAMPLE_MAX/(amp*scale));
+    return ST_SUCCESS;
+  }
+  if (stat->volume == 2)
+    fprintf(stderr, "\n\n");
+  /* print out the info */
+  fprintf(stderr, "Samples read:      %12u\n", stat->read);
+  fprintf(stderr, "Length (seconds):  %12.6f\n", (double)stat->read/effp->ininfo.rate/effp->ininfo.channels);
+  if (stat->srms)
+    fprintf(stderr, "Scaled by rms:     %12.6f\n", rms);
+  else
+    fprintf(stderr, "Scaled by:         %12.1f\n", scale);
+  fprintf(stderr, "Maximum amplitude: %12.6f\n", stat->max);
+  fprintf(stderr, "Minimum amplitude: %12.6f\n", stat->min);
+  fprintf(stderr, "Midline amplitude: %12.6f\n", stat->mid);
+  fprintf(stderr, "Mean    norm:      %12.6f\n", stat->asum/ct);
+  fprintf(stderr, "Mean    amplitude: %12.6f\n", stat->sum1/ct);
+  fprintf(stderr, "RMS     amplitude: %12.6f\n", sqrt(stat->sum2/ct));
 
-        fprintf(stderr, "Maximum delta:     %12.6f\n", stat->dmax);
-        fprintf(stderr, "Minimum delta:     %12.6f\n", stat->dmin);
-        fprintf(stderr, "Mean    delta:     %12.6f\n", stat->dsum1/(ct-1));
-        fprintf(stderr, "RMS     delta:     %12.6f\n", sqrt(stat->dsum2/(ct-1)));
-        freq = sqrt(stat->dsum2/stat->sum2)*effp->ininfo.rate/(M_PI*2);
-        fprintf(stderr, "Rough   frequency: %12d\n", (int)freq);
+  fprintf(stderr, "Maximum delta:     %12.6f\n", stat->dmax);
+  fprintf(stderr, "Minimum delta:     %12.6f\n", stat->dmin);
+  fprintf(stderr, "Mean    delta:     %12.6f\n", stat->dsum1/(ct-1));
+  fprintf(stderr, "RMS     delta:     %12.6f\n", sqrt(stat->dsum2/(ct-1)));
+  freq = sqrt(stat->dsum2/stat->sum2)*effp->ininfo.rate/(M_PI*2);
+  fprintf(stderr, "Rough   frequency: %12d\n", (int)freq);
 
-        if (amp>0)
-        	fprintf(stderr, "Volume adjustment: %12.3f\n", ST_SAMPLE_MAX/(amp*scale));
+  if (amp>0)
+    fprintf(stderr, "Volume adjustment: %12.3f\n", ST_SAMPLE_MAX/(amp*scale));
 
-        if (stat->bin[2] == 0 && stat->bin[3] == 0)
-                fprintf(stderr, "\nProbably text, not sound\n");
-        else {
+  if (stat->bin[2] == 0 && stat->bin[3] == 0)
+    fprintf(stderr, "\nProbably text, not sound\n");
+  else {
 
-                x = (float)(stat->bin[0] + stat->bin[3]) / (float)(stat->bin[1] + stat->bin[2]);
+    x = (float)(stat->bin[0] + stat->bin[3]) / (float)(stat->bin[1] + stat->bin[2]);
 
-                if (x >= 3.0)                  /* use opposite encoding */
-                {
-                        if (effp->ininfo.encoding == ST_ENCODING_UNSIGNED)
-                                fprintf (stderr,"\nTry: -t raw -b -s \n");
-                        else
-                                fprintf (stderr,"\nTry: -t raw -b -u \n");
+    if (x >= 3.0) {             /* use opposite encoding */
+      if (effp->ininfo.encoding == ST_ENCODING_UNSIGNED)
+        fprintf(stderr,"\nTry: -t raw -b -s \n");
+      else
+        fprintf(stderr,"\nTry: -t raw -b -u \n");
+    } else if (x <= 1.0 / 3.0)
+      ;                         /* correctly decoded */
+    else if (x >= 0.5 && x <= 2.0) { /* use ULAW */
+      if (effp->ininfo.encoding == ST_ENCODING_ULAW)
+        fprintf(stderr,"\nTry: -t raw -b -u \n");
+      else
+        fprintf(stderr,"\nTry: -t raw -b -U \n");
+    } else
+      fprintf(stderr, "\nCan't guess the type\n");
+  }
 
-                }
-                else if (x <= 1.0/3.0)
-                  ;             /* correctly decoded */
-                else if (x >= 0.5 && x <= 2.0)       /* use ULAW */
-                {
-                        if (effp->ininfo.encoding == ST_ENCODING_ULAW)
-                                fprintf (stderr,"\nTry: -t raw -b -u \n");
-                        else
-                                fprintf (stderr,"\nTry: -t raw -b -U \n");
-                }
-                else
-                        fprintf (stderr, "\nCan't guess the type\n");
-        }
+  /* Release FFT memory */
+  free(stat->re_in);
+  free(stat->re_out);
 
-        /* Release FFT memory */
-        free(stat->re_in);
-        free(stat->re_out);
+  return ST_SUCCESS;
 
-        return (ST_SUCCESS);
-
 }
 
 static st_effect_t st_stat_effect = {
@@ -359,5 +331,5 @@
 
 const st_effect_t *st_stat_effect_fn(void)
 {
-    return &st_stat_effect;
+  return &st_stat_effect;
 }
--- a/src/stio.c
+++ b/src/stio.c
@@ -1,6 +1,4 @@
-#include "st.h"
 #include "st_i.h"
-#include "stconfig.h"
 
 #include <string.h>
 #include <errno.h>
@@ -80,20 +78,16 @@
 {
     ft_t ft = (ft_t)xcalloc(sizeof(struct st_soundstream), 1);
 
-    ft->filename = strdup(path);
+    ft->filename = xstrdup(path);
 
     /* Let auto effect do the work if user is not overriding. */
     if (!filetype)
-        ft->filetype = strdup("auto");
+        ft->filetype = xstrdup("auto");
     else
-        ft->filetype = strdup(filetype);
+        ft->filetype = xstrdup(filetype);
 
-    if (!ft->filename || !ft->filetype)
-        goto input_error;
-
-    if (st_gettype(ft, false) != ST_SUCCESS)
-    {
-        st_warn("Unknown input file format for '%s':  %s",
+    if (st_gettype(ft, false) != ST_SUCCESS) {
+        st_warn("Unknown input file format for `%s':  %s",
                 ft->filename,
                 ft->st_errstr);
         goto input_error;
@@ -164,8 +158,8 @@
 #define LASTCHAR '/'
 #endif
 
-ft_t st_open_write_instr(
-    bool (*overwrite_permitted)(char const * filename),
+ft_t st_open_write(
+    bool (*overwrite_permitted)(const char *filename),
     const char *path,
     const st_signalinfo_t *info,
     const char *filetype,
@@ -177,17 +171,11 @@
     int i;
     bool no_filetype_given = filetype == NULL;
 
-    ft->filename = strdup(path);
+    fprintf(stderr, "%s %s\n", filetype, path);
+    ft->filename = xstrdup(path);
 
-    if (!ft->filename)
-    {
-        st_fail_errno(ft,ST_ENOMEM,"st_open_write_instr");
-        goto output_error;
-    }
-
     /* Let auto effect do the work if user is not overriding. */
-    if (!filetype)
-    {
+    if (!filetype) {
         char *chop;
         int len;
 
@@ -201,14 +189,12 @@
         while (chop > ft->filename && *chop != LASTCHAR && *chop != '.')
             chop--;
 
-        if (*chop == '.')
-        {
+        if (*chop == '.') {
             chop++;
-            ft->filetype = strdup(chop);
+            ft->filetype = xstrdup(chop);
         }
-    }
-    else
-        ft->filetype = strdup(filetype);
+    } else
+        ft->filetype = xstrdup(filetype);
 
     if (!ft->filetype || st_gettype(ft, no_filetype_given) != ST_SUCCESS)
     {
@@ -237,7 +223,7 @@
         }
         else {
           struct stat st;
-          if (!stat(ft->filename, &st) && !overwrite_permitted(ft->filename)) {
+          if (!stat(ft->filename, &st) && !(overwrite_permitted(ft->filename)) {
             st_fail("Permission to overwrite '%s' denied", ft->filename);
             goto output_error;
           }
@@ -260,7 +246,7 @@
         ft->seekable = is_seekable(ft);
     }
 
-    ft->comment = strdup(comment);
+    ft->comment = xstrdup(comment);
 
     if (loops)
         for (i = 0; i < ST_MAX_NLOOPS; i++)