shithub: sox

Download patch

ref: 8ae4d299960d9f12f9109f146d7bf9569d19f8c3
parent: fec8e043105f89327eaa7bd362ae4fbd93b5ce41
author: cbagwell <cbagwell>
date: Wed Sep 22 22:39:40 EDT 2004

update to silence effect to allow removing silence from middle of file.

--- a/Changelog
+++ b/Changelog
@@ -51,6 +51,9 @@
     processing large files.
   o Daniel Pouzzner implemented a multi-band compander (using
     the butterworth filters to split the audio into bands).
+  o Donnie Smith updated the silence effect so that its possible
+    to remove silence from the middle of a sound file by
+    using a negative value for stop_periods.
 
 sox-12.17.5
 -----------
--- a/sox.1
+++ b/sox.1
@@ -1146,9 +1146,9 @@
           threshold\fR[ \fId\fR | \fI%\fR ]]
 Removes silence from the beginning or end of a sound file.  Silence is anything below a specified threshold.
 .br
-When trimming silence from the beginning of a sound file, you specify a duration of audio that is above a given silence threshold before audio data is processed.  You can also specify the count of periods of none silence you want to detect before processing audio data.  Specify a period of 0 if you do not want to trim data from the front of the sound file.
+When trimming silence from the beginning of a sound file, you specify a duration of audio that is above a given silence threshold before audio data is processed.  You can also specify the count of periods of none-silence you want to detect before processing audio data.  Specify a period of 0 if you do not want to trim data from the front of the sound file.
 .br
-When optionally trimming silence form the end of a sound file, you specify the duration of audio that must be below a given threshold before stopping to process audio data.  A count of periods that occur below the threshold may also be specified.  If this options are not specified then data is not trimmed from the end of the audio file.
+When optionally trimming silence form the end of a sound file, you specify the duration of audio that must be below a given threshold before stopping to process audio data.  A count of periods that occur below the threshold may also be specified.  If this options are not specified then data is not trimmed from the end of the audio file.  If \fIbelow_periods\fR is negative, it is treated as a positive value and is also used to indicate the effect should restart processing as specified by the \fIabove_periods\fR, making it suitable for removing periods of silence in the middle of a sound file.
 .br
 Duration counts may be in the format of time, hh:mm:ss.frac, or in the exact count of samples.
 .br
--- a/sox.txt
+++ b/sox.txt
@@ -952,7 +952,7 @@
 		 When trimming silence from the beginning of a sound file, you
 		 specify  a  duration  of  audio that is above a given silence
 		 threshold before audio data is processed.  You can also spec-
-		 ify  the  count of periods of none silence you want to detect
+		 ify  the  count of periods of none-silence you want to detect
 		 before processing audio data.	Specify a period of 0  if  you
 		 do not want to trim data from the front of the sound file.
 		 When  optionally  trimming  silence  form  the end of a sound
@@ -960,7 +960,11 @@
 		 given	threshold  before  stopping  to process audio data.  A
 		 count of periods that occur below the threshold may  also  be
 		 specified.   If  this	options are not specified then data is
-		 not trimmed from the end of the audio file.
+		 not trimmed from the end of the audio file.  If below_periods
+		 is  negative,	it  is treated as a positive value and is also
+		 used to indicate the  effect  should  restart	processing  as
+		 specified by the above_periods, making it suitable for remov-
+		 ing periods of silence in the middle of a sound file.
 		 Duration counts may be in the format of time,	hh:mm:ss.frac,
 		 or in the exact count of samples.
 		 Threshold may be suffixed with d, or % to indicated the value
--- a/src/silence.c
+++ b/src/silence.c
@@ -1,6 +1,7 @@
 /* Silence effect for SoX
  * by Heikki Leinonen (heilei@iki.fi) 25.03.2001
  * Major Modifications by Chris Bagwell 06.08.2001
+ * Minor addition by Donnie Smith 13.08.2003
  *
  * This effect can delete samples from the start of a sound file
  * until it sees a specified count of samples exceed a given threshold 
@@ -8,6 +9,7 @@
  * This effect can also delete samples from the end of a sound file
  * when it sees a specified count of samples below a given threshold
  * (all channels).
+ * It may also be used to delete samples anywhere in a sound file.
  * Theshold's can be given as either a percentage or in decibels.
  */
 
@@ -44,6 +46,7 @@
     st_size_t   start_duration;
     double      start_threshold;
     char        start_unit; /* "d" for decibels or "%" for percent. */
+    int         restart;
 
     st_sample_t *start_holdoff;
     st_size_t   start_holdoff_offset;
@@ -159,9 +162,13 @@
         }
         if (silence->stop_periods < 0)
         {
+            silence->stop_periods = -silence->stop_periods;
+            silence->restart = 1;
             st_fail("Periods must not be greater then zero");
             return(ST_EOF);
         }
+        else
+            silence->restart = 0;
         silence->stop = TRUE;
         argv++;
         n--;
@@ -549,13 +556,21 @@
                             if (++silence->stop_found_periods >= 
                                     silence->stop_periods)
                             {
-                                silence->mode = SILENCE_STOP;
                                 silence->stop_holdoff_offset = 0;
                                 silence->stop_holdoff_end = 0;
-                                *isamp = nrOfInSamplesRead;
-                                *osamp = nrOfOutSamplesWritten;
-                                /* Return ST_EOF since no more processing */
-                                return (ST_EOF);
+                                if (!silence->restart)
+                                {
+                                    silence->mode = SILENCE_STOP;
+                                    *isamp = nrOfInSamplesRead;
+                                    *osamp = nrOfOutSamplesWritten;
+                                    /* Return ST_EOF since no more processing */
+                                    return (ST_EOF);
+                                }
+                                else
+                                {
+                                    silence->mode = SILENCE_TRIM;
+                                    return (ST_SUCCESS);
+                                }
                             }
                             else
                             {