shithub: sox

Download patch

ref: c728e263d18ab49be4b4266a75a9ce25d931852b
parent: 3e72daf94c73c8ef82868a2e5cbef422843e6375
author: cbagwell <cbagwell>
date: Sun Sep 30 18:28:48 EDT 2001

Fixed trim bug were it was off by one.  Fixed parsing of fade option.
Made sox more verbose when options are not specified.

--- a/Changelog
+++ b/Changelog
@@ -12,7 +12,12 @@
     settings.  This is because it can start buffering data as soon
     as the device is open and the buffered data can be in a
     wrong format.
-  o trim wasn't accounting for # of channels.
+  o trim wasn't accounting for # of channels and was generally broken.
+  o auto effect wasn't rewinding the file if the file was less then
+    132 bytes.  Changed auto parsing of header to be incremental
+    instead of reading in a large buffer.
+  o William Plant pointed out a bad pointer access in fade effect's
+    parsing of options.
 
 sox-12.17.2
 -----------
--- a/src/auto.c
+++ b/src/auto.c
@@ -27,8 +27,9 @@
 ft_t ft;
 {
     char *type;
-    char header[132];
+    char header[20];
     int rc;
+    int loop;
 
     type = 0;
 
@@ -37,11 +38,13 @@
      */
     if (ft->seekable)
     {
-	if (fread(header, 1, sizeof(header), ft->fp) == sizeof(header))
+	/* Most files only have 4-byte magic headers at first of
+	 * file.  So we start checking for those filetypes first.
+	 */
+	memset(header,0,4);
+	if (fread(header, 1, 4, ft->fp) == 4)
 	{
-
-	    fseek(ft->fp, 0L - sizeof header, 1); /* Seek back */
-	    type = 0;
+	    /* Look for .snd or dns. header of AU files */
 	    if ((strncmp(header, ".snd", 4) == 0) ||
 		    (strncmp(header, "dns.", 4) == 0) ||
 		    ((header[0] == '\0') && (strncmp(header+1, "ds.", 3) == 0))) 
@@ -50,45 +53,91 @@
 	    }
 	    else if (strncmp(header, "FORM", 4) == 0) 
 	    {
-		if (strncmp(header + 8, "AIFF", 4) == 0)
-		    type = "aiff";
-		else if (strncmp(header + 8, "8SVX", 4) == 0)
-		    type = "8svx";
-		else if (strncmp(header + 8, "MAUD", 4) == 0)
-		    type = "maud";
+		/* Need to read more data to see what type of FORM file */
+		if (fread(header, 1, 8, ft->fp) == 8)
+		{
+		    if (strncmp(header + 4, "AIFF", 4) == 0)
+			type = "aiff";
+		    else if (strncmp(header + 4, "8SVX", 4) == 0)
+			type = "8svx";
+		    else if (strncmp(header + 4, "MAUD", 4) == 0)
+			type = "maud";
+		}
 	    }
-	    else if (strncmp(header, "RIFF", 4) == 0 &&
-		    strncmp(header + 8, "WAVE", 4) == 0) 
+	    else if (strncmp(header, "RIFF", 4) == 0)
 	    {
-		type = "wav";
+		if (fread(header, 1, 8, ft->fp) == 8)
+		{
+		    if (strncmp(header + 4, "WAVE", 4) == 0)
+			type = "wav";
+		}
 	    }
-	    else if (strncmp(header, "Creative Voice File", 19) == 0) 
+	    else if (strncmp(header, "Crea", 4) == 0) 
 	    {
-		type = "voc";
+		if (fread(header, 1, 15, ft->fp) == 15)
+		{
+		    if (strncmp(header, "tive Voice File", 15) == 0) 
+			type = "voc";
+		}
 	    }
-	    else if (strncmp(header+65, "FSSD", 4) == 0 &&
-		    strncmp(header+128, "HCOM", 4) == 0) 
+	    else if (strncmp(header, "SOUN", 4) == 0)
 	    {
-		type = "hcom";
+		/* Check for SOUND magic header */
+		if (fread(header, 1, 1, ft->fp) == 1 && *header == 'D')
+		{
+		    /* Once we've found SOUND see if its smp or sndt */
+		    if (fread(header, 1, 13, ft->fp) == 13)
+		    {
+			if (strncmp(header, "D SAMPLE DATA", 13) == 0)
+    			    type = "smp";
+			else
+			    type = "sndt";
+		    }
+		    else
+			type = "sndt";
+		}
 	    }
-	    else if (strncmp(header, "SOUND SAMPLE DATA", 17) == 0)
+	    else if (strncmp(header, "2BIT", 4) == 0) 
 	    {
-		type = "smp";
+		type = "avr";
 	    }
-	    else if (strncmp(header, "SOUND", 5) == 0) 
+	    else if (strncmp(header, "NIST", 4) == 0) 
 	    {
-		type = "sndt";
+		if (fread(header, 1, 3, ft->fp) == 3)
+		{
+		    if (strncmp(header, "_1A", 3) == 0) 
+			type = "sph";
+		}
 	    }
-	    else if (strncmp(header, "2BIT", 4) == 0) 
+	} /* read 4-byte header */
+
+	/* If we didn't find type yet then start looking for file
+	 * formats that the magic header is deeper in the file.
+	 */
+	if (type == 0)
+	{
+	    loop = 61;
+	    while (loop--)
 	    {
-		type = "avr";
+		if (fread(header, 1, 1, ft->fp) != 1)
+		    loop = 0;
 	    }
-	    else if (strncmp(header, "NIST_1A", 4) == 0) 
+	    if (fread(header, 1, 4, ft->fp) == 4 && 
+		strncmp(header, "FSSD", 4) == 0)
 	    {
-		type = "sph";
+		loop = 63;
+		while (loop--)
+		{
+		    if (fread(header, 1, 1, ft->fp) != 1)
+			loop = 0;
+		}
+		if (fread(header, 1, 4, ft->fp) == 0 && 
+		    strncmp(header, "HCOM", 4) == 0)
+		    type = "hcom";
 	    }
 	}
-    }
+	rewind(ft->fp);
+    } /* if (seekable) */
 
     if (type == 0)
     {
--- a/src/fade.c
+++ b/src/fade.c
@@ -49,7 +49,7 @@
 {
 
     fade_t fade = (fade_t) effp->priv;
-    char t_char;
+    char t_char[2];
     int t_argno;
 
     if (n < 1 || n > 4)
@@ -62,10 +62,10 @@
      * string off for later computations.
      */
 
-    if (sscanf(argv[0], "%1[qhltp]", &t_char))
+    if (sscanf(argv[0], "%1[qhltp]", t_char))
     {
-        fade->in_fadetype = t_char;
-        fade->out_fadetype = t_char;
+        fade->in_fadetype = *t_char;
+        fade->out_fadetype = *t_char;
 
         argv++;
         n--;
--- a/src/sox.c
+++ b/src/sox.c
@@ -575,7 +575,7 @@
             informat[f]->info.channels = 1;
 
         if ( st_checkformat(informat[f]) )
-            st_fail("bad input format for file %s",informat[f]->filename);
+            st_fail("bad input format for file %s: %s",informat[f]->filename,informat[f]->st_errstr);
 
         st_report("Input file %s: using sample rate %lu\n\tsize %s, encoding %s, %d %s",
                   informat[f]->filename, informat[f]->info.rate,
@@ -614,7 +614,7 @@
         }
 
         if (st_checkformat(outformat))
-                st_fail("bad output format");
+                st_fail("bad output format: %s",outformat->st_errstr);
 
         st_report("Output file %s: using sample rate %lu\n\tsize %s, encoding %s, %d %s",
                outformat->filename, outformat->info.rate,
--- a/src/trim.c
+++ b/src/trim.c
@@ -186,14 +186,11 @@
     if (trim->trimmed || start_trim ) {
 
 	if (trim->length && ( (trim->trimmed+done) > trim->length)) {
-	    /* Remove extra processing from input count */
-	    *isamp -= ((trim->trimmed+done) - trim->length) - 1;
-	    /* Set done to be only enough samples to fulfill
-	     * this copy request.
-	     * Need to subtract one since length will always be at
-	     * least 1 below trimmed+done.
+	    /* Compute the amount of unprocessed data left in this
+	     * buffer and remove from isamp count and amount done.
 	     */
-	    done -= ((trim->trimmed+done) - trim->length) - 1;
+	    *isamp -= ((trim->trimmed+done) - trim->length);
+	    done -= ((trim->trimmed+done) - trim->length);
 	    *osamp = done;
 	    trim->done = 1;
 	}
--- a/src/util.c
+++ b/src/util.c
@@ -418,25 +418,25 @@
 
         if (ft->info.rate == 0)
         {
-                st_fail_errno(ft,ST_EFMT,"Sampling rate for %s file was not given\n", ft->filename);
+                st_fail_errno(ft,ST_EFMT,"sampling rate was not specified");
                 return ST_EOF;
         }
 
         if (ft->info.size == -1)
         {
-                st_fail_errno(ft,ST_EFMT,"Data size was not given for %s file\nUse one of -b/-w/-l/-f/-d/-D", ft->filename);
+                st_fail_errno(ft,ST_EFMT,"data size was not specified");
                 return ST_EOF;
         }
 
         if (ft->info.encoding == -1 && ft->info.size != ST_SIZE_FLOAT)
         {
-                st_fail_errno(ft,ST_EFMT,"Data encoding was not given for %s file\nUse one of -s/-u/-U/-A", ft->filename);
+                st_fail_errno(ft,ST_EFMT,"data encoding was not specified");
                 return ST_EOF;
         }
 
         if ((ft->info.size <= 0) || (ft->info.size > ST_SIZE_MAX))
         {
-                st_fail_errno(ft,ST_EFMT,"Data size %i for %s file is bogus\n", ft->info.size, ft->filename);
+                st_fail_errno(ft,ST_EFMT,"data size %i is invalid");
                 return ST_EOF;
         }
 
@@ -443,7 +443,7 @@
         /* anyway to check length on st_encoding_str[] ? */
         if (ft->info.encoding <= 0  || ft->info.encoding > ST_ENCODING_MAX)
         {
-                st_fail_errno(ft,ST_EFMT,"Data encoding %i for %s file is bogus\n", ft->info.encoding, ft->filename);
+                st_fail_errno(ft,ST_EFMT,"data encoding %i is invalid");
                 return ST_EOF;
         }