shithub: sox

Download patch

ref: b4e5bf240085d7fa7078556a1842b47a1cd1bdf1
parent: c694b451670e3c54ce38844504bd59c3944507bd
author: cbagwell <cbagwell>
date: Sun Sep 11 13:53:10 EDT 2005

Allow "swap 1 1" to work.

--- a/src/swap.c
+++ b/src/swap.c
@@ -15,7 +15,8 @@
 
 /* Private data for SKEL file */
 typedef struct swapstuff {
-    int		order[4];
+    int         order[4];
+    int         def_opts;
 } *swap_t;
 
 /*
@@ -31,45 +32,28 @@
     swap->order[0] = swap->order[1] = swap->order[2] = swap->order[3] = 0;
     if (n)
     {
-	if (n != 2 && n != 4)
-	{
-	    st_fail("Usage: swap [1 2 | 1 2 3 4]");
-	    return (ST_EOF);
-	}
-	else if (n == 2)
-	{
-	    sscanf(argv[0],"%d",&swap->order[0]);
-	    sscanf(argv[1],"%d",&swap->order[1]);
-	}
-	else
-	{
-	    sscanf(argv[0],"%d",&swap->order[0]);
-	    sscanf(argv[1],"%d",&swap->order[1]);
-	    sscanf(argv[2],"%d",&swap->order[2]);
-	    sscanf(argv[3],"%d",&swap->order[3]);
-	}
-
-	/* Some basic error checking */
-	if (swap->order[0] < 1 || swap->order[0] > 4)
-	    swap->order[0] = 1;
-	if (swap->order[1] < 1 || swap->order[1] > 4)
-	    swap->order[1] = 1;
-
-	/* If 2 and 3 weren't specified, this logic still forces
-	 * it to equal 0 (our default)
-	 */
-	if (swap->order[2] < 1 || swap->order[2] > 4)
-	    swap->order[2] = 1;
-	if (swap->order[3] < 1 || swap->order[3] > 4)
-	    swap->order[3] = 1;
-
-	/* Convert to array offsets */
-	swap->order[0]--;
-	swap->order[1]--;
-        swap->order[2]--;
-	swap->order[3]--;
-
+        swap->def_opts = 0;
+        if (n != 2 && n != 4)
+        {
+            st_fail("Usage: swap [1 2 | 1 2 3 4]");
+            return (ST_EOF);
+        }
+        else if (n == 2)
+        {
+            sscanf(argv[0],"%d",&swap->order[0]);
+            sscanf(argv[1],"%d",&swap->order[1]);
+        }
+        else
+        {
+            sscanf(argv[0],"%d",&swap->order[0]);
+            sscanf(argv[1],"%d",&swap->order[1]);
+            sscanf(argv[2],"%d",&swap->order[2]);
+            sscanf(argv[3],"%d",&swap->order[3]);
+        }
     }
+    else
+        swap->def_opts = 1;
+
     return (ST_SUCCESS);
 }
 
@@ -83,36 +67,51 @@
 
     if (effp->outinfo.channels == 1)
     {
-	st_fail("Can't swap channels on mono data.");
-	return (ST_EOF);
+        st_fail("Can't swap channels on mono data.");
+        return (ST_EOF);
     }
 
     if (effp->outinfo.channels == 2)
     {
-	if (swap->order[2] || swap->order[3])
+        if (swap->def_opts)
         {
-	    st_fail("invalid swap channel options used");
+            swap->order[0] = 2;
+            swap->order[1] = 1;
         }
-	if (swap->order[0] != 0 && swap->order[0] != 1)
-	    st_fail("invalid swap channel options used");
-	if (swap->order[1] != 0 && swap->order[1] != 1)
-	    st_fail("invalid swap channel options used");
+
+        if (swap->order[2] || swap->order[3])
+        {
+            st_fail("invalid swap channel options used");
+        }
+        if (swap->order[0] != 1 && swap->order[0] != 2)
+            st_fail("invalid swap channel options used");
+        if (swap->order[1] != 1 && swap->order[1] != 2)
+            st_fail("invalid swap channel options used");
+
+        /* Convert to array offsets */
+        swap->order[0]--;
+        swap->order[1]--;
     }
 
     if (effp->outinfo.channels == 4)
     {
-	if (!swap->order[2] && !swap->order[3])
-	    st_fail("invalid swap channel options used");
-    }
+        if (swap->def_opts)
+        {
+            swap->order[0] = 2;
+            swap->order[1] = 1;
+            swap->order[2] = 4;
+            swap->order[3] = 3;
+        }
 
