shithub: sox

Download patch

ref: 8025a99ac69ba3354f478316e5c209772eea3ee0
parent: 902c63082ef28518b49625fbc0a8b6cda97e792d
author: robs <robs>
date: Sat Dec 13 12:38:31 EST 2008

fix corrupted audio

--- a/src/repeat.c
+++ b/src/repeat.c
@@ -1,4 +1,5 @@
 /* libSoX repeat effect  Copyright (c) 2004 Jan Paul Schmidt <jps@fundament.org>
+ * Re-write (c) 2008 robs@users.sourceforge.net
  *
  * This library is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as published by
@@ -17,22 +18,17 @@
 
 #include "sox_i.h"
 
-#include <stdio.h>
-#include <string.h>
-#include <sys/types.h>  /* for off_t on OS/2 and possibly others */
-
 typedef struct {
-  FILE * tmp_file;
-  int first_drain;
-  size_t total;
-  size_t remaining;
-  int repeats;
+  unsigned      num_repeats, remaining_repeats;
+  off_t         num_samples, remaining_samples;
+  FILE          * tmp_file;
 } priv_t;
 
-static int getopts(sox_effect_t * effp, int argc, char **argv)
+static int create(sox_effect_t * effp, int argc, char * * argv)
 {
   priv_t * p = (priv_t *)effp->priv;
-  do {NUMERIC_PARAMETER(repeats, 0, 1e6)} while (0);
+  p->num_repeats = 1;
+  do {NUMERIC_PARAMETER(num_repeats, 0, UINT_MAX - 1)} while (0);
   return argc? lsx_usage(effp) : SOX_SUCCESS;
 }
 
@@ -39,14 +35,15 @@
 static int start(sox_effect_t * effp)
 {
   priv_t * p = (priv_t *)effp->priv;
-  if (p->repeats == 0)
+  if (!p->num_repeats)
     return SOX_EFF_NULL;
 
-  if ((p->tmp_file = lsx_tmpfile()) == NULL) {
+  if (!(p->tmp_file = lsx_tmpfile())) {
     lsx_fail("can't create temporary file: %s", strerror(errno));
     return SOX_EOF;
   }
-  p->first_drain = 1;
+  p->num_samples = p->remaining_samples = 0;
+  p->remaining_repeats = p->num_repeats + 1;
   return SOX_SUCCESS;
 }
 
@@ -58,6 +55,7 @@
     lsx_fail("error writing temporary file: %s", strerror(errno));
     return SOX_EOF;
   }
+  p->num_samples += *isamp;
   (void)obuf, *osamp = 0; /* samples not output until drain */
   return SOX_SUCCESS;
 }
@@ -65,106 +63,36 @@
 static int drain(sox_effect_t * effp, sox_sample_t * obuf, size_t * osamp)
 {
   priv_t * p = (priv_t *)effp->priv;
-  size_t read = 0;
-  sox_sample_t *buf;
-  size_t samp;
-  size_t done;
+  size_t odone = 0, n;
 
-  if (p->first_drain == 1) {
-    p->first_drain = 0;
-
-    fseeko(p->tmp_file, (off_t) 0, SEEK_END);
-    p->total = ftello(p->tmp_file);
-
-    if ((p->total % sizeof(sox_sample_t)) != 0) {
-      lsx_fail("corrupted temporary file");
-      return (SOX_EOF);
+  while ((p->remaining_samples || p->remaining_repeats) && odone < *osamp) {
+    if (!p->remaining_samples) {
+      p->remaining_samples = p->num_samples;
+      --p->remaining_repeats;
+      rewind(p->tmp_file);
     }
-
-    p->total /= sizeof(sox_sample_t);
-    p->remaining = p->total;
-
-    fseeko(p->tmp_file, (off_t) 0, SEEK_SET);
-  }
-
-  if (p->remaining == 0) {
-    if (p->repeats == 0) {
-      *osamp = 0;
-      return (SOX_EOF);
-    } else {
-      p->repeats--;
-      fseeko(p->tmp_file, (off_t) 0, SEEK_SET);
-      p->remaining = p->total;
+    n = min(p->remaining_samples, (off_t)(*osamp - odone));
+    if ((fread(obuf + odone, sizeof(*obuf), n, p->tmp_file)) != n) {
+      lsx_fail("error reading temporary file: %s", strerror(errno));
+      return SOX_EOF;
     }
+    p->remaining_samples -= n;
+    odone += n;
   }
-  if (*osamp > p->remaining) {
-    buf = obuf;
-    samp = p->remaining;
-
-    read = fread(buf, sizeof(sox_sample_t), samp, p->tmp_file);
-    if (read != samp) {
-      perror(strerror(errno));
-      lsx_fail("read error on temporary file");
-      return (SOX_EOF);
-    }
-
-    done = samp;
-    buf = &obuf[samp];
-    p->remaining = 0;
-
-    while (p->repeats > 0) {
-      lsx_debug("repeats=%u", p->repeats);
-      p->repeats--;
-      fseeko(p->tmp_file, (off_t) 0, SEEK_SET);
-
-      if (p->total >= *osamp - done) {
-        samp = *osamp - done;
-      } else {
-        samp = p->total;
-        if (samp > *osamp - done)
-          samp = *osamp - done;
-      }
-
-      p->remaining = p->total - samp;
-
-      read = fread(buf, sizeof(sox_sample_t), samp, p->tmp_file);
-      if (read != samp) {
-        perror(strerror(errno));
-        lsx_fail("repeat2: read error on temporary " "file\n");
-        return (SOX_EOF);
-      }
-
-      done += samp;
-      if (done == *osamp)
-        break;
-    }
-    *osamp = done;
-  } else {
-    read = fread(obuf, sizeof(sox_sample_t), *osamp, p->tmp_file);
-    if (read != *osamp) {
-      perror(strerror(errno));
-      lsx_fail("repeat3: read error on temporary file");
-      return (SOX_EOF);
-    }
-    p->remaining -= *osamp;
-  }
-
-  if (p->repeats == 0 && p->remaining == 0)
-    return SOX_EOF;
-  else
-    return SOX_SUCCESS;
+  *osamp = odone;
+  return p->remaining_samples || p->remaining_repeats? SOX_SUCCESS : SOX_EOF;
 }
 
 static int stop(sox_effect_t * effp)
 {
   priv_t * p = (priv_t *)effp->priv;
-  fclose(p->tmp_file);
+  fclose(p->tmp_file); /* auto-deleted by lsx_tmpfile */
   return SOX_SUCCESS;
 }
 
-const sox_effect_handler_t *sox_repeat_effect_fn(void)
+sox_effect_handler_t const * sox_repeat_effect_fn(void)
 {
-  static sox_effect_handler_t effect = {"repeat", "count", SOX_EFF_MCHAN |
-    SOX_EFF_LENGTH, getopts, start, flow, drain, stop, NULL, sizeof(priv_t)};
+  static sox_effect_handler_t effect = {"repeat", "[count]", SOX_EFF_MCHAN |
+    SOX_EFF_LENGTH, create, start, flow, drain, stop, NULL, sizeof(priv_t)};
   return &effect;
 }