shithub: sox

Download patch

ref: 36a3fc784e78cb877fb88f15b64ab915255b3a91
parent: f6e5f2ed506ccd737e6607b680f229cf30c3df69
author: cbagwell <cbagwell>
date: Wed Feb 7 20:04:14 EST 2007

Reverting removal of st_seek() and optimize_trim() functions.

--- a/src/sox.c
+++ b/src/sox.c
@@ -554,6 +554,37 @@
   return p->value;
 }
 
+static void optimize_trim(void)          
+{
+    /* Speed hack.  If the "trim" effect is the first effect then
+     * peak inside its "effect descriptor" and see what the
+     * start location is.  This has to be done after its start()
+     * is called to have the correct location.
+     * Also, only do this when only working with one input file.
+     * This is because the logic to do it for multiple files is
+     * complex and problably never used.
+     * This hack is a huge time savings when trimming
+     * gigs of audio data into managable chunks
+     */ 
+    if (input_count == 1 && neffects > 1 &&
+        strcmp(efftab[1].name, "trim") == 0)
+    {
+        if ((files[0]->desc->h->flags & ST_FILE_SEEK) &&
+            files[0]->desc->seekable)
+        { 
+            if (st_seek(files[0]->desc, st_trim_get_start(&efftab[1]), 
+                        ST_SEEK_SET) != ST_EOF)
+            { 
+                /* Assuming a failed seek stayed where it was.  If the 
+                 * seek worked then reset the start location of 
+                 * trim so that it thinks user didn't request a skip.
+                 */ 
+                st_trim_clear_start(&efftab[1]);
+            }    
+        }        
+    }    
+}
+
 static st_bool doopts(file_t f, int argc, char **argv)
 {
   while (st_true) {
@@ -969,6 +1000,8 @@
     }
     input_wide_samples = ws; /* Output length is that of longest input file. */
   }
+
+  optimize_trim();
 
   input_eff = 0;
   input_eff_eof = 0;
--- a/src/st.h
+++ b/src/st.h
@@ -419,6 +419,18 @@
 ft_t st_initformat(void);
 char const * st_parsesamples(st_rate_t rate, const char *str, st_size_t *samples, char def);
 
+/* The following routines are unique to the trim effect.
+ * st_trim_get_start can be used to find what is the start
+ * of the trim operation as specified by the user.
+ * st_trim_clear_start will reset what ever the user specified
+ * back to 0.
+ * These two can be used together to find out what the user
+ * wants to trim and use a st_seek() operation instead.  After
+ * st_seek()'ing, you should set the trim option to 0.
+ */
+st_size_t st_trim_get_start(eff_t effp);
+void st_trim_clear_start(eff_t effp);
+
 extern char const * st_message_filename;
 
 #define ST_EOF (-1)
--- a/src/stio.c
+++ b/src/stio.c
@@ -330,3 +330,18 @@
 
     return rc;
 }
+
+int st_seek(ft_t ft, st_size_t offset, int whence)       
+{       
+    /* FIXME: Implement ST_SEEK_CUR and ST_SEEK_END. */         
+    if (whence != ST_SEEK_SET)          
+        return ST_EOF; /* FIXME: return ST_EINVAL */    
+
+    /* If file is a seekable file and this handler supports seeking,    
+     * the invoke handlers function.    
+     */         
+    if (ft->seekable  && (ft->h->flags & ST_FILE_SEEK))         
+        return (*ft->h->seek)(ft, offset);      
+    else        
+        return ST_EOF; /* FIXME: return ST_EBADF */     
+}
--- a/src/trim.c
+++ b/src/trim.c
@@ -189,6 +189,18 @@
     return (ST_SUCCESS);
 }
 
+st_size_t st_trim_get_start(eff_t effp)          
+{        
+    trim_t trim = (trim_t)effp->priv;    
+    return trim->start;          
+}        
+
+void st_trim_clear_start(eff_t effp)     
+{        
+    trim_t trim = (trim_t)effp->priv;    
+    trim->start = 0;     
+}
+
 static st_effect_t st_trim_effect = {
   "trim",
   "Usage: trim start [length]",