shithub: sox

Download patch

ref: a1fea160b4111bbd744941c1586c0b6e0311bc6f
parent: d24b42abd6b43b227fbe108a0c2e67c809435c32
author: Ulrich Klauer <ulrich@chirlu.de>
date: Sun Jan 13 03:10:33 EST 2013

tempo: use 64-bit numbers to count samples

Use 64-bit numbers to count samples and segments in the tempo effect.

This fixes the bug reported in tracker item 3594822 by MrMod, where
  sox -V -S -r 192000 -n -n trim 0 3:15:00 tempo -q 0.9
would hang in an endless loop until memory exhaustion.

--- a/src/tempo.c
+++ b/src/tempo.c
@@ -38,10 +38,10 @@
   fifo_t output_fifo;
 
   /* Counters: */
-  size_t samples_in;
-  size_t samples_out;
-  size_t segments_total;
-  size_t skip_total;
+  uint64_t samples_in;
+  uint64_t samples_out;
+  uint64_t segments_total;
+  uint64_t skip_total;
 } tempo_t;
 
 /* Waveform Similarity by least squares; works across multi-channels */
@@ -147,11 +147,12 @@
 /* Flush samples remaining in overlap_buf & input_fifo to the output. */
 static void tempo_flush(tempo_t * t)
 {
-  size_t samples_out = t->samples_in / t->factor + .5;
-  size_t remaining = samples_out - t->samples_out;
+  uint64_t samples_out = t->samples_in / t->factor + .5;
+  size_t remaining = samples_out > t->samples_out ?
+      (size_t)(samples_out - t->samples_out) : 0;
   float * buff = lsx_calloc(128 * t->channels, sizeof(*buff));
 
-  if ((int)remaining > 0) {
+  if (remaining > 0) {
     while (fifo_occupancy(&t->output_fifo) < remaining) {
       tempo_input(t, buff, (size_t) 128);
       tempo_process(t);