-    /* If nothing set then default to the following order */
-    if (!swap->order[0] && !swap->order[1] &&
-        !swap->order[2] && !swap->order[3])
-    {
-        swap->order[0] = 1;
-        swap->order[1] = 0;
-        swap->order[2] = 3;
-        swap->order[3] = 2;
+        if (!swap->order[2] && !swap->order[3])
+            st_fail("invalid swap channel options used");
+
+        /* Convert to array offsets */
+        swap->order[0]--;
+        swap->order[1]--;
+        swap->order[2]--;
+        swap->order[3]--;
+
     }
 
     return (ST_SUCCESS);
@@ -131,43 +130,43 @@
     switch (effp->outinfo.channels)
     {
     case 2:
-	/* Length to process will be buffer length / 2 since we
-	 * work with two samples at a time.
-	 */
-	len = ((*isamp > *osamp) ? *osamp : *isamp) / 2;
-	for(done = 0; done < len; done++)
-	{
-	    obuf[0] = ibuf[swap->order[0]];
-	    obuf[1] = ibuf[swap->order[1]];
-	    /* Advance buffer by 2 samples */
-	    ibuf += 2;
-	    obuf += 2;
-	}
-	
-	*isamp = len * 2;
-	*osamp = len * 2;
-	
-	break;
-	
+        /* Length to process will be buffer length / 2 since we
+         * work with two samples at a time.
+         */
+        len = ((*isamp > *osamp) ? *osamp : *isamp) / 2;
+        for(done = 0; done < len; done++)
+        {
+            obuf[0] = ibuf[swap->order[0]];
+            obuf[1] = ibuf[swap->order[1]];
+            /* Advance buffer by 2 samples */
+            ibuf += 2;
+            obuf += 2;
+        }
+        
+        *isamp = len * 2;
+        *osamp = len * 2;
+        
+        break;
+        
     case 4:
-	/* Length to process will be buffer length / 4 since we
-	 * work with four samples at a time.
-	 */
-	len = ((*isamp > *osamp) ? *osamp : *isamp) / 4;
-	for(done = 0; done < len; done++)
-	{
-	    obuf[0] = ibuf[swap->order[0]];
-	    obuf[1] = ibuf[swap->order[1]];
-	    obuf[2] = ibuf[swap->order[2]];
-	    obuf[3] = ibuf[swap->order[3]];
-	    /* Advance buffer by 2 samples */
-	    ibuf += 4;
-	    obuf += 4;
-	}
-	*isamp = len * 4;
-	*osamp = len * 4;
-	
-	break;
+        /* Length to process will be buffer length / 4 since we
+         * work with four samples at a time.
+         */
+        len = ((*isamp > *osamp) ? *osamp : *isamp) / 4;
+        for(done = 0; done < len; done++)
+        {
+            obuf[0] = ibuf[swap->order[0]];
+            obuf[1] = ibuf[swap->order[1]];
+            obuf[2] = ibuf[swap->order[2]];
+            obuf[3] = ibuf[swap->order[3]];
+            /* Advance buffer by 2 samples */
+            ibuf += 4;
+            obuf += 4;
+        }
+        *isamp = len * 4;
+        *osamp = len * 4;
+        
+        break;
     }
     return (ST_SUCCESS);
 }
@@ -178,16 +177,16 @@
 
 int st_swap_drain(eff_t effp, st_sample_t *obuf, st_size_t *osamp)
 {
-	*osamp = 0;
-	return (ST_SUCCESS);
+        *osamp = 0;
+        return (ST_SUCCESS);
 }
 
 /*
  * Do anything required when you stop reading samples.  
- *	(free allocated memory, etc.)
+ *      (free allocated memory, etc.)
  */
 int st_swap_stop(eff_t effp)
 {
-	/* nothing to do */
+        /* nothing to do */
     return (ST_SUCCESS);
 }